gcc/testsuite/
[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 "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
100
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
104
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
107
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
120
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
128
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
137
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
143
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
150
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
169
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
172
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
177
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
180
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
184
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
188
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
196
197
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
202
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
204
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
211 };
212
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
214
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
218
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
221
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
224
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
227
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
233
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
236
237 static GTY(()) int dw2_string_counter;
238
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
241
242 /* Whether the default text and cold text sections have been used at all. */
243
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
246
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
249
250 /* The DIE for C++1y 'auto' in a function return type. */
251 static GTY(()) dw_die_ref auto_die;
252
253 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
254 static GTY(()) dw_die_ref decltype_auto_die;
255
256 /* Forward declarations for functions defined in this file. */
257
258 static char *stripattributes (const char *);
259 static void output_call_frame_info (int);
260 static void dwarf2out_note_section_used (void);
261
262 /* Personality decl of current unit. Used only when assembler does not support
263 personality CFI. */
264 static GTY(()) rtx current_unit_personality;
265
266 /* Data and reference forms for relocatable data. */
267 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
268 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
269
270 #ifndef DEBUG_FRAME_SECTION
271 #define DEBUG_FRAME_SECTION ".debug_frame"
272 #endif
273
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL "LFB"
276 #endif
277
278 #ifndef FUNC_END_LABEL
279 #define FUNC_END_LABEL "LFE"
280 #endif
281
282 #ifndef PROLOGUE_END_LABEL
283 #define PROLOGUE_END_LABEL "LPE"
284 #endif
285
286 #ifndef EPILOGUE_BEGIN_LABEL
287 #define EPILOGUE_BEGIN_LABEL "LEB"
288 #endif
289
290 #ifndef FRAME_BEGIN_LABEL
291 #define FRAME_BEGIN_LABEL "Lframe"
292 #endif
293 #define CIE_AFTER_SIZE_LABEL "LSCIE"
294 #define CIE_END_LABEL "LECIE"
295 #define FDE_LABEL "LSFDE"
296 #define FDE_AFTER_SIZE_LABEL "LASFDE"
297 #define FDE_END_LABEL "LEFDE"
298 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
299 #define LINE_NUMBER_END_LABEL "LELT"
300 #define LN_PROLOG_AS_LABEL "LASLTP"
301 #define LN_PROLOG_END_LABEL "LELTP"
302 #define DIE_LABEL_PREFIX "DW"
303 \f
304 /* Match the base name of a file to the base name of a compilation unit. */
305
306 static int
307 matches_main_base (const char *path)
308 {
309 /* Cache the last query. */
310 static const char *last_path = NULL;
311 static int last_match = 0;
312 if (path != last_path)
313 {
314 const char *base;
315 int length = base_of_path (path, &base);
316 last_path = path;
317 last_match = (length == main_input_baselength
318 && memcmp (base, main_input_basename, length) == 0);
319 }
320 return last_match;
321 }
322
323 #ifdef DEBUG_DEBUG_STRUCT
324
325 static int
326 dump_struct_debug (tree type, enum debug_info_usage usage,
327 enum debug_struct_file criterion, int generic,
328 int matches, int result)
329 {
330 /* Find the type name. */
331 tree type_decl = TYPE_STUB_DECL (type);
332 tree t = type_decl;
333 const char *name = 0;
334 if (TREE_CODE (t) == TYPE_DECL)
335 t = DECL_NAME (t);
336 if (t)
337 name = IDENTIFIER_POINTER (t);
338
339 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
340 criterion,
341 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
342 matches ? "bas" : "hdr",
343 generic ? "gen" : "ord",
344 usage == DINFO_USAGE_DFN ? ";" :
345 usage == DINFO_USAGE_DIR_USE ? "." : "*",
346 result,
347 (void*) type_decl, name);
348 return result;
349 }
350 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
351 dump_struct_debug (type, usage, criterion, generic, matches, result)
352
353 #else
354
355 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
356 (result)
357
358 #endif
359
360 /* Get the number of HOST_WIDE_INTs needed to represent the precision
361 of the number. */
362
363 static unsigned int
364 get_full_len (const wide_int &op)
365 {
366 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
367 / HOST_BITS_PER_WIDE_INT);
368 }
369
370 static bool
371 should_emit_struct_debug (tree type, enum debug_info_usage usage)
372 {
373 enum debug_struct_file criterion;
374 tree type_decl;
375 bool generic = lang_hooks.types.generic_p (type);
376
377 if (generic)
378 criterion = debug_struct_generic[usage];
379 else
380 criterion = debug_struct_ordinary[usage];
381
382 if (criterion == DINFO_STRUCT_FILE_NONE)
383 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
384 if (criterion == DINFO_STRUCT_FILE_ANY)
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
386
387 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
388
389 if (type_decl != NULL)
390 {
391 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
392 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
393
394 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
395 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
396 }
397
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
399 }
400 \f
401 /* Return a pointer to a copy of the section string name S with all
402 attributes stripped off, and an asterisk prepended (for assemble_name). */
403
404 static inline char *
405 stripattributes (const char *s)
406 {
407 char *stripped = XNEWVEC (char, strlen (s) + 2);
408 char *p = stripped;
409
410 *p++ = '*';
411
412 while (*s && *s != ',')
413 *p++ = *s++;
414
415 *p = '\0';
416 return stripped;
417 }
418
419 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
420 switch to the data section instead, and write out a synthetic start label
421 for collect2 the first time around. */
422
423 static void
424 switch_to_eh_frame_section (bool back)
425 {
426 tree label;
427
428 #ifdef EH_FRAME_SECTION_NAME
429 if (eh_frame_section == 0)
430 {
431 int flags;
432
433 if (EH_TABLES_CAN_BE_READ_ONLY)
434 {
435 int fde_encoding;
436 int per_encoding;
437 int lsda_encoding;
438
439 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
440 /*global=*/0);
441 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
442 /*global=*/1);
443 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
444 /*global=*/0);
445 flags = ((! flag_pic
446 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
447 && (fde_encoding & 0x70) != DW_EH_PE_aligned
448 && (per_encoding & 0x70) != DW_EH_PE_absptr
449 && (per_encoding & 0x70) != DW_EH_PE_aligned
450 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
451 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
452 ? 0 : SECTION_WRITE);
453 }
454 else
455 flags = SECTION_WRITE;
456 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
457 }
458 #endif /* EH_FRAME_SECTION_NAME */
459
460 if (eh_frame_section)
461 switch_to_section (eh_frame_section);
462 else
463 {
464 /* We have no special eh_frame section. Put the information in
465 the data section and emit special labels to guide collect2. */
466 switch_to_section (data_section);
467
468 if (!back)
469 {
470 label = get_file_function_name ("F");
471 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
472 targetm.asm_out.globalize_label (asm_out_file,
473 IDENTIFIER_POINTER (label));
474 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
475 }
476 }
477 }
478
479 /* Switch [BACK] to the eh or debug frame table section, depending on
480 FOR_EH. */
481
482 static void
483 switch_to_frame_table_section (int for_eh, bool back)
484 {
485 if (for_eh)
486 switch_to_eh_frame_section (back);
487 else
488 {
489 if (!debug_frame_section)
490 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
491 SECTION_DEBUG, NULL);
492 switch_to_section (debug_frame_section);
493 }
494 }
495
496 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
497
498 enum dw_cfi_oprnd_type
499 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
500 {
501 switch (cfi)
502 {
503 case DW_CFA_nop:
504 case DW_CFA_GNU_window_save:
505 case DW_CFA_remember_state:
506 case DW_CFA_restore_state:
507 return dw_cfi_oprnd_unused;
508
509 case DW_CFA_set_loc:
510 case DW_CFA_advance_loc1:
511 case DW_CFA_advance_loc2:
512 case DW_CFA_advance_loc4:
513 case DW_CFA_MIPS_advance_loc8:
514 return dw_cfi_oprnd_addr;
515
516 case DW_CFA_offset:
517 case DW_CFA_offset_extended:
518 case DW_CFA_def_cfa:
519 case DW_CFA_offset_extended_sf:
520 case DW_CFA_def_cfa_sf:
521 case DW_CFA_restore:
522 case DW_CFA_restore_extended:
523 case DW_CFA_undefined:
524 case DW_CFA_same_value:
525 case DW_CFA_def_cfa_register:
526 case DW_CFA_register:
527 case DW_CFA_expression:
528 return dw_cfi_oprnd_reg_num;
529
530 case DW_CFA_def_cfa_offset:
531 case DW_CFA_GNU_args_size:
532 case DW_CFA_def_cfa_offset_sf:
533 return dw_cfi_oprnd_offset;
534
535 case DW_CFA_def_cfa_expression:
536 return dw_cfi_oprnd_loc;
537
538 default:
539 gcc_unreachable ();
540 }
541 }
542
543 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
544
545 enum dw_cfi_oprnd_type
546 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
547 {
548 switch (cfi)
549 {
550 case DW_CFA_def_cfa:
551 case DW_CFA_def_cfa_sf:
552 case DW_CFA_offset:
553 case DW_CFA_offset_extended_sf:
554 case DW_CFA_offset_extended:
555 return dw_cfi_oprnd_offset;
556
557 case DW_CFA_register:
558 return dw_cfi_oprnd_reg_num;
559
560 case DW_CFA_expression:
561 return dw_cfi_oprnd_loc;
562
563 default:
564 return dw_cfi_oprnd_unused;
565 }
566 }
567
568 /* Output one FDE. */
569
570 static void
571 output_fde (dw_fde_ref fde, bool for_eh, bool second,
572 char *section_start_label, int fde_encoding, char *augmentation,
573 bool any_lsda_needed, int lsda_encoding)
574 {
575 const char *begin, *end;
576 static unsigned int j;
577 char l1[20], l2[20];
578
579 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
580 /* empty */ 0);
581 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
582 for_eh + j);
583 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
584 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
585 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
586 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
587 " indicating 64-bit DWARF extension");
588 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
589 "FDE Length");
590 ASM_OUTPUT_LABEL (asm_out_file, l1);
591
592 if (for_eh)
593 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
594 else
595 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
596 debug_frame_section, "FDE CIE offset");
597
598 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
599 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
600
601 if (for_eh)
602 {
603 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
604 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
605 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
606 "FDE initial location");
607 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
608 end, begin, "FDE address range");
609 }
610 else
611 {
612 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
613 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
614 }
615
616 if (augmentation[0])
617 {
618 if (any_lsda_needed)
619 {
620 int size = size_of_encoded_value (lsda_encoding);
621
622 if (lsda_encoding == DW_EH_PE_aligned)
623 {
624 int offset = ( 4 /* Length */
625 + 4 /* CIE offset */
626 + 2 * size_of_encoded_value (fde_encoding)
627 + 1 /* Augmentation size */ );
628 int pad = -offset & (PTR_SIZE - 1);
629
630 size += pad;
631 gcc_assert (size_of_uleb128 (size) == 1);
632 }
633
634 dw2_asm_output_data_uleb128 (size, "Augmentation size");
635
636 if (fde->uses_eh_lsda)
637 {
638 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
639 fde->funcdef_number);
640 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
641 gen_rtx_SYMBOL_REF (Pmode, l1),
642 false,
643 "Language Specific Data Area");
644 }
645 else
646 {
647 if (lsda_encoding == DW_EH_PE_aligned)
648 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
649 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
650 "Language Specific Data Area (none)");
651 }
652 }
653 else
654 dw2_asm_output_data_uleb128 (0, "Augmentation size");
655 }
656
657 /* Loop through the Call Frame Instructions associated with this FDE. */
658 fde->dw_fde_current_label = begin;
659 {
660 size_t from, until, i;
661
662 from = 0;
663 until = vec_safe_length (fde->dw_fde_cfi);
664
665 if (fde->dw_fde_second_begin == NULL)
666 ;
667 else if (!second)
668 until = fde->dw_fde_switch_cfi_index;
669 else
670 from = fde->dw_fde_switch_cfi_index;
671
672 for (i = from; i < until; i++)
673 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
674 }
675
676 /* If we are to emit a ref/link from function bodies to their frame tables,
677 do it now. This is typically performed to make sure that tables
678 associated with functions are dragged with them and not discarded in
679 garbage collecting links. We need to do this on a per function basis to
680 cope with -ffunction-sections. */
681
682 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
683 /* Switch to the function section, emit the ref to the tables, and
684 switch *back* into the table section. */
685 switch_to_section (function_section (fde->decl));
686 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
687 switch_to_frame_table_section (for_eh, true);
688 #endif
689
690 /* Pad the FDE out to an address sized boundary. */
691 ASM_OUTPUT_ALIGN (asm_out_file,
692 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
693 ASM_OUTPUT_LABEL (asm_out_file, l2);
694
695 j += 2;
696 }
697
698 /* Return true if frame description entry FDE is needed for EH. */
699
700 static bool
701 fde_needed_for_eh_p (dw_fde_ref fde)
702 {
703 if (flag_asynchronous_unwind_tables)
704 return true;
705
706 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
707 return true;
708
709 if (fde->uses_eh_lsda)
710 return true;
711
712 /* If exceptions are enabled, we have collected nothrow info. */
713 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
714 return false;
715
716 return true;
717 }
718
719 /* Output the call frame information used to record information
720 that relates to calculating the frame pointer, and records the
721 location of saved registers. */
722
723 static void
724 output_call_frame_info (int for_eh)
725 {
726 unsigned int i;
727 dw_fde_ref fde;
728 dw_cfi_ref cfi;
729 char l1[20], l2[20], section_start_label[20];
730 bool any_lsda_needed = false;
731 char augmentation[6];
732 int augmentation_size;
733 int fde_encoding = DW_EH_PE_absptr;
734 int per_encoding = DW_EH_PE_absptr;
735 int lsda_encoding = DW_EH_PE_absptr;
736 int return_reg;
737 rtx personality = NULL;
738 int dw_cie_version;
739
740 /* Don't emit a CIE if there won't be any FDEs. */
741 if (!fde_vec)
742 return;
743
744 /* Nothing to do if the assembler's doing it all. */
745 if (dwarf2out_do_cfi_asm ())
746 return;
747
748 /* If we don't have any functions we'll want to unwind out of, don't emit
749 any EH unwind information. If we make FDEs linkonce, we may have to
750 emit an empty label for an FDE that wouldn't otherwise be emitted. We
751 want to avoid having an FDE kept around when the function it refers to
752 is discarded. Example where this matters: a primary function template
753 in C++ requires EH information, an explicit specialization doesn't. */
754 if (for_eh)
755 {
756 bool any_eh_needed = false;
757
758 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
759 {
760 if (fde->uses_eh_lsda)
761 any_eh_needed = any_lsda_needed = true;
762 else if (fde_needed_for_eh_p (fde))
763 any_eh_needed = true;
764 else if (TARGET_USES_WEAK_UNWIND_INFO)
765 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
766 }
767
768 if (!any_eh_needed)
769 return;
770 }
771
772 /* We're going to be generating comments, so turn on app. */
773 if (flag_debug_asm)
774 app_enable ();
775
776 /* Switch to the proper frame section, first time. */
777 switch_to_frame_table_section (for_eh, false);
778
779 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
780 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
781
782 /* Output the CIE. */
783 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
784 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
785 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
786 dw2_asm_output_data (4, 0xffffffff,
787 "Initial length escape value indicating 64-bit DWARF extension");
788 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
789 "Length of Common Information Entry");
790 ASM_OUTPUT_LABEL (asm_out_file, l1);
791
792 /* Now that the CIE pointer is PC-relative for EH,
793 use 0 to identify the CIE. */
794 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
795 (for_eh ? 0 : DWARF_CIE_ID),
796 "CIE Identifier Tag");
797
798 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
799 use CIE version 1, unless that would produce incorrect results
800 due to overflowing the return register column. */
801 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
802 dw_cie_version = 1;
803 if (return_reg >= 256 || dwarf_version > 2)
804 dw_cie_version = 3;
805 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
806
807 augmentation[0] = 0;
808 augmentation_size = 0;
809
810 personality = current_unit_personality;
811 if (for_eh)
812 {
813 char *p;
814
815 /* Augmentation:
816 z Indicates that a uleb128 is present to size the
817 augmentation section.
818 L Indicates the encoding (and thus presence) of
819 an LSDA pointer in the FDE augmentation.
820 R Indicates a non-default pointer encoding for
821 FDE code pointers.
822 P Indicates the presence of an encoding + language
823 personality routine in the CIE augmentation. */
824
825 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
826 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
827 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
828
829 p = augmentation + 1;
830 if (personality)
831 {
832 *p++ = 'P';
833 augmentation_size += 1 + size_of_encoded_value (per_encoding);
834 assemble_external_libcall (personality);
835 }
836 if (any_lsda_needed)
837 {
838 *p++ = 'L';
839 augmentation_size += 1;
840 }
841 if (fde_encoding != DW_EH_PE_absptr)
842 {
843 *p++ = 'R';
844 augmentation_size += 1;
845 }
846 if (p > augmentation + 1)
847 {
848 augmentation[0] = 'z';
849 *p = '\0';
850 }
851
852 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
853 if (personality && per_encoding == DW_EH_PE_aligned)
854 {
855 int offset = ( 4 /* Length */
856 + 4 /* CIE Id */
857 + 1 /* CIE version */
858 + strlen (augmentation) + 1 /* Augmentation */
859 + size_of_uleb128 (1) /* Code alignment */
860 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
861 + 1 /* RA column */
862 + 1 /* Augmentation size */
863 + 1 /* Personality encoding */ );
864 int pad = -offset & (PTR_SIZE - 1);
865
866 augmentation_size += pad;
867
868 /* Augmentations should be small, so there's scarce need to
869 iterate for a solution. Die if we exceed one uleb128 byte. */
870 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
871 }
872 }
873
874 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
875 if (dw_cie_version >= 4)
876 {
877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
878 dw2_asm_output_data (1, 0, "CIE Segment Size");
879 }
880 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
881 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
882 "CIE Data Alignment Factor");
883
884 if (dw_cie_version == 1)
885 dw2_asm_output_data (1, return_reg, "CIE RA Column");
886 else
887 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
888
889 if (augmentation[0])
890 {
891 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
892 if (personality)
893 {
894 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
895 eh_data_format_name (per_encoding));
896 dw2_asm_output_encoded_addr_rtx (per_encoding,
897 personality,
898 true, NULL);
899 }
900
901 if (any_lsda_needed)
902 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
903 eh_data_format_name (lsda_encoding));
904
905 if (fde_encoding != DW_EH_PE_absptr)
906 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
907 eh_data_format_name (fde_encoding));
908 }
909
910 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
911 output_cfi (cfi, NULL, for_eh);
912
913 /* Pad the CIE out to an address sized boundary. */
914 ASM_OUTPUT_ALIGN (asm_out_file,
915 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
916 ASM_OUTPUT_LABEL (asm_out_file, l2);
917
918 /* Loop through all of the FDE's. */
919 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
920 {
921 unsigned int k;
922
923 /* Don't emit EH unwind info for leaf functions that don't need it. */
924 if (for_eh && !fde_needed_for_eh_p (fde))
925 continue;
926
927 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
928 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
929 augmentation, any_lsda_needed, lsda_encoding);
930 }
931
932 if (for_eh && targetm.terminate_dw2_eh_frame_info)
933 dw2_asm_output_data (4, 0, "End of Table");
934
935 /* Turn off app to make assembly quicker. */
936 if (flag_debug_asm)
937 app_disable ();
938 }
939
940 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
941
942 static void
943 dwarf2out_do_cfi_startproc (bool second)
944 {
945 int enc;
946 rtx ref;
947 rtx personality = get_personality_function (current_function_decl);
948
949 fprintf (asm_out_file, "\t.cfi_startproc\n");
950
951 if (personality)
952 {
953 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
954 ref = personality;
955
956 /* ??? The GAS support isn't entirely consistent. We have to
957 handle indirect support ourselves, but PC-relative is done
958 in the assembler. Further, the assembler can't handle any
959 of the weirder relocation types. */
960 if (enc & DW_EH_PE_indirect)
961 ref = dw2_force_const_mem (ref, true);
962
963 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
964 output_addr_const (asm_out_file, ref);
965 fputc ('\n', asm_out_file);
966 }
967
968 if (crtl->uses_eh_lsda)
969 {
970 char lab[20];
971
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
973 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
974 current_function_funcdef_no);
975 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
976 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
977
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
980
981 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
984 }
985 }
986
987 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
988 this allocation may be done before pass_final. */
989
990 dw_fde_ref
991 dwarf2out_alloc_current_fde (void)
992 {
993 dw_fde_ref fde;
994
995 fde = ggc_cleared_alloc<dw_fde_node> ();
996 fde->decl = current_function_decl;
997 fde->funcdef_number = current_function_funcdef_no;
998 fde->fde_index = vec_safe_length (fde_vec);
999 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1000 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1001 fde->nothrow = crtl->nothrow;
1002 fde->drap_reg = INVALID_REGNUM;
1003 fde->vdrap_reg = INVALID_REGNUM;
1004
1005 /* Record the FDE associated with this function. */
1006 cfun->fde = fde;
1007 vec_safe_push (fde_vec, fde);
1008
1009 return fde;
1010 }
1011
1012 /* Output a marker (i.e. a label) for the beginning of a function, before
1013 the prologue. */
1014
1015 void
1016 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1017 const char *file ATTRIBUTE_UNUSED)
1018 {
1019 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1020 char * dup_label;
1021 dw_fde_ref fde;
1022 section *fnsec;
1023 bool do_frame;
1024
1025 current_function_func_begin_label = NULL;
1026
1027 do_frame = dwarf2out_do_frame ();
1028
1029 /* ??? current_function_func_begin_label is also used by except.c for
1030 call-site information. We must emit this label if it might be used. */
1031 if (!do_frame
1032 && (!flag_exceptions
1033 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1034 return;
1035
1036 fnsec = function_section (current_function_decl);
1037 switch_to_section (fnsec);
1038 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1039 current_function_funcdef_no);
1040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 dup_label = xstrdup (label);
1043 current_function_func_begin_label = dup_label;
1044
1045 /* We can elide the fde allocation if we're not emitting debug info. */
1046 if (!do_frame)
1047 return;
1048
1049 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1050 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1051 would include pass_dwarf2_frame. If we've not created the FDE yet,
1052 do so now. */
1053 fde = cfun->fde;
1054 if (fde == NULL)
1055 fde = dwarf2out_alloc_current_fde ();
1056
1057 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1058 fde->dw_fde_begin = dup_label;
1059 fde->dw_fde_current_label = dup_label;
1060 fde->in_std_section = (fnsec == text_section
1061 || (cold_text_section && fnsec == cold_text_section));
1062
1063 /* We only want to output line number information for the genuine dwarf2
1064 prologue case, not the eh frame case. */
1065 #ifdef DWARF2_DEBUGGING_INFO
1066 if (file)
1067 dwarf2out_source_line (line, file, 0, true);
1068 #endif
1069
1070 if (dwarf2out_do_cfi_asm ())
1071 dwarf2out_do_cfi_startproc (false);
1072 else
1073 {
1074 rtx personality = get_personality_function (current_function_decl);
1075 if (!current_unit_personality)
1076 current_unit_personality = personality;
1077
1078 /* We cannot keep a current personality per function as without CFI
1079 asm, at the point where we emit the CFI data, there is no current
1080 function anymore. */
1081 if (personality && current_unit_personality != personality)
1082 sorry ("multiple EH personalities are supported only with assemblers "
1083 "supporting .cfi_personality directive");
1084 }
1085 }
1086
1087 /* Output a marker (i.e. a label) for the end of the generated code
1088 for a function prologue. This gets called *after* the prologue code has
1089 been generated. */
1090
1091 void
1092 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1093 const char *file ATTRIBUTE_UNUSED)
1094 {
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1096
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1104 }
1105
1106 /* Output a marker (i.e. a label) for the beginning of the generated code
1107 for a function epilogue. This gets called *before* the prologue code has
1108 been generated. */
1109
1110 void
1111 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1113 {
1114 dw_fde_ref fde = cfun->fde;
1115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1116
1117 if (fde->dw_fde_vms_begin_epilogue)
1118 return;
1119
1120 /* Output a label to mark the endpoint of the code generated for this
1121 function. */
1122 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1123 current_function_funcdef_no);
1124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1127 }
1128
1129 /* Output a marker (i.e. a label) for the absolute end of the generated code
1130 for a function definition. This gets called *after* the epilogue code has
1131 been generated. */
1132
1133 void
1134 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1135 const char *file ATTRIBUTE_UNUSED)
1136 {
1137 dw_fde_ref fde;
1138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1139
1140 last_var_location_insn = NULL_RTX;
1141 cached_next_real_insn = NULL_RTX;
1142
1143 if (dwarf2out_do_cfi_asm ())
1144 fprintf (asm_out_file, "\t.cfi_endproc\n");
1145
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_LABEL (asm_out_file, label);
1151 fde = cfun->fde;
1152 gcc_assert (fde != NULL);
1153 if (fde->dw_fde_second_begin == NULL)
1154 fde->dw_fde_end = xstrdup (label);
1155 }
1156
1157 void
1158 dwarf2out_frame_finish (void)
1159 {
1160 /* Output call frame information. */
1161 if (targetm.debug_unwind_info () == UI_DWARF2)
1162 output_call_frame_info (0);
1163
1164 /* Output another copy for the unwinder. */
1165 if ((flag_unwind_tables || flag_exceptions)
1166 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1167 output_call_frame_info (1);
1168 }
1169
1170 /* Note that the current function section is being used for code. */
1171
1172 static void
1173 dwarf2out_note_section_used (void)
1174 {
1175 section *sec = current_function_section ();
1176 if (sec == text_section)
1177 text_section_used = true;
1178 else if (sec == cold_text_section)
1179 cold_text_section_used = true;
1180 }
1181
1182 static void var_location_switch_text_section (void);
1183 static void set_cur_line_info_table (section *);
1184
1185 void
1186 dwarf2out_switch_text_section (void)
1187 {
1188 section *sect;
1189 dw_fde_ref fde = cfun->fde;
1190
1191 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1192
1193 if (!in_cold_section_p)
1194 {
1195 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1196 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1197 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1198 }
1199 else
1200 {
1201 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1202 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1203 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1204 }
1205 have_multiple_function_sections = true;
1206
1207 /* There is no need to mark used sections when not debugging. */
1208 if (cold_text_section != NULL)
1209 dwarf2out_note_section_used ();
1210
1211 if (dwarf2out_do_cfi_asm ())
1212 fprintf (asm_out_file, "\t.cfi_endproc\n");
1213
1214 /* Now do the real section switch. */
1215 sect = current_function_section ();
1216 switch_to_section (sect);
1217
1218 fde->second_in_std_section
1219 = (sect == text_section
1220 || (cold_text_section && sect == cold_text_section));
1221
1222 if (dwarf2out_do_cfi_asm ())
1223 dwarf2out_do_cfi_startproc (true);
1224
1225 var_location_switch_text_section ();
1226
1227 if (cold_text_section != NULL)
1228 set_cur_line_info_table (sect);
1229 }
1230 \f
1231 /* And now, the subset of the debugging information support code necessary
1232 for emitting location expressions. */
1233
1234 /* Data about a single source file. */
1235 struct GTY(()) dwarf_file_data {
1236 const char * filename;
1237 int emitted_number;
1238 };
1239
1240 typedef struct GTY(()) deferred_locations_struct
1241 {
1242 tree variable;
1243 dw_die_ref die;
1244 } deferred_locations;
1245
1246
1247 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1248
1249
1250 /* Describe an entry into the .debug_addr section. */
1251
1252 enum ate_kind {
1253 ate_kind_rtx,
1254 ate_kind_rtx_dtprel,
1255 ate_kind_label
1256 };
1257
1258 typedef struct GTY(()) addr_table_entry_struct {
1259 enum ate_kind kind;
1260 unsigned int refcount;
1261 unsigned int index;
1262 union addr_table_entry_struct_union
1263 {
1264 rtx GTY ((tag ("0"))) rtl;
1265 char * GTY ((tag ("1"))) label;
1266 }
1267 GTY ((desc ("%1.kind"))) addr;
1268 }
1269 addr_table_entry;
1270
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 bool emitted;
1290 /* True if the range should be emitted even if begin and end
1291 are the same. */
1292 bool force;
1293 } dw_loc_list_node;
1294
1295 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1296
1297 /* Convert a DWARF stack opcode into its string name. */
1298
1299 static const char *
1300 dwarf_stack_op_name (unsigned int op)
1301 {
1302 const char *name = get_DW_OP_name (op);
1303
1304 if (name != NULL)
1305 return name;
1306
1307 return "OP_<unknown>";
1308 }
1309
1310 /* Return a pointer to a newly allocated location description. Location
1311 descriptions are simple expression terms that can be strung
1312 together to form more complicated location (address) descriptions. */
1313
1314 static inline dw_loc_descr_ref
1315 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1316 unsigned HOST_WIDE_INT oprnd2)
1317 {
1318 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1319
1320 descr->dw_loc_opc = op;
1321 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1322 descr->dw_loc_oprnd1.val_entry = NULL;
1323 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1324 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1325 descr->dw_loc_oprnd2.val_entry = NULL;
1326 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1327
1328 return descr;
1329 }
1330
1331 /* Return a pointer to a newly allocated location description for
1332 REG and OFFSET. */
1333
1334 static inline dw_loc_descr_ref
1335 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1336 {
1337 if (reg <= 31)
1338 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1339 offset, 0);
1340 else
1341 return new_loc_descr (DW_OP_bregx, reg, offset);
1342 }
1343
1344 /* Add a location description term to a location description expression. */
1345
1346 static inline void
1347 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1348 {
1349 dw_loc_descr_ref *d;
1350
1351 /* Find the end of the chain. */
1352 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1353 ;
1354
1355 *d = descr;
1356 }
1357
1358 /* Compare two location operands for exact equality. */
1359
1360 static bool
1361 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1362 {
1363 if (a->val_class != b->val_class)
1364 return false;
1365 switch (a->val_class)
1366 {
1367 case dw_val_class_none:
1368 return true;
1369 case dw_val_class_addr:
1370 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1371
1372 case dw_val_class_offset:
1373 case dw_val_class_unsigned_const:
1374 case dw_val_class_const:
1375 case dw_val_class_range_list:
1376 case dw_val_class_lineptr:
1377 case dw_val_class_macptr:
1378 /* These are all HOST_WIDE_INT, signed or unsigned. */
1379 return a->v.val_unsigned == b->v.val_unsigned;
1380
1381 case dw_val_class_loc:
1382 return a->v.val_loc == b->v.val_loc;
1383 case dw_val_class_loc_list:
1384 return a->v.val_loc_list == b->v.val_loc_list;
1385 case dw_val_class_die_ref:
1386 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1387 case dw_val_class_fde_ref:
1388 return a->v.val_fde_index == b->v.val_fde_index;
1389 case dw_val_class_lbl_id:
1390 case dw_val_class_high_pc:
1391 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1392 case dw_val_class_str:
1393 return a->v.val_str == b->v.val_str;
1394 case dw_val_class_flag:
1395 return a->v.val_flag == b->v.val_flag;
1396 case dw_val_class_file:
1397 return a->v.val_file == b->v.val_file;
1398 case dw_val_class_decl_ref:
1399 return a->v.val_decl_ref == b->v.val_decl_ref;
1400
1401 case dw_val_class_const_double:
1402 return (a->v.val_double.high == b->v.val_double.high
1403 && a->v.val_double.low == b->v.val_double.low);
1404
1405 case dw_val_class_wide_int:
1406 return *a->v.val_wide == *b->v.val_wide;
1407
1408 case dw_val_class_vec:
1409 {
1410 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1411 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1412
1413 return (a_len == b_len
1414 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1415 }
1416
1417 case dw_val_class_data8:
1418 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1419
1420 case dw_val_class_vms_delta:
1421 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1422 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1423 }
1424 gcc_unreachable ();
1425 }
1426
1427 /* Compare two location atoms for exact equality. */
1428
1429 static bool
1430 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1431 {
1432 if (a->dw_loc_opc != b->dw_loc_opc)
1433 return false;
1434
1435 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1436 address size, but since we always allocate cleared storage it
1437 should be zero for other types of locations. */
1438 if (a->dtprel != b->dtprel)
1439 return false;
1440
1441 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1442 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1443 }
1444
1445 /* Compare two complete location expressions for exact equality. */
1446
1447 bool
1448 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1449 {
1450 while (1)
1451 {
1452 if (a == b)
1453 return true;
1454 if (a == NULL || b == NULL)
1455 return false;
1456 if (!loc_descr_equal_p_1 (a, b))
1457 return false;
1458
1459 a = a->dw_loc_next;
1460 b = b->dw_loc_next;
1461 }
1462 }
1463
1464
1465 /* Add a constant OFFSET to a location expression. */
1466
1467 static void
1468 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1469 {
1470 dw_loc_descr_ref loc;
1471 HOST_WIDE_INT *p;
1472
1473 gcc_assert (*list_head != NULL);
1474
1475 if (!offset)
1476 return;
1477
1478 /* Find the end of the chain. */
1479 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1480 ;
1481
1482 p = NULL;
1483 if (loc->dw_loc_opc == DW_OP_fbreg
1484 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1485 p = &loc->dw_loc_oprnd1.v.val_int;
1486 else if (loc->dw_loc_opc == DW_OP_bregx)
1487 p = &loc->dw_loc_oprnd2.v.val_int;
1488
1489 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1490 offset. Don't optimize if an signed integer overflow would happen. */
1491 if (p != NULL
1492 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1493 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1494 *p += offset;
1495
1496 else if (offset > 0)
1497 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1498
1499 else
1500 {
1501 loc->dw_loc_next = int_loc_descriptor (-offset);
1502 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1503 }
1504 }
1505
1506 /* Add a constant OFFSET to a location list. */
1507
1508 static void
1509 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1510 {
1511 dw_loc_list_ref d;
1512 for (d = list_head; d != NULL; d = d->dw_loc_next)
1513 loc_descr_plus_const (&d->expr, offset);
1514 }
1515
1516 #define DWARF_REF_SIZE \
1517 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1518
1519 static unsigned long int get_base_type_offset (dw_die_ref);
1520
1521 /* Return the size of a location descriptor. */
1522
1523 static unsigned long
1524 size_of_loc_descr (dw_loc_descr_ref loc)
1525 {
1526 unsigned long size = 1;
1527
1528 switch (loc->dw_loc_opc)
1529 {
1530 case DW_OP_addr:
1531 size += DWARF2_ADDR_SIZE;
1532 break;
1533 case DW_OP_GNU_addr_index:
1534 case DW_OP_GNU_const_index:
1535 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1537 break;
1538 case DW_OP_const1u:
1539 case DW_OP_const1s:
1540 size += 1;
1541 break;
1542 case DW_OP_const2u:
1543 case DW_OP_const2s:
1544 size += 2;
1545 break;
1546 case DW_OP_const4u:
1547 case DW_OP_const4s:
1548 size += 4;
1549 break;
1550 case DW_OP_const8u:
1551 case DW_OP_const8s:
1552 size += 8;
1553 break;
1554 case DW_OP_constu:
1555 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1556 break;
1557 case DW_OP_consts:
1558 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1559 break;
1560 case DW_OP_pick:
1561 size += 1;
1562 break;
1563 case DW_OP_plus_uconst:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_skip:
1567 case DW_OP_bra:
1568 size += 2;
1569 break;
1570 case DW_OP_breg0:
1571 case DW_OP_breg1:
1572 case DW_OP_breg2:
1573 case DW_OP_breg3:
1574 case DW_OP_breg4:
1575 case DW_OP_breg5:
1576 case DW_OP_breg6:
1577 case DW_OP_breg7:
1578 case DW_OP_breg8:
1579 case DW_OP_breg9:
1580 case DW_OP_breg10:
1581 case DW_OP_breg11:
1582 case DW_OP_breg12:
1583 case DW_OP_breg13:
1584 case DW_OP_breg14:
1585 case DW_OP_breg15:
1586 case DW_OP_breg16:
1587 case DW_OP_breg17:
1588 case DW_OP_breg18:
1589 case DW_OP_breg19:
1590 case DW_OP_breg20:
1591 case DW_OP_breg21:
1592 case DW_OP_breg22:
1593 case DW_OP_breg23:
1594 case DW_OP_breg24:
1595 case DW_OP_breg25:
1596 case DW_OP_breg26:
1597 case DW_OP_breg27:
1598 case DW_OP_breg28:
1599 case DW_OP_breg29:
1600 case DW_OP_breg30:
1601 case DW_OP_breg31:
1602 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1603 break;
1604 case DW_OP_regx:
1605 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1606 break;
1607 case DW_OP_fbreg:
1608 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1609 break;
1610 case DW_OP_bregx:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1613 break;
1614 case DW_OP_piece:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 break;
1617 case DW_OP_bit_piece:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1620 break;
1621 case DW_OP_deref_size:
1622 case DW_OP_xderef_size:
1623 size += 1;
1624 break;
1625 case DW_OP_call2:
1626 size += 2;
1627 break;
1628 case DW_OP_call4:
1629 size += 4;
1630 break;
1631 case DW_OP_call_ref:
1632 size += DWARF_REF_SIZE;
1633 break;
1634 case DW_OP_implicit_value:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1636 + loc->dw_loc_oprnd1.v.val_unsigned;
1637 break;
1638 case DW_OP_GNU_implicit_pointer:
1639 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1640 break;
1641 case DW_OP_GNU_entry_value:
1642 {
1643 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1644 size += size_of_uleb128 (op_size) + op_size;
1645 break;
1646 }
1647 case DW_OP_GNU_const_type:
1648 {
1649 unsigned long o
1650 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1651 size += size_of_uleb128 (o) + 1;
1652 switch (loc->dw_loc_oprnd2.val_class)
1653 {
1654 case dw_val_class_vec:
1655 size += loc->dw_loc_oprnd2.v.val_vec.length
1656 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1657 break;
1658 case dw_val_class_const:
1659 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1660 break;
1661 case dw_val_class_const_double:
1662 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1663 break;
1664 case dw_val_class_wide_int:
1665 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1666 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1667 break;
1668 default:
1669 gcc_unreachable ();
1670 }
1671 break;
1672 }
1673 case DW_OP_GNU_regval_type:
1674 {
1675 unsigned long o
1676 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1677 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1678 + size_of_uleb128 (o);
1679 }
1680 break;
1681 case DW_OP_GNU_deref_type:
1682 {
1683 unsigned long o
1684 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1685 size += 1 + size_of_uleb128 (o);
1686 }
1687 break;
1688 case DW_OP_GNU_convert:
1689 case DW_OP_GNU_reinterpret:
1690 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1691 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1692 else
1693 {
1694 unsigned long o
1695 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1696 size += size_of_uleb128 (o);
1697 }
1698 break;
1699 case DW_OP_GNU_parameter_ref:
1700 size += 4;
1701 break;
1702 default:
1703 break;
1704 }
1705
1706 return size;
1707 }
1708
1709 /* Return the size of a series of location descriptors. */
1710
1711 unsigned long
1712 size_of_locs (dw_loc_descr_ref loc)
1713 {
1714 dw_loc_descr_ref l;
1715 unsigned long size;
1716
1717 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1718 field, to avoid writing to a PCH file. */
1719 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1720 {
1721 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1722 break;
1723 size += size_of_loc_descr (l);
1724 }
1725 if (! l)
1726 return size;
1727
1728 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1729 {
1730 l->dw_loc_addr = size;
1731 size += size_of_loc_descr (l);
1732 }
1733
1734 return size;
1735 }
1736
1737 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1738 static void get_ref_die_offset_label (char *, dw_die_ref);
1739 static unsigned long int get_ref_die_offset (dw_die_ref);
1740
1741 /* Output location description stack opcode's operands (if any).
1742 The for_eh_or_skip parameter controls whether register numbers are
1743 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1744 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1745 info). This should be suppressed for the cases that have not been converted
1746 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1747
1748 static void
1749 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1750 {
1751 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1752 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1753
1754 switch (loc->dw_loc_opc)
1755 {
1756 #ifdef DWARF2_DEBUGGING_INFO
1757 case DW_OP_const2u:
1758 case DW_OP_const2s:
1759 dw2_asm_output_data (2, val1->v.val_int, NULL);
1760 break;
1761 case DW_OP_const4u:
1762 if (loc->dtprel)
1763 {
1764 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1765 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1766 val1->v.val_addr);
1767 fputc ('\n', asm_out_file);
1768 break;
1769 }
1770 /* FALLTHRU */
1771 case DW_OP_const4s:
1772 dw2_asm_output_data (4, val1->v.val_int, NULL);
1773 break;
1774 case DW_OP_const8u:
1775 if (loc->dtprel)
1776 {
1777 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1778 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1779 val1->v.val_addr);
1780 fputc ('\n', asm_out_file);
1781 break;
1782 }
1783 /* FALLTHRU */
1784 case DW_OP_const8s:
1785 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1786 dw2_asm_output_data (8, val1->v.val_int, NULL);
1787 break;
1788 case DW_OP_skip:
1789 case DW_OP_bra:
1790 {
1791 int offset;
1792
1793 gcc_assert (val1->val_class == dw_val_class_loc);
1794 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1795
1796 dw2_asm_output_data (2, offset, NULL);
1797 }
1798 break;
1799 case DW_OP_implicit_value:
1800 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1801 switch (val2->val_class)
1802 {
1803 case dw_val_class_const:
1804 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1805 break;
1806 case dw_val_class_vec:
1807 {
1808 unsigned int elt_size = val2->v.val_vec.elt_size;
1809 unsigned int len = val2->v.val_vec.length;
1810 unsigned int i;
1811 unsigned char *p;
1812
1813 if (elt_size > sizeof (HOST_WIDE_INT))
1814 {
1815 elt_size /= 2;
1816 len *= 2;
1817 }
1818 for (i = 0, p = val2->v.val_vec.array;
1819 i < len;
1820 i++, p += elt_size)
1821 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1822 "fp or vector constant word %u", i);
1823 }
1824 break;
1825 case dw_val_class_const_double:
1826 {
1827 unsigned HOST_WIDE_INT first, second;
1828
1829 if (WORDS_BIG_ENDIAN)
1830 {
1831 first = val2->v.val_double.high;
1832 second = val2->v.val_double.low;
1833 }
1834 else
1835 {
1836 first = val2->v.val_double.low;
1837 second = val2->v.val_double.high;
1838 }
1839 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1840 first, NULL);
1841 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842 second, NULL);
1843 }
1844 break;
1845 case dw_val_class_wide_int:
1846 {
1847 int i;
1848 int len = get_full_len (*val2->v.val_wide);
1849 if (WORDS_BIG_ENDIAN)
1850 for (i = len - 1; i >= 0; --i)
1851 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1852 val2->v.val_wide->elt (i), NULL);
1853 else
1854 for (i = 0; i < len; ++i)
1855 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1856 val2->v.val_wide->elt (i), NULL);
1857 }
1858 break;
1859 case dw_val_class_addr:
1860 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1861 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1862 break;
1863 default:
1864 gcc_unreachable ();
1865 }
1866 break;
1867 #else
1868 case DW_OP_const2u:
1869 case DW_OP_const2s:
1870 case DW_OP_const4u:
1871 case DW_OP_const4s:
1872 case DW_OP_const8u:
1873 case DW_OP_const8s:
1874 case DW_OP_skip:
1875 case DW_OP_bra:
1876 case DW_OP_implicit_value:
1877 /* We currently don't make any attempt to make sure these are
1878 aligned properly like we do for the main unwind info, so
1879 don't support emitting things larger than a byte if we're
1880 only doing unwinding. */
1881 gcc_unreachable ();
1882 #endif
1883 case DW_OP_const1u:
1884 case DW_OP_const1s:
1885 dw2_asm_output_data (1, val1->v.val_int, NULL);
1886 break;
1887 case DW_OP_constu:
1888 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1889 break;
1890 case DW_OP_consts:
1891 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1892 break;
1893 case DW_OP_pick:
1894 dw2_asm_output_data (1, val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_plus_uconst:
1897 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1898 break;
1899 case DW_OP_breg0:
1900 case DW_OP_breg1:
1901 case DW_OP_breg2:
1902 case DW_OP_breg3:
1903 case DW_OP_breg4:
1904 case DW_OP_breg5:
1905 case DW_OP_breg6:
1906 case DW_OP_breg7:
1907 case DW_OP_breg8:
1908 case DW_OP_breg9:
1909 case DW_OP_breg10:
1910 case DW_OP_breg11:
1911 case DW_OP_breg12:
1912 case DW_OP_breg13:
1913 case DW_OP_breg14:
1914 case DW_OP_breg15:
1915 case DW_OP_breg16:
1916 case DW_OP_breg17:
1917 case DW_OP_breg18:
1918 case DW_OP_breg19:
1919 case DW_OP_breg20:
1920 case DW_OP_breg21:
1921 case DW_OP_breg22:
1922 case DW_OP_breg23:
1923 case DW_OP_breg24:
1924 case DW_OP_breg25:
1925 case DW_OP_breg26:
1926 case DW_OP_breg27:
1927 case DW_OP_breg28:
1928 case DW_OP_breg29:
1929 case DW_OP_breg30:
1930 case DW_OP_breg31:
1931 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1932 break;
1933 case DW_OP_regx:
1934 {
1935 unsigned r = val1->v.val_unsigned;
1936 if (for_eh_or_skip >= 0)
1937 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1938 gcc_assert (size_of_uleb128 (r)
1939 == size_of_uleb128 (val1->v.val_unsigned));
1940 dw2_asm_output_data_uleb128 (r, NULL);
1941 }
1942 break;
1943 case DW_OP_fbreg:
1944 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1945 break;
1946 case DW_OP_bregx:
1947 {
1948 unsigned r = val1->v.val_unsigned;
1949 if (for_eh_or_skip >= 0)
1950 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1951 gcc_assert (size_of_uleb128 (r)
1952 == size_of_uleb128 (val1->v.val_unsigned));
1953 dw2_asm_output_data_uleb128 (r, NULL);
1954 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1955 }
1956 break;
1957 case DW_OP_piece:
1958 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1959 break;
1960 case DW_OP_bit_piece:
1961 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1962 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1963 break;
1964 case DW_OP_deref_size:
1965 case DW_OP_xderef_size:
1966 dw2_asm_output_data (1, val1->v.val_int, NULL);
1967 break;
1968
1969 case DW_OP_addr:
1970 if (loc->dtprel)
1971 {
1972 if (targetm.asm_out.output_dwarf_dtprel)
1973 {
1974 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1975 DWARF2_ADDR_SIZE,
1976 val1->v.val_addr);
1977 fputc ('\n', asm_out_file);
1978 }
1979 else
1980 gcc_unreachable ();
1981 }
1982 else
1983 {
1984 #ifdef DWARF2_DEBUGGING_INFO
1985 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1986 #else
1987 gcc_unreachable ();
1988 #endif
1989 }
1990 break;
1991
1992 case DW_OP_GNU_addr_index:
1993 case DW_OP_GNU_const_index:
1994 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1995 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1996 "(index into .debug_addr)");
1997 break;
1998
1999 case DW_OP_GNU_implicit_pointer:
2000 {
2001 char label[MAX_ARTIFICIAL_LABEL_BYTES
2002 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2003 gcc_assert (val1->val_class == dw_val_class_die_ref);
2004 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2005 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2006 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2007 }
2008 break;
2009
2010 case DW_OP_GNU_entry_value:
2011 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2012 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2013 break;
2014
2015 case DW_OP_GNU_const_type:
2016 {
2017 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2018 gcc_assert (o);
2019 dw2_asm_output_data_uleb128 (o, NULL);
2020 switch (val2->val_class)
2021 {
2022 case dw_val_class_const:
2023 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2024 dw2_asm_output_data (1, l, NULL);
2025 dw2_asm_output_data (l, val2->v.val_int, NULL);
2026 break;
2027 case dw_val_class_vec:
2028 {
2029 unsigned int elt_size = val2->v.val_vec.elt_size;
2030 unsigned int len = val2->v.val_vec.length;
2031 unsigned int i;
2032 unsigned char *p;
2033
2034 l = len * elt_size;
2035 dw2_asm_output_data (1, l, NULL);
2036 if (elt_size > sizeof (HOST_WIDE_INT))
2037 {
2038 elt_size /= 2;
2039 len *= 2;
2040 }
2041 for (i = 0, p = val2->v.val_vec.array;
2042 i < len;
2043 i++, p += elt_size)
2044 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2045 "fp or vector constant word %u", i);
2046 }
2047 break;
2048 case dw_val_class_const_double:
2049 {
2050 unsigned HOST_WIDE_INT first, second;
2051 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2052
2053 dw2_asm_output_data (1, 2 * l, NULL);
2054 if (WORDS_BIG_ENDIAN)
2055 {
2056 first = val2->v.val_double.high;
2057 second = val2->v.val_double.low;
2058 }
2059 else
2060 {
2061 first = val2->v.val_double.low;
2062 second = val2->v.val_double.high;
2063 }
2064 dw2_asm_output_data (l, first, NULL);
2065 dw2_asm_output_data (l, second, NULL);
2066 }
2067 break;
2068 case dw_val_class_wide_int:
2069 {
2070 int i;
2071 int len = get_full_len (*val2->v.val_wide);
2072 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2073
2074 dw2_asm_output_data (1, len * l, NULL);
2075 if (WORDS_BIG_ENDIAN)
2076 for (i = len - 1; i >= 0; --i)
2077 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2078 else
2079 for (i = 0; i < len; ++i)
2080 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2081 }
2082 break;
2083 default:
2084 gcc_unreachable ();
2085 }
2086 }
2087 break;
2088 case DW_OP_GNU_regval_type:
2089 {
2090 unsigned r = val1->v.val_unsigned;
2091 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2092 gcc_assert (o);
2093 if (for_eh_or_skip >= 0)
2094 {
2095 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2096 gcc_assert (size_of_uleb128 (r)
2097 == size_of_uleb128 (val1->v.val_unsigned));
2098 }
2099 dw2_asm_output_data_uleb128 (r, NULL);
2100 dw2_asm_output_data_uleb128 (o, NULL);
2101 }
2102 break;
2103 case DW_OP_GNU_deref_type:
2104 {
2105 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2106 gcc_assert (o);
2107 dw2_asm_output_data (1, val1->v.val_int, NULL);
2108 dw2_asm_output_data_uleb128 (o, NULL);
2109 }
2110 break;
2111 case DW_OP_GNU_convert:
2112 case DW_OP_GNU_reinterpret:
2113 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2114 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115 else
2116 {
2117 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2118 gcc_assert (o);
2119 dw2_asm_output_data_uleb128 (o, NULL);
2120 }
2121 break;
2122
2123 case DW_OP_GNU_parameter_ref:
2124 {
2125 unsigned long o;
2126 gcc_assert (val1->val_class == dw_val_class_die_ref);
2127 o = get_ref_die_offset (val1->v.val_die_ref.die);
2128 dw2_asm_output_data (4, o, NULL);
2129 }
2130 break;
2131
2132 default:
2133 /* Other codes have no operands. */
2134 break;
2135 }
2136 }
2137
2138 /* Output a sequence of location operations.
2139 The for_eh_or_skip parameter controls whether register numbers are
2140 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2141 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2142 info). This should be suppressed for the cases that have not been converted
2143 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2144
2145 void
2146 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2147 {
2148 for (; loc != NULL; loc = loc->dw_loc_next)
2149 {
2150 enum dwarf_location_atom opc = loc->dw_loc_opc;
2151 /* Output the opcode. */
2152 if (for_eh_or_skip >= 0
2153 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2154 {
2155 unsigned r = (opc - DW_OP_breg0);
2156 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2157 gcc_assert (r <= 31);
2158 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2159 }
2160 else if (for_eh_or_skip >= 0
2161 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2162 {
2163 unsigned r = (opc - DW_OP_reg0);
2164 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2165 gcc_assert (r <= 31);
2166 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2167 }
2168
2169 dw2_asm_output_data (1, opc,
2170 "%s", dwarf_stack_op_name (opc));
2171
2172 /* Output the operand(s) (if any). */
2173 output_loc_operands (loc, for_eh_or_skip);
2174 }
2175 }
2176
2177 /* Output location description stack opcode's operands (if any).
2178 The output is single bytes on a line, suitable for .cfi_escape. */
2179
2180 static void
2181 output_loc_operands_raw (dw_loc_descr_ref loc)
2182 {
2183 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2184 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2185
2186 switch (loc->dw_loc_opc)
2187 {
2188 case DW_OP_addr:
2189 case DW_OP_GNU_addr_index:
2190 case DW_OP_GNU_const_index:
2191 case DW_OP_implicit_value:
2192 /* We cannot output addresses in .cfi_escape, only bytes. */
2193 gcc_unreachable ();
2194
2195 case DW_OP_const1u:
2196 case DW_OP_const1s:
2197 case DW_OP_pick:
2198 case DW_OP_deref_size:
2199 case DW_OP_xderef_size:
2200 fputc (',', asm_out_file);
2201 dw2_asm_output_data_raw (1, val1->v.val_int);
2202 break;
2203
2204 case DW_OP_const2u:
2205 case DW_OP_const2s:
2206 fputc (',', asm_out_file);
2207 dw2_asm_output_data_raw (2, val1->v.val_int);
2208 break;
2209
2210 case DW_OP_const4u:
2211 case DW_OP_const4s:
2212 fputc (',', asm_out_file);
2213 dw2_asm_output_data_raw (4, val1->v.val_int);
2214 break;
2215
2216 case DW_OP_const8u:
2217 case DW_OP_const8s:
2218 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (8, val1->v.val_int);
2221 break;
2222
2223 case DW_OP_skip:
2224 case DW_OP_bra:
2225 {
2226 int offset;
2227
2228 gcc_assert (val1->val_class == dw_val_class_loc);
2229 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2230
2231 fputc (',', asm_out_file);
2232 dw2_asm_output_data_raw (2, offset);
2233 }
2234 break;
2235
2236 case DW_OP_regx:
2237 {
2238 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2239 gcc_assert (size_of_uleb128 (r)
2240 == size_of_uleb128 (val1->v.val_unsigned));
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_uleb128_raw (r);
2243 }
2244 break;
2245
2246 case DW_OP_constu:
2247 case DW_OP_plus_uconst:
2248 case DW_OP_piece:
2249 fputc (',', asm_out_file);
2250 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2251 break;
2252
2253 case DW_OP_bit_piece:
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2256 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2257 break;
2258
2259 case DW_OP_consts:
2260 case DW_OP_breg0:
2261 case DW_OP_breg1:
2262 case DW_OP_breg2:
2263 case DW_OP_breg3:
2264 case DW_OP_breg4:
2265 case DW_OP_breg5:
2266 case DW_OP_breg6:
2267 case DW_OP_breg7:
2268 case DW_OP_breg8:
2269 case DW_OP_breg9:
2270 case DW_OP_breg10:
2271 case DW_OP_breg11:
2272 case DW_OP_breg12:
2273 case DW_OP_breg13:
2274 case DW_OP_breg14:
2275 case DW_OP_breg15:
2276 case DW_OP_breg16:
2277 case DW_OP_breg17:
2278 case DW_OP_breg18:
2279 case DW_OP_breg19:
2280 case DW_OP_breg20:
2281 case DW_OP_breg21:
2282 case DW_OP_breg22:
2283 case DW_OP_breg23:
2284 case DW_OP_breg24:
2285 case DW_OP_breg25:
2286 case DW_OP_breg26:
2287 case DW_OP_breg27:
2288 case DW_OP_breg28:
2289 case DW_OP_breg29:
2290 case DW_OP_breg30:
2291 case DW_OP_breg31:
2292 case DW_OP_fbreg:
2293 fputc (',', asm_out_file);
2294 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2295 break;
2296
2297 case DW_OP_bregx:
2298 {
2299 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2300 gcc_assert (size_of_uleb128 (r)
2301 == size_of_uleb128 (val1->v.val_unsigned));
2302 fputc (',', asm_out_file);
2303 dw2_asm_output_data_uleb128_raw (r);
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2306 }
2307 break;
2308
2309 case DW_OP_GNU_implicit_pointer:
2310 case DW_OP_GNU_entry_value:
2311 case DW_OP_GNU_const_type:
2312 case DW_OP_GNU_regval_type:
2313 case DW_OP_GNU_deref_type:
2314 case DW_OP_GNU_convert:
2315 case DW_OP_GNU_reinterpret:
2316 case DW_OP_GNU_parameter_ref:
2317 gcc_unreachable ();
2318 break;
2319
2320 default:
2321 /* Other codes have no operands. */
2322 break;
2323 }
2324 }
2325
2326 void
2327 output_loc_sequence_raw (dw_loc_descr_ref loc)
2328 {
2329 while (1)
2330 {
2331 enum dwarf_location_atom opc = loc->dw_loc_opc;
2332 /* Output the opcode. */
2333 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2334 {
2335 unsigned r = (opc - DW_OP_breg0);
2336 r = DWARF2_FRAME_REG_OUT (r, 1);
2337 gcc_assert (r <= 31);
2338 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2339 }
2340 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2341 {
2342 unsigned r = (opc - DW_OP_reg0);
2343 r = DWARF2_FRAME_REG_OUT (r, 1);
2344 gcc_assert (r <= 31);
2345 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2346 }
2347 /* Output the opcode. */
2348 fprintf (asm_out_file, "%#x", opc);
2349 output_loc_operands_raw (loc);
2350
2351 if (!loc->dw_loc_next)
2352 break;
2353 loc = loc->dw_loc_next;
2354
2355 fputc (',', asm_out_file);
2356 }
2357 }
2358
2359 /* This function builds a dwarf location descriptor sequence from a
2360 dw_cfa_location, adding the given OFFSET to the result of the
2361 expression. */
2362
2363 struct dw_loc_descr_node *
2364 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2365 {
2366 struct dw_loc_descr_node *head, *tmp;
2367
2368 offset += cfa->offset;
2369
2370 if (cfa->indirect)
2371 {
2372 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2373 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2374 head->dw_loc_oprnd1.val_entry = NULL;
2375 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2376 add_loc_descr (&head, tmp);
2377 if (offset != 0)
2378 {
2379 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2380 add_loc_descr (&head, tmp);
2381 }
2382 }
2383 else
2384 head = new_reg_loc_descr (cfa->reg, offset);
2385
2386 return head;
2387 }
2388
2389 /* This function builds a dwarf location descriptor sequence for
2390 the address at OFFSET from the CFA when stack is aligned to
2391 ALIGNMENT byte. */
2392
2393 struct dw_loc_descr_node *
2394 build_cfa_aligned_loc (dw_cfa_location *cfa,
2395 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2396 {
2397 struct dw_loc_descr_node *head;
2398 unsigned int dwarf_fp
2399 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2400
2401 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2402 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2403 {
2404 head = new_reg_loc_descr (dwarf_fp, 0);
2405 add_loc_descr (&head, int_loc_descriptor (alignment));
2406 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2407 loc_descr_plus_const (&head, offset);
2408 }
2409 else
2410 head = new_reg_loc_descr (dwarf_fp, offset);
2411 return head;
2412 }
2413 \f
2414 /* And now, the support for symbolic debugging information. */
2415
2416 /* .debug_str support. */
2417 static int output_indirect_string (void **, void *);
2418
2419 static void dwarf2out_init (const char *);
2420 static void dwarf2out_finish (const char *);
2421 static void dwarf2out_assembly_start (void);
2422 static void dwarf2out_define (unsigned int, const char *);
2423 static void dwarf2out_undef (unsigned int, const char *);
2424 static void dwarf2out_start_source_file (unsigned, const char *);
2425 static void dwarf2out_end_source_file (unsigned);
2426 static void dwarf2out_function_decl (tree);
2427 static void dwarf2out_begin_block (unsigned, unsigned);
2428 static void dwarf2out_end_block (unsigned, unsigned);
2429 static bool dwarf2out_ignore_block (const_tree);
2430 static void dwarf2out_global_decl (tree);
2431 static void dwarf2out_type_decl (tree, int);
2432 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2433 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2434 dw_die_ref);
2435 static void dwarf2out_abstract_function (tree);
2436 static void dwarf2out_var_location (rtx);
2437 static void dwarf2out_begin_function (tree);
2438 static void dwarf2out_end_function (unsigned int);
2439 static void dwarf2out_set_name (tree, tree);
2440
2441 /* The debug hooks structure. */
2442
2443 const struct gcc_debug_hooks dwarf2_debug_hooks =
2444 {
2445 dwarf2out_init,
2446 dwarf2out_finish,
2447 dwarf2out_assembly_start,
2448 dwarf2out_define,
2449 dwarf2out_undef,
2450 dwarf2out_start_source_file,
2451 dwarf2out_end_source_file,
2452 dwarf2out_begin_block,
2453 dwarf2out_end_block,
2454 dwarf2out_ignore_block,
2455 dwarf2out_source_line,
2456 dwarf2out_begin_prologue,
2457 #if VMS_DEBUGGING_INFO
2458 dwarf2out_vms_end_prologue,
2459 dwarf2out_vms_begin_epilogue,
2460 #else
2461 debug_nothing_int_charstar,
2462 debug_nothing_int_charstar,
2463 #endif
2464 dwarf2out_end_epilogue,
2465 dwarf2out_begin_function,
2466 dwarf2out_end_function, /* end_function */
2467 dwarf2out_function_decl, /* function_decl */
2468 dwarf2out_global_decl,
2469 dwarf2out_type_decl, /* type_decl */
2470 dwarf2out_imported_module_or_decl,
2471 debug_nothing_tree, /* deferred_inline_function */
2472 /* The DWARF 2 backend tries to reduce debugging bloat by not
2473 emitting the abstract description of inline functions until
2474 something tries to reference them. */
2475 dwarf2out_abstract_function, /* outlining_inline_function */
2476 debug_nothing_rtx, /* label */
2477 debug_nothing_int, /* handle_pch */
2478 dwarf2out_var_location,
2479 dwarf2out_switch_text_section,
2480 dwarf2out_set_name,
2481 1, /* start_end_main_source_file */
2482 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2483 };
2484 \f
2485 /* NOTE: In the comments in this file, many references are made to
2486 "Debugging Information Entries". This term is abbreviated as `DIE'
2487 throughout the remainder of this file. */
2488
2489 /* An internal representation of the DWARF output is built, and then
2490 walked to generate the DWARF debugging info. The walk of the internal
2491 representation is done after the entire program has been compiled.
2492 The types below are used to describe the internal representation. */
2493
2494 /* Whether to put type DIEs into their own section .debug_types instead
2495 of making them part of the .debug_info section. Only supported for
2496 Dwarf V4 or higher and the user didn't disable them through
2497 -fno-debug-types-section. It is more efficient to put them in a
2498 separate comdat sections since the linker will then be able to
2499 remove duplicates. But not all tools support .debug_types sections
2500 yet. */
2501
2502 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2503
2504 /* Various DIE's use offsets relative to the beginning of the
2505 .debug_info section to refer to each other. */
2506
2507 typedef long int dw_offset;
2508
2509 /* Define typedefs here to avoid circular dependencies. */
2510
2511 typedef struct dw_attr_struct *dw_attr_ref;
2512 typedef struct dw_line_info_struct *dw_line_info_ref;
2513 typedef struct pubname_struct *pubname_ref;
2514 typedef struct dw_ranges_struct *dw_ranges_ref;
2515 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2516 typedef struct comdat_type_struct *comdat_type_node_ref;
2517
2518 /* The entries in the line_info table more-or-less mirror the opcodes
2519 that are used in the real dwarf line table. Arrays of these entries
2520 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2521 supported. */
2522
2523 enum dw_line_info_opcode {
2524 /* Emit DW_LNE_set_address; the operand is the label index. */
2525 LI_set_address,
2526
2527 /* Emit a row to the matrix with the given line. This may be done
2528 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2529 special opcodes. */
2530 LI_set_line,
2531
2532 /* Emit a DW_LNS_set_file. */
2533 LI_set_file,
2534
2535 /* Emit a DW_LNS_set_column. */
2536 LI_set_column,
2537
2538 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2539 LI_negate_stmt,
2540
2541 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2542 LI_set_prologue_end,
2543 LI_set_epilogue_begin,
2544
2545 /* Emit a DW_LNE_set_discriminator. */
2546 LI_set_discriminator
2547 };
2548
2549 typedef struct GTY(()) dw_line_info_struct {
2550 enum dw_line_info_opcode opcode;
2551 unsigned int val;
2552 } dw_line_info_entry;
2553
2554
2555 typedef struct GTY(()) dw_line_info_table_struct {
2556 /* The label that marks the end of this section. */
2557 const char *end_label;
2558
2559 /* The values for the last row of the matrix, as collected in the table.
2560 These are used to minimize the changes to the next row. */
2561 unsigned int file_num;
2562 unsigned int line_num;
2563 unsigned int column_num;
2564 int discrim_num;
2565 bool is_stmt;
2566 bool in_use;
2567
2568 vec<dw_line_info_entry, va_gc> *entries;
2569 } dw_line_info_table;
2570
2571 typedef dw_line_info_table *dw_line_info_table_p;
2572
2573
2574 /* Each DIE attribute has a field specifying the attribute kind,
2575 a link to the next attribute in the chain, and an attribute value.
2576 Attributes are typically linked below the DIE they modify. */
2577
2578 typedef struct GTY(()) dw_attr_struct {
2579 enum dwarf_attribute dw_attr;
2580 dw_val_node dw_attr_val;
2581 }
2582 dw_attr_node;
2583
2584
2585 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2586 The children of each node form a circular list linked by
2587 die_sib. die_child points to the node *before* the "first" child node. */
2588
2589 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2590 union die_symbol_or_type_node
2591 {
2592 const char * GTY ((tag ("0"))) die_symbol;
2593 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2594 }
2595 GTY ((desc ("%0.comdat_type_p"))) die_id;
2596 vec<dw_attr_node, va_gc> *die_attr;
2597 dw_die_ref die_parent;
2598 dw_die_ref die_child;
2599 dw_die_ref die_sib;
2600 dw_die_ref die_definition; /* ref from a specification to its definition */
2601 dw_offset die_offset;
2602 unsigned long die_abbrev;
2603 int die_mark;
2604 unsigned int decl_id;
2605 enum dwarf_tag die_tag;
2606 /* Die is used and must not be pruned as unused. */
2607 BOOL_BITFIELD die_perennial_p : 1;
2608 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2609 /* Lots of spare bits. */
2610 }
2611 die_node;
2612
2613 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2614 #define FOR_EACH_CHILD(die, c, expr) do { \
2615 c = die->die_child; \
2616 if (c) do { \
2617 c = c->die_sib; \
2618 expr; \
2619 } while (c != die->die_child); \
2620 } while (0)
2621
2622 /* The pubname structure */
2623
2624 typedef struct GTY(()) pubname_struct {
2625 dw_die_ref die;
2626 const char *name;
2627 }
2628 pubname_entry;
2629
2630
2631 struct GTY(()) dw_ranges_struct {
2632 /* If this is positive, it's a block number, otherwise it's a
2633 bitwise-negated index into dw_ranges_by_label. */
2634 int num;
2635 };
2636
2637 /* A structure to hold a macinfo entry. */
2638
2639 typedef struct GTY(()) macinfo_struct {
2640 unsigned char code;
2641 unsigned HOST_WIDE_INT lineno;
2642 const char *info;
2643 }
2644 macinfo_entry;
2645
2646
2647 struct GTY(()) dw_ranges_by_label_struct {
2648 const char *begin;
2649 const char *end;
2650 };
2651
2652 /* The comdat type node structure. */
2653 typedef struct GTY(()) comdat_type_struct
2654 {
2655 dw_die_ref root_die;
2656 dw_die_ref type_die;
2657 dw_die_ref skeleton_die;
2658 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2659 struct comdat_type_struct *next;
2660 }
2661 comdat_type_node;
2662
2663 /* The limbo die list structure. */
2664 typedef struct GTY(()) limbo_die_struct {
2665 dw_die_ref die;
2666 tree created_for;
2667 struct limbo_die_struct *next;
2668 }
2669 limbo_die_node;
2670
2671 typedef struct skeleton_chain_struct
2672 {
2673 dw_die_ref old_die;
2674 dw_die_ref new_die;
2675 struct skeleton_chain_struct *parent;
2676 }
2677 skeleton_chain_node;
2678
2679 /* Define a macro which returns nonzero for a TYPE_DECL which was
2680 implicitly generated for a type.
2681
2682 Note that, unlike the C front-end (which generates a NULL named
2683 TYPE_DECL node for each complete tagged type, each array type,
2684 and each function type node created) the C++ front-end generates
2685 a _named_ TYPE_DECL node for each tagged type node created.
2686 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2687 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2688 front-end, but for each type, tagged or not. */
2689
2690 #define TYPE_DECL_IS_STUB(decl) \
2691 (DECL_NAME (decl) == NULL_TREE \
2692 || (DECL_ARTIFICIAL (decl) \
2693 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2694 /* This is necessary for stub decls that \
2695 appear in nested inline functions. */ \
2696 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2697 && (decl_ultimate_origin (decl) \
2698 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2699
2700 /* Information concerning the compilation unit's programming
2701 language, and compiler version. */
2702
2703 /* Fixed size portion of the DWARF compilation unit header. */
2704 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2705 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2706
2707 /* Fixed size portion of the DWARF comdat type unit header. */
2708 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2709 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2710 + DWARF_OFFSET_SIZE)
2711
2712 /* Fixed size portion of public names info. */
2713 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2714
2715 /* Fixed size portion of the address range info. */
2716 #define DWARF_ARANGES_HEADER_SIZE \
2717 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2718 DWARF2_ADDR_SIZE * 2) \
2719 - DWARF_INITIAL_LENGTH_SIZE)
2720
2721 /* Size of padding portion in the address range info. It must be
2722 aligned to twice the pointer size. */
2723 #define DWARF_ARANGES_PAD_SIZE \
2724 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2725 DWARF2_ADDR_SIZE * 2) \
2726 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2727
2728 /* Use assembler line directives if available. */
2729 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2730 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2731 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2732 #else
2733 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2734 #endif
2735 #endif
2736
2737 /* Minimum line offset in a special line info. opcode.
2738 This value was chosen to give a reasonable range of values. */
2739 #define DWARF_LINE_BASE -10
2740
2741 /* First special line opcode - leave room for the standard opcodes. */
2742 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2743
2744 /* Range of line offsets in a special line info. opcode. */
2745 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2746
2747 /* Flag that indicates the initial value of the is_stmt_start flag.
2748 In the present implementation, we do not mark any lines as
2749 the beginning of a source statement, because that information
2750 is not made available by the GCC front-end. */
2751 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2752
2753 /* Maximum number of operations per instruction bundle. */
2754 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2755 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2756 #endif
2757
2758 /* This location is used by calc_die_sizes() to keep track
2759 the offset of each DIE within the .debug_info section. */
2760 static unsigned long next_die_offset;
2761
2762 /* Record the root of the DIE's built for the current compilation unit. */
2763 static GTY(()) dw_die_ref single_comp_unit_die;
2764
2765 /* A list of type DIEs that have been separated into comdat sections. */
2766 static GTY(()) comdat_type_node *comdat_type_list;
2767
2768 /* A list of DIEs with a NULL parent waiting to be relocated. */
2769 static GTY(()) limbo_die_node *limbo_die_list;
2770
2771 /* A list of DIEs for which we may have to generate
2772 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2773 static GTY(()) limbo_die_node *deferred_asm_name;
2774
2775 /* Filenames referenced by this compilation unit. */
2776 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2777
2778 /* A hash table of references to DIE's that describe declarations.
2779 The key is a DECL_UID() which is a unique number identifying each decl. */
2780 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2781
2782 /* A hash table of references to DIE's that describe COMMON blocks.
2783 The key is DECL_UID() ^ die_parent. */
2784 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2785
2786 typedef struct GTY(()) die_arg_entry_struct {
2787 dw_die_ref die;
2788 tree arg;
2789 } die_arg_entry;
2790
2791
2792 /* Node of the variable location list. */
2793 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2794 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2795 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2796 in mode of the EXPR_LIST node and first EXPR_LIST operand
2797 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2798 location or NULL for padding. For larger bitsizes,
2799 mode is 0 and first operand is a CONCAT with bitsize
2800 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2801 NULL as second operand. */
2802 rtx GTY (()) loc;
2803 const char * GTY (()) label;
2804 struct var_loc_node * GTY (()) next;
2805 };
2806
2807 /* Variable location list. */
2808 struct GTY (()) var_loc_list_def {
2809 struct var_loc_node * GTY (()) first;
2810
2811 /* Pointer to the last but one or last element of the
2812 chained list. If the list is empty, both first and
2813 last are NULL, if the list contains just one node
2814 or the last node certainly is not redundant, it points
2815 to the last node, otherwise points to the last but one.
2816 Do not mark it for GC because it is marked through the chain. */
2817 struct var_loc_node * GTY ((skip ("%h"))) last;
2818
2819 /* Pointer to the last element before section switch,
2820 if NULL, either sections weren't switched or first
2821 is after section switch. */
2822 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2823
2824 /* DECL_UID of the variable decl. */
2825 unsigned int decl_id;
2826 };
2827 typedef struct var_loc_list_def var_loc_list;
2828
2829 /* Call argument location list. */
2830 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2831 rtx GTY (()) call_arg_loc_note;
2832 const char * GTY (()) label;
2833 tree GTY (()) block;
2834 bool tail_call_p;
2835 rtx GTY (()) symbol_ref;
2836 struct call_arg_loc_node * GTY (()) next;
2837 };
2838
2839
2840 /* Table of decl location linked lists. */
2841 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2842
2843 /* Head and tail of call_arg_loc chain. */
2844 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2845 static struct call_arg_loc_node *call_arg_loc_last;
2846
2847 /* Number of call sites in the current function. */
2848 static int call_site_count = -1;
2849 /* Number of tail call sites in the current function. */
2850 static int tail_call_site_count = -1;
2851
2852 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2853 DIEs. */
2854 static vec<dw_die_ref> block_map;
2855
2856 /* A cached location list. */
2857 struct GTY (()) cached_dw_loc_list_def {
2858 /* The DECL_UID of the decl that this entry describes. */
2859 unsigned int decl_id;
2860
2861 /* The cached location list. */
2862 dw_loc_list_ref loc_list;
2863 };
2864 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2865
2866 /* Table of cached location lists. */
2867 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2868
2869 /* A pointer to the base of a list of references to DIE's that
2870 are uniquely identified by their tag, presence/absence of
2871 children DIE's, and list of attribute/value pairs. */
2872 static GTY((length ("abbrev_die_table_allocated")))
2873 dw_die_ref *abbrev_die_table;
2874
2875 /* Number of elements currently allocated for abbrev_die_table. */
2876 static GTY(()) unsigned abbrev_die_table_allocated;
2877
2878 /* Number of elements in type_die_table currently in use. */
2879 static GTY(()) unsigned abbrev_die_table_in_use;
2880
2881 /* Size (in elements) of increments by which we may expand the
2882 abbrev_die_table. */
2883 #define ABBREV_DIE_TABLE_INCREMENT 256
2884
2885 /* A global counter for generating labels for line number data. */
2886 static unsigned int line_info_label_num;
2887
2888 /* The current table to which we should emit line number information
2889 for the current function. This will be set up at the beginning of
2890 assembly for the function. */
2891 static dw_line_info_table *cur_line_info_table;
2892
2893 /* The two default tables of line number info. */
2894 static GTY(()) dw_line_info_table *text_section_line_info;
2895 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2896
2897 /* The set of all non-default tables of line number info. */
2898 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2899
2900 /* A flag to tell pubnames/types export if there is an info section to
2901 refer to. */
2902 static bool info_section_emitted;
2903
2904 /* A pointer to the base of a table that contains a list of publicly
2905 accessible names. */
2906 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2907
2908 /* A pointer to the base of a table that contains a list of publicly
2909 accessible types. */
2910 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2911
2912 /* A pointer to the base of a table that contains a list of macro
2913 defines/undefines (and file start/end markers). */
2914 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2915
2916 /* True if .debug_macinfo or .debug_macros section is going to be
2917 emitted. */
2918 #define have_macinfo \
2919 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2920 && !macinfo_table->is_empty ())
2921
2922 /* Array of dies for which we should generate .debug_ranges info. */
2923 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2924
2925 /* Number of elements currently allocated for ranges_table. */
2926 static GTY(()) unsigned ranges_table_allocated;
2927
2928 /* Number of elements in ranges_table currently in use. */
2929 static GTY(()) unsigned ranges_table_in_use;
2930
2931 /* Array of pairs of labels referenced in ranges_table. */
2932 static GTY ((length ("ranges_by_label_allocated")))
2933 dw_ranges_by_label_ref ranges_by_label;
2934
2935 /* Number of elements currently allocated for ranges_by_label. */
2936 static GTY(()) unsigned ranges_by_label_allocated;
2937
2938 /* Number of elements in ranges_by_label currently in use. */
2939 static GTY(()) unsigned ranges_by_label_in_use;
2940
2941 /* Size (in elements) of increments by which we may expand the
2942 ranges_table. */
2943 #define RANGES_TABLE_INCREMENT 64
2944
2945 /* Whether we have location lists that need outputting */
2946 static GTY(()) bool have_location_lists;
2947
2948 /* Unique label counter. */
2949 static GTY(()) unsigned int loclabel_num;
2950
2951 /* Unique label counter for point-of-call tables. */
2952 static GTY(()) unsigned int poc_label_num;
2953
2954 /* Record whether the function being analyzed contains inlined functions. */
2955 static int current_function_has_inlines;
2956
2957 /* The last file entry emitted by maybe_emit_file(). */
2958 static GTY(()) struct dwarf_file_data * last_emitted_file;
2959
2960 /* Number of internal labels generated by gen_internal_sym(). */
2961 static GTY(()) int label_num;
2962
2963 /* Cached result of previous call to lookup_filename. */
2964 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2965
2966 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2967
2968 /* Instances of generic types for which we need to generate debug
2969 info that describe their generic parameters and arguments. That
2970 generation needs to happen once all types are properly laid out so
2971 we do it at the end of compilation. */
2972 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2973
2974 /* Offset from the "steady-state frame pointer" to the frame base,
2975 within the current function. */
2976 static HOST_WIDE_INT frame_pointer_fb_offset;
2977 static bool frame_pointer_fb_offset_valid;
2978
2979 static vec<dw_die_ref> base_types;
2980
2981 /* Forward declarations for functions defined in this file. */
2982
2983 static int is_pseudo_reg (const_rtx);
2984 static tree type_main_variant (tree);
2985 static int is_tagged_type (const_tree);
2986 static const char *dwarf_tag_name (unsigned);
2987 static const char *dwarf_attr_name (unsigned);
2988 static const char *dwarf_form_name (unsigned);
2989 static tree decl_ultimate_origin (const_tree);
2990 static tree decl_class_context (tree);
2991 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2992 static inline enum dw_val_class AT_class (dw_attr_ref);
2993 static inline unsigned int AT_index (dw_attr_ref);
2994 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2995 static inline unsigned AT_flag (dw_attr_ref);
2996 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2997 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2998 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2999 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3000 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3001 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3002 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3003 unsigned int, unsigned char *);
3004 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3005 static hashval_t debug_str_do_hash (const void *);
3006 static int debug_str_eq (const void *, const void *);
3007 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3008 static inline const char *AT_string (dw_attr_ref);
3009 static enum dwarf_form AT_string_form (dw_attr_ref);
3010 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3011 static void add_AT_specification (dw_die_ref, dw_die_ref);
3012 static inline dw_die_ref AT_ref (dw_attr_ref);
3013 static inline int AT_ref_external (dw_attr_ref);
3014 static inline void set_AT_ref_external (dw_attr_ref, int);
3015 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3016 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3017 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3018 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3019 dw_loc_list_ref);
3020 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3021 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3022 static void remove_addr_table_entry (addr_table_entry *);
3023 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3024 static inline rtx AT_addr (dw_attr_ref);
3025 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3026 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3027 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3028 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3029 unsigned HOST_WIDE_INT);
3030 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3031 unsigned long, bool);
3032 static inline const char *AT_lbl (dw_attr_ref);
3033 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3034 static const char *get_AT_low_pc (dw_die_ref);
3035 static const char *get_AT_hi_pc (dw_die_ref);
3036 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3037 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3038 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3039 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3040 static bool is_cxx (void);
3041 static bool is_fortran (void);
3042 static bool is_ada (void);
3043 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3044 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3045 static void add_child_die (dw_die_ref, dw_die_ref);
3046 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3047 static dw_die_ref lookup_type_die (tree);
3048 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3049 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3050 static void equate_type_number_to_die (tree, dw_die_ref);
3051 static hashval_t decl_die_table_hash (const void *);
3052 static int decl_die_table_eq (const void *, const void *);
3053 static dw_die_ref lookup_decl_die (tree);
3054 static hashval_t common_block_die_table_hash (const void *);
3055 static int common_block_die_table_eq (const void *, const void *);
3056 static hashval_t decl_loc_table_hash (const void *);
3057 static int decl_loc_table_eq (const void *, const void *);
3058 static var_loc_list *lookup_decl_loc (const_tree);
3059 static void equate_decl_number_to_die (tree, dw_die_ref);
3060 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3061 static void print_spaces (FILE *);
3062 static void print_die (dw_die_ref, FILE *);
3063 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3064 static dw_die_ref pop_compile_unit (dw_die_ref);
3065 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3066 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3067 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3068 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3069 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3070 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3071 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3072 struct md5_ctx *, int *);
3073 struct checksum_attributes;
3074 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3075 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3076 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3077 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3078 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3079 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3080 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3081 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3082 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3083 static void compute_section_prefix (dw_die_ref);
3084 static int is_type_die (dw_die_ref);
3085 static int is_comdat_die (dw_die_ref);
3086 static int is_symbol_die (dw_die_ref);
3087 static inline bool is_template_instantiation (dw_die_ref);
3088 static void assign_symbol_names (dw_die_ref);
3089 static void break_out_includes (dw_die_ref);
3090 static int is_declaration_die (dw_die_ref);
3091 static int should_move_die_to_comdat (dw_die_ref);
3092 static dw_die_ref clone_as_declaration (dw_die_ref);
3093 static dw_die_ref clone_die (dw_die_ref);
3094 static dw_die_ref clone_tree (dw_die_ref);
3095 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3096 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3097 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3098 static dw_die_ref generate_skeleton (dw_die_ref);
3099 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3100 dw_die_ref,
3101 dw_die_ref);
3102 static void break_out_comdat_types (dw_die_ref);
3103 static void copy_decls_for_unworthy_types (dw_die_ref);
3104
3105 static void add_sibling_attributes (dw_die_ref);
3106 static void output_location_lists (dw_die_ref);
3107 static int constant_size (unsigned HOST_WIDE_INT);
3108 static unsigned long size_of_die (dw_die_ref);
3109 static void calc_die_sizes (dw_die_ref);
3110 static void calc_base_type_die_sizes (void);
3111 static void mark_dies (dw_die_ref);
3112 static void unmark_dies (dw_die_ref);
3113 static void unmark_all_dies (dw_die_ref);
3114 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3115 static unsigned long size_of_aranges (void);
3116 static enum dwarf_form value_format (dw_attr_ref);
3117 static void output_value_format (dw_attr_ref);
3118 static void output_abbrev_section (void);
3119 static void output_die_abbrevs (unsigned long, dw_die_ref);
3120 static void output_die_symbol (dw_die_ref);
3121 static void output_die (dw_die_ref);
3122 static void output_compilation_unit_header (void);
3123 static void output_comp_unit (dw_die_ref, int);
3124 static void output_comdat_type_unit (comdat_type_node *);
3125 static const char *dwarf2_name (tree, int);
3126 static void add_pubname (tree, dw_die_ref);
3127 static void add_enumerator_pubname (const char *, dw_die_ref);
3128 static void add_pubname_string (const char *, dw_die_ref);
3129 static void add_pubtype (tree, dw_die_ref);
3130 static void output_pubnames (vec<pubname_entry, va_gc> *);
3131 static void output_aranges (unsigned long);
3132 static unsigned int add_ranges_num (int);
3133 static unsigned int add_ranges (const_tree);
3134 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3135 bool *, bool);
3136 static void output_ranges (void);
3137 static dw_line_info_table *new_line_info_table (void);
3138 static void output_line_info (bool);
3139 static void output_file_names (void);
3140 static dw_die_ref base_type_die (tree);
3141 static int is_base_type (tree);
3142 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3143 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3144 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3145 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3146 static int type_is_enum (const_tree);
3147 static unsigned int dbx_reg_number (const_rtx);
3148 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3149 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3150 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3151 enum var_init_status);
3152 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3153 enum var_init_status);
3154 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3155 enum var_init_status);
3156 static int is_based_loc (const_rtx);
3157 static int resolve_one_addr (rtx *, void *);
3158 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3159 enum var_init_status);
3160 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3161 enum var_init_status);
3162 static dw_loc_list_ref loc_list_from_tree (tree, int);
3163 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3164 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3165 static tree field_type (const_tree);
3166 static unsigned int simple_type_align_in_bits (const_tree);
3167 static unsigned int simple_decl_align_in_bits (const_tree);
3168 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3169 static HOST_WIDE_INT field_byte_offset (const_tree);
3170 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3171 dw_loc_list_ref);
3172 static void add_data_member_location_attribute (dw_die_ref, tree);
3173 static bool add_const_value_attribute (dw_die_ref, rtx);
3174 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3175 static void insert_wide_int (const wide_int &, unsigned char *, int);
3176 static void insert_float (const_rtx, unsigned char *);
3177 static rtx rtl_for_decl_location (tree);
3178 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3179 enum dwarf_attribute);
3180 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3181 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3182 static void add_name_attribute (dw_die_ref, const char *);
3183 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3184 static void add_comp_dir_attribute (dw_die_ref);
3185 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3186 static void add_subscript_info (dw_die_ref, tree, bool);
3187 static void add_byte_size_attribute (dw_die_ref, tree);
3188 static void add_bit_offset_attribute (dw_die_ref, tree);
3189 static void add_bit_size_attribute (dw_die_ref, tree);
3190 static void add_prototyped_attribute (dw_die_ref, tree);
3191 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3192 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3193 static void add_src_coords_attributes (dw_die_ref, tree);
3194 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3195 static void push_decl_scope (tree);
3196 static void pop_decl_scope (void);
3197 static dw_die_ref scope_die_for (tree, dw_die_ref);
3198 static inline int local_scope_p (dw_die_ref);
3199 static inline int class_scope_p (dw_die_ref);
3200 static inline int class_or_namespace_scope_p (dw_die_ref);
3201 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3202 static void add_calling_convention_attribute (dw_die_ref, tree);
3203 static const char *type_tag (const_tree);
3204 static tree member_declared_type (const_tree);
3205 #if 0
3206 static const char *decl_start_label (tree);
3207 #endif
3208 static void gen_array_type_die (tree, dw_die_ref);
3209 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3210 #if 0
3211 static void gen_entry_point_die (tree, dw_die_ref);
3212 #endif
3213 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3214 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3215 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3216 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3217 static void gen_formal_types_die (tree, dw_die_ref);
3218 static void gen_subprogram_die (tree, dw_die_ref);
3219 static void gen_variable_die (tree, tree, dw_die_ref);
3220 static void gen_const_die (tree, dw_die_ref);
3221 static void gen_label_die (tree, dw_die_ref);
3222 static void gen_lexical_block_die (tree, dw_die_ref, int);
3223 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3224 static void gen_field_die (tree, dw_die_ref);
3225 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3226 static dw_die_ref gen_compile_unit_die (const char *);
3227 static void gen_inheritance_die (tree, tree, dw_die_ref);
3228 static void gen_member_die (tree, dw_die_ref);
3229 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3230 enum debug_info_usage);
3231 static void gen_subroutine_type_die (tree, dw_die_ref);
3232 static void gen_typedef_die (tree, dw_die_ref);
3233 static void gen_type_die (tree, dw_die_ref);
3234 static void gen_block_die (tree, dw_die_ref, int);
3235 static void decls_for_scope (tree, dw_die_ref, int);
3236 static inline int is_redundant_typedef (const_tree);
3237 static bool is_naming_typedef_decl (const_tree);
3238 static inline dw_die_ref get_context_die (tree);
3239 static void gen_namespace_die (tree, dw_die_ref);
3240 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3241 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3242 static dw_die_ref force_decl_die (tree);
3243 static dw_die_ref force_type_die (tree);
3244 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3245 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3246 static struct dwarf_file_data * lookup_filename (const char *);
3247 static void retry_incomplete_types (void);
3248 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3249 static void gen_generic_params_dies (tree);
3250 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3251 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3252 static void splice_child_die (dw_die_ref, dw_die_ref);
3253 static int file_info_cmp (const void *, const void *);
3254 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3255 const char *, const char *);
3256 static void output_loc_list (dw_loc_list_ref);
3257 static char *gen_internal_sym (const char *);
3258 static bool want_pubnames (void);
3259
3260 static void prune_unmark_dies (dw_die_ref);
3261 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3262 static void prune_unused_types_mark (dw_die_ref, int);
3263 static void prune_unused_types_walk (dw_die_ref);
3264 static void prune_unused_types_walk_attribs (dw_die_ref);
3265 static void prune_unused_types_prune (dw_die_ref);
3266 static void prune_unused_types (void);
3267 static int maybe_emit_file (struct dwarf_file_data *fd);
3268 static inline const char *AT_vms_delta1 (dw_attr_ref);
3269 static inline const char *AT_vms_delta2 (dw_attr_ref);
3270 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3271 const char *, const char *);
3272 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3273 static void gen_remaining_tmpl_value_param_die_attribute (void);
3274 static bool generic_type_p (tree);
3275 static void schedule_generic_params_dies_gen (tree t);
3276 static void gen_scheduled_generic_parms_dies (void);
3277
3278 static const char *comp_dir_string (void);
3279
3280 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3281
3282 /* enum for tracking thread-local variables whose address is really an offset
3283 relative to the TLS pointer, which will need link-time relocation, but will
3284 not need relocation by the DWARF consumer. */
3285
3286 enum dtprel_bool
3287 {
3288 dtprel_false = 0,
3289 dtprel_true = 1
3290 };
3291
3292 /* Return the operator to use for an address of a variable. For dtprel_true, we
3293 use DW_OP_const*. For regular variables, which need both link-time
3294 relocation and consumer-level relocation (e.g., to account for shared objects
3295 loaded at a random address), we use DW_OP_addr*. */
3296
3297 static inline enum dwarf_location_atom
3298 dw_addr_op (enum dtprel_bool dtprel)
3299 {
3300 if (dtprel == dtprel_true)
3301 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3302 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3303 else
3304 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3305 }
3306
3307 /* Return a pointer to a newly allocated address location description. If
3308 dwarf_split_debug_info is true, then record the address with the appropriate
3309 relocation. */
3310 static inline dw_loc_descr_ref
3311 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3312 {
3313 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3314
3315 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3316 ref->dw_loc_oprnd1.v.val_addr = addr;
3317 ref->dtprel = dtprel;
3318 if (dwarf_split_debug_info)
3319 ref->dw_loc_oprnd1.val_entry
3320 = add_addr_table_entry (addr,
3321 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3322 else
3323 ref->dw_loc_oprnd1.val_entry = NULL;
3324
3325 return ref;
3326 }
3327
3328 /* Section names used to hold DWARF debugging information. */
3329
3330 #ifndef DEBUG_INFO_SECTION
3331 #define DEBUG_INFO_SECTION ".debug_info"
3332 #endif
3333 #ifndef DEBUG_DWO_INFO_SECTION
3334 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3335 #endif
3336 #ifndef DEBUG_ABBREV_SECTION
3337 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3338 #endif
3339 #ifndef DEBUG_DWO_ABBREV_SECTION
3340 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3341 #endif
3342 #ifndef DEBUG_ARANGES_SECTION
3343 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3344 #endif
3345 #ifndef DEBUG_ADDR_SECTION
3346 #define DEBUG_ADDR_SECTION ".debug_addr"
3347 #endif
3348 #ifndef DEBUG_NORM_MACINFO_SECTION
3349 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3350 #endif
3351 #ifndef DEBUG_DWO_MACINFO_SECTION
3352 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3353 #endif
3354 #ifndef DEBUG_MACINFO_SECTION
3355 #define DEBUG_MACINFO_SECTION \
3356 (!dwarf_split_debug_info \
3357 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3358 #endif
3359 #ifndef DEBUG_NORM_MACRO_SECTION
3360 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3361 #endif
3362 #ifndef DEBUG_DWO_MACRO_SECTION
3363 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3364 #endif
3365 #ifndef DEBUG_MACRO_SECTION
3366 #define DEBUG_MACRO_SECTION \
3367 (!dwarf_split_debug_info \
3368 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3369 #endif
3370 #ifndef DEBUG_LINE_SECTION
3371 #define DEBUG_LINE_SECTION ".debug_line"
3372 #endif
3373 #ifndef DEBUG_DWO_LINE_SECTION
3374 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3375 #endif
3376 #ifndef DEBUG_LOC_SECTION
3377 #define DEBUG_LOC_SECTION ".debug_loc"
3378 #endif
3379 #ifndef DEBUG_DWO_LOC_SECTION
3380 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3381 #endif
3382 #ifndef DEBUG_PUBNAMES_SECTION
3383 #define DEBUG_PUBNAMES_SECTION \
3384 ((debug_generate_pub_sections == 2) \
3385 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3386 #endif
3387 #ifndef DEBUG_PUBTYPES_SECTION
3388 #define DEBUG_PUBTYPES_SECTION \
3389 ((debug_generate_pub_sections == 2) \
3390 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3391 #endif
3392 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3393 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3394 #ifndef DEBUG_STR_OFFSETS_SECTION
3395 #define DEBUG_STR_OFFSETS_SECTION \
3396 (!dwarf_split_debug_info \
3397 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3398 #endif
3399 #ifndef DEBUG_STR_DWO_SECTION
3400 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3401 #endif
3402 #ifndef DEBUG_STR_SECTION
3403 #define DEBUG_STR_SECTION ".debug_str"
3404 #endif
3405 #ifndef DEBUG_RANGES_SECTION
3406 #define DEBUG_RANGES_SECTION ".debug_ranges"
3407 #endif
3408
3409 /* Standard ELF section names for compiled code and data. */
3410 #ifndef TEXT_SECTION_NAME
3411 #define TEXT_SECTION_NAME ".text"
3412 #endif
3413
3414 /* Section flags for .debug_macinfo/.debug_macro section. */
3415 #define DEBUG_MACRO_SECTION_FLAGS \
3416 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3417
3418 /* Section flags for .debug_str section. */
3419 #define DEBUG_STR_SECTION_FLAGS \
3420 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3421 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3422 : SECTION_DEBUG)
3423
3424 /* Section flags for .debug_str.dwo section. */
3425 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3426
3427 /* Labels we insert at beginning sections we can reference instead of
3428 the section names themselves. */
3429
3430 #ifndef TEXT_SECTION_LABEL
3431 #define TEXT_SECTION_LABEL "Ltext"
3432 #endif
3433 #ifndef COLD_TEXT_SECTION_LABEL
3434 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3435 #endif
3436 #ifndef DEBUG_LINE_SECTION_LABEL
3437 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3438 #endif
3439 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3440 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3441 #endif
3442 #ifndef DEBUG_INFO_SECTION_LABEL
3443 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3444 #endif
3445 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3446 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3447 #endif
3448 #ifndef DEBUG_ABBREV_SECTION_LABEL
3449 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3450 #endif
3451 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3452 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3453 #endif
3454 #ifndef DEBUG_ADDR_SECTION_LABEL
3455 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3456 #endif
3457 #ifndef DEBUG_LOC_SECTION_LABEL
3458 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3459 #endif
3460 #ifndef DEBUG_RANGES_SECTION_LABEL
3461 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3462 #endif
3463 #ifndef DEBUG_MACINFO_SECTION_LABEL
3464 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3465 #endif
3466 #ifndef DEBUG_MACRO_SECTION_LABEL
3467 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3468 #endif
3469 #define SKELETON_COMP_DIE_ABBREV 1
3470 #define SKELETON_TYPE_DIE_ABBREV 2
3471
3472 /* Definitions of defaults for formats and names of various special
3473 (artificial) labels which may be generated within this file (when the -g
3474 options is used and DWARF2_DEBUGGING_INFO is in effect.
3475 If necessary, these may be overridden from within the tm.h file, but
3476 typically, overriding these defaults is unnecessary. */
3477
3478 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3479 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3492
3493 #ifndef TEXT_END_LABEL
3494 #define TEXT_END_LABEL "Letext"
3495 #endif
3496 #ifndef COLD_END_LABEL
3497 #define COLD_END_LABEL "Letext_cold"
3498 #endif
3499 #ifndef BLOCK_BEGIN_LABEL
3500 #define BLOCK_BEGIN_LABEL "LBB"
3501 #endif
3502 #ifndef BLOCK_END_LABEL
3503 #define BLOCK_END_LABEL "LBE"
3504 #endif
3505 #ifndef LINE_CODE_LABEL
3506 #define LINE_CODE_LABEL "LM"
3507 #endif
3508
3509 \f
3510 /* Return the root of the DIE's built for the current compilation unit. */
3511 static dw_die_ref
3512 comp_unit_die (void)
3513 {
3514 if (!single_comp_unit_die)
3515 single_comp_unit_die = gen_compile_unit_die (NULL);
3516 return single_comp_unit_die;
3517 }
3518
3519 /* We allow a language front-end to designate a function that is to be
3520 called to "demangle" any name before it is put into a DIE. */
3521
3522 static const char *(*demangle_name_func) (const char *);
3523
3524 void
3525 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3526 {
3527 demangle_name_func = func;
3528 }
3529
3530 /* Test if rtl node points to a pseudo register. */
3531
3532 static inline int
3533 is_pseudo_reg (const_rtx rtl)
3534 {
3535 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3536 || (GET_CODE (rtl) == SUBREG
3537 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3538 }
3539
3540 /* Return a reference to a type, with its const and volatile qualifiers
3541 removed. */
3542
3543 static inline tree
3544 type_main_variant (tree type)
3545 {
3546 type = TYPE_MAIN_VARIANT (type);
3547
3548 /* ??? There really should be only one main variant among any group of
3549 variants of a given type (and all of the MAIN_VARIANT values for all
3550 members of the group should point to that one type) but sometimes the C
3551 front-end messes this up for array types, so we work around that bug
3552 here. */
3553 if (TREE_CODE (type) == ARRAY_TYPE)
3554 while (type != TYPE_MAIN_VARIANT (type))
3555 type = TYPE_MAIN_VARIANT (type);
3556
3557 return type;
3558 }
3559
3560 /* Return nonzero if the given type node represents a tagged type. */
3561
3562 static inline int
3563 is_tagged_type (const_tree type)
3564 {
3565 enum tree_code code = TREE_CODE (type);
3566
3567 return (code == RECORD_TYPE || code == UNION_TYPE
3568 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3569 }
3570
3571 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3572
3573 static void
3574 get_ref_die_offset_label (char *label, dw_die_ref ref)
3575 {
3576 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3577 }
3578
3579 /* Return die_offset of a DIE reference to a base type. */
3580
3581 static unsigned long int
3582 get_base_type_offset (dw_die_ref ref)
3583 {
3584 if (ref->die_offset)
3585 return ref->die_offset;
3586 if (comp_unit_die ()->die_abbrev)
3587 {
3588 calc_base_type_die_sizes ();
3589 gcc_assert (ref->die_offset);
3590 }
3591 return ref->die_offset;
3592 }
3593
3594 /* Return die_offset of a DIE reference other than base type. */
3595
3596 static unsigned long int
3597 get_ref_die_offset (dw_die_ref ref)
3598 {
3599 gcc_assert (ref->die_offset);
3600 return ref->die_offset;
3601 }
3602
3603 /* Convert a DIE tag into its string name. */
3604
3605 static const char *
3606 dwarf_tag_name (unsigned int tag)
3607 {
3608 const char *name = get_DW_TAG_name (tag);
3609
3610 if (name != NULL)
3611 return name;
3612
3613 return "DW_TAG_<unknown>";
3614 }
3615
3616 /* Convert a DWARF attribute code into its string name. */
3617
3618 static const char *
3619 dwarf_attr_name (unsigned int attr)
3620 {
3621 const char *name;
3622
3623 switch (attr)
3624 {
3625 #if VMS_DEBUGGING_INFO
3626 case DW_AT_HP_prologue:
3627 return "DW_AT_HP_prologue";
3628 #else
3629 case DW_AT_MIPS_loop_unroll_factor:
3630 return "DW_AT_MIPS_loop_unroll_factor";
3631 #endif
3632
3633 #if VMS_DEBUGGING_INFO
3634 case DW_AT_HP_epilogue:
3635 return "DW_AT_HP_epilogue";
3636 #else
3637 case DW_AT_MIPS_stride:
3638 return "DW_AT_MIPS_stride";
3639 #endif
3640 }
3641
3642 name = get_DW_AT_name (attr);
3643
3644 if (name != NULL)
3645 return name;
3646
3647 return "DW_AT_<unknown>";
3648 }
3649
3650 /* Convert a DWARF value form code into its string name. */
3651
3652 static const char *
3653 dwarf_form_name (unsigned int form)
3654 {
3655 const char *name = get_DW_FORM_name (form);
3656
3657 if (name != NULL)
3658 return name;
3659
3660 return "DW_FORM_<unknown>";
3661 }
3662 \f
3663 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3664 instance of an inlined instance of a decl which is local to an inline
3665 function, so we have to trace all of the way back through the origin chain
3666 to find out what sort of node actually served as the original seed for the
3667 given block. */
3668
3669 static tree
3670 decl_ultimate_origin (const_tree decl)
3671 {
3672 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3673 return NULL_TREE;
3674
3675 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3676 nodes in the function to point to themselves; ignore that if
3677 we're trying to output the abstract instance of this function. */
3678 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3679 return NULL_TREE;
3680
3681 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3682 most distant ancestor, this should never happen. */
3683 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3684
3685 return DECL_ABSTRACT_ORIGIN (decl);
3686 }
3687
3688 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3689 of a virtual function may refer to a base class, so we check the 'this'
3690 parameter. */
3691
3692 static tree
3693 decl_class_context (tree decl)
3694 {
3695 tree context = NULL_TREE;
3696
3697 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3698 context = DECL_CONTEXT (decl);
3699 else
3700 context = TYPE_MAIN_VARIANT
3701 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3702
3703 if (context && !TYPE_P (context))
3704 context = NULL_TREE;
3705
3706 return context;
3707 }
3708 \f
3709 /* Add an attribute/value pair to a DIE. */
3710
3711 static inline void
3712 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3713 {
3714 /* Maybe this should be an assert? */
3715 if (die == NULL)
3716 return;
3717
3718 vec_safe_reserve (die->die_attr, 1);
3719 vec_safe_push (die->die_attr, *attr);
3720 }
3721
3722 static inline enum dw_val_class
3723 AT_class (dw_attr_ref a)
3724 {
3725 return a->dw_attr_val.val_class;
3726 }
3727
3728 /* Return the index for any attribute that will be referenced with a
3729 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3730 are stored in dw_attr_val.v.val_str for reference counting
3731 pruning. */
3732
3733 static inline unsigned int
3734 AT_index (dw_attr_ref a)
3735 {
3736 if (AT_class (a) == dw_val_class_str)
3737 return a->dw_attr_val.v.val_str->index;
3738 else if (a->dw_attr_val.val_entry != NULL)
3739 return a->dw_attr_val.val_entry->index;
3740 return NOT_INDEXED;
3741 }
3742
3743 /* Add a flag value attribute to a DIE. */
3744
3745 static inline void
3746 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3747 {
3748 dw_attr_node attr;
3749
3750 attr.dw_attr = attr_kind;
3751 attr.dw_attr_val.val_class = dw_val_class_flag;
3752 attr.dw_attr_val.val_entry = NULL;
3753 attr.dw_attr_val.v.val_flag = flag;
3754 add_dwarf_attr (die, &attr);
3755 }
3756
3757 static inline unsigned
3758 AT_flag (dw_attr_ref a)
3759 {
3760 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3761 return a->dw_attr_val.v.val_flag;
3762 }
3763
3764 /* Add a signed integer attribute value to a DIE. */
3765
3766 static inline void
3767 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3768 {
3769 dw_attr_node attr;
3770
3771 attr.dw_attr = attr_kind;
3772 attr.dw_attr_val.val_class = dw_val_class_const;
3773 attr.dw_attr_val.val_entry = NULL;
3774 attr.dw_attr_val.v.val_int = int_val;
3775 add_dwarf_attr (die, &attr);
3776 }
3777
3778 static inline HOST_WIDE_INT
3779 AT_int (dw_attr_ref a)
3780 {
3781 gcc_assert (a && AT_class (a) == dw_val_class_const);
3782 return a->dw_attr_val.v.val_int;
3783 }
3784
3785 /* Add an unsigned integer attribute value to a DIE. */
3786
3787 static inline void
3788 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3789 unsigned HOST_WIDE_INT unsigned_val)
3790 {
3791 dw_attr_node attr;
3792
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3797 add_dwarf_attr (die, &attr);
3798 }
3799
3800 static inline unsigned HOST_WIDE_INT
3801 AT_unsigned (dw_attr_ref a)
3802 {
3803 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3804 return a->dw_attr_val.v.val_unsigned;
3805 }
3806
3807 /* Add an unsigned wide integer attribute value to a DIE. */
3808
3809 static inline void
3810 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3811 const wide_int& w)
3812 {
3813 dw_attr_node attr;
3814
3815 attr.dw_attr = attr_kind;
3816 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3817 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3818 *attr.dw_attr_val.v.val_wide = w;
3819 add_dwarf_attr (die, &attr);
3820 }
3821
3822 /* Add an unsigned double integer attribute value to a DIE. */
3823
3824 static inline void
3825 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3826 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3827 {
3828 dw_attr_node attr;
3829
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const_double;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_double.high = high;
3834 attr.dw_attr_val.v.val_double.low = low;
3835 add_dwarf_attr (die, &attr);
3836 }
3837
3838 /* Add a floating point attribute value to a DIE and return it. */
3839
3840 static inline void
3841 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3842 unsigned int length, unsigned int elt_size, unsigned char *array)
3843 {
3844 dw_attr_node attr;
3845
3846 attr.dw_attr = attr_kind;
3847 attr.dw_attr_val.val_class = dw_val_class_vec;
3848 attr.dw_attr_val.val_entry = NULL;
3849 attr.dw_attr_val.v.val_vec.length = length;
3850 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3851 attr.dw_attr_val.v.val_vec.array = array;
3852 add_dwarf_attr (die, &attr);
3853 }
3854
3855 /* Add an 8-byte data attribute value to a DIE. */
3856
3857 static inline void
3858 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3859 unsigned char data8[8])
3860 {
3861 dw_attr_node attr;
3862
3863 attr.dw_attr = attr_kind;
3864 attr.dw_attr_val.val_class = dw_val_class_data8;
3865 attr.dw_attr_val.val_entry = NULL;
3866 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3867 add_dwarf_attr (die, &attr);
3868 }
3869
3870 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3871 dwarf_split_debug_info, address attributes in dies destined for the
3872 final executable have force_direct set to avoid using indexed
3873 references. */
3874
3875 static inline void
3876 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3877 bool force_direct)
3878 {
3879 dw_attr_node attr;
3880 char * lbl_id;
3881
3882 lbl_id = xstrdup (lbl_low);
3883 attr.dw_attr = DW_AT_low_pc;
3884 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3885 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3886 if (dwarf_split_debug_info && !force_direct)
3887 attr.dw_attr_val.val_entry
3888 = add_addr_table_entry (lbl_id, ate_kind_label);
3889 else
3890 attr.dw_attr_val.val_entry = NULL;
3891 add_dwarf_attr (die, &attr);
3892
3893 attr.dw_attr = DW_AT_high_pc;
3894 if (dwarf_version < 4)
3895 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3896 else
3897 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3898 lbl_id = xstrdup (lbl_high);
3899 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3900 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3901 && dwarf_split_debug_info && !force_direct)
3902 attr.dw_attr_val.val_entry
3903 = add_addr_table_entry (lbl_id, ate_kind_label);
3904 else
3905 attr.dw_attr_val.val_entry = NULL;
3906 add_dwarf_attr (die, &attr);
3907 }
3908
3909 /* Hash and equality functions for debug_str_hash. */
3910
3911 static hashval_t
3912 debug_str_do_hash (const void *x)
3913 {
3914 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3915 }
3916
3917 static int
3918 debug_str_eq (const void *x1, const void *x2)
3919 {
3920 return strcmp ((((const struct indirect_string_node *)x1)->str),
3921 (const char *)x2) == 0;
3922 }
3923
3924 /* Add STR to the given string hash table. */
3925
3926 static struct indirect_string_node *
3927 find_AT_string_in_table (const char *str, htab_t table)
3928 {
3929 struct indirect_string_node *node;
3930 void **slot;
3931
3932 slot = htab_find_slot_with_hash (table, str,
3933 htab_hash_string (str), INSERT);
3934 if (*slot == NULL)
3935 {
3936 node = ggc_cleared_alloc<indirect_string_node> ();
3937 node->str = ggc_strdup (str);
3938 *slot = node;
3939 }
3940 else
3941 node = (struct indirect_string_node *) *slot;
3942
3943 node->refcount++;
3944 return node;
3945 }
3946
3947 /* Add STR to the indirect string hash table. */
3948
3949 static struct indirect_string_node *
3950 find_AT_string (const char *str)
3951 {
3952 if (! debug_str_hash)
3953 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3954 debug_str_eq, NULL);
3955
3956 return find_AT_string_in_table (str, debug_str_hash);
3957 }
3958
3959 /* Add a string attribute value to a DIE. */
3960
3961 static inline void
3962 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3963 {
3964 dw_attr_node attr;
3965 struct indirect_string_node *node;
3966
3967 node = find_AT_string (str);
3968
3969 attr.dw_attr = attr_kind;
3970 attr.dw_attr_val.val_class = dw_val_class_str;
3971 attr.dw_attr_val.val_entry = NULL;
3972 attr.dw_attr_val.v.val_str = node;
3973 add_dwarf_attr (die, &attr);
3974 }
3975
3976 static inline const char *
3977 AT_string (dw_attr_ref a)
3978 {
3979 gcc_assert (a && AT_class (a) == dw_val_class_str);
3980 return a->dw_attr_val.v.val_str->str;
3981 }
3982
3983 /* Call this function directly to bypass AT_string_form's logic to put
3984 the string inline in the die. */
3985
3986 static void
3987 set_indirect_string (struct indirect_string_node *node)
3988 {
3989 char label[32];
3990 /* Already indirect is a no op. */
3991 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3992 {
3993 gcc_assert (node->label);
3994 return;
3995 }
3996 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3997 ++dw2_string_counter;
3998 node->label = xstrdup (label);
3999
4000 if (!dwarf_split_debug_info)
4001 {
4002 node->form = DW_FORM_strp;
4003 node->index = NOT_INDEXED;
4004 }
4005 else
4006 {
4007 node->form = DW_FORM_GNU_str_index;
4008 node->index = NO_INDEX_ASSIGNED;
4009 }
4010 }
4011
4012 /* Find out whether a string should be output inline in DIE
4013 or out-of-line in .debug_str section. */
4014
4015 static enum dwarf_form
4016 find_string_form (struct indirect_string_node *node)
4017 {
4018 unsigned int len;
4019
4020 if (node->form)
4021 return node->form;
4022
4023 len = strlen (node->str) + 1;
4024
4025 /* If the string is shorter or equal to the size of the reference, it is
4026 always better to put it inline. */
4027 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4028 return node->form = DW_FORM_string;
4029
4030 /* If we cannot expect the linker to merge strings in .debug_str
4031 section, only put it into .debug_str if it is worth even in this
4032 single module. */
4033 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4034 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4035 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4036 return node->form = DW_FORM_string;
4037
4038 set_indirect_string (node);
4039
4040 return node->form;
4041 }
4042
4043 /* Find out whether the string referenced from the attribute should be
4044 output inline in DIE or out-of-line in .debug_str section. */
4045
4046 static enum dwarf_form
4047 AT_string_form (dw_attr_ref a)
4048 {
4049 gcc_assert (a && AT_class (a) == dw_val_class_str);
4050 return find_string_form (a->dw_attr_val.v.val_str);
4051 }
4052
4053 /* Add a DIE reference attribute value to a DIE. */
4054
4055 static inline void
4056 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4057 {
4058 dw_attr_node attr;
4059
4060 #ifdef ENABLE_CHECKING
4061 gcc_assert (targ_die != NULL);
4062 #else
4063 /* With LTO we can end up trying to reference something we didn't create
4064 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4065 if (targ_die == NULL)
4066 return;
4067 #endif
4068
4069 attr.dw_attr = attr_kind;
4070 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4071 attr.dw_attr_val.val_entry = NULL;
4072 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4073 attr.dw_attr_val.v.val_die_ref.external = 0;
4074 add_dwarf_attr (die, &attr);
4075 }
4076
4077 /* Change DIE reference REF to point to NEW_DIE instead. */
4078
4079 static inline void
4080 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4081 {
4082 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4083 ref->dw_attr_val.v.val_die_ref.die = new_die;
4084 ref->dw_attr_val.v.val_die_ref.external = 0;
4085 }
4086
4087 /* Add an AT_specification attribute to a DIE, and also make the back
4088 pointer from the specification to the definition. */
4089
4090 static inline void
4091 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4092 {
4093 add_AT_die_ref (die, DW_AT_specification, targ_die);
4094 gcc_assert (!targ_die->die_definition);
4095 targ_die->die_definition = die;
4096 }
4097
4098 static inline dw_die_ref
4099 AT_ref (dw_attr_ref a)
4100 {
4101 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4102 return a->dw_attr_val.v.val_die_ref.die;
4103 }
4104
4105 static inline int
4106 AT_ref_external (dw_attr_ref a)
4107 {
4108 if (a && AT_class (a) == dw_val_class_die_ref)
4109 return a->dw_attr_val.v.val_die_ref.external;
4110
4111 return 0;
4112 }
4113
4114 static inline void
4115 set_AT_ref_external (dw_attr_ref a, int i)
4116 {
4117 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4118 a->dw_attr_val.v.val_die_ref.external = i;
4119 }
4120
4121 /* Add an FDE reference attribute value to a DIE. */
4122
4123 static inline void
4124 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4125 {
4126 dw_attr_node attr;
4127
4128 attr.dw_attr = attr_kind;
4129 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4130 attr.dw_attr_val.val_entry = NULL;
4131 attr.dw_attr_val.v.val_fde_index = targ_fde;
4132 add_dwarf_attr (die, &attr);
4133 }
4134
4135 /* Add a location description attribute value to a DIE. */
4136
4137 static inline void
4138 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4139 {
4140 dw_attr_node attr;
4141
4142 attr.dw_attr = attr_kind;
4143 attr.dw_attr_val.val_class = dw_val_class_loc;
4144 attr.dw_attr_val.val_entry = NULL;
4145 attr.dw_attr_val.v.val_loc = loc;
4146 add_dwarf_attr (die, &attr);
4147 }
4148
4149 static inline dw_loc_descr_ref
4150 AT_loc (dw_attr_ref a)
4151 {
4152 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4153 return a->dw_attr_val.v.val_loc;
4154 }
4155
4156 static inline void
4157 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4158 {
4159 dw_attr_node attr;
4160
4161 attr.dw_attr = attr_kind;
4162 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4163 attr.dw_attr_val.val_entry = NULL;
4164 attr.dw_attr_val.v.val_loc_list = loc_list;
4165 add_dwarf_attr (die, &attr);
4166 have_location_lists = true;
4167 }
4168
4169 static inline dw_loc_list_ref
4170 AT_loc_list (dw_attr_ref a)
4171 {
4172 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4173 return a->dw_attr_val.v.val_loc_list;
4174 }
4175
4176 static inline dw_loc_list_ref *
4177 AT_loc_list_ptr (dw_attr_ref a)
4178 {
4179 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4180 return &a->dw_attr_val.v.val_loc_list;
4181 }
4182
4183 /* Table of entries into the .debug_addr section. */
4184
4185 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4186
4187 /* Hash an address_table_entry. */
4188
4189 static hashval_t
4190 addr_table_entry_do_hash (const void *x)
4191 {
4192 const addr_table_entry *a = (const addr_table_entry *) x;
4193 switch (a->kind)
4194 {
4195 case ate_kind_rtx:
4196 return iterative_hash_rtx (a->addr.rtl, 0);
4197 case ate_kind_rtx_dtprel:
4198 return iterative_hash_rtx (a->addr.rtl, 1);
4199 case ate_kind_label:
4200 return htab_hash_string (a->addr.label);
4201 default:
4202 gcc_unreachable ();
4203 }
4204 }
4205
4206 /* Determine equality for two address_table_entries. */
4207
4208 static int
4209 addr_table_entry_eq (const void *x1, const void *x2)
4210 {
4211 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4212 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4213
4214 if (a1->kind != a2->kind)
4215 return 0;
4216 switch (a1->kind)
4217 {
4218 case ate_kind_rtx:
4219 case ate_kind_rtx_dtprel:
4220 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4221 case ate_kind_label:
4222 return strcmp (a1->addr.label, a2->addr.label) == 0;
4223 default:
4224 gcc_unreachable ();
4225 }
4226 }
4227
4228 /* Initialize an addr_table_entry. */
4229
4230 void
4231 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4232 {
4233 e->kind = kind;
4234 switch (kind)
4235 {
4236 case ate_kind_rtx:
4237 case ate_kind_rtx_dtprel:
4238 e->addr.rtl = (rtx) addr;
4239 break;
4240 case ate_kind_label:
4241 e->addr.label = (char *) addr;
4242 break;
4243 }
4244 e->refcount = 0;
4245 e->index = NO_INDEX_ASSIGNED;
4246 }
4247
4248 /* Add attr to the address table entry to the table. Defer setting an
4249 index until output time. */
4250
4251 static addr_table_entry *
4252 add_addr_table_entry (void *addr, enum ate_kind kind)
4253 {
4254 addr_table_entry *node;
4255 addr_table_entry finder;
4256 void **slot;
4257
4258 gcc_assert (dwarf_split_debug_info);
4259 if (! addr_index_table)
4260 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4261 addr_table_entry_eq, NULL);
4262 init_addr_table_entry (&finder, kind, addr);
4263 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4264
4265 if (*slot == HTAB_EMPTY_ENTRY)
4266 {
4267 node = ggc_cleared_alloc<addr_table_entry> ();
4268 init_addr_table_entry (node, kind, addr);
4269 *slot = node;
4270 }
4271 else
4272 node = (addr_table_entry *) *slot;
4273
4274 node->refcount++;
4275 return node;
4276 }
4277
4278 /* Remove an entry from the addr table by decrementing its refcount.
4279 Strictly, decrementing the refcount would be enough, but the
4280 assertion that the entry is actually in the table has found
4281 bugs. */
4282
4283 static void
4284 remove_addr_table_entry (addr_table_entry *entry)
4285 {
4286 addr_table_entry *node;
4287
4288 gcc_assert (dwarf_split_debug_info && addr_index_table);
4289 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4290 /* After an index is assigned, the table is frozen. */
4291 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4292 node->refcount--;
4293 }
4294
4295 /* Given a location list, remove all addresses it refers to from the
4296 address_table. */
4297
4298 static void
4299 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4300 {
4301 for (; descr; descr = descr->dw_loc_next)
4302 if (descr->dw_loc_oprnd1.val_entry != NULL)
4303 {
4304 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4305 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4306 }
4307 }
4308
4309 /* A helper function for dwarf2out_finish called through
4310 htab_traverse. Assign an addr_table_entry its index. All entries
4311 must be collected into the table when this function is called,
4312 because the indexing code relies on htab_traverse to traverse nodes
4313 in the same order for each run. */
4314
4315 static int
4316 index_addr_table_entry (void **h, void *v)
4317 {
4318 addr_table_entry *node = (addr_table_entry *) *h;
4319 unsigned int *index = (unsigned int *) v;
4320
4321 /* Don't index unreferenced nodes. */
4322 if (node->refcount == 0)
4323 return 1;
4324
4325 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4326 node->index = *index;
4327 *index += 1;
4328
4329 return 1;
4330 }
4331
4332 /* Add an address constant attribute value to a DIE. When using
4333 dwarf_split_debug_info, address attributes in dies destined for the
4334 final executable should be direct references--setting the parameter
4335 force_direct ensures this behavior. */
4336
4337 static inline void
4338 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4339 bool force_direct)
4340 {
4341 dw_attr_node attr;
4342
4343 attr.dw_attr = attr_kind;
4344 attr.dw_attr_val.val_class = dw_val_class_addr;
4345 attr.dw_attr_val.v.val_addr = addr;
4346 if (dwarf_split_debug_info && !force_direct)
4347 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4348 else
4349 attr.dw_attr_val.val_entry = NULL;
4350 add_dwarf_attr (die, &attr);
4351 }
4352
4353 /* Get the RTX from to an address DIE attribute. */
4354
4355 static inline rtx
4356 AT_addr (dw_attr_ref a)
4357 {
4358 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4359 return a->dw_attr_val.v.val_addr;
4360 }
4361
4362 /* Add a file attribute value to a DIE. */
4363
4364 static inline void
4365 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4366 struct dwarf_file_data *fd)
4367 {
4368 dw_attr_node attr;
4369
4370 attr.dw_attr = attr_kind;
4371 attr.dw_attr_val.val_class = dw_val_class_file;
4372 attr.dw_attr_val.val_entry = NULL;
4373 attr.dw_attr_val.v.val_file = fd;
4374 add_dwarf_attr (die, &attr);
4375 }
4376
4377 /* Get the dwarf_file_data from a file DIE attribute. */
4378
4379 static inline struct dwarf_file_data *
4380 AT_file (dw_attr_ref a)
4381 {
4382 gcc_assert (a && AT_class (a) == dw_val_class_file);
4383 return a->dw_attr_val.v.val_file;
4384 }
4385
4386 /* Add a vms delta attribute value to a DIE. */
4387
4388 static inline void
4389 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4390 const char *lbl1, const char *lbl2)
4391 {
4392 dw_attr_node attr;
4393
4394 attr.dw_attr = attr_kind;
4395 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4396 attr.dw_attr_val.val_entry = NULL;
4397 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4398 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4399 add_dwarf_attr (die, &attr);
4400 }
4401
4402 /* Add a label identifier attribute value to a DIE. */
4403
4404 static inline void
4405 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4406 const char *lbl_id)
4407 {
4408 dw_attr_node attr;
4409
4410 attr.dw_attr = attr_kind;
4411 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4412 attr.dw_attr_val.val_entry = NULL;
4413 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4414 if (dwarf_split_debug_info)
4415 attr.dw_attr_val.val_entry
4416 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4417 ate_kind_label);
4418 add_dwarf_attr (die, &attr);
4419 }
4420
4421 /* Add a section offset attribute value to a DIE, an offset into the
4422 debug_line section. */
4423
4424 static inline void
4425 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4426 const char *label)
4427 {
4428 dw_attr_node attr;
4429
4430 attr.dw_attr = attr_kind;
4431 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4432 attr.dw_attr_val.val_entry = NULL;
4433 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4434 add_dwarf_attr (die, &attr);
4435 }
4436
4437 /* Add a section offset attribute value to a DIE, an offset into the
4438 debug_macinfo section. */
4439
4440 static inline void
4441 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4442 const char *label)
4443 {
4444 dw_attr_node attr;
4445
4446 attr.dw_attr = attr_kind;
4447 attr.dw_attr_val.val_class = dw_val_class_macptr;
4448 attr.dw_attr_val.val_entry = NULL;
4449 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4450 add_dwarf_attr (die, &attr);
4451 }
4452
4453 /* Add an offset attribute value to a DIE. */
4454
4455 static inline void
4456 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4457 unsigned HOST_WIDE_INT offset)
4458 {
4459 dw_attr_node attr;
4460
4461 attr.dw_attr = attr_kind;
4462 attr.dw_attr_val.val_class = dw_val_class_offset;
4463 attr.dw_attr_val.val_entry = NULL;
4464 attr.dw_attr_val.v.val_offset = offset;
4465 add_dwarf_attr (die, &attr);
4466 }
4467
4468 /* Add a range_list attribute value to a DIE. When using
4469 dwarf_split_debug_info, address attributes in dies destined for the
4470 final executable should be direct references--setting the parameter
4471 force_direct ensures this behavior. */
4472
4473 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4474 #define RELOCATED_OFFSET (NULL)
4475
4476 static void
4477 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4478 long unsigned int offset, bool force_direct)
4479 {
4480 dw_attr_node attr;
4481
4482 attr.dw_attr = attr_kind;
4483 attr.dw_attr_val.val_class = dw_val_class_range_list;
4484 /* For the range_list attribute, use val_entry to store whether the
4485 offset should follow split-debug-info or normal semantics. This
4486 value is read in output_range_list_offset. */
4487 if (dwarf_split_debug_info && !force_direct)
4488 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4489 else
4490 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4491 attr.dw_attr_val.v.val_offset = offset;
4492 add_dwarf_attr (die, &attr);
4493 }
4494
4495 /* Return the start label of a delta attribute. */
4496
4497 static inline const char *
4498 AT_vms_delta1 (dw_attr_ref a)
4499 {
4500 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4501 return a->dw_attr_val.v.val_vms_delta.lbl1;
4502 }
4503
4504 /* Return the end label of a delta attribute. */
4505
4506 static inline const char *
4507 AT_vms_delta2 (dw_attr_ref a)
4508 {
4509 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4510 return a->dw_attr_val.v.val_vms_delta.lbl2;
4511 }
4512
4513 static inline const char *
4514 AT_lbl (dw_attr_ref a)
4515 {
4516 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4517 || AT_class (a) == dw_val_class_lineptr
4518 || AT_class (a) == dw_val_class_macptr
4519 || AT_class (a) == dw_val_class_high_pc));
4520 return a->dw_attr_val.v.val_lbl_id;
4521 }
4522
4523 /* Get the attribute of type attr_kind. */
4524
4525 static dw_attr_ref
4526 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4527 {
4528 dw_attr_ref a;
4529 unsigned ix;
4530 dw_die_ref spec = NULL;
4531
4532 if (! die)
4533 return NULL;
4534
4535 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4536 if (a->dw_attr == attr_kind)
4537 return a;
4538 else if (a->dw_attr == DW_AT_specification
4539 || a->dw_attr == DW_AT_abstract_origin)
4540 spec = AT_ref (a);
4541
4542 if (spec)
4543 return get_AT (spec, attr_kind);
4544
4545 return NULL;
4546 }
4547
4548 /* Returns the parent of the declaration of DIE. */
4549
4550 static dw_die_ref
4551 get_die_parent (dw_die_ref die)
4552 {
4553 dw_die_ref t;
4554
4555 if (!die)
4556 return NULL;
4557
4558 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4559 || (t = get_AT_ref (die, DW_AT_specification)))
4560 die = t;
4561
4562 return die->die_parent;
4563 }
4564
4565 /* Return the "low pc" attribute value, typically associated with a subprogram
4566 DIE. Return null if the "low pc" attribute is either not present, or if it
4567 cannot be represented as an assembler label identifier. */
4568
4569 static inline const char *
4570 get_AT_low_pc (dw_die_ref die)
4571 {
4572 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4573
4574 return a ? AT_lbl (a) : NULL;
4575 }
4576
4577 /* Return the "high pc" attribute value, typically associated with a subprogram
4578 DIE. Return null if the "high pc" attribute is either not present, or if it
4579 cannot be represented as an assembler label identifier. */
4580
4581 static inline const char *
4582 get_AT_hi_pc (dw_die_ref die)
4583 {
4584 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4585
4586 return a ? AT_lbl (a) : NULL;
4587 }
4588
4589 /* Return the value of the string attribute designated by ATTR_KIND, or
4590 NULL if it is not present. */
4591
4592 static inline const char *
4593 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4594 {
4595 dw_attr_ref a = get_AT (die, attr_kind);
4596
4597 return a ? AT_string (a) : NULL;
4598 }
4599
4600 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4601 if it is not present. */
4602
4603 static inline int
4604 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4605 {
4606 dw_attr_ref a = get_AT (die, attr_kind);
4607
4608 return a ? AT_flag (a) : 0;
4609 }
4610
4611 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4612 if it is not present. */
4613
4614 static inline unsigned
4615 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4616 {
4617 dw_attr_ref a = get_AT (die, attr_kind);
4618
4619 return a ? AT_unsigned (a) : 0;
4620 }
4621
4622 static inline dw_die_ref
4623 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4624 {
4625 dw_attr_ref a = get_AT (die, attr_kind);
4626
4627 return a ? AT_ref (a) : NULL;
4628 }
4629
4630 static inline struct dwarf_file_data *
4631 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4632 {
4633 dw_attr_ref a = get_AT (die, attr_kind);
4634
4635 return a ? AT_file (a) : NULL;
4636 }
4637
4638 /* Return TRUE if the language is C++. */
4639
4640 static inline bool
4641 is_cxx (void)
4642 {
4643 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4644
4645 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4646 }
4647
4648 /* Return TRUE if the language is Java. */
4649
4650 static inline bool
4651 is_java (void)
4652 {
4653 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4654
4655 return lang == DW_LANG_Java;
4656 }
4657
4658 /* Return TRUE if the language is Fortran. */
4659
4660 static inline bool
4661 is_fortran (void)
4662 {
4663 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4664
4665 return (lang == DW_LANG_Fortran77
4666 || lang == DW_LANG_Fortran90
4667 || lang == DW_LANG_Fortran95);
4668 }
4669
4670 /* Return TRUE if the language is Ada. */
4671
4672 static inline bool
4673 is_ada (void)
4674 {
4675 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4676
4677 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4678 }
4679
4680 /* Remove the specified attribute if present. */
4681
4682 static void
4683 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4684 {
4685 dw_attr_ref a;
4686 unsigned ix;
4687
4688 if (! die)
4689 return;
4690
4691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4692 if (a->dw_attr == attr_kind)
4693 {
4694 if (AT_class (a) == dw_val_class_str)
4695 if (a->dw_attr_val.v.val_str->refcount)
4696 a->dw_attr_val.v.val_str->refcount--;
4697
4698 /* vec::ordered_remove should help reduce the number of abbrevs
4699 that are needed. */
4700 die->die_attr->ordered_remove (ix);
4701 return;
4702 }
4703 }
4704
4705 /* Remove CHILD from its parent. PREV must have the property that
4706 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4707
4708 static void
4709 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4710 {
4711 gcc_assert (child->die_parent == prev->die_parent);
4712 gcc_assert (prev->die_sib == child);
4713 if (prev == child)
4714 {
4715 gcc_assert (child->die_parent->die_child == child);
4716 prev = NULL;
4717 }
4718 else
4719 prev->die_sib = child->die_sib;
4720 if (child->die_parent->die_child == child)
4721 child->die_parent->die_child = prev;
4722 }
4723
4724 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4725 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4726
4727 static void
4728 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4729 {
4730 dw_die_ref parent = old_child->die_parent;
4731
4732 gcc_assert (parent == prev->die_parent);
4733 gcc_assert (prev->die_sib == old_child);
4734
4735 new_child->die_parent = parent;
4736 if (prev == old_child)
4737 {
4738 gcc_assert (parent->die_child == old_child);
4739 new_child->die_sib = new_child;
4740 }
4741 else
4742 {
4743 prev->die_sib = new_child;
4744 new_child->die_sib = old_child->die_sib;
4745 }
4746 if (old_child->die_parent->die_child == old_child)
4747 old_child->die_parent->die_child = new_child;
4748 }
4749
4750 /* Move all children from OLD_PARENT to NEW_PARENT. */
4751
4752 static void
4753 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4754 {
4755 dw_die_ref c;
4756 new_parent->die_child = old_parent->die_child;
4757 old_parent->die_child = NULL;
4758 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4759 }
4760
4761 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4762 matches TAG. */
4763
4764 static void
4765 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4766 {
4767 dw_die_ref c;
4768
4769 c = die->die_child;
4770 if (c) do {
4771 dw_die_ref prev = c;
4772 c = c->die_sib;
4773 while (c->die_tag == tag)
4774 {
4775 remove_child_with_prev (c, prev);
4776 /* Might have removed every child. */
4777 if (c == c->die_sib)
4778 return;
4779 c = c->die_sib;
4780 }
4781 } while (c != die->die_child);
4782 }
4783
4784 /* Add a CHILD_DIE as the last child of DIE. */
4785
4786 static void
4787 add_child_die (dw_die_ref die, dw_die_ref child_die)
4788 {
4789 /* FIXME this should probably be an assert. */
4790 if (! die || ! child_die)
4791 return;
4792 gcc_assert (die != child_die);
4793
4794 child_die->die_parent = die;
4795 if (die->die_child)
4796 {
4797 child_die->die_sib = die->die_child->die_sib;
4798 die->die_child->die_sib = child_die;
4799 }
4800 else
4801 child_die->die_sib = child_die;
4802 die->die_child = child_die;
4803 }
4804
4805 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4806 is the specification, to the end of PARENT's list of children.
4807 This is done by removing and re-adding it. */
4808
4809 static void
4810 splice_child_die (dw_die_ref parent, dw_die_ref child)
4811 {
4812 dw_die_ref p;
4813
4814 /* We want the declaration DIE from inside the class, not the
4815 specification DIE at toplevel. */
4816 if (child->die_parent != parent)
4817 {
4818 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4819
4820 if (tmp)
4821 child = tmp;
4822 }
4823
4824 gcc_assert (child->die_parent == parent
4825 || (child->die_parent
4826 == get_AT_ref (parent, DW_AT_specification)));
4827
4828 for (p = child->die_parent->die_child; ; p = p->die_sib)
4829 if (p->die_sib == child)
4830 {
4831 remove_child_with_prev (child, p);
4832 break;
4833 }
4834
4835 add_child_die (parent, child);
4836 }
4837
4838 /* Return a pointer to a newly created DIE node. */
4839
4840 static inline dw_die_ref
4841 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4842 {
4843 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4844
4845 die->die_tag = tag_value;
4846
4847 if (parent_die != NULL)
4848 add_child_die (parent_die, die);
4849 else
4850 {
4851 limbo_die_node *limbo_node;
4852
4853 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4854 limbo_node->die = die;
4855 limbo_node->created_for = t;
4856 limbo_node->next = limbo_die_list;
4857 limbo_die_list = limbo_node;
4858 }
4859
4860 return die;
4861 }
4862
4863 /* Return the DIE associated with the given type specifier. */
4864
4865 static inline dw_die_ref
4866 lookup_type_die (tree type)
4867 {
4868 return TYPE_SYMTAB_DIE (type);
4869 }
4870
4871 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4872 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4873 anonymous type instead the one of the naming typedef. */
4874
4875 static inline dw_die_ref
4876 strip_naming_typedef (tree type, dw_die_ref type_die)
4877 {
4878 if (type
4879 && TREE_CODE (type) == RECORD_TYPE
4880 && type_die
4881 && type_die->die_tag == DW_TAG_typedef
4882 && is_naming_typedef_decl (TYPE_NAME (type)))
4883 type_die = get_AT_ref (type_die, DW_AT_type);
4884 return type_die;
4885 }
4886
4887 /* Like lookup_type_die, but if type is an anonymous type named by a
4888 typedef[1], return the DIE of the anonymous type instead the one of
4889 the naming typedef. This is because in gen_typedef_die, we did
4890 equate the anonymous struct named by the typedef with the DIE of
4891 the naming typedef. So by default, lookup_type_die on an anonymous
4892 struct yields the DIE of the naming typedef.
4893
4894 [1]: Read the comment of is_naming_typedef_decl to learn about what
4895 a naming typedef is. */
4896
4897 static inline dw_die_ref
4898 lookup_type_die_strip_naming_typedef (tree type)
4899 {
4900 dw_die_ref die = lookup_type_die (type);
4901 return strip_naming_typedef (type, die);
4902 }
4903
4904 /* Equate a DIE to a given type specifier. */
4905
4906 static inline void
4907 equate_type_number_to_die (tree type, dw_die_ref type_die)
4908 {
4909 TYPE_SYMTAB_DIE (type) = type_die;
4910 }
4911
4912 /* Returns a hash value for X (which really is a die_struct). */
4913
4914 static hashval_t
4915 decl_die_table_hash (const void *x)
4916 {
4917 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4918 }
4919
4920 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4921
4922 static int
4923 decl_die_table_eq (const void *x, const void *y)
4924 {
4925 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4926 }
4927
4928 /* Return the DIE associated with a given declaration. */
4929
4930 static inline dw_die_ref
4931 lookup_decl_die (tree decl)
4932 {
4933 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4934 }
4935
4936 /* Returns a hash value for X (which really is a var_loc_list). */
4937
4938 static hashval_t
4939 decl_loc_table_hash (const void *x)
4940 {
4941 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4942 }
4943
4944 /* Return nonzero if decl_id of var_loc_list X is the same as
4945 UID of decl *Y. */
4946
4947 static int
4948 decl_loc_table_eq (const void *x, const void *y)
4949 {
4950 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4951 }
4952
4953 /* Return the var_loc list associated with a given declaration. */
4954
4955 static inline var_loc_list *
4956 lookup_decl_loc (const_tree decl)
4957 {
4958 if (!decl_loc_table)
4959 return NULL;
4960 return (var_loc_list *)
4961 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4962 }
4963
4964 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4965
4966 static hashval_t
4967 cached_dw_loc_list_table_hash (const void *x)
4968 {
4969 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4970 }
4971
4972 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4973 UID of decl *Y. */
4974
4975 static int
4976 cached_dw_loc_list_table_eq (const void *x, const void *y)
4977 {
4978 return (((const cached_dw_loc_list *) x)->decl_id
4979 == DECL_UID ((const_tree) y));
4980 }
4981
4982 /* Equate a DIE to a particular declaration. */
4983
4984 static void
4985 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4986 {
4987 unsigned int decl_id = DECL_UID (decl);
4988 void **slot;
4989
4990 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4991 *slot = decl_die;
4992 decl_die->decl_id = decl_id;
4993 }
4994
4995 /* Return how many bits covers PIECE EXPR_LIST. */
4996
4997 static int
4998 decl_piece_bitsize (rtx piece)
4999 {
5000 int ret = (int) GET_MODE (piece);
5001 if (ret)
5002 return ret;
5003 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5004 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5005 return INTVAL (XEXP (XEXP (piece, 0), 0));
5006 }
5007
5008 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5009
5010 static rtx *
5011 decl_piece_varloc_ptr (rtx piece)
5012 {
5013 if ((int) GET_MODE (piece))
5014 return &XEXP (piece, 0);
5015 else
5016 return &XEXP (XEXP (piece, 0), 1);
5017 }
5018
5019 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5020 Next is the chain of following piece nodes. */
5021
5022 static rtx
5023 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5024 {
5025 if (bitsize <= (int) MAX_MACHINE_MODE)
5026 return alloc_EXPR_LIST (bitsize, loc_note, next);
5027 else
5028 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5029 GEN_INT (bitsize),
5030 loc_note), next);
5031 }
5032
5033 /* Return rtx that should be stored into loc field for
5034 LOC_NOTE and BITPOS/BITSIZE. */
5035
5036 static rtx
5037 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5038 HOST_WIDE_INT bitsize)
5039 {
5040 if (bitsize != -1)
5041 {
5042 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5043 if (bitpos != 0)
5044 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5045 }
5046 return loc_note;
5047 }
5048
5049 /* This function either modifies location piece list *DEST in
5050 place (if SRC and INNER is NULL), or copies location piece list
5051 *SRC to *DEST while modifying it. Location BITPOS is modified
5052 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5053 not copied and if needed some padding around it is added.
5054 When modifying in place, DEST should point to EXPR_LIST where
5055 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5056 to the start of the whole list and INNER points to the EXPR_LIST
5057 where earlier pieces cover PIECE_BITPOS bits. */
5058
5059 static void
5060 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5061 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5062 HOST_WIDE_INT bitsize, rtx loc_note)
5063 {
5064 int diff;
5065 bool copy = inner != NULL;
5066
5067 if (copy)
5068 {
5069 /* First copy all nodes preceding the current bitpos. */
5070 while (src != inner)
5071 {
5072 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5073 decl_piece_bitsize (*src), NULL_RTX);
5074 dest = &XEXP (*dest, 1);
5075 src = &XEXP (*src, 1);
5076 }
5077 }
5078 /* Add padding if needed. */
5079 if (bitpos != piece_bitpos)
5080 {
5081 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5082 copy ? NULL_RTX : *dest);
5083 dest = &XEXP (*dest, 1);
5084 }
5085 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5086 {
5087 gcc_assert (!copy);
5088 /* A piece with correct bitpos and bitsize already exist,
5089 just update the location for it and return. */
5090 *decl_piece_varloc_ptr (*dest) = loc_note;
5091 return;
5092 }
5093 /* Add the piece that changed. */
5094 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5095 dest = &XEXP (*dest, 1);
5096 /* Skip over pieces that overlap it. */
5097 diff = bitpos - piece_bitpos + bitsize;
5098 if (!copy)
5099 src = dest;
5100 while (diff > 0 && *src)
5101 {
5102 rtx piece = *src;
5103 diff -= decl_piece_bitsize (piece);
5104 if (copy)
5105 src = &XEXP (piece, 1);
5106 else
5107 {
5108 *src = XEXP (piece, 1);
5109 free_EXPR_LIST_node (piece);
5110 }
5111 }
5112 /* Add padding if needed. */
5113 if (diff < 0 && *src)
5114 {
5115 if (!copy)
5116 dest = src;
5117 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5118 dest = &XEXP (*dest, 1);
5119 }
5120 if (!copy)
5121 return;
5122 /* Finally copy all nodes following it. */
5123 while (*src)
5124 {
5125 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5126 decl_piece_bitsize (*src), NULL_RTX);
5127 dest = &XEXP (*dest, 1);
5128 src = &XEXP (*src, 1);
5129 }
5130 }
5131
5132 /* Add a variable location node to the linked list for DECL. */
5133
5134 static struct var_loc_node *
5135 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5136 {
5137 unsigned int decl_id;
5138 var_loc_list *temp;
5139 void **slot;
5140 struct var_loc_node *loc = NULL;
5141 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5142
5143 if (TREE_CODE (decl) == VAR_DECL
5144 && DECL_HAS_DEBUG_EXPR_P (decl))
5145 {
5146 tree realdecl = DECL_DEBUG_EXPR (decl);
5147 if (handled_component_p (realdecl)
5148 || (TREE_CODE (realdecl) == MEM_REF
5149 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5150 {
5151 HOST_WIDE_INT maxsize;
5152 tree innerdecl;
5153 innerdecl
5154 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5155 if (!DECL_P (innerdecl)
5156 || DECL_IGNORED_P (innerdecl)
5157 || TREE_STATIC (innerdecl)
5158 || bitsize <= 0
5159 || bitpos + bitsize > 256
5160 || bitsize != maxsize)
5161 return NULL;
5162 decl = innerdecl;
5163 }
5164 }
5165
5166 decl_id = DECL_UID (decl);
5167 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5168 if (*slot == NULL)
5169 {
5170 temp = ggc_cleared_alloc<var_loc_list> ();
5171 temp->decl_id = decl_id;
5172 *slot = temp;
5173 }
5174 else
5175 temp = (var_loc_list *) *slot;
5176
5177 /* For PARM_DECLs try to keep around the original incoming value,
5178 even if that means we'll emit a zero-range .debug_loc entry. */
5179 if (temp->last
5180 && temp->first == temp->last
5181 && TREE_CODE (decl) == PARM_DECL
5182 && NOTE_P (temp->first->loc)
5183 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5184 && DECL_INCOMING_RTL (decl)
5185 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5186 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5187 == GET_CODE (DECL_INCOMING_RTL (decl))
5188 && prev_real_insn (temp->first->loc) == NULL_RTX
5189 && (bitsize != -1
5190 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5191 NOTE_VAR_LOCATION_LOC (loc_note))
5192 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5193 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5194 {
5195 loc = ggc_cleared_alloc<var_loc_node> ();
5196 temp->first->next = loc;
5197 temp->last = loc;
5198 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5199 }
5200 else if (temp->last)
5201 {
5202 struct var_loc_node *last = temp->last, *unused = NULL;
5203 rtx *piece_loc = NULL, last_loc_note;
5204 int piece_bitpos = 0;
5205 if (last->next)
5206 {
5207 last = last->next;
5208 gcc_assert (last->next == NULL);
5209 }
5210 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5211 {
5212 piece_loc = &last->loc;
5213 do
5214 {
5215 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5216 if (piece_bitpos + cur_bitsize > bitpos)
5217 break;
5218 piece_bitpos += cur_bitsize;
5219 piece_loc = &XEXP (*piece_loc, 1);
5220 }
5221 while (*piece_loc);
5222 }
5223 /* TEMP->LAST here is either pointer to the last but one or
5224 last element in the chained list, LAST is pointer to the
5225 last element. */
5226 if (label && strcmp (last->label, label) == 0)
5227 {
5228 /* For SRA optimized variables if there weren't any real
5229 insns since last note, just modify the last node. */
5230 if (piece_loc != NULL)
5231 {
5232 adjust_piece_list (piece_loc, NULL, NULL,
5233 bitpos, piece_bitpos, bitsize, loc_note);
5234 return NULL;
5235 }
5236 /* If the last note doesn't cover any instructions, remove it. */
5237 if (temp->last != last)
5238 {
5239 temp->last->next = NULL;
5240 unused = last;
5241 last = temp->last;
5242 gcc_assert (strcmp (last->label, label) != 0);
5243 }
5244 else
5245 {
5246 gcc_assert (temp->first == temp->last
5247 || (temp->first->next == temp->last
5248 && TREE_CODE (decl) == PARM_DECL));
5249 memset (temp->last, '\0', sizeof (*temp->last));
5250 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5251 return temp->last;
5252 }
5253 }
5254 if (bitsize == -1 && NOTE_P (last->loc))
5255 last_loc_note = last->loc;
5256 else if (piece_loc != NULL
5257 && *piece_loc != NULL_RTX
5258 && piece_bitpos == bitpos
5259 && decl_piece_bitsize (*piece_loc) == bitsize)
5260 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5261 else
5262 last_loc_note = NULL_RTX;
5263 /* If the current location is the same as the end of the list,
5264 and either both or neither of the locations is uninitialized,
5265 we have nothing to do. */
5266 if (last_loc_note == NULL_RTX
5267 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5268 NOTE_VAR_LOCATION_LOC (loc_note)))
5269 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5270 != NOTE_VAR_LOCATION_STATUS (loc_note))
5271 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5272 == VAR_INIT_STATUS_UNINITIALIZED)
5273 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5274 == VAR_INIT_STATUS_UNINITIALIZED))))
5275 {
5276 /* Add LOC to the end of list and update LAST. If the last
5277 element of the list has been removed above, reuse its
5278 memory for the new node, otherwise allocate a new one. */
5279 if (unused)
5280 {
5281 loc = unused;
5282 memset (loc, '\0', sizeof (*loc));
5283 }
5284 else
5285 loc = ggc_cleared_alloc<var_loc_node> ();
5286 if (bitsize == -1 || piece_loc == NULL)
5287 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5288 else
5289 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5290 bitpos, piece_bitpos, bitsize, loc_note);
5291 last->next = loc;
5292 /* Ensure TEMP->LAST will point either to the new last but one
5293 element of the chain, or to the last element in it. */
5294 if (last != temp->last)
5295 temp->last = last;
5296 }
5297 else if (unused)
5298 ggc_free (unused);
5299 }
5300 else
5301 {
5302 loc = ggc_cleared_alloc<var_loc_node> ();
5303 temp->first = loc;
5304 temp->last = loc;
5305 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5306 }
5307 return loc;
5308 }
5309 \f
5310 /* Keep track of the number of spaces used to indent the
5311 output of the debugging routines that print the structure of
5312 the DIE internal representation. */
5313 static int print_indent;
5314
5315 /* Indent the line the number of spaces given by print_indent. */
5316
5317 static inline void
5318 print_spaces (FILE *outfile)
5319 {
5320 fprintf (outfile, "%*s", print_indent, "");
5321 }
5322
5323 /* Print a type signature in hex. */
5324
5325 static inline void
5326 print_signature (FILE *outfile, char *sig)
5327 {
5328 int i;
5329
5330 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5331 fprintf (outfile, "%02x", sig[i] & 0xff);
5332 }
5333
5334 /* Print the information associated with a given DIE, and its children.
5335 This routine is a debugging aid only. */
5336
5337 static void
5338 print_die (dw_die_ref die, FILE *outfile)
5339 {
5340 dw_attr_ref a;
5341 dw_die_ref c;
5342 unsigned ix;
5343
5344 print_spaces (outfile);
5345 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5346 die->die_offset, dwarf_tag_name (die->die_tag),
5347 (void*) die);
5348 print_spaces (outfile);
5349 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5350 fprintf (outfile, " offset: %ld", die->die_offset);
5351 fprintf (outfile, " mark: %d\n", die->die_mark);
5352
5353 if (die->comdat_type_p)
5354 {
5355 print_spaces (outfile);
5356 fprintf (outfile, " signature: ");
5357 print_signature (outfile, die->die_id.die_type_node->signature);
5358 fprintf (outfile, "\n");
5359 }
5360
5361 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5362 {
5363 print_spaces (outfile);
5364 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5365
5366 switch (AT_class (a))
5367 {
5368 case dw_val_class_addr:
5369 fprintf (outfile, "address");
5370 break;
5371 case dw_val_class_offset:
5372 fprintf (outfile, "offset");
5373 break;
5374 case dw_val_class_loc:
5375 fprintf (outfile, "location descriptor");
5376 break;
5377 case dw_val_class_loc_list:
5378 fprintf (outfile, "location list -> label:%s",
5379 AT_loc_list (a)->ll_symbol);
5380 break;
5381 case dw_val_class_range_list:
5382 fprintf (outfile, "range list");
5383 break;
5384 case dw_val_class_const:
5385 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5386 break;
5387 case dw_val_class_unsigned_const:
5388 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5389 break;
5390 case dw_val_class_const_double:
5391 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5392 HOST_WIDE_INT_PRINT_UNSIGNED")",
5393 a->dw_attr_val.v.val_double.high,
5394 a->dw_attr_val.v.val_double.low);
5395 break;
5396 case dw_val_class_wide_int:
5397 {
5398 int i = a->dw_attr_val.v.val_wide->get_len ();
5399 fprintf (outfile, "constant (");
5400 gcc_assert (i > 0);
5401 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5402 fprintf (outfile, "0x");
5403 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5404 a->dw_attr_val.v.val_wide->elt (--i));
5405 while (--i >= 0)
5406 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5407 a->dw_attr_val.v.val_wide->elt (i));
5408 fprintf (outfile, ")");
5409 break;
5410 }
5411 case dw_val_class_vec:
5412 fprintf (outfile, "floating-point or vector constant");
5413 break;
5414 case dw_val_class_flag:
5415 fprintf (outfile, "%u", AT_flag (a));
5416 break;
5417 case dw_val_class_die_ref:
5418 if (AT_ref (a) != NULL)
5419 {
5420 if (AT_ref (a)->comdat_type_p)
5421 {
5422 fprintf (outfile, "die -> signature: ");
5423 print_signature (outfile,
5424 AT_ref (a)->die_id.die_type_node->signature);
5425 }
5426 else if (AT_ref (a)->die_id.die_symbol)
5427 fprintf (outfile, "die -> label: %s",
5428 AT_ref (a)->die_id.die_symbol);
5429 else
5430 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5431 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5432 }
5433 else
5434 fprintf (outfile, "die -> <null>");
5435 break;
5436 case dw_val_class_vms_delta:
5437 fprintf (outfile, "delta: @slotcount(%s-%s)",
5438 AT_vms_delta2 (a), AT_vms_delta1 (a));
5439 break;
5440 case dw_val_class_lbl_id:
5441 case dw_val_class_lineptr:
5442 case dw_val_class_macptr:
5443 case dw_val_class_high_pc:
5444 fprintf (outfile, "label: %s", AT_lbl (a));
5445 break;
5446 case dw_val_class_str:
5447 if (AT_string (a) != NULL)
5448 fprintf (outfile, "\"%s\"", AT_string (a));
5449 else
5450 fprintf (outfile, "<null>");
5451 break;
5452 case dw_val_class_file:
5453 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5454 AT_file (a)->emitted_number);
5455 break;
5456 case dw_val_class_data8:
5457 {
5458 int i;
5459
5460 for (i = 0; i < 8; i++)
5461 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5462 break;
5463 }
5464 default:
5465 break;
5466 }
5467
5468 fprintf (outfile, "\n");
5469 }
5470
5471 if (die->die_child != NULL)
5472 {
5473 print_indent += 4;
5474 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5475 print_indent -= 4;
5476 }
5477 if (print_indent == 0)
5478 fprintf (outfile, "\n");
5479 }
5480
5481 /* Print the information collected for a given DIE. */
5482
5483 DEBUG_FUNCTION void
5484 debug_dwarf_die (dw_die_ref die)
5485 {
5486 print_die (die, stderr);
5487 }
5488
5489 DEBUG_FUNCTION void
5490 debug (die_struct &ref)
5491 {
5492 print_die (&ref, stderr);
5493 }
5494
5495 DEBUG_FUNCTION void
5496 debug (die_struct *ptr)
5497 {
5498 if (ptr)
5499 debug (*ptr);
5500 else
5501 fprintf (stderr, "<nil>\n");
5502 }
5503
5504
5505 /* Print all DWARF information collected for the compilation unit.
5506 This routine is a debugging aid only. */
5507
5508 DEBUG_FUNCTION void
5509 debug_dwarf (void)
5510 {
5511 print_indent = 0;
5512 print_die (comp_unit_die (), stderr);
5513 }
5514 \f
5515 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5516 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5517 DIE that marks the start of the DIEs for this include file. */
5518
5519 static dw_die_ref
5520 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5521 {
5522 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5523 dw_die_ref new_unit = gen_compile_unit_die (filename);
5524
5525 new_unit->die_sib = old_unit;
5526 return new_unit;
5527 }
5528
5529 /* Close an include-file CU and reopen the enclosing one. */
5530
5531 static dw_die_ref
5532 pop_compile_unit (dw_die_ref old_unit)
5533 {
5534 dw_die_ref new_unit = old_unit->die_sib;
5535
5536 old_unit->die_sib = NULL;
5537 return new_unit;
5538 }
5539
5540 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5541 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5542 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5543
5544 /* Calculate the checksum of a location expression. */
5545
5546 static inline void
5547 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5548 {
5549 int tem;
5550 hashval_t hash = 0;
5551
5552 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5553 CHECKSUM (tem);
5554 hash = hash_loc_operands (loc, hash);
5555 CHECKSUM (hash);
5556 }
5557
5558 /* Calculate the checksum of an attribute. */
5559
5560 static void
5561 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5562 {
5563 dw_loc_descr_ref loc;
5564 rtx r;
5565
5566 CHECKSUM (at->dw_attr);
5567
5568 /* We don't care that this was compiled with a different compiler
5569 snapshot; if the output is the same, that's what matters. */
5570 if (at->dw_attr == DW_AT_producer)
5571 return;
5572
5573 switch (AT_class (at))
5574 {
5575 case dw_val_class_const:
5576 CHECKSUM (at->dw_attr_val.v.val_int);
5577 break;
5578 case dw_val_class_unsigned_const:
5579 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5580 break;
5581 case dw_val_class_const_double:
5582 CHECKSUM (at->dw_attr_val.v.val_double);
5583 break;
5584 case dw_val_class_wide_int:
5585 CHECKSUM (*at->dw_attr_val.v.val_wide);
5586 break;
5587 case dw_val_class_vec:
5588 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5589 (at->dw_attr_val.v.val_vec.length
5590 * at->dw_attr_val.v.val_vec.elt_size));
5591 break;
5592 case dw_val_class_flag:
5593 CHECKSUM (at->dw_attr_val.v.val_flag);
5594 break;
5595 case dw_val_class_str:
5596 CHECKSUM_STRING (AT_string (at));
5597 break;
5598
5599 case dw_val_class_addr:
5600 r = AT_addr (at);
5601 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5602 CHECKSUM_STRING (XSTR (r, 0));
5603 break;
5604
5605 case dw_val_class_offset:
5606 CHECKSUM (at->dw_attr_val.v.val_offset);
5607 break;
5608
5609 case dw_val_class_loc:
5610 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5611 loc_checksum (loc, ctx);
5612 break;
5613
5614 case dw_val_class_die_ref:
5615 die_checksum (AT_ref (at), ctx, mark);
5616 break;
5617
5618 case dw_val_class_fde_ref:
5619 case dw_val_class_vms_delta:
5620 case dw_val_class_lbl_id:
5621 case dw_val_class_lineptr:
5622 case dw_val_class_macptr:
5623 case dw_val_class_high_pc:
5624 break;
5625
5626 case dw_val_class_file:
5627 CHECKSUM_STRING (AT_file (at)->filename);
5628 break;
5629
5630 case dw_val_class_data8:
5631 CHECKSUM (at->dw_attr_val.v.val_data8);
5632 break;
5633
5634 default:
5635 break;
5636 }
5637 }
5638
5639 /* Calculate the checksum of a DIE. */
5640
5641 static void
5642 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5643 {
5644 dw_die_ref c;
5645 dw_attr_ref a;
5646 unsigned ix;
5647
5648 /* To avoid infinite recursion. */
5649 if (die->die_mark)
5650 {
5651 CHECKSUM (die->die_mark);
5652 return;
5653 }
5654 die->die_mark = ++(*mark);
5655
5656 CHECKSUM (die->die_tag);
5657
5658 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5659 attr_checksum (a, ctx, mark);
5660
5661 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5662 }
5663
5664 #undef CHECKSUM
5665 #undef CHECKSUM_BLOCK
5666 #undef CHECKSUM_STRING
5667
5668 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5669 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5670 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5671 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5672 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5673 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5674 #define CHECKSUM_ATTR(FOO) \
5675 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5676
5677 /* Calculate the checksum of a number in signed LEB128 format. */
5678
5679 static void
5680 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5681 {
5682 unsigned char byte;
5683 bool more;
5684
5685 while (1)
5686 {
5687 byte = (value & 0x7f);
5688 value >>= 7;
5689 more = !((value == 0 && (byte & 0x40) == 0)
5690 || (value == -1 && (byte & 0x40) != 0));
5691 if (more)
5692 byte |= 0x80;
5693 CHECKSUM (byte);
5694 if (!more)
5695 break;
5696 }
5697 }
5698
5699 /* Calculate the checksum of a number in unsigned LEB128 format. */
5700
5701 static void
5702 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5703 {
5704 while (1)
5705 {
5706 unsigned char byte = (value & 0x7f);
5707 value >>= 7;
5708 if (value != 0)
5709 /* More bytes to follow. */
5710 byte |= 0x80;
5711 CHECKSUM (byte);
5712 if (value == 0)
5713 break;
5714 }
5715 }
5716
5717 /* Checksum the context of the DIE. This adds the names of any
5718 surrounding namespaces or structures to the checksum. */
5719
5720 static void
5721 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5722 {
5723 const char *name;
5724 dw_die_ref spec;
5725 int tag = die->die_tag;
5726
5727 if (tag != DW_TAG_namespace
5728 && tag != DW_TAG_structure_type
5729 && tag != DW_TAG_class_type)
5730 return;
5731
5732 name = get_AT_string (die, DW_AT_name);
5733
5734 spec = get_AT_ref (die, DW_AT_specification);
5735 if (spec != NULL)
5736 die = spec;
5737
5738 if (die->die_parent != NULL)
5739 checksum_die_context (die->die_parent, ctx);
5740
5741 CHECKSUM_ULEB128 ('C');
5742 CHECKSUM_ULEB128 (tag);
5743 if (name != NULL)
5744 CHECKSUM_STRING (name);
5745 }
5746
5747 /* Calculate the checksum of a location expression. */
5748
5749 static inline void
5750 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5751 {
5752 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5753 were emitted as a DW_FORM_sdata instead of a location expression. */
5754 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5755 {
5756 CHECKSUM_ULEB128 (DW_FORM_sdata);
5757 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5758 return;
5759 }
5760
5761 /* Otherwise, just checksum the raw location expression. */
5762 while (loc != NULL)
5763 {
5764 hashval_t hash = 0;
5765
5766 CHECKSUM_ULEB128 (loc->dtprel);
5767 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5768 hash = hash_loc_operands (loc, hash);
5769 CHECKSUM (hash);
5770 loc = loc->dw_loc_next;
5771 }
5772 }
5773
5774 /* Calculate the checksum of an attribute. */
5775
5776 static void
5777 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5778 struct md5_ctx *ctx, int *mark)
5779 {
5780 dw_loc_descr_ref loc;
5781 rtx r;
5782
5783 if (AT_class (at) == dw_val_class_die_ref)
5784 {
5785 dw_die_ref target_die = AT_ref (at);
5786
5787 /* For pointer and reference types, we checksum only the (qualified)
5788 name of the target type (if there is a name). For friend entries,
5789 we checksum only the (qualified) name of the target type or function.
5790 This allows the checksum to remain the same whether the target type
5791 is complete or not. */
5792 if ((at->dw_attr == DW_AT_type
5793 && (tag == DW_TAG_pointer_type
5794 || tag == DW_TAG_reference_type
5795 || tag == DW_TAG_rvalue_reference_type
5796 || tag == DW_TAG_ptr_to_member_type))
5797 || (at->dw_attr == DW_AT_friend
5798 && tag == DW_TAG_friend))
5799 {
5800 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5801
5802 if (name_attr != NULL)
5803 {
5804 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5805
5806 if (decl == NULL)
5807 decl = target_die;
5808 CHECKSUM_ULEB128 ('N');
5809 CHECKSUM_ULEB128 (at->dw_attr);
5810 if (decl->die_parent != NULL)
5811 checksum_die_context (decl->die_parent, ctx);
5812 CHECKSUM_ULEB128 ('E');
5813 CHECKSUM_STRING (AT_string (name_attr));
5814 return;
5815 }
5816 }
5817
5818 /* For all other references to another DIE, we check to see if the
5819 target DIE has already been visited. If it has, we emit a
5820 backward reference; if not, we descend recursively. */
5821 if (target_die->die_mark > 0)
5822 {
5823 CHECKSUM_ULEB128 ('R');
5824 CHECKSUM_ULEB128 (at->dw_attr);
5825 CHECKSUM_ULEB128 (target_die->die_mark);
5826 }
5827 else
5828 {
5829 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5830
5831 if (decl == NULL)
5832 decl = target_die;
5833 target_die->die_mark = ++(*mark);
5834 CHECKSUM_ULEB128 ('T');
5835 CHECKSUM_ULEB128 (at->dw_attr);
5836 if (decl->die_parent != NULL)
5837 checksum_die_context (decl->die_parent, ctx);
5838 die_checksum_ordered (target_die, ctx, mark);
5839 }
5840 return;
5841 }
5842
5843 CHECKSUM_ULEB128 ('A');
5844 CHECKSUM_ULEB128 (at->dw_attr);
5845
5846 switch (AT_class (at))
5847 {
5848 case dw_val_class_const:
5849 CHECKSUM_ULEB128 (DW_FORM_sdata);
5850 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5851 break;
5852
5853 case dw_val_class_unsigned_const:
5854 CHECKSUM_ULEB128 (DW_FORM_sdata);
5855 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5856 break;
5857
5858 case dw_val_class_const_double:
5859 CHECKSUM_ULEB128 (DW_FORM_block);
5860 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5861 CHECKSUM (at->dw_attr_val.v.val_double);
5862 break;
5863
5864 case dw_val_class_wide_int:
5865 CHECKSUM_ULEB128 (DW_FORM_block);
5866 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5867 CHECKSUM (*at->dw_attr_val.v.val_wide);
5868 break;
5869
5870 case dw_val_class_vec:
5871 CHECKSUM_ULEB128 (DW_FORM_block);
5872 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5873 * at->dw_attr_val.v.val_vec.elt_size);
5874 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5875 (at->dw_attr_val.v.val_vec.length
5876 * at->dw_attr_val.v.val_vec.elt_size));
5877 break;
5878
5879 case dw_val_class_flag:
5880 CHECKSUM_ULEB128 (DW_FORM_flag);
5881 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5882 break;
5883
5884 case dw_val_class_str:
5885 CHECKSUM_ULEB128 (DW_FORM_string);
5886 CHECKSUM_STRING (AT_string (at));
5887 break;
5888
5889 case dw_val_class_addr:
5890 r = AT_addr (at);
5891 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5892 CHECKSUM_ULEB128 (DW_FORM_string);
5893 CHECKSUM_STRING (XSTR (r, 0));
5894 break;
5895
5896 case dw_val_class_offset:
5897 CHECKSUM_ULEB128 (DW_FORM_sdata);
5898 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5899 break;
5900
5901 case dw_val_class_loc:
5902 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5903 loc_checksum_ordered (loc, ctx);
5904 break;
5905
5906 case dw_val_class_fde_ref:
5907 case dw_val_class_lbl_id:
5908 case dw_val_class_lineptr:
5909 case dw_val_class_macptr:
5910 case dw_val_class_high_pc:
5911 break;
5912
5913 case dw_val_class_file:
5914 CHECKSUM_ULEB128 (DW_FORM_string);
5915 CHECKSUM_STRING (AT_file (at)->filename);
5916 break;
5917
5918 case dw_val_class_data8:
5919 CHECKSUM (at->dw_attr_val.v.val_data8);
5920 break;
5921
5922 default:
5923 break;
5924 }
5925 }
5926
5927 struct checksum_attributes
5928 {
5929 dw_attr_ref at_name;
5930 dw_attr_ref at_type;
5931 dw_attr_ref at_friend;
5932 dw_attr_ref at_accessibility;
5933 dw_attr_ref at_address_class;
5934 dw_attr_ref at_allocated;
5935 dw_attr_ref at_artificial;
5936 dw_attr_ref at_associated;
5937 dw_attr_ref at_binary_scale;
5938 dw_attr_ref at_bit_offset;
5939 dw_attr_ref at_bit_size;
5940 dw_attr_ref at_bit_stride;
5941 dw_attr_ref at_byte_size;
5942 dw_attr_ref at_byte_stride;
5943 dw_attr_ref at_const_value;
5944 dw_attr_ref at_containing_type;
5945 dw_attr_ref at_count;
5946 dw_attr_ref at_data_location;
5947 dw_attr_ref at_data_member_location;
5948 dw_attr_ref at_decimal_scale;
5949 dw_attr_ref at_decimal_sign;
5950 dw_attr_ref at_default_value;
5951 dw_attr_ref at_digit_count;
5952 dw_attr_ref at_discr;
5953 dw_attr_ref at_discr_list;
5954 dw_attr_ref at_discr_value;
5955 dw_attr_ref at_encoding;
5956 dw_attr_ref at_endianity;
5957 dw_attr_ref at_explicit;
5958 dw_attr_ref at_is_optional;
5959 dw_attr_ref at_location;
5960 dw_attr_ref at_lower_bound;
5961 dw_attr_ref at_mutable;
5962 dw_attr_ref at_ordering;
5963 dw_attr_ref at_picture_string;
5964 dw_attr_ref at_prototyped;
5965 dw_attr_ref at_small;
5966 dw_attr_ref at_segment;
5967 dw_attr_ref at_string_length;
5968 dw_attr_ref at_threads_scaled;
5969 dw_attr_ref at_upper_bound;
5970 dw_attr_ref at_use_location;
5971 dw_attr_ref at_use_UTF8;
5972 dw_attr_ref at_variable_parameter;
5973 dw_attr_ref at_virtuality;
5974 dw_attr_ref at_visibility;
5975 dw_attr_ref at_vtable_elem_location;
5976 };
5977
5978 /* Collect the attributes that we will want to use for the checksum. */
5979
5980 static void
5981 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5982 {
5983 dw_attr_ref a;
5984 unsigned ix;
5985
5986 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5987 {
5988 switch (a->dw_attr)
5989 {
5990 case DW_AT_name:
5991 attrs->at_name = a;
5992 break;
5993 case DW_AT_type:
5994 attrs->at_type = a;
5995 break;
5996 case DW_AT_friend:
5997 attrs->at_friend = a;
5998 break;
5999 case DW_AT_accessibility:
6000 attrs->at_accessibility = a;
6001 break;
6002 case DW_AT_address_class:
6003 attrs->at_address_class = a;
6004 break;
6005 case DW_AT_allocated:
6006 attrs->at_allocated = a;
6007 break;
6008 case DW_AT_artificial:
6009 attrs->at_artificial = a;
6010 break;
6011 case DW_AT_associated:
6012 attrs->at_associated = a;
6013 break;
6014 case DW_AT_binary_scale:
6015 attrs->at_binary_scale = a;
6016 break;
6017 case DW_AT_bit_offset:
6018 attrs->at_bit_offset = a;
6019 break;
6020 case DW_AT_bit_size:
6021 attrs->at_bit_size = a;
6022 break;
6023 case DW_AT_bit_stride:
6024 attrs->at_bit_stride = a;
6025 break;
6026 case DW_AT_byte_size:
6027 attrs->at_byte_size = a;
6028 break;
6029 case DW_AT_byte_stride:
6030 attrs->at_byte_stride = a;
6031 break;
6032 case DW_AT_const_value:
6033 attrs->at_const_value = a;
6034 break;
6035 case DW_AT_containing_type:
6036 attrs->at_containing_type = a;
6037 break;
6038 case DW_AT_count:
6039 attrs->at_count = a;
6040 break;
6041 case DW_AT_data_location:
6042 attrs->at_data_location = a;
6043 break;
6044 case DW_AT_data_member_location:
6045 attrs->at_data_member_location = a;
6046 break;
6047 case DW_AT_decimal_scale:
6048 attrs->at_decimal_scale = a;
6049 break;
6050 case DW_AT_decimal_sign:
6051 attrs->at_decimal_sign = a;
6052 break;
6053 case DW_AT_default_value:
6054 attrs->at_default_value = a;
6055 break;
6056 case DW_AT_digit_count:
6057 attrs->at_digit_count = a;
6058 break;
6059 case DW_AT_discr:
6060 attrs->at_discr = a;
6061 break;
6062 case DW_AT_discr_list:
6063 attrs->at_discr_list = a;
6064 break;
6065 case DW_AT_discr_value:
6066 attrs->at_discr_value = a;
6067 break;
6068 case DW_AT_encoding:
6069 attrs->at_encoding = a;
6070 break;
6071 case DW_AT_endianity:
6072 attrs->at_endianity = a;
6073 break;
6074 case DW_AT_explicit:
6075 attrs->at_explicit = a;
6076 break;
6077 case DW_AT_is_optional:
6078 attrs->at_is_optional = a;
6079 break;
6080 case DW_AT_location:
6081 attrs->at_location = a;
6082 break;
6083 case DW_AT_lower_bound:
6084 attrs->at_lower_bound = a;
6085 break;
6086 case DW_AT_mutable:
6087 attrs->at_mutable = a;
6088 break;
6089 case DW_AT_ordering:
6090 attrs->at_ordering = a;
6091 break;
6092 case DW_AT_picture_string:
6093 attrs->at_picture_string = a;
6094 break;
6095 case DW_AT_prototyped:
6096 attrs->at_prototyped = a;
6097 break;
6098 case DW_AT_small:
6099 attrs->at_small = a;
6100 break;
6101 case DW_AT_segment:
6102 attrs->at_segment = a;
6103 break;
6104 case DW_AT_string_length:
6105 attrs->at_string_length = a;
6106 break;
6107 case DW_AT_threads_scaled:
6108 attrs->at_threads_scaled = a;
6109 break;
6110 case DW_AT_upper_bound:
6111 attrs->at_upper_bound = a;
6112 break;
6113 case DW_AT_use_location:
6114 attrs->at_use_location = a;
6115 break;
6116 case DW_AT_use_UTF8:
6117 attrs->at_use_UTF8 = a;
6118 break;
6119 case DW_AT_variable_parameter:
6120 attrs->at_variable_parameter = a;
6121 break;
6122 case DW_AT_virtuality:
6123 attrs->at_virtuality = a;
6124 break;
6125 case DW_AT_visibility:
6126 attrs->at_visibility = a;
6127 break;
6128 case DW_AT_vtable_elem_location:
6129 attrs->at_vtable_elem_location = a;
6130 break;
6131 default:
6132 break;
6133 }
6134 }
6135 }
6136
6137 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6138
6139 static void
6140 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6141 {
6142 dw_die_ref c;
6143 dw_die_ref decl;
6144 struct checksum_attributes attrs;
6145
6146 CHECKSUM_ULEB128 ('D');
6147 CHECKSUM_ULEB128 (die->die_tag);
6148
6149 memset (&attrs, 0, sizeof (attrs));
6150
6151 decl = get_AT_ref (die, DW_AT_specification);
6152 if (decl != NULL)
6153 collect_checksum_attributes (&attrs, decl);
6154 collect_checksum_attributes (&attrs, die);
6155
6156 CHECKSUM_ATTR (attrs.at_name);
6157 CHECKSUM_ATTR (attrs.at_accessibility);
6158 CHECKSUM_ATTR (attrs.at_address_class);
6159 CHECKSUM_ATTR (attrs.at_allocated);
6160 CHECKSUM_ATTR (attrs.at_artificial);
6161 CHECKSUM_ATTR (attrs.at_associated);
6162 CHECKSUM_ATTR (attrs.at_binary_scale);
6163 CHECKSUM_ATTR (attrs.at_bit_offset);
6164 CHECKSUM_ATTR (attrs.at_bit_size);
6165 CHECKSUM_ATTR (attrs.at_bit_stride);
6166 CHECKSUM_ATTR (attrs.at_byte_size);
6167 CHECKSUM_ATTR (attrs.at_byte_stride);
6168 CHECKSUM_ATTR (attrs.at_const_value);
6169 CHECKSUM_ATTR (attrs.at_containing_type);
6170 CHECKSUM_ATTR (attrs.at_count);
6171 CHECKSUM_ATTR (attrs.at_data_location);
6172 CHECKSUM_ATTR (attrs.at_data_member_location);
6173 CHECKSUM_ATTR (attrs.at_decimal_scale);
6174 CHECKSUM_ATTR (attrs.at_decimal_sign);
6175 CHECKSUM_ATTR (attrs.at_default_value);
6176 CHECKSUM_ATTR (attrs.at_digit_count);
6177 CHECKSUM_ATTR (attrs.at_discr);
6178 CHECKSUM_ATTR (attrs.at_discr_list);
6179 CHECKSUM_ATTR (attrs.at_discr_value);
6180 CHECKSUM_ATTR (attrs.at_encoding);
6181 CHECKSUM_ATTR (attrs.at_endianity);
6182 CHECKSUM_ATTR (attrs.at_explicit);
6183 CHECKSUM_ATTR (attrs.at_is_optional);
6184 CHECKSUM_ATTR (attrs.at_location);
6185 CHECKSUM_ATTR (attrs.at_lower_bound);
6186 CHECKSUM_ATTR (attrs.at_mutable);
6187 CHECKSUM_ATTR (attrs.at_ordering);
6188 CHECKSUM_ATTR (attrs.at_picture_string);
6189 CHECKSUM_ATTR (attrs.at_prototyped);
6190 CHECKSUM_ATTR (attrs.at_small);
6191 CHECKSUM_ATTR (attrs.at_segment);
6192 CHECKSUM_ATTR (attrs.at_string_length);
6193 CHECKSUM_ATTR (attrs.at_threads_scaled);
6194 CHECKSUM_ATTR (attrs.at_upper_bound);
6195 CHECKSUM_ATTR (attrs.at_use_location);
6196 CHECKSUM_ATTR (attrs.at_use_UTF8);
6197 CHECKSUM_ATTR (attrs.at_variable_parameter);
6198 CHECKSUM_ATTR (attrs.at_virtuality);
6199 CHECKSUM_ATTR (attrs.at_visibility);
6200 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6201 CHECKSUM_ATTR (attrs.at_type);
6202 CHECKSUM_ATTR (attrs.at_friend);
6203
6204 /* Checksum the child DIEs. */
6205 c = die->die_child;
6206 if (c) do {
6207 dw_attr_ref name_attr;
6208
6209 c = c->die_sib;
6210 name_attr = get_AT (c, DW_AT_name);
6211 if (is_template_instantiation (c))
6212 {
6213 /* Ignore instantiations of member type and function templates. */
6214 }
6215 else if (name_attr != NULL
6216 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6217 {
6218 /* Use a shallow checksum for named nested types and member
6219 functions. */
6220 CHECKSUM_ULEB128 ('S');
6221 CHECKSUM_ULEB128 (c->die_tag);
6222 CHECKSUM_STRING (AT_string (name_attr));
6223 }
6224 else
6225 {
6226 /* Use a deep checksum for other children. */
6227 /* Mark this DIE so it gets processed when unmarking. */
6228 if (c->die_mark == 0)
6229 c->die_mark = -1;
6230 die_checksum_ordered (c, ctx, mark);
6231 }
6232 } while (c != die->die_child);
6233
6234 CHECKSUM_ULEB128 (0);
6235 }
6236
6237 /* Add a type name and tag to a hash. */
6238 static void
6239 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6240 {
6241 CHECKSUM_ULEB128 (tag);
6242 CHECKSUM_STRING (name);
6243 }
6244
6245 #undef CHECKSUM
6246 #undef CHECKSUM_STRING
6247 #undef CHECKSUM_ATTR
6248 #undef CHECKSUM_LEB128
6249 #undef CHECKSUM_ULEB128
6250
6251 /* Generate the type signature for DIE. This is computed by generating an
6252 MD5 checksum over the DIE's tag, its relevant attributes, and its
6253 children. Attributes that are references to other DIEs are processed
6254 by recursion, using the MARK field to prevent infinite recursion.
6255 If the DIE is nested inside a namespace or another type, we also
6256 need to include that context in the signature. The lower 64 bits
6257 of the resulting MD5 checksum comprise the signature. */
6258
6259 static void
6260 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6261 {
6262 int mark;
6263 const char *name;
6264 unsigned char checksum[16];
6265 struct md5_ctx ctx;
6266 dw_die_ref decl;
6267 dw_die_ref parent;
6268
6269 name = get_AT_string (die, DW_AT_name);
6270 decl = get_AT_ref (die, DW_AT_specification);
6271 parent = get_die_parent (die);
6272
6273 /* First, compute a signature for just the type name (and its surrounding
6274 context, if any. This is stored in the type unit DIE for link-time
6275 ODR (one-definition rule) checking. */
6276
6277 if (is_cxx () && name != NULL)
6278 {
6279 md5_init_ctx (&ctx);
6280
6281 /* Checksum the names of surrounding namespaces and structures. */
6282 if (parent != NULL)
6283 checksum_die_context (parent, &ctx);
6284
6285 /* Checksum the current DIE. */
6286 die_odr_checksum (die->die_tag, name, &ctx);
6287 md5_finish_ctx (&ctx, checksum);
6288
6289 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6290 }
6291
6292 /* Next, compute the complete type signature. */
6293
6294 md5_init_ctx (&ctx);
6295 mark = 1;
6296 die->die_mark = mark;
6297
6298 /* Checksum the names of surrounding namespaces and structures. */
6299 if (parent != NULL)
6300 checksum_die_context (parent, &ctx);
6301
6302 /* Checksum the DIE and its children. */
6303 die_checksum_ordered (die, &ctx, &mark);
6304 unmark_all_dies (die);
6305 md5_finish_ctx (&ctx, checksum);
6306
6307 /* Store the signature in the type node and link the type DIE and the
6308 type node together. */
6309 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6310 DWARF_TYPE_SIGNATURE_SIZE);
6311 die->comdat_type_p = true;
6312 die->die_id.die_type_node = type_node;
6313 type_node->type_die = die;
6314
6315 /* If the DIE is a specification, link its declaration to the type node
6316 as well. */
6317 if (decl != NULL)
6318 {
6319 decl->comdat_type_p = true;
6320 decl->die_id.die_type_node = type_node;
6321 }
6322 }
6323
6324 /* Do the location expressions look same? */
6325 static inline int
6326 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6327 {
6328 return loc1->dw_loc_opc == loc2->dw_loc_opc
6329 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6330 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6331 }
6332
6333 /* Do the values look the same? */
6334 static int
6335 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6336 {
6337 dw_loc_descr_ref loc1, loc2;
6338 rtx r1, r2;
6339
6340 if (v1->val_class != v2->val_class)
6341 return 0;
6342
6343 switch (v1->val_class)
6344 {
6345 case dw_val_class_const:
6346 return v1->v.val_int == v2->v.val_int;
6347 case dw_val_class_unsigned_const:
6348 return v1->v.val_unsigned == v2->v.val_unsigned;
6349 case dw_val_class_const_double:
6350 return v1->v.val_double.high == v2->v.val_double.high
6351 && v1->v.val_double.low == v2->v.val_double.low;
6352 case dw_val_class_wide_int:
6353 return *v1->v.val_wide == *v2->v.val_wide;
6354 case dw_val_class_vec:
6355 if (v1->v.val_vec.length != v2->v.val_vec.length
6356 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6357 return 0;
6358 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6359 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6360 return 0;
6361 return 1;
6362 case dw_val_class_flag:
6363 return v1->v.val_flag == v2->v.val_flag;
6364 case dw_val_class_str:
6365 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6366
6367 case dw_val_class_addr:
6368 r1 = v1->v.val_addr;
6369 r2 = v2->v.val_addr;
6370 if (GET_CODE (r1) != GET_CODE (r2))
6371 return 0;
6372 return !rtx_equal_p (r1, r2);
6373
6374 case dw_val_class_offset:
6375 return v1->v.val_offset == v2->v.val_offset;
6376
6377 case dw_val_class_loc:
6378 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6379 loc1 && loc2;
6380 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6381 if (!same_loc_p (loc1, loc2, mark))
6382 return 0;
6383 return !loc1 && !loc2;
6384
6385 case dw_val_class_die_ref:
6386 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6387
6388 case dw_val_class_fde_ref:
6389 case dw_val_class_vms_delta:
6390 case dw_val_class_lbl_id:
6391 case dw_val_class_lineptr:
6392 case dw_val_class_macptr:
6393 case dw_val_class_high_pc:
6394 return 1;
6395
6396 case dw_val_class_file:
6397 return v1->v.val_file == v2->v.val_file;
6398
6399 case dw_val_class_data8:
6400 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6401
6402 default:
6403 return 1;
6404 }
6405 }
6406
6407 /* Do the attributes look the same? */
6408
6409 static int
6410 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6411 {
6412 if (at1->dw_attr != at2->dw_attr)
6413 return 0;
6414
6415 /* We don't care that this was compiled with a different compiler
6416 snapshot; if the output is the same, that's what matters. */
6417 if (at1->dw_attr == DW_AT_producer)
6418 return 1;
6419
6420 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6421 }
6422
6423 /* Do the dies look the same? */
6424
6425 static int
6426 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6427 {
6428 dw_die_ref c1, c2;
6429 dw_attr_ref a1;
6430 unsigned ix;
6431
6432 /* To avoid infinite recursion. */
6433 if (die1->die_mark)
6434 return die1->die_mark == die2->die_mark;
6435 die1->die_mark = die2->die_mark = ++(*mark);
6436
6437 if (die1->die_tag != die2->die_tag)
6438 return 0;
6439
6440 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6441 return 0;
6442
6443 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6444 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6445 return 0;
6446
6447 c1 = die1->die_child;
6448 c2 = die2->die_child;
6449 if (! c1)
6450 {
6451 if (c2)
6452 return 0;
6453 }
6454 else
6455 for (;;)
6456 {
6457 if (!same_die_p (c1, c2, mark))
6458 return 0;
6459 c1 = c1->die_sib;
6460 c2 = c2->die_sib;
6461 if (c1 == die1->die_child)
6462 {
6463 if (c2 == die2->die_child)
6464 break;
6465 else
6466 return 0;
6467 }
6468 }
6469
6470 return 1;
6471 }
6472
6473 /* Do the dies look the same? Wrapper around same_die_p. */
6474
6475 static int
6476 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6477 {
6478 int mark = 0;
6479 int ret = same_die_p (die1, die2, &mark);
6480
6481 unmark_all_dies (die1);
6482 unmark_all_dies (die2);
6483
6484 return ret;
6485 }
6486
6487 /* The prefix to attach to symbols on DIEs in the current comdat debug
6488 info section. */
6489 static const char *comdat_symbol_id;
6490
6491 /* The index of the current symbol within the current comdat CU. */
6492 static unsigned int comdat_symbol_number;
6493
6494 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6495 children, and set comdat_symbol_id accordingly. */
6496
6497 static void
6498 compute_section_prefix (dw_die_ref unit_die)
6499 {
6500 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6501 const char *base = die_name ? lbasename (die_name) : "anonymous";
6502 char *name = XALLOCAVEC (char, strlen (base) + 64);
6503 char *p;
6504 int i, mark;
6505 unsigned char checksum[16];
6506 struct md5_ctx ctx;
6507
6508 /* Compute the checksum of the DIE, then append part of it as hex digits to
6509 the name filename of the unit. */
6510
6511 md5_init_ctx (&ctx);
6512 mark = 0;
6513 die_checksum (unit_die, &ctx, &mark);
6514 unmark_all_dies (unit_die);
6515 md5_finish_ctx (&ctx, checksum);
6516
6517 sprintf (name, "%s.", base);
6518 clean_symbol_name (name);
6519
6520 p = name + strlen (name);
6521 for (i = 0; i < 4; i++)
6522 {
6523 sprintf (p, "%.2x", checksum[i]);
6524 p += 2;
6525 }
6526
6527 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6528 comdat_symbol_number = 0;
6529 }
6530
6531 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6532
6533 static int
6534 is_type_die (dw_die_ref die)
6535 {
6536 switch (die->die_tag)
6537 {
6538 case DW_TAG_array_type:
6539 case DW_TAG_class_type:
6540 case DW_TAG_interface_type:
6541 case DW_TAG_enumeration_type:
6542 case DW_TAG_pointer_type:
6543 case DW_TAG_reference_type:
6544 case DW_TAG_rvalue_reference_type:
6545 case DW_TAG_string_type:
6546 case DW_TAG_structure_type:
6547 case DW_TAG_subroutine_type:
6548 case DW_TAG_union_type:
6549 case DW_TAG_ptr_to_member_type:
6550 case DW_TAG_set_type:
6551 case DW_TAG_subrange_type:
6552 case DW_TAG_base_type:
6553 case DW_TAG_const_type:
6554 case DW_TAG_file_type:
6555 case DW_TAG_packed_type:
6556 case DW_TAG_volatile_type:
6557 case DW_TAG_typedef:
6558 return 1;
6559 default:
6560 return 0;
6561 }
6562 }
6563
6564 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6565 Basically, we want to choose the bits that are likely to be shared between
6566 compilations (types) and leave out the bits that are specific to individual
6567 compilations (functions). */
6568
6569 static int
6570 is_comdat_die (dw_die_ref c)
6571 {
6572 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6573 we do for stabs. The advantage is a greater likelihood of sharing between
6574 objects that don't include headers in the same order (and therefore would
6575 put the base types in a different comdat). jason 8/28/00 */
6576
6577 if (c->die_tag == DW_TAG_base_type)
6578 return 0;
6579
6580 if (c->die_tag == DW_TAG_pointer_type
6581 || c->die_tag == DW_TAG_reference_type
6582 || c->die_tag == DW_TAG_rvalue_reference_type
6583 || c->die_tag == DW_TAG_const_type
6584 || c->die_tag == DW_TAG_volatile_type)
6585 {
6586 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6587
6588 return t ? is_comdat_die (t) : 0;
6589 }
6590
6591 return is_type_die (c);
6592 }
6593
6594 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6595 compilation unit. */
6596
6597 static int
6598 is_symbol_die (dw_die_ref c)
6599 {
6600 return (is_type_die (c)
6601 || is_declaration_die (c)
6602 || c->die_tag == DW_TAG_namespace
6603 || c->die_tag == DW_TAG_module);
6604 }
6605
6606 /* Returns true iff C is a compile-unit DIE. */
6607
6608 static inline bool
6609 is_cu_die (dw_die_ref c)
6610 {
6611 return c && c->die_tag == DW_TAG_compile_unit;
6612 }
6613
6614 /* Returns true iff C is a unit DIE of some sort. */
6615
6616 static inline bool
6617 is_unit_die (dw_die_ref c)
6618 {
6619 return c && (c->die_tag == DW_TAG_compile_unit
6620 || c->die_tag == DW_TAG_partial_unit
6621 || c->die_tag == DW_TAG_type_unit);
6622 }
6623
6624 /* Returns true iff C is a namespace DIE. */
6625
6626 static inline bool
6627 is_namespace_die (dw_die_ref c)
6628 {
6629 return c && c->die_tag == DW_TAG_namespace;
6630 }
6631
6632 /* Returns true iff C is a class or structure DIE. */
6633
6634 static inline bool
6635 is_class_die (dw_die_ref c)
6636 {
6637 return c && (c->die_tag == DW_TAG_class_type
6638 || c->die_tag == DW_TAG_structure_type);
6639 }
6640
6641 /* Return non-zero if this DIE is a template parameter. */
6642
6643 static inline bool
6644 is_template_parameter (dw_die_ref die)
6645 {
6646 switch (die->die_tag)
6647 {
6648 case DW_TAG_template_type_param:
6649 case DW_TAG_template_value_param:
6650 case DW_TAG_GNU_template_template_param:
6651 case DW_TAG_GNU_template_parameter_pack:
6652 return true;
6653 default:
6654 return false;
6655 }
6656 }
6657
6658 /* Return non-zero if this DIE represents a template instantiation. */
6659
6660 static inline bool
6661 is_template_instantiation (dw_die_ref die)
6662 {
6663 dw_die_ref c;
6664
6665 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6666 return false;
6667 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6668 return false;
6669 }
6670
6671 static char *
6672 gen_internal_sym (const char *prefix)
6673 {
6674 char buf[256];
6675
6676 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6677 return xstrdup (buf);
6678 }
6679
6680 /* Assign symbols to all worthy DIEs under DIE. */
6681
6682 static void
6683 assign_symbol_names (dw_die_ref die)
6684 {
6685 dw_die_ref c;
6686
6687 if (is_symbol_die (die) && !die->comdat_type_p)
6688 {
6689 if (comdat_symbol_id)
6690 {
6691 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6692
6693 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6694 comdat_symbol_id, comdat_symbol_number++);
6695 die->die_id.die_symbol = xstrdup (p);
6696 }
6697 else
6698 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6699 }
6700
6701 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6702 }
6703
6704 struct cu_hash_table_entry
6705 {
6706 dw_die_ref cu;
6707 unsigned min_comdat_num, max_comdat_num;
6708 struct cu_hash_table_entry *next;
6709 };
6710
6711 /* Helpers to manipulate hash table of CUs. */
6712
6713 struct cu_hash_table_entry_hasher
6714 {
6715 typedef cu_hash_table_entry value_type;
6716 typedef die_struct compare_type;
6717 static inline hashval_t hash (const value_type *);
6718 static inline bool equal (const value_type *, const compare_type *);
6719 static inline void remove (value_type *);
6720 };
6721
6722 inline hashval_t
6723 cu_hash_table_entry_hasher::hash (const value_type *entry)
6724 {
6725 return htab_hash_string (entry->cu->die_id.die_symbol);
6726 }
6727
6728 inline bool
6729 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6730 const compare_type *entry2)
6731 {
6732 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6733 }
6734
6735 inline void
6736 cu_hash_table_entry_hasher::remove (value_type *entry)
6737 {
6738 struct cu_hash_table_entry *next;
6739
6740 while (entry)
6741 {
6742 next = entry->next;
6743 free (entry);
6744 entry = next;
6745 }
6746 }
6747
6748 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6749
6750 /* Check whether we have already seen this CU and set up SYM_NUM
6751 accordingly. */
6752 static int
6753 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6754 {
6755 struct cu_hash_table_entry dummy;
6756 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6757
6758 dummy.max_comdat_num = 0;
6759
6760 slot = htable->find_slot_with_hash (cu,
6761 htab_hash_string (cu->die_id.die_symbol),
6762 INSERT);
6763 entry = *slot;
6764
6765 for (; entry; last = entry, entry = entry->next)
6766 {
6767 if (same_die_p_wrap (cu, entry->cu))
6768 break;
6769 }
6770
6771 if (entry)
6772 {
6773 *sym_num = entry->min_comdat_num;
6774 return 1;
6775 }
6776
6777 entry = XCNEW (struct cu_hash_table_entry);
6778 entry->cu = cu;
6779 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6780 entry->next = *slot;
6781 *slot = entry;
6782
6783 return 0;
6784 }
6785
6786 /* Record SYM_NUM to record of CU in HTABLE. */
6787 static void
6788 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6789 unsigned int sym_num)
6790 {
6791 struct cu_hash_table_entry **slot, *entry;
6792
6793 slot = htable->find_slot_with_hash (cu,
6794 htab_hash_string (cu->die_id.die_symbol),
6795 NO_INSERT);
6796 entry = *slot;
6797
6798 entry->max_comdat_num = sym_num;
6799 }
6800
6801 /* Traverse the DIE (which is always comp_unit_die), and set up
6802 additional compilation units for each of the include files we see
6803 bracketed by BINCL/EINCL. */
6804
6805 static void
6806 break_out_includes (dw_die_ref die)
6807 {
6808 dw_die_ref c;
6809 dw_die_ref unit = NULL;
6810 limbo_die_node *node, **pnode;
6811
6812 c = die->die_child;
6813 if (c) do {
6814 dw_die_ref prev = c;
6815 c = c->die_sib;
6816 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6817 || (unit && is_comdat_die (c)))
6818 {
6819 dw_die_ref next = c->die_sib;
6820
6821 /* This DIE is for a secondary CU; remove it from the main one. */
6822 remove_child_with_prev (c, prev);
6823
6824 if (c->die_tag == DW_TAG_GNU_BINCL)
6825 unit = push_new_compile_unit (unit, c);
6826 else if (c->die_tag == DW_TAG_GNU_EINCL)
6827 unit = pop_compile_unit (unit);
6828 else
6829 add_child_die (unit, c);
6830 c = next;
6831 if (c == die->die_child)
6832 break;
6833 }
6834 } while (c != die->die_child);
6835
6836 #if 0
6837 /* We can only use this in debugging, since the frontend doesn't check
6838 to make sure that we leave every include file we enter. */
6839 gcc_assert (!unit);
6840 #endif
6841
6842 assign_symbol_names (die);
6843 cu_hash_type cu_hash_table (10);
6844 for (node = limbo_die_list, pnode = &limbo_die_list;
6845 node;
6846 node = node->next)
6847 {
6848 int is_dupl;
6849
6850 compute_section_prefix (node->die);
6851 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6852 &comdat_symbol_number);
6853 assign_symbol_names (node->die);
6854 if (is_dupl)
6855 *pnode = node->next;
6856 else
6857 {
6858 pnode = &node->next;
6859 record_comdat_symbol_number (node->die, &cu_hash_table,
6860 comdat_symbol_number);
6861 }
6862 }
6863 }
6864
6865 /* Return non-zero if this DIE is a declaration. */
6866
6867 static int
6868 is_declaration_die (dw_die_ref die)
6869 {
6870 dw_attr_ref a;
6871 unsigned ix;
6872
6873 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6874 if (a->dw_attr == DW_AT_declaration)
6875 return 1;
6876
6877 return 0;
6878 }
6879
6880 /* Return non-zero if this DIE is nested inside a subprogram. */
6881
6882 static int
6883 is_nested_in_subprogram (dw_die_ref die)
6884 {
6885 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6886
6887 if (decl == NULL)
6888 decl = die;
6889 return local_scope_p (decl);
6890 }
6891
6892 /* Return non-zero if this DIE contains a defining declaration of a
6893 subprogram. */
6894
6895 static int
6896 contains_subprogram_definition (dw_die_ref die)
6897 {
6898 dw_die_ref c;
6899
6900 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6901 return 1;
6902 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6903 return 0;
6904 }
6905
6906 /* Return non-zero if this is a type DIE that should be moved to a
6907 COMDAT .debug_types section. */
6908
6909 static int
6910 should_move_die_to_comdat (dw_die_ref die)
6911 {
6912 switch (die->die_tag)
6913 {
6914 case DW_TAG_class_type:
6915 case DW_TAG_structure_type:
6916 case DW_TAG_enumeration_type:
6917 case DW_TAG_union_type:
6918 /* Don't move declarations, inlined instances, types nested in a
6919 subprogram, or types that contain subprogram definitions. */
6920 if (is_declaration_die (die)
6921 || get_AT (die, DW_AT_abstract_origin)
6922 || is_nested_in_subprogram (die)
6923 || contains_subprogram_definition (die))
6924 return 0;
6925 return 1;
6926 case DW_TAG_array_type:
6927 case DW_TAG_interface_type:
6928 case DW_TAG_pointer_type:
6929 case DW_TAG_reference_type:
6930 case DW_TAG_rvalue_reference_type:
6931 case DW_TAG_string_type:
6932 case DW_TAG_subroutine_type:
6933 case DW_TAG_ptr_to_member_type:
6934 case DW_TAG_set_type:
6935 case DW_TAG_subrange_type:
6936 case DW_TAG_base_type:
6937 case DW_TAG_const_type:
6938 case DW_TAG_file_type:
6939 case DW_TAG_packed_type:
6940 case DW_TAG_volatile_type:
6941 case DW_TAG_typedef:
6942 default:
6943 return 0;
6944 }
6945 }
6946
6947 /* Make a clone of DIE. */
6948
6949 static dw_die_ref
6950 clone_die (dw_die_ref die)
6951 {
6952 dw_die_ref clone;
6953 dw_attr_ref a;
6954 unsigned ix;
6955
6956 clone = ggc_cleared_alloc<die_node> ();
6957 clone->die_tag = die->die_tag;
6958
6959 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6960 add_dwarf_attr (clone, a);
6961
6962 return clone;
6963 }
6964
6965 /* Make a clone of the tree rooted at DIE. */
6966
6967 static dw_die_ref
6968 clone_tree (dw_die_ref die)
6969 {
6970 dw_die_ref c;
6971 dw_die_ref clone = clone_die (die);
6972
6973 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6974
6975 return clone;
6976 }
6977
6978 /* Make a clone of DIE as a declaration. */
6979
6980 static dw_die_ref
6981 clone_as_declaration (dw_die_ref die)
6982 {
6983 dw_die_ref clone;
6984 dw_die_ref decl;
6985 dw_attr_ref a;
6986 unsigned ix;
6987
6988 /* If the DIE is already a declaration, just clone it. */
6989 if (is_declaration_die (die))
6990 return clone_die (die);
6991
6992 /* If the DIE is a specification, just clone its declaration DIE. */
6993 decl = get_AT_ref (die, DW_AT_specification);
6994 if (decl != NULL)
6995 {
6996 clone = clone_die (decl);
6997 if (die->comdat_type_p)
6998 add_AT_die_ref (clone, DW_AT_signature, die);
6999 return clone;
7000 }
7001
7002 clone = ggc_cleared_alloc<die_node> ();
7003 clone->die_tag = die->die_tag;
7004
7005 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7006 {
7007 /* We don't want to copy over all attributes.
7008 For example we don't want DW_AT_byte_size because otherwise we will no
7009 longer have a declaration and GDB will treat it as a definition. */
7010
7011 switch (a->dw_attr)
7012 {
7013 case DW_AT_abstract_origin:
7014 case DW_AT_artificial:
7015 case DW_AT_containing_type:
7016 case DW_AT_external:
7017 case DW_AT_name:
7018 case DW_AT_type:
7019 case DW_AT_virtuality:
7020 case DW_AT_linkage_name:
7021 case DW_AT_MIPS_linkage_name:
7022 add_dwarf_attr (clone, a);
7023 break;
7024 case DW_AT_byte_size:
7025 default:
7026 break;
7027 }
7028 }
7029
7030 if (die->comdat_type_p)
7031 add_AT_die_ref (clone, DW_AT_signature, die);
7032
7033 add_AT_flag (clone, DW_AT_declaration, 1);
7034 return clone;
7035 }
7036
7037
7038 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7039
7040 struct decl_table_entry
7041 {
7042 dw_die_ref orig;
7043 dw_die_ref copy;
7044 };
7045
7046 /* Helpers to manipulate hash table of copied declarations. */
7047
7048 /* Hashtable helpers. */
7049
7050 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7051 {
7052 typedef decl_table_entry value_type;
7053 typedef die_struct compare_type;
7054 static inline hashval_t hash (const value_type *);
7055 static inline bool equal (const value_type *, const compare_type *);
7056 };
7057
7058 inline hashval_t
7059 decl_table_entry_hasher::hash (const value_type *entry)
7060 {
7061 return htab_hash_pointer (entry->orig);
7062 }
7063
7064 inline bool
7065 decl_table_entry_hasher::equal (const value_type *entry1,
7066 const compare_type *entry2)
7067 {
7068 return entry1->orig == entry2;
7069 }
7070
7071 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7072
7073 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7074 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7075 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7076 to check if the ancestor has already been copied into UNIT. */
7077
7078 static dw_die_ref
7079 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7080 decl_hash_type *decl_table)
7081 {
7082 dw_die_ref parent = die->die_parent;
7083 dw_die_ref new_parent = unit;
7084 dw_die_ref copy;
7085 decl_table_entry **slot = NULL;
7086 struct decl_table_entry *entry = NULL;
7087
7088 if (decl_table)
7089 {
7090 /* Check if the entry has already been copied to UNIT. */
7091 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7092 INSERT);
7093 if (*slot != HTAB_EMPTY_ENTRY)
7094 {
7095 entry = *slot;
7096 return entry->copy;
7097 }
7098
7099 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7100 entry = XCNEW (struct decl_table_entry);
7101 entry->orig = die;
7102 entry->copy = NULL;
7103 *slot = entry;
7104 }
7105
7106 if (parent != NULL)
7107 {
7108 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7109 if (spec != NULL)
7110 parent = spec;
7111 if (!is_unit_die (parent))
7112 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7113 }
7114
7115 copy = clone_as_declaration (die);
7116 add_child_die (new_parent, copy);
7117
7118 if (decl_table)
7119 {
7120 /* Record the pointer to the copy. */
7121 entry->copy = copy;
7122 }
7123
7124 return copy;
7125 }
7126 /* Copy the declaration context to the new type unit DIE. This includes
7127 any surrounding namespace or type declarations. If the DIE has an
7128 AT_specification attribute, it also includes attributes and children
7129 attached to the specification, and returns a pointer to the original
7130 parent of the declaration DIE. Returns NULL otherwise. */
7131
7132 static dw_die_ref
7133 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7134 {
7135 dw_die_ref decl;
7136 dw_die_ref new_decl;
7137 dw_die_ref orig_parent = NULL;
7138
7139 decl = get_AT_ref (die, DW_AT_specification);
7140 if (decl == NULL)
7141 decl = die;
7142 else
7143 {
7144 unsigned ix;
7145 dw_die_ref c;
7146 dw_attr_ref a;
7147
7148 /* The original DIE will be changed to a declaration, and must
7149 be moved to be a child of the original declaration DIE. */
7150 orig_parent = decl->die_parent;
7151
7152 /* Copy the type node pointer from the new DIE to the original
7153 declaration DIE so we can forward references later. */
7154 decl->comdat_type_p = true;
7155 decl->die_id.die_type_node = die->die_id.die_type_node;
7156
7157 remove_AT (die, DW_AT_specification);
7158
7159 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7160 {
7161 if (a->dw_attr != DW_AT_name
7162 && a->dw_attr != DW_AT_declaration
7163 && a->dw_attr != DW_AT_external)
7164 add_dwarf_attr (die, a);
7165 }
7166
7167 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7168 }
7169
7170 if (decl->die_parent != NULL
7171 && !is_unit_die (decl->die_parent))
7172 {
7173 new_decl = copy_ancestor_tree (unit, decl, NULL);
7174 if (new_decl != NULL)
7175 {
7176 remove_AT (new_decl, DW_AT_signature);
7177 add_AT_specification (die, new_decl);
7178 }
7179 }
7180
7181 return orig_parent;
7182 }
7183
7184 /* Generate the skeleton ancestor tree for the given NODE, then clone
7185 the DIE and add the clone into the tree. */
7186
7187 static void
7188 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7189 {
7190 if (node->new_die != NULL)
7191 return;
7192
7193 node->new_die = clone_as_declaration (node->old_die);
7194
7195 if (node->parent != NULL)
7196 {
7197 generate_skeleton_ancestor_tree (node->parent);
7198 add_child_die (node->parent->new_die, node->new_die);
7199 }
7200 }
7201
7202 /* Generate a skeleton tree of DIEs containing any declarations that are
7203 found in the original tree. We traverse the tree looking for declaration
7204 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7205
7206 static void
7207 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7208 {
7209 skeleton_chain_node node;
7210 dw_die_ref c;
7211 dw_die_ref first;
7212 dw_die_ref prev = NULL;
7213 dw_die_ref next = NULL;
7214
7215 node.parent = parent;
7216
7217 first = c = parent->old_die->die_child;
7218 if (c)
7219 next = c->die_sib;
7220 if (c) do {
7221 if (prev == NULL || prev->die_sib == c)
7222 prev = c;
7223 c = next;
7224 next = (c == first ? NULL : c->die_sib);
7225 node.old_die = c;
7226 node.new_die = NULL;
7227 if (is_declaration_die (c))
7228 {
7229 if (is_template_instantiation (c))
7230 {
7231 /* Instantiated templates do not need to be cloned into the
7232 type unit. Just move the DIE and its children back to
7233 the skeleton tree (in the main CU). */
7234 remove_child_with_prev (c, prev);
7235 add_child_die (parent->new_die, c);
7236 c = prev;
7237 }
7238 else
7239 {
7240 /* Clone the existing DIE, move the original to the skeleton
7241 tree (which is in the main CU), and put the clone, with
7242 all the original's children, where the original came from
7243 (which is about to be moved to the type unit). */
7244 dw_die_ref clone = clone_die (c);
7245 move_all_children (c, clone);
7246
7247 /* If the original has a DW_AT_object_pointer attribute,
7248 it would now point to a child DIE just moved to the
7249 cloned tree, so we need to remove that attribute from
7250 the original. */
7251 remove_AT (c, DW_AT_object_pointer);
7252
7253 replace_child (c, clone, prev);
7254 generate_skeleton_ancestor_tree (parent);
7255 add_child_die (parent->new_die, c);
7256 node.new_die = c;
7257 c = clone;
7258 }
7259 }
7260 generate_skeleton_bottom_up (&node);
7261 } while (next != NULL);
7262 }
7263
7264 /* Wrapper function for generate_skeleton_bottom_up. */
7265
7266 static dw_die_ref
7267 generate_skeleton (dw_die_ref die)
7268 {
7269 skeleton_chain_node node;
7270
7271 node.old_die = die;
7272 node.new_die = NULL;
7273 node.parent = NULL;
7274
7275 /* If this type definition is nested inside another type,
7276 and is not an instantiation of a template, always leave
7277 at least a declaration in its place. */
7278 if (die->die_parent != NULL
7279 && is_type_die (die->die_parent)
7280 && !is_template_instantiation (die))
7281 node.new_die = clone_as_declaration (die);
7282
7283 generate_skeleton_bottom_up (&node);
7284 return node.new_die;
7285 }
7286
7287 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7288 declaration. The original DIE is moved to a new compile unit so that
7289 existing references to it follow it to the new location. If any of the
7290 original DIE's descendants is a declaration, we need to replace the
7291 original DIE with a skeleton tree and move the declarations back into the
7292 skeleton tree. */
7293
7294 static dw_die_ref
7295 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7296 dw_die_ref prev)
7297 {
7298 dw_die_ref skeleton, orig_parent;
7299
7300 /* Copy the declaration context to the type unit DIE. If the returned
7301 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7302 that DIE. */
7303 orig_parent = copy_declaration_context (unit, child);
7304
7305 skeleton = generate_skeleton (child);
7306 if (skeleton == NULL)
7307 remove_child_with_prev (child, prev);
7308 else
7309 {
7310 skeleton->comdat_type_p = true;
7311 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7312
7313 /* If the original DIE was a specification, we need to put
7314 the skeleton under the parent DIE of the declaration.
7315 This leaves the original declaration in the tree, but
7316 it will be pruned later since there are no longer any
7317 references to it. */
7318 if (orig_parent != NULL)
7319 {
7320 remove_child_with_prev (child, prev);
7321 add_child_die (orig_parent, skeleton);
7322 }
7323 else
7324 replace_child (child, skeleton, prev);
7325 }
7326
7327 return skeleton;
7328 }
7329
7330 /* Traverse the DIE and set up additional .debug_types sections for each
7331 type worthy of being placed in a COMDAT section. */
7332
7333 static void
7334 break_out_comdat_types (dw_die_ref die)
7335 {
7336 dw_die_ref c;
7337 dw_die_ref first;
7338 dw_die_ref prev = NULL;
7339 dw_die_ref next = NULL;
7340 dw_die_ref unit = NULL;
7341
7342 first = c = die->die_child;
7343 if (c)
7344 next = c->die_sib;
7345 if (c) do {
7346 if (prev == NULL || prev->die_sib == c)
7347 prev = c;
7348 c = next;
7349 next = (c == first ? NULL : c->die_sib);
7350 if (should_move_die_to_comdat (c))
7351 {
7352 dw_die_ref replacement;
7353 comdat_type_node_ref type_node;
7354
7355 /* Break out nested types into their own type units. */
7356 break_out_comdat_types (c);
7357
7358 /* Create a new type unit DIE as the root for the new tree, and
7359 add it to the list of comdat types. */
7360 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7361 add_AT_unsigned (unit, DW_AT_language,
7362 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7363 type_node = ggc_cleared_alloc<comdat_type_node> ();
7364 type_node->root_die = unit;
7365 type_node->next = comdat_type_list;
7366 comdat_type_list = type_node;
7367
7368 /* Generate the type signature. */
7369 generate_type_signature (c, type_node);
7370
7371 /* Copy the declaration context, attributes, and children of the
7372 declaration into the new type unit DIE, then remove this DIE
7373 from the main CU (or replace it with a skeleton if necessary). */
7374 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7375 type_node->skeleton_die = replacement;
7376
7377 /* Add the DIE to the new compunit. */
7378 add_child_die (unit, c);
7379
7380 if (replacement != NULL)
7381 c = replacement;
7382 }
7383 else if (c->die_tag == DW_TAG_namespace
7384 || c->die_tag == DW_TAG_class_type
7385 || c->die_tag == DW_TAG_structure_type
7386 || c->die_tag == DW_TAG_union_type)
7387 {
7388 /* Look for nested types that can be broken out. */
7389 break_out_comdat_types (c);
7390 }
7391 } while (next != NULL);
7392 }
7393
7394 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7395 Enter all the cloned children into the hash table decl_table. */
7396
7397 static dw_die_ref
7398 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7399 {
7400 dw_die_ref c;
7401 dw_die_ref clone;
7402 struct decl_table_entry *entry;
7403 decl_table_entry **slot;
7404
7405 if (die->die_tag == DW_TAG_subprogram)
7406 clone = clone_as_declaration (die);
7407 else
7408 clone = clone_die (die);
7409
7410 slot = decl_table->find_slot_with_hash (die,
7411 htab_hash_pointer (die), INSERT);
7412
7413 /* Assert that DIE isn't in the hash table yet. If it would be there
7414 before, the ancestors would be necessarily there as well, therefore
7415 clone_tree_partial wouldn't be called. */
7416 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7417
7418 entry = XCNEW (struct decl_table_entry);
7419 entry->orig = die;
7420 entry->copy = clone;
7421 *slot = entry;
7422
7423 if (die->die_tag != DW_TAG_subprogram)
7424 FOR_EACH_CHILD (die, c,
7425 add_child_die (clone, clone_tree_partial (c, decl_table)));
7426
7427 return clone;
7428 }
7429
7430 /* Walk the DIE and its children, looking for references to incomplete
7431 or trivial types that are unmarked (i.e., that are not in the current
7432 type_unit). */
7433
7434 static void
7435 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7436 {
7437 dw_die_ref c;
7438 dw_attr_ref a;
7439 unsigned ix;
7440
7441 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7442 {
7443 if (AT_class (a) == dw_val_class_die_ref)
7444 {
7445 dw_die_ref targ = AT_ref (a);
7446 decl_table_entry **slot;
7447 struct decl_table_entry *entry;
7448
7449 if (targ->die_mark != 0 || targ->comdat_type_p)
7450 continue;
7451
7452 slot = decl_table->find_slot_with_hash (targ,
7453 htab_hash_pointer (targ),
7454 INSERT);
7455
7456 if (*slot != HTAB_EMPTY_ENTRY)
7457 {
7458 /* TARG has already been copied, so we just need to
7459 modify the reference to point to the copy. */
7460 entry = *slot;
7461 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7462 }
7463 else
7464 {
7465 dw_die_ref parent = unit;
7466 dw_die_ref copy = clone_die (targ);
7467
7468 /* Record in DECL_TABLE that TARG has been copied.
7469 Need to do this now, before the recursive call,
7470 because DECL_TABLE may be expanded and SLOT
7471 would no longer be a valid pointer. */
7472 entry = XCNEW (struct decl_table_entry);
7473 entry->orig = targ;
7474 entry->copy = copy;
7475 *slot = entry;
7476
7477 /* If TARG is not a declaration DIE, we need to copy its
7478 children. */
7479 if (!is_declaration_die (targ))
7480 {
7481 FOR_EACH_CHILD (
7482 targ, c,
7483 add_child_die (copy,
7484 clone_tree_partial (c, decl_table)));
7485 }
7486
7487 /* Make sure the cloned tree is marked as part of the
7488 type unit. */
7489 mark_dies (copy);
7490
7491 /* If TARG has surrounding context, copy its ancestor tree
7492 into the new type unit. */
7493 if (targ->die_parent != NULL
7494 && !is_unit_die (targ->die_parent))
7495 parent = copy_ancestor_tree (unit, targ->die_parent,
7496 decl_table);
7497
7498 add_child_die (parent, copy);
7499 a->dw_attr_val.v.val_die_ref.die = copy;
7500
7501 /* Make sure the newly-copied DIE is walked. If it was
7502 installed in a previously-added context, it won't
7503 get visited otherwise. */
7504 if (parent != unit)
7505 {
7506 /* Find the highest point of the newly-added tree,
7507 mark each node along the way, and walk from there. */
7508 parent->die_mark = 1;
7509 while (parent->die_parent
7510 && parent->die_parent->die_mark == 0)
7511 {
7512 parent = parent->die_parent;
7513 parent->die_mark = 1;
7514 }
7515 copy_decls_walk (unit, parent, decl_table);
7516 }
7517 }
7518 }
7519 }
7520
7521 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7522 }
7523
7524 /* Copy declarations for "unworthy" types into the new comdat section.
7525 Incomplete types, modified types, and certain other types aren't broken
7526 out into comdat sections of their own, so they don't have a signature,
7527 and we need to copy the declaration into the same section so that we
7528 don't have an external reference. */
7529
7530 static void
7531 copy_decls_for_unworthy_types (dw_die_ref unit)
7532 {
7533 mark_dies (unit);
7534 decl_hash_type decl_table (10);
7535 copy_decls_walk (unit, unit, &decl_table);
7536 unmark_dies (unit);
7537 }
7538
7539 /* Traverse the DIE and add a sibling attribute if it may have the
7540 effect of speeding up access to siblings. To save some space,
7541 avoid generating sibling attributes for DIE's without children. */
7542
7543 static void
7544 add_sibling_attributes (dw_die_ref die)
7545 {
7546 dw_die_ref c;
7547
7548 if (! die->die_child)
7549 return;
7550
7551 if (die->die_parent && die != die->die_parent->die_child)
7552 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7553
7554 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7555 }
7556
7557 /* Output all location lists for the DIE and its children. */
7558
7559 static void
7560 output_location_lists (dw_die_ref die)
7561 {
7562 dw_die_ref c;
7563 dw_attr_ref a;
7564 unsigned ix;
7565
7566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7567 if (AT_class (a) == dw_val_class_loc_list)
7568 output_loc_list (AT_loc_list (a));
7569
7570 FOR_EACH_CHILD (die, c, output_location_lists (c));
7571 }
7572
7573 /* We want to limit the number of external references, because they are
7574 larger than local references: a relocation takes multiple words, and
7575 even a sig8 reference is always eight bytes, whereas a local reference
7576 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7577 So if we encounter multiple external references to the same type DIE, we
7578 make a local typedef stub for it and redirect all references there.
7579
7580 This is the element of the hash table for keeping track of these
7581 references. */
7582
7583 struct external_ref
7584 {
7585 dw_die_ref type;
7586 dw_die_ref stub;
7587 unsigned n_refs;
7588 };
7589
7590 /* Hashtable helpers. */
7591
7592 struct external_ref_hasher : typed_free_remove <external_ref>
7593 {
7594 typedef external_ref value_type;
7595 typedef external_ref compare_type;
7596 static inline hashval_t hash (const value_type *);
7597 static inline bool equal (const value_type *, const compare_type *);
7598 };
7599
7600 inline hashval_t
7601 external_ref_hasher::hash (const value_type *r)
7602 {
7603 dw_die_ref die = r->type;
7604 hashval_t h = 0;
7605
7606 /* We can't use the address of the DIE for hashing, because
7607 that will make the order of the stub DIEs non-deterministic. */
7608 if (! die->comdat_type_p)
7609 /* We have a symbol; use it to compute a hash. */
7610 h = htab_hash_string (die->die_id.die_symbol);
7611 else
7612 {
7613 /* We have a type signature; use a subset of the bits as the hash.
7614 The 8-byte signature is at least as large as hashval_t. */
7615 comdat_type_node_ref type_node = die->die_id.die_type_node;
7616 memcpy (&h, type_node->signature, sizeof (h));
7617 }
7618 return h;
7619 }
7620
7621 inline bool
7622 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7623 {
7624 return r1->type == r2->type;
7625 }
7626
7627 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7628
7629 /* Return a pointer to the external_ref for references to DIE. */
7630
7631 static struct external_ref *
7632 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7633 {
7634 struct external_ref ref, *ref_p;
7635 external_ref **slot;
7636
7637 ref.type = die;
7638 slot = map->find_slot (&ref, INSERT);
7639 if (*slot != HTAB_EMPTY_ENTRY)
7640 return *slot;
7641
7642 ref_p = XCNEW (struct external_ref);
7643 ref_p->type = die;
7644 *slot = ref_p;
7645 return ref_p;
7646 }
7647
7648 /* Subroutine of optimize_external_refs, below.
7649
7650 If we see a type skeleton, record it as our stub. If we see external
7651 references, remember how many we've seen. */
7652
7653 static void
7654 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7655 {
7656 dw_die_ref c;
7657 dw_attr_ref a;
7658 unsigned ix;
7659 struct external_ref *ref_p;
7660
7661 if (is_type_die (die)
7662 && (c = get_AT_ref (die, DW_AT_signature)))
7663 {
7664 /* This is a local skeleton; use it for local references. */
7665 ref_p = lookup_external_ref (map, c);
7666 ref_p->stub = die;
7667 }
7668
7669 /* Scan the DIE references, and remember any that refer to DIEs from
7670 other CUs (i.e. those which are not marked). */
7671 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7672 if (AT_class (a) == dw_val_class_die_ref
7673 && (c = AT_ref (a))->die_mark == 0
7674 && is_type_die (c))
7675 {
7676 ref_p = lookup_external_ref (map, c);
7677 ref_p->n_refs++;
7678 }
7679
7680 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7681 }
7682
7683 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7684 points to an external_ref, DATA is the CU we're processing. If we don't
7685 already have a local stub, and we have multiple refs, build a stub. */
7686
7687 int
7688 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7689 {
7690 struct external_ref *ref_p = *slot;
7691
7692 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7693 {
7694 /* We have multiple references to this type, so build a small stub.
7695 Both of these forms are a bit dodgy from the perspective of the
7696 DWARF standard, since technically they should have names. */
7697 dw_die_ref cu = data;
7698 dw_die_ref type = ref_p->type;
7699 dw_die_ref stub = NULL;
7700
7701 if (type->comdat_type_p)
7702 {
7703 /* If we refer to this type via sig8, use AT_signature. */
7704 stub = new_die (type->die_tag, cu, NULL_TREE);
7705 add_AT_die_ref (stub, DW_AT_signature, type);
7706 }
7707 else
7708 {
7709 /* Otherwise, use a typedef with no name. */
7710 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7711 add_AT_die_ref (stub, DW_AT_type, type);
7712 }
7713
7714 stub->die_mark++;
7715 ref_p->stub = stub;
7716 }
7717 return 1;
7718 }
7719
7720 /* DIE is a unit; look through all the DIE references to see if there are
7721 any external references to types, and if so, create local stubs for
7722 them which will be applied in build_abbrev_table. This is useful because
7723 references to local DIEs are smaller. */
7724
7725 static external_ref_hash_type *
7726 optimize_external_refs (dw_die_ref die)
7727 {
7728 external_ref_hash_type *map = new external_ref_hash_type (10);
7729 optimize_external_refs_1 (die, map);
7730 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7731 return map;
7732 }
7733
7734 /* The format of each DIE (and its attribute value pairs) is encoded in an
7735 abbreviation table. This routine builds the abbreviation table and assigns
7736 a unique abbreviation id for each abbreviation entry. The children of each
7737 die are visited recursively. */
7738
7739 static void
7740 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7741 {
7742 unsigned long abbrev_id;
7743 unsigned int n_alloc;
7744 dw_die_ref c;
7745 dw_attr_ref a;
7746 unsigned ix;
7747
7748 /* Scan the DIE references, and replace any that refer to
7749 DIEs from other CUs (i.e. those which are not marked) with
7750 the local stubs we built in optimize_external_refs. */
7751 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7752 if (AT_class (a) == dw_val_class_die_ref
7753 && (c = AT_ref (a))->die_mark == 0)
7754 {
7755 struct external_ref *ref_p;
7756 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7757
7758 ref_p = lookup_external_ref (extern_map, c);
7759 if (ref_p->stub && ref_p->stub != die)
7760 change_AT_die_ref (a, ref_p->stub);
7761 else
7762 /* We aren't changing this reference, so mark it external. */
7763 set_AT_ref_external (a, 1);
7764 }
7765
7766 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7767 {
7768 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7769 dw_attr_ref die_a, abbrev_a;
7770 unsigned ix;
7771 bool ok = true;
7772
7773 if (abbrev->die_tag != die->die_tag)
7774 continue;
7775 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7776 continue;
7777
7778 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7779 continue;
7780
7781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7782 {
7783 abbrev_a = &(*abbrev->die_attr)[ix];
7784 if ((abbrev_a->dw_attr != die_a->dw_attr)
7785 || (value_format (abbrev_a) != value_format (die_a)))
7786 {
7787 ok = false;
7788 break;
7789 }
7790 }
7791 if (ok)
7792 break;
7793 }
7794
7795 if (abbrev_id >= abbrev_die_table_in_use)
7796 {
7797 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7798 {
7799 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7800 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7801 n_alloc);
7802
7803 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7804 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7805 abbrev_die_table_allocated = n_alloc;
7806 }
7807
7808 ++abbrev_die_table_in_use;
7809 abbrev_die_table[abbrev_id] = die;
7810 }
7811
7812 die->die_abbrev = abbrev_id;
7813 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7814 }
7815 \f
7816 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7817
7818 static int
7819 constant_size (unsigned HOST_WIDE_INT value)
7820 {
7821 int log;
7822
7823 if (value == 0)
7824 log = 0;
7825 else
7826 log = floor_log2 (value);
7827
7828 log = log / 8;
7829 log = 1 << (floor_log2 (log) + 1);
7830
7831 return log;
7832 }
7833
7834 /* Return the size of a DIE as it is represented in the
7835 .debug_info section. */
7836
7837 static unsigned long
7838 size_of_die (dw_die_ref die)
7839 {
7840 unsigned long size = 0;
7841 dw_attr_ref a;
7842 unsigned ix;
7843 enum dwarf_form form;
7844
7845 size += size_of_uleb128 (die->die_abbrev);
7846 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7847 {
7848 switch (AT_class (a))
7849 {
7850 case dw_val_class_addr:
7851 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7852 {
7853 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7854 size += size_of_uleb128 (AT_index (a));
7855 }
7856 else
7857 size += DWARF2_ADDR_SIZE;
7858 break;
7859 case dw_val_class_offset:
7860 size += DWARF_OFFSET_SIZE;
7861 break;
7862 case dw_val_class_loc:
7863 {
7864 unsigned long lsize = size_of_locs (AT_loc (a));
7865
7866 /* Block length. */
7867 if (dwarf_version >= 4)
7868 size += size_of_uleb128 (lsize);
7869 else
7870 size += constant_size (lsize);
7871 size += lsize;
7872 }
7873 break;
7874 case dw_val_class_loc_list:
7875 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7876 {
7877 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7878 size += size_of_uleb128 (AT_index (a));
7879 }
7880 else
7881 size += DWARF_OFFSET_SIZE;
7882 break;
7883 case dw_val_class_range_list:
7884 size += DWARF_OFFSET_SIZE;
7885 break;
7886 case dw_val_class_const:
7887 size += size_of_sleb128 (AT_int (a));
7888 break;
7889 case dw_val_class_unsigned_const:
7890 {
7891 int csize = constant_size (AT_unsigned (a));
7892 if (dwarf_version == 3
7893 && a->dw_attr == DW_AT_data_member_location
7894 && csize >= 4)
7895 size += size_of_uleb128 (AT_unsigned (a));
7896 else
7897 size += csize;
7898 }
7899 break;
7900 case dw_val_class_const_double:
7901 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7902 if (HOST_BITS_PER_WIDE_INT >= 64)
7903 size++; /* block */
7904 break;
7905 case dw_val_class_wide_int:
7906 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7907 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7908 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7909 > 64)
7910 size++; /* block */
7911 break;
7912 case dw_val_class_vec:
7913 size += constant_size (a->dw_attr_val.v.val_vec.length
7914 * a->dw_attr_val.v.val_vec.elt_size)
7915 + a->dw_attr_val.v.val_vec.length
7916 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7917 break;
7918 case dw_val_class_flag:
7919 if (dwarf_version >= 4)
7920 /* Currently all add_AT_flag calls pass in 1 as last argument,
7921 so DW_FORM_flag_present can be used. If that ever changes,
7922 we'll need to use DW_FORM_flag and have some optimization
7923 in build_abbrev_table that will change those to
7924 DW_FORM_flag_present if it is set to 1 in all DIEs using
7925 the same abbrev entry. */
7926 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7927 else
7928 size += 1;
7929 break;
7930 case dw_val_class_die_ref:
7931 if (AT_ref_external (a))
7932 {
7933 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7934 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7935 is sized by target address length, whereas in DWARF3
7936 it's always sized as an offset. */
7937 if (use_debug_types)
7938 size += DWARF_TYPE_SIGNATURE_SIZE;
7939 else if (dwarf_version == 2)
7940 size += DWARF2_ADDR_SIZE;
7941 else
7942 size += DWARF_OFFSET_SIZE;
7943 }
7944 else
7945 size += DWARF_OFFSET_SIZE;
7946 break;
7947 case dw_val_class_fde_ref:
7948 size += DWARF_OFFSET_SIZE;
7949 break;
7950 case dw_val_class_lbl_id:
7951 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7952 {
7953 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7954 size += size_of_uleb128 (AT_index (a));
7955 }
7956 else
7957 size += DWARF2_ADDR_SIZE;
7958 break;
7959 case dw_val_class_lineptr:
7960 case dw_val_class_macptr:
7961 size += DWARF_OFFSET_SIZE;
7962 break;
7963 case dw_val_class_str:
7964 form = AT_string_form (a);
7965 if (form == DW_FORM_strp)
7966 size += DWARF_OFFSET_SIZE;
7967 else if (form == DW_FORM_GNU_str_index)
7968 size += size_of_uleb128 (AT_index (a));
7969 else
7970 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7971 break;
7972 case dw_val_class_file:
7973 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7974 break;
7975 case dw_val_class_data8:
7976 size += 8;
7977 break;
7978 case dw_val_class_vms_delta:
7979 size += DWARF_OFFSET_SIZE;
7980 break;
7981 case dw_val_class_high_pc:
7982 size += DWARF2_ADDR_SIZE;
7983 break;
7984 default:
7985 gcc_unreachable ();
7986 }
7987 }
7988
7989 return size;
7990 }
7991
7992 /* Size the debugging information associated with a given DIE. Visits the
7993 DIE's children recursively. Updates the global variable next_die_offset, on
7994 each time through. Uses the current value of next_die_offset to update the
7995 die_offset field in each DIE. */
7996
7997 static void
7998 calc_die_sizes (dw_die_ref die)
7999 {
8000 dw_die_ref c;
8001
8002 gcc_assert (die->die_offset == 0
8003 || (unsigned long int) die->die_offset == next_die_offset);
8004 die->die_offset = next_die_offset;
8005 next_die_offset += size_of_die (die);
8006
8007 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8008
8009 if (die->die_child != NULL)
8010 /* Count the null byte used to terminate sibling lists. */
8011 next_die_offset += 1;
8012 }
8013
8014 /* Size just the base type children at the start of the CU.
8015 This is needed because build_abbrev needs to size locs
8016 and sizing of type based stack ops needs to know die_offset
8017 values for the base types. */
8018
8019 static void
8020 calc_base_type_die_sizes (void)
8021 {
8022 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8023 unsigned int i;
8024 dw_die_ref base_type;
8025 #if ENABLE_ASSERT_CHECKING
8026 dw_die_ref prev = comp_unit_die ()->die_child;
8027 #endif
8028
8029 die_offset += size_of_die (comp_unit_die ());
8030 for (i = 0; base_types.iterate (i, &base_type); i++)
8031 {
8032 #if ENABLE_ASSERT_CHECKING
8033 gcc_assert (base_type->die_offset == 0
8034 && prev->die_sib == base_type
8035 && base_type->die_child == NULL
8036 && base_type->die_abbrev);
8037 prev = base_type;
8038 #endif
8039 base_type->die_offset = die_offset;
8040 die_offset += size_of_die (base_type);
8041 }
8042 }
8043
8044 /* Set the marks for a die and its children. We do this so
8045 that we know whether or not a reference needs to use FORM_ref_addr; only
8046 DIEs in the same CU will be marked. We used to clear out the offset
8047 and use that as the flag, but ran into ordering problems. */
8048
8049 static void
8050 mark_dies (dw_die_ref die)
8051 {
8052 dw_die_ref c;
8053
8054 gcc_assert (!die->die_mark);
8055
8056 die->die_mark = 1;
8057 FOR_EACH_CHILD (die, c, mark_dies (c));
8058 }
8059
8060 /* Clear the marks for a die and its children. */
8061
8062 static void
8063 unmark_dies (dw_die_ref die)
8064 {
8065 dw_die_ref c;
8066
8067 if (! use_debug_types)
8068 gcc_assert (die->die_mark);
8069
8070 die->die_mark = 0;
8071 FOR_EACH_CHILD (die, c, unmark_dies (c));
8072 }
8073
8074 /* Clear the marks for a die, its children and referred dies. */
8075
8076 static void
8077 unmark_all_dies (dw_die_ref die)
8078 {
8079 dw_die_ref c;
8080 dw_attr_ref a;
8081 unsigned ix;
8082
8083 if (!die->die_mark)
8084 return;
8085 die->die_mark = 0;
8086
8087 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8088
8089 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8090 if (AT_class (a) == dw_val_class_die_ref)
8091 unmark_all_dies (AT_ref (a));
8092 }
8093
8094 /* Calculate if the entry should appear in the final output file. It may be
8095 from a pruned a type. */
8096
8097 static bool
8098 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8099 {
8100 /* By limiting gnu pubnames to definitions only, gold can generate a
8101 gdb index without entries for declarations, which don't include
8102 enough information to be useful. */
8103 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8104 return false;
8105
8106 if (table == pubname_table)
8107 {
8108 /* Enumerator names are part of the pubname table, but the
8109 parent DW_TAG_enumeration_type die may have been pruned.
8110 Don't output them if that is the case. */
8111 if (p->die->die_tag == DW_TAG_enumerator &&
8112 (p->die->die_parent == NULL
8113 || !p->die->die_parent->die_perennial_p))
8114 return false;
8115
8116 /* Everything else in the pubname table is included. */
8117 return true;
8118 }
8119
8120 /* The pubtypes table shouldn't include types that have been
8121 pruned. */
8122 return (p->die->die_offset != 0
8123 || !flag_eliminate_unused_debug_types);
8124 }
8125
8126 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8127 generated for the compilation unit. */
8128
8129 static unsigned long
8130 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8131 {
8132 unsigned long size;
8133 unsigned i;
8134 pubname_ref p;
8135 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8136
8137 size = DWARF_PUBNAMES_HEADER_SIZE;
8138 FOR_EACH_VEC_ELT (*names, i, p)
8139 if (include_pubname_in_output (names, p))
8140 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8141
8142 size += DWARF_OFFSET_SIZE;
8143 return size;
8144 }
8145
8146 /* Return the size of the information in the .debug_aranges section. */
8147
8148 static unsigned long
8149 size_of_aranges (void)
8150 {
8151 unsigned long size;
8152
8153 size = DWARF_ARANGES_HEADER_SIZE;
8154
8155 /* Count the address/length pair for this compilation unit. */
8156 if (text_section_used)
8157 size += 2 * DWARF2_ADDR_SIZE;
8158 if (cold_text_section_used)
8159 size += 2 * DWARF2_ADDR_SIZE;
8160 if (have_multiple_function_sections)
8161 {
8162 unsigned fde_idx;
8163 dw_fde_ref fde;
8164
8165 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8166 {
8167 if (DECL_IGNORED_P (fde->decl))
8168 continue;
8169 if (!fde->in_std_section)
8170 size += 2 * DWARF2_ADDR_SIZE;
8171 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8172 size += 2 * DWARF2_ADDR_SIZE;
8173 }
8174 }
8175
8176 /* Count the two zero words used to terminated the address range table. */
8177 size += 2 * DWARF2_ADDR_SIZE;
8178 return size;
8179 }
8180 \f
8181 /* Select the encoding of an attribute value. */
8182
8183 static enum dwarf_form
8184 value_format (dw_attr_ref a)
8185 {
8186 switch (AT_class (a))
8187 {
8188 case dw_val_class_addr:
8189 /* Only very few attributes allow DW_FORM_addr. */
8190 switch (a->dw_attr)
8191 {
8192 case DW_AT_low_pc:
8193 case DW_AT_high_pc:
8194 case DW_AT_entry_pc:
8195 case DW_AT_trampoline:
8196 return (AT_index (a) == NOT_INDEXED
8197 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8198 default:
8199 break;
8200 }
8201 switch (DWARF2_ADDR_SIZE)
8202 {
8203 case 1:
8204 return DW_FORM_data1;
8205 case 2:
8206 return DW_FORM_data2;
8207 case 4:
8208 return DW_FORM_data4;
8209 case 8:
8210 return DW_FORM_data8;
8211 default:
8212 gcc_unreachable ();
8213 }
8214 case dw_val_class_range_list:
8215 case dw_val_class_loc_list:
8216 if (dwarf_version >= 4)
8217 return DW_FORM_sec_offset;
8218 /* FALLTHRU */
8219 case dw_val_class_vms_delta:
8220 case dw_val_class_offset:
8221 switch (DWARF_OFFSET_SIZE)
8222 {
8223 case 4:
8224 return DW_FORM_data4;
8225 case 8:
8226 return DW_FORM_data8;
8227 default:
8228 gcc_unreachable ();
8229 }
8230 case dw_val_class_loc:
8231 if (dwarf_version >= 4)
8232 return DW_FORM_exprloc;
8233 switch (constant_size (size_of_locs (AT_loc (a))))
8234 {
8235 case 1:
8236 return DW_FORM_block1;
8237 case 2:
8238 return DW_FORM_block2;
8239 case 4:
8240 return DW_FORM_block4;
8241 default:
8242 gcc_unreachable ();
8243 }
8244 case dw_val_class_const:
8245 return DW_FORM_sdata;
8246 case dw_val_class_unsigned_const:
8247 switch (constant_size (AT_unsigned (a)))
8248 {
8249 case 1:
8250 return DW_FORM_data1;
8251 case 2:
8252 return DW_FORM_data2;
8253 case 4:
8254 /* In DWARF3 DW_AT_data_member_location with
8255 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8256 constant, so we need to use DW_FORM_udata if we need
8257 a large constant. */
8258 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8259 return DW_FORM_udata;
8260 return DW_FORM_data4;
8261 case 8:
8262 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8263 return DW_FORM_udata;
8264 return DW_FORM_data8;
8265 default:
8266 gcc_unreachable ();
8267 }
8268 case dw_val_class_const_double:
8269 switch (HOST_BITS_PER_WIDE_INT)
8270 {
8271 case 8:
8272 return DW_FORM_data2;
8273 case 16:
8274 return DW_FORM_data4;
8275 case 32:
8276 return DW_FORM_data8;
8277 case 64:
8278 default:
8279 return DW_FORM_block1;
8280 }
8281 case dw_val_class_wide_int:
8282 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8283 {
8284 case 8:
8285 return DW_FORM_data1;
8286 case 16:
8287 return DW_FORM_data2;
8288 case 32:
8289 return DW_FORM_data4;
8290 case 64:
8291 return DW_FORM_data8;
8292 default:
8293 return DW_FORM_block1;
8294 }
8295 case dw_val_class_vec:
8296 switch (constant_size (a->dw_attr_val.v.val_vec.length
8297 * a->dw_attr_val.v.val_vec.elt_size))
8298 {
8299 case 1:
8300 return DW_FORM_block1;
8301 case 2:
8302 return DW_FORM_block2;
8303 case 4:
8304 return DW_FORM_block4;
8305 default:
8306 gcc_unreachable ();
8307 }
8308 case dw_val_class_flag:
8309 if (dwarf_version >= 4)
8310 {
8311 /* Currently all add_AT_flag calls pass in 1 as last argument,
8312 so DW_FORM_flag_present can be used. If that ever changes,
8313 we'll need to use DW_FORM_flag and have some optimization
8314 in build_abbrev_table that will change those to
8315 DW_FORM_flag_present if it is set to 1 in all DIEs using
8316 the same abbrev entry. */
8317 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8318 return DW_FORM_flag_present;
8319 }
8320 return DW_FORM_flag;
8321 case dw_val_class_die_ref:
8322 if (AT_ref_external (a))
8323 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8324 else
8325 return DW_FORM_ref;
8326 case dw_val_class_fde_ref:
8327 return DW_FORM_data;
8328 case dw_val_class_lbl_id:
8329 return (AT_index (a) == NOT_INDEXED
8330 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8331 case dw_val_class_lineptr:
8332 case dw_val_class_macptr:
8333 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8334 case dw_val_class_str:
8335 return AT_string_form (a);
8336 case dw_val_class_file:
8337 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8338 {
8339 case 1:
8340 return DW_FORM_data1;
8341 case 2:
8342 return DW_FORM_data2;
8343 case 4:
8344 return DW_FORM_data4;
8345 default:
8346 gcc_unreachable ();
8347 }
8348
8349 case dw_val_class_data8:
8350 return DW_FORM_data8;
8351
8352 case dw_val_class_high_pc:
8353 switch (DWARF2_ADDR_SIZE)
8354 {
8355 case 1:
8356 return DW_FORM_data1;
8357 case 2:
8358 return DW_FORM_data2;
8359 case 4:
8360 return DW_FORM_data4;
8361 case 8:
8362 return DW_FORM_data8;
8363 default:
8364 gcc_unreachable ();
8365 }
8366
8367 default:
8368 gcc_unreachable ();
8369 }
8370 }
8371
8372 /* Output the encoding of an attribute value. */
8373
8374 static void
8375 output_value_format (dw_attr_ref a)
8376 {
8377 enum dwarf_form form = value_format (a);
8378
8379 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8380 }
8381
8382 /* Given a die and id, produce the appropriate abbreviations. */
8383
8384 static void
8385 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8386 {
8387 unsigned ix;
8388 dw_attr_ref a_attr;
8389
8390 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8391 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8392 dwarf_tag_name (abbrev->die_tag));
8393
8394 if (abbrev->die_child != NULL)
8395 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8396 else
8397 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8398
8399 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8400 {
8401 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8402 dwarf_attr_name (a_attr->dw_attr));
8403 output_value_format (a_attr);
8404 }
8405
8406 dw2_asm_output_data (1, 0, NULL);
8407 dw2_asm_output_data (1, 0, NULL);
8408 }
8409
8410
8411 /* Output the .debug_abbrev section which defines the DIE abbreviation
8412 table. */
8413
8414 static void
8415 output_abbrev_section (void)
8416 {
8417 unsigned long abbrev_id;
8418
8419 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8420 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8421
8422 /* Terminate the table. */
8423 dw2_asm_output_data (1, 0, NULL);
8424 }
8425
8426 /* Output a symbol we can use to refer to this DIE from another CU. */
8427
8428 static inline void
8429 output_die_symbol (dw_die_ref die)
8430 {
8431 const char *sym = die->die_id.die_symbol;
8432
8433 gcc_assert (!die->comdat_type_p);
8434
8435 if (sym == 0)
8436 return;
8437
8438 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8439 /* We make these global, not weak; if the target doesn't support
8440 .linkonce, it doesn't support combining the sections, so debugging
8441 will break. */
8442 targetm.asm_out.globalize_label (asm_out_file, sym);
8443
8444 ASM_OUTPUT_LABEL (asm_out_file, sym);
8445 }
8446
8447 /* Return a new location list, given the begin and end range, and the
8448 expression. */
8449
8450 static inline dw_loc_list_ref
8451 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8452 const char *section)
8453 {
8454 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8455
8456 retlist->begin = begin;
8457 retlist->begin_entry = NULL;
8458 retlist->end = end;
8459 retlist->expr = expr;
8460 retlist->section = section;
8461
8462 return retlist;
8463 }
8464
8465 /* Generate a new internal symbol for this location list node, if it
8466 hasn't got one yet. */
8467
8468 static inline void
8469 gen_llsym (dw_loc_list_ref list)
8470 {
8471 gcc_assert (!list->ll_symbol);
8472 list->ll_symbol = gen_internal_sym ("LLST");
8473 }
8474
8475 /* Output the location list given to us. */
8476
8477 static void
8478 output_loc_list (dw_loc_list_ref list_head)
8479 {
8480 dw_loc_list_ref curr = list_head;
8481
8482 if (list_head->emitted)
8483 return;
8484 list_head->emitted = true;
8485
8486 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8487
8488 /* Walk the location list, and output each range + expression. */
8489 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8490 {
8491 unsigned long size;
8492 /* Don't output an entry that starts and ends at the same address. */
8493 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8494 continue;
8495 size = size_of_locs (curr->expr);
8496 /* If the expression is too large, drop it on the floor. We could
8497 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8498 in the expression, but >= 64KB expressions for a single value
8499 in a single range are unlikely very useful. */
8500 if (size > 0xffff)
8501 continue;
8502 if (dwarf_split_debug_info)
8503 {
8504 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8505 "Location list start/length entry (%s)",
8506 list_head->ll_symbol);
8507 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8508 "Location list range start index (%s)",
8509 curr->begin);
8510 /* The length field is 4 bytes. If we ever need to support
8511 an 8-byte length, we can add a new DW_LLE code or fall back
8512 to DW_LLE_GNU_start_end_entry. */
8513 dw2_asm_output_delta (4, curr->end, curr->begin,
8514 "Location list range length (%s)",
8515 list_head->ll_symbol);
8516 }
8517 else if (!have_multiple_function_sections)
8518 {
8519 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8520 "Location list begin address (%s)",
8521 list_head->ll_symbol);
8522 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8523 "Location list end address (%s)",
8524 list_head->ll_symbol);
8525 }
8526 else
8527 {
8528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8529 "Location list begin address (%s)",
8530 list_head->ll_symbol);
8531 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8532 "Location list end address (%s)",
8533 list_head->ll_symbol);
8534 }
8535
8536 /* Output the block length for this list of location operations. */
8537 gcc_assert (size <= 0xffff);
8538 dw2_asm_output_data (2, size, "%s", "Location expression size");
8539
8540 output_loc_sequence (curr->expr, -1);
8541 }
8542
8543 if (dwarf_split_debug_info)
8544 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8545 "Location list terminator (%s)",
8546 list_head->ll_symbol);
8547 else
8548 {
8549 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8550 "Location list terminator begin (%s)",
8551 list_head->ll_symbol);
8552 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8553 "Location list terminator end (%s)",
8554 list_head->ll_symbol);
8555 }
8556 }
8557
8558 /* Output a range_list offset into the debug_range section. Emit a
8559 relocated reference if val_entry is NULL, otherwise, emit an
8560 indirect reference. */
8561
8562 static void
8563 output_range_list_offset (dw_attr_ref a)
8564 {
8565 const char *name = dwarf_attr_name (a->dw_attr);
8566
8567 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8568 {
8569 char *p = strchr (ranges_section_label, '\0');
8570 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8571 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8572 debug_ranges_section, "%s", name);
8573 *p = '\0';
8574 }
8575 else
8576 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8577 "%s (offset from %s)", name, ranges_section_label);
8578 }
8579
8580 /* Output the offset into the debug_loc section. */
8581
8582 static void
8583 output_loc_list_offset (dw_attr_ref a)
8584 {
8585 char *sym = AT_loc_list (a)->ll_symbol;
8586
8587 gcc_assert (sym);
8588 if (dwarf_split_debug_info)
8589 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8590 "%s", dwarf_attr_name (a->dw_attr));
8591 else
8592 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8593 "%s", dwarf_attr_name (a->dw_attr));
8594 }
8595
8596 /* Output an attribute's index or value appropriately. */
8597
8598 static void
8599 output_attr_index_or_value (dw_attr_ref a)
8600 {
8601 const char *name = dwarf_attr_name (a->dw_attr);
8602
8603 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8604 {
8605 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8606 return;
8607 }
8608 switch (AT_class (a))
8609 {
8610 case dw_val_class_addr:
8611 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8612 break;
8613 case dw_val_class_high_pc:
8614 case dw_val_class_lbl_id:
8615 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8616 break;
8617 case dw_val_class_loc_list:
8618 output_loc_list_offset (a);
8619 break;
8620 default:
8621 gcc_unreachable ();
8622 }
8623 }
8624
8625 /* Output a type signature. */
8626
8627 static inline void
8628 output_signature (const char *sig, const char *name)
8629 {
8630 int i;
8631
8632 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8633 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8634 }
8635
8636 /* Output the DIE and its attributes. Called recursively to generate
8637 the definitions of each child DIE. */
8638
8639 static void
8640 output_die (dw_die_ref die)
8641 {
8642 dw_attr_ref a;
8643 dw_die_ref c;
8644 unsigned long size;
8645 unsigned ix;
8646
8647 /* If someone in another CU might refer to us, set up a symbol for
8648 them to point to. */
8649 if (! die->comdat_type_p && die->die_id.die_symbol)
8650 output_die_symbol (die);
8651
8652 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8653 (unsigned long)die->die_offset,
8654 dwarf_tag_name (die->die_tag));
8655
8656 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8657 {
8658 const char *name = dwarf_attr_name (a->dw_attr);
8659
8660 switch (AT_class (a))
8661 {
8662 case dw_val_class_addr:
8663 output_attr_index_or_value (a);
8664 break;
8665
8666 case dw_val_class_offset:
8667 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8668 "%s", name);
8669 break;
8670
8671 case dw_val_class_range_list:
8672 output_range_list_offset (a);
8673 break;
8674
8675 case dw_val_class_loc:
8676 size = size_of_locs (AT_loc (a));
8677
8678 /* Output the block length for this list of location operations. */
8679 if (dwarf_version >= 4)
8680 dw2_asm_output_data_uleb128 (size, "%s", name);
8681 else
8682 dw2_asm_output_data (constant_size (size), size, "%s", name);
8683
8684 output_loc_sequence (AT_loc (a), -1);
8685 break;
8686
8687 case dw_val_class_const:
8688 /* ??? It would be slightly more efficient to use a scheme like is
8689 used for unsigned constants below, but gdb 4.x does not sign
8690 extend. Gdb 5.x does sign extend. */
8691 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8692 break;
8693
8694 case dw_val_class_unsigned_const:
8695 {
8696 int csize = constant_size (AT_unsigned (a));
8697 if (dwarf_version == 3
8698 && a->dw_attr == DW_AT_data_member_location
8699 && csize >= 4)
8700 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8701 else
8702 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8703 }
8704 break;
8705
8706 case dw_val_class_const_double:
8707 {
8708 unsigned HOST_WIDE_INT first, second;
8709
8710 if (HOST_BITS_PER_WIDE_INT >= 64)
8711 dw2_asm_output_data (1,
8712 HOST_BITS_PER_DOUBLE_INT
8713 / HOST_BITS_PER_CHAR,
8714 NULL);
8715
8716 if (WORDS_BIG_ENDIAN)
8717 {
8718 first = a->dw_attr_val.v.val_double.high;
8719 second = a->dw_attr_val.v.val_double.low;
8720 }
8721 else
8722 {
8723 first = a->dw_attr_val.v.val_double.low;
8724 second = a->dw_attr_val.v.val_double.high;
8725 }
8726
8727 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8728 first, "%s", name);
8729 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8730 second, NULL);
8731 }
8732 break;
8733
8734 case dw_val_class_wide_int:
8735 {
8736 int i;
8737 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8738 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8739 if (len * HOST_BITS_PER_WIDE_INT > 64)
8740 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8741 NULL);
8742
8743 if (WORDS_BIG_ENDIAN)
8744 for (i = len - 1; i >= 0; --i)
8745 {
8746 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8747 name);
8748 name = NULL;
8749 }
8750 else
8751 for (i = 0; i < len; ++i)
8752 {
8753 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8754 name);
8755 name = NULL;
8756 }
8757 }
8758 break;
8759
8760 case dw_val_class_vec:
8761 {
8762 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8763 unsigned int len = a->dw_attr_val.v.val_vec.length;
8764 unsigned int i;
8765 unsigned char *p;
8766
8767 dw2_asm_output_data (constant_size (len * elt_size),
8768 len * elt_size, "%s", name);
8769 if (elt_size > sizeof (HOST_WIDE_INT))
8770 {
8771 elt_size /= 2;
8772 len *= 2;
8773 }
8774 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8775 i < len;
8776 i++, p += elt_size)
8777 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8778 "fp or vector constant word %u", i);
8779 break;
8780 }
8781
8782 case dw_val_class_flag:
8783 if (dwarf_version >= 4)
8784 {
8785 /* Currently all add_AT_flag calls pass in 1 as last argument,
8786 so DW_FORM_flag_present can be used. If that ever changes,
8787 we'll need to use DW_FORM_flag and have some optimization
8788 in build_abbrev_table that will change those to
8789 DW_FORM_flag_present if it is set to 1 in all DIEs using
8790 the same abbrev entry. */
8791 gcc_assert (AT_flag (a) == 1);
8792 if (flag_debug_asm)
8793 fprintf (asm_out_file, "\t\t\t%s %s\n",
8794 ASM_COMMENT_START, name);
8795 break;
8796 }
8797 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8798 break;
8799
8800 case dw_val_class_loc_list:
8801 output_attr_index_or_value (a);
8802 break;
8803
8804 case dw_val_class_die_ref:
8805 if (AT_ref_external (a))
8806 {
8807 if (AT_ref (a)->comdat_type_p)
8808 {
8809 comdat_type_node_ref type_node =
8810 AT_ref (a)->die_id.die_type_node;
8811
8812 gcc_assert (type_node);
8813 output_signature (type_node->signature, name);
8814 }
8815 else
8816 {
8817 const char *sym = AT_ref (a)->die_id.die_symbol;
8818 int size;
8819
8820 gcc_assert (sym);
8821 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8822 length, whereas in DWARF3 it's always sized as an
8823 offset. */
8824 if (dwarf_version == 2)
8825 size = DWARF2_ADDR_SIZE;
8826 else
8827 size = DWARF_OFFSET_SIZE;
8828 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8829 name);
8830 }
8831 }
8832 else
8833 {
8834 gcc_assert (AT_ref (a)->die_offset);
8835 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8836 "%s", name);
8837 }
8838 break;
8839
8840 case dw_val_class_fde_ref:
8841 {
8842 char l1[20];
8843
8844 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8845 a->dw_attr_val.v.val_fde_index * 2);
8846 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8847 "%s", name);
8848 }
8849 break;
8850
8851 case dw_val_class_vms_delta:
8852 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8853 AT_vms_delta2 (a), AT_vms_delta1 (a),
8854 "%s", name);
8855 break;
8856
8857 case dw_val_class_lbl_id:
8858 output_attr_index_or_value (a);
8859 break;
8860
8861 case dw_val_class_lineptr:
8862 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8863 debug_line_section, "%s", name);
8864 break;
8865
8866 case dw_val_class_macptr:
8867 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8868 debug_macinfo_section, "%s", name);
8869 break;
8870
8871 case dw_val_class_str:
8872 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8873 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8874 a->dw_attr_val.v.val_str->label,
8875 debug_str_section,
8876 "%s: \"%s\"", name, AT_string (a));
8877 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8878 dw2_asm_output_data_uleb128 (AT_index (a),
8879 "%s: \"%s\"", name, AT_string (a));
8880 else
8881 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8882 break;
8883
8884 case dw_val_class_file:
8885 {
8886 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8887
8888 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8889 a->dw_attr_val.v.val_file->filename);
8890 break;
8891 }
8892
8893 case dw_val_class_data8:
8894 {
8895 int i;
8896
8897 for (i = 0; i < 8; i++)
8898 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8899 i == 0 ? "%s" : NULL, name);
8900 break;
8901 }
8902
8903 case dw_val_class_high_pc:
8904 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8905 get_AT_low_pc (die), "DW_AT_high_pc");
8906 break;
8907
8908 default:
8909 gcc_unreachable ();
8910 }
8911 }
8912
8913 FOR_EACH_CHILD (die, c, output_die (c));
8914
8915 /* Add null byte to terminate sibling list. */
8916 if (die->die_child != NULL)
8917 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8918 (unsigned long) die->die_offset);
8919 }
8920
8921 /* Output the compilation unit that appears at the beginning of the
8922 .debug_info section, and precedes the DIE descriptions. */
8923
8924 static void
8925 output_compilation_unit_header (void)
8926 {
8927 int ver = dwarf_version;
8928
8929 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8930 dw2_asm_output_data (4, 0xffffffff,
8931 "Initial length escape value indicating 64-bit DWARF extension");
8932 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8933 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8934 "Length of Compilation Unit Info");
8935 dw2_asm_output_data (2, ver, "DWARF version number");
8936 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8937 debug_abbrev_section,
8938 "Offset Into Abbrev. Section");
8939 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8940 }
8941
8942 /* Output the compilation unit DIE and its children. */
8943
8944 static void
8945 output_comp_unit (dw_die_ref die, int output_if_empty)
8946 {
8947 const char *secname, *oldsym;
8948 char *tmp;
8949
8950 /* Unless we are outputting main CU, we may throw away empty ones. */
8951 if (!output_if_empty && die->die_child == NULL)
8952 return;
8953
8954 /* Even if there are no children of this DIE, we must output the information
8955 about the compilation unit. Otherwise, on an empty translation unit, we
8956 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8957 will then complain when examining the file. First mark all the DIEs in
8958 this CU so we know which get local refs. */
8959 mark_dies (die);
8960
8961 external_ref_hash_type *extern_map = optimize_external_refs (die);
8962
8963 build_abbrev_table (die, extern_map);
8964
8965 delete extern_map;
8966
8967 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8968 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8969 calc_die_sizes (die);
8970
8971 oldsym = die->die_id.die_symbol;
8972 if (oldsym)
8973 {
8974 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8975
8976 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8977 secname = tmp;
8978 die->die_id.die_symbol = NULL;
8979 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8980 }
8981 else
8982 {
8983 switch_to_section (debug_info_section);
8984 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8985 info_section_emitted = true;
8986 }
8987
8988 /* Output debugging information. */
8989 output_compilation_unit_header ();
8990 output_die (die);
8991
8992 /* Leave the marks on the main CU, so we can check them in
8993 output_pubnames. */
8994 if (oldsym)
8995 {
8996 unmark_dies (die);
8997 die->die_id.die_symbol = oldsym;
8998 }
8999 }
9000
9001 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9002 and .debug_pubtypes. This is configured per-target, but can be
9003 overridden by the -gpubnames or -gno-pubnames options. */
9004
9005 static inline bool
9006 want_pubnames (void)
9007 {
9008 if (debug_info_level <= DINFO_LEVEL_TERSE)
9009 return false;
9010 if (debug_generate_pub_sections != -1)
9011 return debug_generate_pub_sections;
9012 return targetm.want_debug_pub_sections;
9013 }
9014
9015 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9016
9017 static void
9018 add_AT_pubnames (dw_die_ref die)
9019 {
9020 if (want_pubnames ())
9021 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9022 }
9023
9024 /* Add a string attribute value to a skeleton DIE. */
9025
9026 static inline void
9027 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9028 const char *str)
9029 {
9030 dw_attr_node attr;
9031 struct indirect_string_node *node;
9032
9033 if (! skeleton_debug_str_hash)
9034 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9035 debug_str_eq, NULL);
9036
9037 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9038 find_string_form (node);
9039 if (node->form == DW_FORM_GNU_str_index)
9040 node->form = DW_FORM_strp;
9041
9042 attr.dw_attr = attr_kind;
9043 attr.dw_attr_val.val_class = dw_val_class_str;
9044 attr.dw_attr_val.val_entry = NULL;
9045 attr.dw_attr_val.v.val_str = node;
9046 add_dwarf_attr (die, &attr);
9047 }
9048
9049 /* Helper function to generate top-level dies for skeleton debug_info and
9050 debug_types. */
9051
9052 static void
9053 add_top_level_skeleton_die_attrs (dw_die_ref die)
9054 {
9055 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9056 const char *comp_dir = comp_dir_string ();
9057
9058 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9059 if (comp_dir != NULL)
9060 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9061 add_AT_pubnames (die);
9062 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9063 }
9064
9065 /* Return the single type-unit die for skeleton type units. */
9066
9067 static dw_die_ref
9068 get_skeleton_type_unit (void)
9069 {
9070 /* For dwarf_split_debug_sections with use_type info, all type units in the
9071 skeleton sections have identical dies (but different headers). This
9072 single die will be output many times. */
9073
9074 static dw_die_ref skeleton_type_unit = NULL;
9075
9076 if (skeleton_type_unit == NULL)
9077 {
9078 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
9079 add_top_level_skeleton_die_attrs (skeleton_type_unit);
9080 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
9081 }
9082 return skeleton_type_unit;
9083 }
9084
9085 /* Output skeleton debug sections that point to the dwo file. */
9086
9087 static void
9088 output_skeleton_debug_sections (dw_die_ref comp_unit)
9089 {
9090 /* These attributes will be found in the full debug_info section. */
9091 remove_AT (comp_unit, DW_AT_producer);
9092 remove_AT (comp_unit, DW_AT_language);
9093
9094 switch_to_section (debug_skeleton_info_section);
9095 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9096
9097 /* Produce the skeleton compilation-unit header. This one differs enough from
9098 a normal CU header that it's better not to call output_compilation_unit
9099 header. */
9100 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9101 dw2_asm_output_data (4, 0xffffffff,
9102 "Initial length escape value indicating 64-bit DWARF extension");
9103
9104 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9105 DWARF_COMPILE_UNIT_HEADER_SIZE
9106 - DWARF_INITIAL_LENGTH_SIZE
9107 + size_of_die (comp_unit),
9108 "Length of Compilation Unit Info");
9109 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9110 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9111 debug_abbrev_section,
9112 "Offset Into Abbrev. Section");
9113 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9114
9115 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9116 output_die (comp_unit);
9117
9118 /* Build the skeleton debug_abbrev section. */
9119 switch_to_section (debug_skeleton_abbrev_section);
9120 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9121
9122 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9123 if (use_debug_types)
9124 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
9125
9126 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9127 }
9128
9129 /* Output a comdat type unit DIE and its children. */
9130
9131 static void
9132 output_comdat_type_unit (comdat_type_node *node)
9133 {
9134 const char *secname;
9135 char *tmp;
9136 int i;
9137 #if defined (OBJECT_FORMAT_ELF)
9138 tree comdat_key;
9139 #endif
9140
9141 /* First mark all the DIEs in this CU so we know which get local refs. */
9142 mark_dies (node->root_die);
9143
9144 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9145
9146 build_abbrev_table (node->root_die, extern_map);
9147
9148 delete extern_map;
9149 extern_map = NULL;
9150
9151 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9152 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9153 calc_die_sizes (node->root_die);
9154
9155 #if defined (OBJECT_FORMAT_ELF)
9156 if (!dwarf_split_debug_info)
9157 secname = ".debug_types";
9158 else
9159 secname = ".debug_types.dwo";
9160
9161 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9162 sprintf (tmp, "wt.");
9163 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9164 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9165 comdat_key = get_identifier (tmp);
9166 targetm.asm_out.named_section (secname,
9167 SECTION_DEBUG | SECTION_LINKONCE,
9168 comdat_key);
9169 #else
9170 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9171 sprintf (tmp, ".gnu.linkonce.wt.");
9172 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9173 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9174 secname = tmp;
9175 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9176 #endif
9177
9178 /* Output debugging information. */
9179 output_compilation_unit_header ();
9180 output_signature (node->signature, "Type Signature");
9181 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9182 "Offset to Type DIE");
9183 output_die (node->root_die);
9184
9185 unmark_dies (node->root_die);
9186
9187 #if defined (OBJECT_FORMAT_ELF)
9188 if (dwarf_split_debug_info)
9189 {
9190 /* Produce the skeleton type-unit header. */
9191 const char *secname = ".debug_types";
9192
9193 targetm.asm_out.named_section (secname,
9194 SECTION_DEBUG | SECTION_LINKONCE,
9195 comdat_key);
9196 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9197 dw2_asm_output_data (4, 0xffffffff,
9198 "Initial length escape value indicating 64-bit DWARF extension");
9199
9200 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9201 DWARF_COMPILE_UNIT_HEADER_SIZE
9202 - DWARF_INITIAL_LENGTH_SIZE
9203 + size_of_die (get_skeleton_type_unit ())
9204 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9205 "Length of Type Unit Info");
9206 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9207 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9208 debug_skeleton_abbrev_section_label,
9209 debug_abbrev_section,
9210 "Offset Into Abbrev. Section");
9211 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9212 output_signature (node->signature, "Type Signature");
9213 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9214
9215 output_die (get_skeleton_type_unit ());
9216 }
9217 #endif
9218 }
9219
9220 /* Return the DWARF2/3 pubname associated with a decl. */
9221
9222 static const char *
9223 dwarf2_name (tree decl, int scope)
9224 {
9225 if (DECL_NAMELESS (decl))
9226 return NULL;
9227 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9228 }
9229
9230 /* Add a new entry to .debug_pubnames if appropriate. */
9231
9232 static void
9233 add_pubname_string (const char *str, dw_die_ref die)
9234 {
9235 pubname_entry e;
9236
9237 e.die = die;
9238 e.name = xstrdup (str);
9239 vec_safe_push (pubname_table, e);
9240 }
9241
9242 static void
9243 add_pubname (tree decl, dw_die_ref die)
9244 {
9245 if (!want_pubnames ())
9246 return;
9247
9248 /* Don't add items to the table when we expect that the consumer will have
9249 just read the enclosing die. For example, if the consumer is looking at a
9250 class_member, it will either be inside the class already, or will have just
9251 looked up the class to find the member. Either way, searching the class is
9252 faster than searching the index. */
9253 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9254 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9255 {
9256 const char *name = dwarf2_name (decl, 1);
9257
9258 if (name)
9259 add_pubname_string (name, die);
9260 }
9261 }
9262
9263 /* Add an enumerator to the pubnames section. */
9264
9265 static void
9266 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9267 {
9268 pubname_entry e;
9269
9270 gcc_assert (scope_name);
9271 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9272 e.die = die;
9273 vec_safe_push (pubname_table, e);
9274 }
9275
9276 /* Add a new entry to .debug_pubtypes if appropriate. */
9277
9278 static void
9279 add_pubtype (tree decl, dw_die_ref die)
9280 {
9281 pubname_entry e;
9282
9283 if (!want_pubnames ())
9284 return;
9285
9286 if ((TREE_PUBLIC (decl)
9287 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9288 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9289 {
9290 tree scope = NULL;
9291 const char *scope_name = "";
9292 const char *sep = is_cxx () ? "::" : ".";
9293 const char *name;
9294
9295 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9296 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9297 {
9298 scope_name = lang_hooks.dwarf_name (scope, 1);
9299 if (scope_name != NULL && scope_name[0] != '\0')
9300 scope_name = concat (scope_name, sep, NULL);
9301 else
9302 scope_name = "";
9303 }
9304
9305 if (TYPE_P (decl))
9306 name = type_tag (decl);
9307 else
9308 name = lang_hooks.dwarf_name (decl, 1);
9309
9310 /* If we don't have a name for the type, there's no point in adding
9311 it to the table. */
9312 if (name != NULL && name[0] != '\0')
9313 {
9314 e.die = die;
9315 e.name = concat (scope_name, name, NULL);
9316 vec_safe_push (pubtype_table, e);
9317 }
9318
9319 /* Although it might be more consistent to add the pubinfo for the
9320 enumerators as their dies are created, they should only be added if the
9321 enum type meets the criteria above. So rather than re-check the parent
9322 enum type whenever an enumerator die is created, just output them all
9323 here. This isn't protected by the name conditional because anonymous
9324 enums don't have names. */
9325 if (die->die_tag == DW_TAG_enumeration_type)
9326 {
9327 dw_die_ref c;
9328
9329 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9330 }
9331 }
9332 }
9333
9334 /* Output a single entry in the pubnames table. */
9335
9336 static void
9337 output_pubname (dw_offset die_offset, pubname_entry *entry)
9338 {
9339 dw_die_ref die = entry->die;
9340 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9341
9342 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9343
9344 if (debug_generate_pub_sections == 2)
9345 {
9346 /* This logic follows gdb's method for determining the value of the flag
9347 byte. */
9348 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9349 switch (die->die_tag)
9350 {
9351 case DW_TAG_typedef:
9352 case DW_TAG_base_type:
9353 case DW_TAG_subrange_type:
9354 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9355 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9356 break;
9357 case DW_TAG_enumerator:
9358 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9359 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9360 if (!is_cxx () && !is_java ())
9361 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9362 break;
9363 case DW_TAG_subprogram:
9364 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9365 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9366 if (!is_ada ())
9367 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9368 break;
9369 case DW_TAG_constant:
9370 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9371 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9372 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9373 break;
9374 case DW_TAG_variable:
9375 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9376 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9377 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9378 break;
9379 case DW_TAG_namespace:
9380 case DW_TAG_imported_declaration:
9381 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9382 break;
9383 case DW_TAG_class_type:
9384 case DW_TAG_interface_type:
9385 case DW_TAG_structure_type:
9386 case DW_TAG_union_type:
9387 case DW_TAG_enumeration_type:
9388 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9389 if (!is_cxx () && !is_java ())
9390 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9391 break;
9392 default:
9393 /* An unusual tag. Leave the flag-byte empty. */
9394 break;
9395 }
9396 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9397 "GDB-index flags");
9398 }
9399
9400 dw2_asm_output_nstring (entry->name, -1, "external name");
9401 }
9402
9403
9404 /* Output the public names table used to speed up access to externally
9405 visible names; or the public types table used to find type definitions. */
9406
9407 static void
9408 output_pubnames (vec<pubname_entry, va_gc> *names)
9409 {
9410 unsigned i;
9411 unsigned long pubnames_length = size_of_pubnames (names);
9412 pubname_ref pub;
9413
9414 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9415 dw2_asm_output_data (4, 0xffffffff,
9416 "Initial length escape value indicating 64-bit DWARF extension");
9417 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9418
9419 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9420 dw2_asm_output_data (2, 2, "DWARF Version");
9421
9422 if (dwarf_split_debug_info)
9423 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9424 debug_skeleton_info_section,
9425 "Offset of Compilation Unit Info");
9426 else
9427 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9428 debug_info_section,
9429 "Offset of Compilation Unit Info");
9430 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9431 "Compilation Unit Length");
9432
9433 FOR_EACH_VEC_ELT (*names, i, pub)
9434 {
9435 if (include_pubname_in_output (names, pub))
9436 {
9437 dw_offset die_offset = pub->die->die_offset;
9438
9439 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9440 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9441 gcc_assert (pub->die->die_mark);
9442
9443 /* If we're putting types in their own .debug_types sections,
9444 the .debug_pubtypes table will still point to the compile
9445 unit (not the type unit), so we want to use the offset of
9446 the skeleton DIE (if there is one). */
9447 if (pub->die->comdat_type_p && names == pubtype_table)
9448 {
9449 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9450
9451 if (type_node != NULL)
9452 die_offset = (type_node->skeleton_die != NULL
9453 ? type_node->skeleton_die->die_offset
9454 : comp_unit_die ()->die_offset);
9455 }
9456
9457 output_pubname (die_offset, pub);
9458 }
9459 }
9460
9461 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9462 }
9463
9464 /* Output public names and types tables if necessary. */
9465
9466 static void
9467 output_pubtables (void)
9468 {
9469 if (!want_pubnames () || !info_section_emitted)
9470 return;
9471
9472 switch_to_section (debug_pubnames_section);
9473 output_pubnames (pubname_table);
9474 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9475 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9476 simply won't look for the section. */
9477 switch_to_section (debug_pubtypes_section);
9478 output_pubnames (pubtype_table);
9479 }
9480
9481
9482 /* Output the information that goes into the .debug_aranges table.
9483 Namely, define the beginning and ending address range of the
9484 text section generated for this compilation unit. */
9485
9486 static void
9487 output_aranges (unsigned long aranges_length)
9488 {
9489 unsigned i;
9490
9491 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9492 dw2_asm_output_data (4, 0xffffffff,
9493 "Initial length escape value indicating 64-bit DWARF extension");
9494 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9495 "Length of Address Ranges Info");
9496 /* Version number for aranges is still 2, even in DWARF3. */
9497 dw2_asm_output_data (2, 2, "DWARF Version");
9498 if (dwarf_split_debug_info)
9499 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9500 debug_skeleton_info_section,
9501 "Offset of Compilation Unit Info");
9502 else
9503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9504 debug_info_section,
9505 "Offset of Compilation Unit Info");
9506 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9507 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9508
9509 /* We need to align to twice the pointer size here. */
9510 if (DWARF_ARANGES_PAD_SIZE)
9511 {
9512 /* Pad using a 2 byte words so that padding is correct for any
9513 pointer size. */
9514 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9515 2 * DWARF2_ADDR_SIZE);
9516 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9517 dw2_asm_output_data (2, 0, NULL);
9518 }
9519
9520 /* It is necessary not to output these entries if the sections were
9521 not used; if the sections were not used, the length will be 0 and
9522 the address may end up as 0 if the section is discarded by ld
9523 --gc-sections, leaving an invalid (0, 0) entry that can be
9524 confused with the terminator. */
9525 if (text_section_used)
9526 {
9527 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9529 text_section_label, "Length");
9530 }
9531 if (cold_text_section_used)
9532 {
9533 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9534 "Address");
9535 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9536 cold_text_section_label, "Length");
9537 }
9538
9539 if (have_multiple_function_sections)
9540 {
9541 unsigned fde_idx;
9542 dw_fde_ref fde;
9543
9544 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9545 {
9546 if (DECL_IGNORED_P (fde->decl))
9547 continue;
9548 if (!fde->in_std_section)
9549 {
9550 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9551 "Address");
9552 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9553 fde->dw_fde_begin, "Length");
9554 }
9555 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9556 {
9557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9558 "Address");
9559 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9560 fde->dw_fde_second_begin, "Length");
9561 }
9562 }
9563 }
9564
9565 /* Output the terminator words. */
9566 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9567 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9568 }
9569
9570 /* Add a new entry to .debug_ranges. Return the offset at which it
9571 was placed. */
9572
9573 static unsigned int
9574 add_ranges_num (int num)
9575 {
9576 unsigned int in_use = ranges_table_in_use;
9577
9578 if (in_use == ranges_table_allocated)
9579 {
9580 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9581 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9582 ranges_table_allocated);
9583 memset (ranges_table + ranges_table_in_use, 0,
9584 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9585 }
9586
9587 ranges_table[in_use].num = num;
9588 ranges_table_in_use = in_use + 1;
9589
9590 return in_use * 2 * DWARF2_ADDR_SIZE;
9591 }
9592
9593 /* Add a new entry to .debug_ranges corresponding to a block, or a
9594 range terminator if BLOCK is NULL. */
9595
9596 static unsigned int
9597 add_ranges (const_tree block)
9598 {
9599 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9600 }
9601
9602 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9603 When using dwarf_split_debug_info, address attributes in dies destined
9604 for the final executable should be direct references--setting the
9605 parameter force_direct ensures this behavior. */
9606
9607 static void
9608 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9609 bool *added, bool force_direct)
9610 {
9611 unsigned int in_use = ranges_by_label_in_use;
9612 unsigned int offset;
9613
9614 if (in_use == ranges_by_label_allocated)
9615 {
9616 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9617 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9618 ranges_by_label,
9619 ranges_by_label_allocated);
9620 memset (ranges_by_label + ranges_by_label_in_use, 0,
9621 RANGES_TABLE_INCREMENT
9622 * sizeof (struct dw_ranges_by_label_struct));
9623 }
9624
9625 ranges_by_label[in_use].begin = begin;
9626 ranges_by_label[in_use].end = end;
9627 ranges_by_label_in_use = in_use + 1;
9628
9629 offset = add_ranges_num (-(int)in_use - 1);
9630 if (!*added)
9631 {
9632 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9633 *added = true;
9634 }
9635 }
9636
9637 static void
9638 output_ranges (void)
9639 {
9640 unsigned i;
9641 static const char *const start_fmt = "Offset %#x";
9642 const char *fmt = start_fmt;
9643
9644 for (i = 0; i < ranges_table_in_use; i++)
9645 {
9646 int block_num = ranges_table[i].num;
9647
9648 if (block_num > 0)
9649 {
9650 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9651 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9652
9653 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9654 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9655
9656 /* If all code is in the text section, then the compilation
9657 unit base address defaults to DW_AT_low_pc, which is the
9658 base of the text section. */
9659 if (!have_multiple_function_sections)
9660 {
9661 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9662 text_section_label,
9663 fmt, i * 2 * DWARF2_ADDR_SIZE);
9664 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9665 text_section_label, NULL);
9666 }
9667
9668 /* Otherwise, the compilation unit base address is zero,
9669 which allows us to use absolute addresses, and not worry
9670 about whether the target supports cross-section
9671 arithmetic. */
9672 else
9673 {
9674 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9675 fmt, i * 2 * DWARF2_ADDR_SIZE);
9676 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9677 }
9678
9679 fmt = NULL;
9680 }
9681
9682 /* Negative block_num stands for an index into ranges_by_label. */
9683 else if (block_num < 0)
9684 {
9685 int lab_idx = - block_num - 1;
9686
9687 if (!have_multiple_function_sections)
9688 {
9689 gcc_unreachable ();
9690 #if 0
9691 /* If we ever use add_ranges_by_labels () for a single
9692 function section, all we have to do is to take out
9693 the #if 0 above. */
9694 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9695 ranges_by_label[lab_idx].begin,
9696 text_section_label,
9697 fmt, i * 2 * DWARF2_ADDR_SIZE);
9698 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9699 ranges_by_label[lab_idx].end,
9700 text_section_label, NULL);
9701 #endif
9702 }
9703 else
9704 {
9705 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9706 ranges_by_label[lab_idx].begin,
9707 fmt, i * 2 * DWARF2_ADDR_SIZE);
9708 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9709 ranges_by_label[lab_idx].end,
9710 NULL);
9711 }
9712 }
9713 else
9714 {
9715 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9716 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9717 fmt = start_fmt;
9718 }
9719 }
9720 }
9721
9722 /* Data structure containing information about input files. */
9723 struct file_info
9724 {
9725 const char *path; /* Complete file name. */
9726 const char *fname; /* File name part. */
9727 int length; /* Length of entire string. */
9728 struct dwarf_file_data * file_idx; /* Index in input file table. */
9729 int dir_idx; /* Index in directory table. */
9730 };
9731
9732 /* Data structure containing information about directories with source
9733 files. */
9734 struct dir_info
9735 {
9736 const char *path; /* Path including directory name. */
9737 int length; /* Path length. */
9738 int prefix; /* Index of directory entry which is a prefix. */
9739 int count; /* Number of files in this directory. */
9740 int dir_idx; /* Index of directory used as base. */
9741 };
9742
9743 /* Callback function for file_info comparison. We sort by looking at
9744 the directories in the path. */
9745
9746 static int
9747 file_info_cmp (const void *p1, const void *p2)
9748 {
9749 const struct file_info *const s1 = (const struct file_info *) p1;
9750 const struct file_info *const s2 = (const struct file_info *) p2;
9751 const unsigned char *cp1;
9752 const unsigned char *cp2;
9753
9754 /* Take care of file names without directories. We need to make sure that
9755 we return consistent values to qsort since some will get confused if
9756 we return the same value when identical operands are passed in opposite
9757 orders. So if neither has a directory, return 0 and otherwise return
9758 1 or -1 depending on which one has the directory. */
9759 if ((s1->path == s1->fname || s2->path == s2->fname))
9760 return (s2->path == s2->fname) - (s1->path == s1->fname);
9761
9762 cp1 = (const unsigned char *) s1->path;
9763 cp2 = (const unsigned char *) s2->path;
9764
9765 while (1)
9766 {
9767 ++cp1;
9768 ++cp2;
9769 /* Reached the end of the first path? If so, handle like above. */
9770 if ((cp1 == (const unsigned char *) s1->fname)
9771 || (cp2 == (const unsigned char *) s2->fname))
9772 return ((cp2 == (const unsigned char *) s2->fname)
9773 - (cp1 == (const unsigned char *) s1->fname));
9774
9775 /* Character of current path component the same? */
9776 else if (*cp1 != *cp2)
9777 return *cp1 - *cp2;
9778 }
9779 }
9780
9781 struct file_name_acquire_data
9782 {
9783 struct file_info *files;
9784 int used_files;
9785 int max_files;
9786 };
9787
9788 /* Traversal function for the hash table. */
9789
9790 static int
9791 file_name_acquire (void ** slot, void *data)
9792 {
9793 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9794 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9795 struct file_info *fi;
9796 const char *f;
9797
9798 gcc_assert (fnad->max_files >= d->emitted_number);
9799
9800 if (! d->emitted_number)
9801 return 1;
9802
9803 gcc_assert (fnad->max_files != fnad->used_files);
9804
9805 fi = fnad->files + fnad->used_files++;
9806
9807 /* Skip all leading "./". */
9808 f = d->filename;
9809 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9810 f += 2;
9811
9812 /* Create a new array entry. */
9813 fi->path = f;
9814 fi->length = strlen (f);
9815 fi->file_idx = d;
9816
9817 /* Search for the file name part. */
9818 f = strrchr (f, DIR_SEPARATOR);
9819 #if defined (DIR_SEPARATOR_2)
9820 {
9821 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9822
9823 if (g != NULL)
9824 {
9825 if (f == NULL || f < g)
9826 f = g;
9827 }
9828 }
9829 #endif
9830
9831 fi->fname = f == NULL ? fi->path : f + 1;
9832 return 1;
9833 }
9834
9835 /* Output the directory table and the file name table. We try to minimize
9836 the total amount of memory needed. A heuristic is used to avoid large
9837 slowdowns with many input files. */
9838
9839 static void
9840 output_file_names (void)
9841 {
9842 struct file_name_acquire_data fnad;
9843 int numfiles;
9844 struct file_info *files;
9845 struct dir_info *dirs;
9846 int *saved;
9847 int *savehere;
9848 int *backmap;
9849 int ndirs;
9850 int idx_offset;
9851 int i;
9852
9853 if (!last_emitted_file)
9854 {
9855 dw2_asm_output_data (1, 0, "End directory table");
9856 dw2_asm_output_data (1, 0, "End file name table");
9857 return;
9858 }
9859
9860 numfiles = last_emitted_file->emitted_number;
9861
9862 /* Allocate the various arrays we need. */
9863 files = XALLOCAVEC (struct file_info, numfiles);
9864 dirs = XALLOCAVEC (struct dir_info, numfiles);
9865
9866 fnad.files = files;
9867 fnad.used_files = 0;
9868 fnad.max_files = numfiles;
9869 htab_traverse (file_table, file_name_acquire, &fnad);
9870 gcc_assert (fnad.used_files == fnad.max_files);
9871
9872 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9873
9874 /* Find all the different directories used. */
9875 dirs[0].path = files[0].path;
9876 dirs[0].length = files[0].fname - files[0].path;
9877 dirs[0].prefix = -1;
9878 dirs[0].count = 1;
9879 dirs[0].dir_idx = 0;
9880 files[0].dir_idx = 0;
9881 ndirs = 1;
9882
9883 for (i = 1; i < numfiles; i++)
9884 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9885 && memcmp (dirs[ndirs - 1].path, files[i].path,
9886 dirs[ndirs - 1].length) == 0)
9887 {
9888 /* Same directory as last entry. */
9889 files[i].dir_idx = ndirs - 1;
9890 ++dirs[ndirs - 1].count;
9891 }
9892 else
9893 {
9894 int j;
9895
9896 /* This is a new directory. */
9897 dirs[ndirs].path = files[i].path;
9898 dirs[ndirs].length = files[i].fname - files[i].path;
9899 dirs[ndirs].count = 1;
9900 dirs[ndirs].dir_idx = ndirs;
9901 files[i].dir_idx = ndirs;
9902
9903 /* Search for a prefix. */
9904 dirs[ndirs].prefix = -1;
9905 for (j = 0; j < ndirs; j++)
9906 if (dirs[j].length < dirs[ndirs].length
9907 && dirs[j].length > 1
9908 && (dirs[ndirs].prefix == -1
9909 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9910 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9911 dirs[ndirs].prefix = j;
9912
9913 ++ndirs;
9914 }
9915
9916 /* Now to the actual work. We have to find a subset of the directories which
9917 allow expressing the file name using references to the directory table
9918 with the least amount of characters. We do not do an exhaustive search
9919 where we would have to check out every combination of every single
9920 possible prefix. Instead we use a heuristic which provides nearly optimal
9921 results in most cases and never is much off. */
9922 saved = XALLOCAVEC (int, ndirs);
9923 savehere = XALLOCAVEC (int, ndirs);
9924
9925 memset (saved, '\0', ndirs * sizeof (saved[0]));
9926 for (i = 0; i < ndirs; i++)
9927 {
9928 int j;
9929 int total;
9930
9931 /* We can always save some space for the current directory. But this
9932 does not mean it will be enough to justify adding the directory. */
9933 savehere[i] = dirs[i].length;
9934 total = (savehere[i] - saved[i]) * dirs[i].count;
9935
9936 for (j = i + 1; j < ndirs; j++)
9937 {
9938 savehere[j] = 0;
9939 if (saved[j] < dirs[i].length)
9940 {
9941 /* Determine whether the dirs[i] path is a prefix of the
9942 dirs[j] path. */
9943 int k;
9944
9945 k = dirs[j].prefix;
9946 while (k != -1 && k != (int) i)
9947 k = dirs[k].prefix;
9948
9949 if (k == (int) i)
9950 {
9951 /* Yes it is. We can possibly save some memory by
9952 writing the filenames in dirs[j] relative to
9953 dirs[i]. */
9954 savehere[j] = dirs[i].length;
9955 total += (savehere[j] - saved[j]) * dirs[j].count;
9956 }
9957 }
9958 }
9959
9960 /* Check whether we can save enough to justify adding the dirs[i]
9961 directory. */
9962 if (total > dirs[i].length + 1)
9963 {
9964 /* It's worthwhile adding. */
9965 for (j = i; j < ndirs; j++)
9966 if (savehere[j] > 0)
9967 {
9968 /* Remember how much we saved for this directory so far. */
9969 saved[j] = savehere[j];
9970
9971 /* Remember the prefix directory. */
9972 dirs[j].dir_idx = i;
9973 }
9974 }
9975 }
9976
9977 /* Emit the directory name table. */
9978 idx_offset = dirs[0].length > 0 ? 1 : 0;
9979 for (i = 1 - idx_offset; i < ndirs; i++)
9980 dw2_asm_output_nstring (dirs[i].path,
9981 dirs[i].length
9982 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9983 "Directory Entry: %#x", i + idx_offset);
9984
9985 dw2_asm_output_data (1, 0, "End directory table");
9986
9987 /* We have to emit them in the order of emitted_number since that's
9988 used in the debug info generation. To do this efficiently we
9989 generate a back-mapping of the indices first. */
9990 backmap = XALLOCAVEC (int, numfiles);
9991 for (i = 0; i < numfiles; i++)
9992 backmap[files[i].file_idx->emitted_number - 1] = i;
9993
9994 /* Now write all the file names. */
9995 for (i = 0; i < numfiles; i++)
9996 {
9997 int file_idx = backmap[i];
9998 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9999
10000 #ifdef VMS_DEBUGGING_INFO
10001 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10002
10003 /* Setting these fields can lead to debugger miscomparisons,
10004 but VMS Debug requires them to be set correctly. */
10005
10006 int ver;
10007 long long cdt;
10008 long siz;
10009 int maxfilelen = strlen (files[file_idx].path)
10010 + dirs[dir_idx].length
10011 + MAX_VMS_VERSION_LEN + 1;
10012 char *filebuf = XALLOCAVEC (char, maxfilelen);
10013
10014 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10015 snprintf (filebuf, maxfilelen, "%s;%d",
10016 files[file_idx].path + dirs[dir_idx].length, ver);
10017
10018 dw2_asm_output_nstring
10019 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10020
10021 /* Include directory index. */
10022 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10023
10024 /* Modification time. */
10025 dw2_asm_output_data_uleb128
10026 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10027 ? cdt : 0,
10028 NULL);
10029
10030 /* File length in bytes. */
10031 dw2_asm_output_data_uleb128
10032 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10033 ? siz : 0,
10034 NULL);
10035 #else
10036 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10037 "File Entry: %#x", (unsigned) i + 1);
10038
10039 /* Include directory index. */
10040 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10041
10042 /* Modification time. */
10043 dw2_asm_output_data_uleb128 (0, NULL);
10044
10045 /* File length in bytes. */
10046 dw2_asm_output_data_uleb128 (0, NULL);
10047 #endif /* VMS_DEBUGGING_INFO */
10048 }
10049
10050 dw2_asm_output_data (1, 0, "End file name table");
10051 }
10052
10053
10054 /* Output one line number table into the .debug_line section. */
10055
10056 static void
10057 output_one_line_info_table (dw_line_info_table *table)
10058 {
10059 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10060 unsigned int current_line = 1;
10061 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10062 dw_line_info_entry *ent;
10063 size_t i;
10064
10065 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10066 {
10067 switch (ent->opcode)
10068 {
10069 case LI_set_address:
10070 /* ??? Unfortunately, we have little choice here currently, and
10071 must always use the most general form. GCC does not know the
10072 address delta itself, so we can't use DW_LNS_advance_pc. Many
10073 ports do have length attributes which will give an upper bound
10074 on the address range. We could perhaps use length attributes
10075 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10076 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10077
10078 /* This can handle any delta. This takes
10079 4+DWARF2_ADDR_SIZE bytes. */
10080 dw2_asm_output_data (1, 0, "set address %s", line_label);
10081 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10082 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10083 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10084 break;
10085
10086 case LI_set_line:
10087 if (ent->val == current_line)
10088 {
10089 /* We still need to start a new row, so output a copy insn. */
10090 dw2_asm_output_data (1, DW_LNS_copy,
10091 "copy line %u", current_line);
10092 }
10093 else
10094 {
10095 int line_offset = ent->val - current_line;
10096 int line_delta = line_offset - DWARF_LINE_BASE;
10097
10098 current_line = ent->val;
10099 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10100 {
10101 /* This can handle deltas from -10 to 234, using the current
10102 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10103 This takes 1 byte. */
10104 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10105 "line %u", current_line);
10106 }
10107 else
10108 {
10109 /* This can handle any delta. This takes at least 4 bytes,
10110 depending on the value being encoded. */
10111 dw2_asm_output_data (1, DW_LNS_advance_line,
10112 "advance to line %u", current_line);
10113 dw2_asm_output_data_sleb128 (line_offset, NULL);
10114 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10115 }
10116 }
10117 break;
10118
10119 case LI_set_file:
10120 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10121 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10122 break;
10123
10124 case LI_set_column:
10125 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10126 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10127 break;
10128
10129 case LI_negate_stmt:
10130 current_is_stmt = !current_is_stmt;
10131 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10132 "is_stmt %d", current_is_stmt);
10133 break;
10134
10135 case LI_set_prologue_end:
10136 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10137 "set prologue end");
10138 break;
10139
10140 case LI_set_epilogue_begin:
10141 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10142 "set epilogue begin");
10143 break;
10144
10145 case LI_set_discriminator:
10146 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10147 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10148 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10149 dw2_asm_output_data_uleb128 (ent->val, NULL);
10150 break;
10151 }
10152 }
10153
10154 /* Emit debug info for the address of the end of the table. */
10155 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10156 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10157 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10158 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10159
10160 dw2_asm_output_data (1, 0, "end sequence");
10161 dw2_asm_output_data_uleb128 (1, NULL);
10162 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10163 }
10164
10165 /* Output the source line number correspondence information. This
10166 information goes into the .debug_line section. */
10167
10168 static void
10169 output_line_info (bool prologue_only)
10170 {
10171 char l1[20], l2[20], p1[20], p2[20];
10172 int ver = dwarf_version;
10173 bool saw_one = false;
10174 int opc;
10175
10176 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10177 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10178 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10179 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10180
10181 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10182 dw2_asm_output_data (4, 0xffffffff,
10183 "Initial length escape value indicating 64-bit DWARF extension");
10184 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10185 "Length of Source Line Info");
10186 ASM_OUTPUT_LABEL (asm_out_file, l1);
10187
10188 dw2_asm_output_data (2, ver, "DWARF Version");
10189 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10190 ASM_OUTPUT_LABEL (asm_out_file, p1);
10191
10192 /* Define the architecture-dependent minimum instruction length (in bytes).
10193 In this implementation of DWARF, this field is used for information
10194 purposes only. Since GCC generates assembly language, we have no
10195 a priori knowledge of how many instruction bytes are generated for each
10196 source line, and therefore can use only the DW_LNE_set_address and
10197 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10198 this as '1', which is "correct enough" for all architectures,
10199 and don't let the target override. */
10200 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10201
10202 if (ver >= 4)
10203 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10204 "Maximum Operations Per Instruction");
10205 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10206 "Default is_stmt_start flag");
10207 dw2_asm_output_data (1, DWARF_LINE_BASE,
10208 "Line Base Value (Special Opcodes)");
10209 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10210 "Line Range Value (Special Opcodes)");
10211 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10212 "Special Opcode Base");
10213
10214 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10215 {
10216 int n_op_args;
10217 switch (opc)
10218 {
10219 case DW_LNS_advance_pc:
10220 case DW_LNS_advance_line:
10221 case DW_LNS_set_file:
10222 case DW_LNS_set_column:
10223 case DW_LNS_fixed_advance_pc:
10224 case DW_LNS_set_isa:
10225 n_op_args = 1;
10226 break;
10227 default:
10228 n_op_args = 0;
10229 break;
10230 }
10231
10232 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10233 opc, n_op_args);
10234 }
10235
10236 /* Write out the information about the files we use. */
10237 output_file_names ();
10238 ASM_OUTPUT_LABEL (asm_out_file, p2);
10239 if (prologue_only)
10240 {
10241 /* Output the marker for the end of the line number info. */
10242 ASM_OUTPUT_LABEL (asm_out_file, l2);
10243 return;
10244 }
10245
10246 if (separate_line_info)
10247 {
10248 dw_line_info_table *table;
10249 size_t i;
10250
10251 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10252 if (table->in_use)
10253 {
10254 output_one_line_info_table (table);
10255 saw_one = true;
10256 }
10257 }
10258 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10259 {
10260 output_one_line_info_table (cold_text_section_line_info);
10261 saw_one = true;
10262 }
10263
10264 /* ??? Some Darwin linkers crash on a .debug_line section with no
10265 sequences. Further, merely a DW_LNE_end_sequence entry is not
10266 sufficient -- the address column must also be initialized.
10267 Make sure to output at least one set_address/end_sequence pair,
10268 choosing .text since that section is always present. */
10269 if (text_section_line_info->in_use || !saw_one)
10270 output_one_line_info_table (text_section_line_info);
10271
10272 /* Output the marker for the end of the line number info. */
10273 ASM_OUTPUT_LABEL (asm_out_file, l2);
10274 }
10275 \f
10276 /* Given a pointer to a tree node for some base type, return a pointer to
10277 a DIE that describes the given type.
10278
10279 This routine must only be called for GCC type nodes that correspond to
10280 Dwarf base (fundamental) types. */
10281
10282 static dw_die_ref
10283 base_type_die (tree type)
10284 {
10285 dw_die_ref base_type_result;
10286 enum dwarf_type encoding;
10287
10288 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10289 return 0;
10290
10291 /* If this is a subtype that should not be emitted as a subrange type,
10292 use the base type. See subrange_type_for_debug_p. */
10293 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10294 type = TREE_TYPE (type);
10295
10296 switch (TREE_CODE (type))
10297 {
10298 case INTEGER_TYPE:
10299 if ((dwarf_version >= 4 || !dwarf_strict)
10300 && TYPE_NAME (type)
10301 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10302 && DECL_IS_BUILTIN (TYPE_NAME (type))
10303 && DECL_NAME (TYPE_NAME (type)))
10304 {
10305 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10306 if (strcmp (name, "char16_t") == 0
10307 || strcmp (name, "char32_t") == 0)
10308 {
10309 encoding = DW_ATE_UTF;
10310 break;
10311 }
10312 }
10313 if (TYPE_STRING_FLAG (type))
10314 {
10315 if (TYPE_UNSIGNED (type))
10316 encoding = DW_ATE_unsigned_char;
10317 else
10318 encoding = DW_ATE_signed_char;
10319 }
10320 else if (TYPE_UNSIGNED (type))
10321 encoding = DW_ATE_unsigned;
10322 else
10323 encoding = DW_ATE_signed;
10324 break;
10325
10326 case REAL_TYPE:
10327 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10328 {
10329 if (dwarf_version >= 3 || !dwarf_strict)
10330 encoding = DW_ATE_decimal_float;
10331 else
10332 encoding = DW_ATE_lo_user;
10333 }
10334 else
10335 encoding = DW_ATE_float;
10336 break;
10337
10338 case FIXED_POINT_TYPE:
10339 if (!(dwarf_version >= 3 || !dwarf_strict))
10340 encoding = DW_ATE_lo_user;
10341 else if (TYPE_UNSIGNED (type))
10342 encoding = DW_ATE_unsigned_fixed;
10343 else
10344 encoding = DW_ATE_signed_fixed;
10345 break;
10346
10347 /* Dwarf2 doesn't know anything about complex ints, so use
10348 a user defined type for it. */
10349 case COMPLEX_TYPE:
10350 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10351 encoding = DW_ATE_complex_float;
10352 else
10353 encoding = DW_ATE_lo_user;
10354 break;
10355
10356 case BOOLEAN_TYPE:
10357 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10358 encoding = DW_ATE_boolean;
10359 break;
10360
10361 default:
10362 /* No other TREE_CODEs are Dwarf fundamental types. */
10363 gcc_unreachable ();
10364 }
10365
10366 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10367
10368 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10369 int_size_in_bytes (type));
10370 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10371 add_pubtype (type, base_type_result);
10372
10373 return base_type_result;
10374 }
10375
10376 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10377 named 'auto' in its type: return true for it, false otherwise. */
10378
10379 static inline bool
10380 is_cxx_auto (tree type)
10381 {
10382 if (is_cxx ())
10383 {
10384 tree name = TYPE_IDENTIFIER (type);
10385 if (name == get_identifier ("auto")
10386 || name == get_identifier ("decltype(auto)"))
10387 return true;
10388 }
10389 return false;
10390 }
10391
10392 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10393 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10394
10395 static inline int
10396 is_base_type (tree type)
10397 {
10398 switch (TREE_CODE (type))
10399 {
10400 case ERROR_MARK:
10401 case VOID_TYPE:
10402 case INTEGER_TYPE:
10403 case REAL_TYPE:
10404 case FIXED_POINT_TYPE:
10405 case COMPLEX_TYPE:
10406 case BOOLEAN_TYPE:
10407 return 1;
10408
10409 case ARRAY_TYPE:
10410 case RECORD_TYPE:
10411 case UNION_TYPE:
10412 case QUAL_UNION_TYPE:
10413 case ENUMERAL_TYPE:
10414 case FUNCTION_TYPE:
10415 case METHOD_TYPE:
10416 case POINTER_TYPE:
10417 case REFERENCE_TYPE:
10418 case NULLPTR_TYPE:
10419 case OFFSET_TYPE:
10420 case LANG_TYPE:
10421 case VECTOR_TYPE:
10422 return 0;
10423
10424 default:
10425 if (is_cxx_auto (type))
10426 return 0;
10427 gcc_unreachable ();
10428 }
10429
10430 return 0;
10431 }
10432
10433 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10434 node, return the size in bits for the type if it is a constant, or else
10435 return the alignment for the type if the type's size is not constant, or
10436 else return BITS_PER_WORD if the type actually turns out to be an
10437 ERROR_MARK node. */
10438
10439 static inline unsigned HOST_WIDE_INT
10440 simple_type_size_in_bits (const_tree type)
10441 {
10442 if (TREE_CODE (type) == ERROR_MARK)
10443 return BITS_PER_WORD;
10444 else if (TYPE_SIZE (type) == NULL_TREE)
10445 return 0;
10446 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10447 return tree_to_uhwi (TYPE_SIZE (type));
10448 else
10449 return TYPE_ALIGN (type);
10450 }
10451
10452 /* Similarly, but return an offset_int instead of UHWI. */
10453
10454 static inline offset_int
10455 offset_int_type_size_in_bits (const_tree type)
10456 {
10457 if (TREE_CODE (type) == ERROR_MARK)
10458 return BITS_PER_WORD;
10459 else if (TYPE_SIZE (type) == NULL_TREE)
10460 return 0;
10461 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10462 return wi::to_offset (TYPE_SIZE (type));
10463 else
10464 return TYPE_ALIGN (type);
10465 }
10466
10467 /* Given a pointer to a tree node for a subrange type, return a pointer
10468 to a DIE that describes the given type. */
10469
10470 static dw_die_ref
10471 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10472 {
10473 dw_die_ref subrange_die;
10474 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10475
10476 if (context_die == NULL)
10477 context_die = comp_unit_die ();
10478
10479 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10480
10481 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10482 {
10483 /* The size of the subrange type and its base type do not match,
10484 so we need to generate a size attribute for the subrange type. */
10485 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10486 }
10487
10488 if (low)
10489 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10490 if (high)
10491 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10492
10493 return subrange_die;
10494 }
10495
10496 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10497 entry that chains various modifiers in front of the given type. */
10498
10499 static dw_die_ref
10500 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10501 dw_die_ref context_die)
10502 {
10503 enum tree_code code = TREE_CODE (type);
10504 dw_die_ref mod_type_die;
10505 dw_die_ref sub_die = NULL;
10506 tree item_type = NULL;
10507 tree qualified_type;
10508 tree name, low, high;
10509 dw_die_ref mod_scope;
10510
10511 if (code == ERROR_MARK)
10512 return NULL;
10513
10514 /* See if we already have the appropriately qualified variant of
10515 this type. */
10516 qualified_type
10517 = get_qualified_type (type,
10518 ((is_const_type ? TYPE_QUAL_CONST : 0)
10519 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10520
10521 if (qualified_type == sizetype
10522 && TYPE_NAME (qualified_type)
10523 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10524 {
10525 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10526
10527 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10528 && TYPE_PRECISION (t)
10529 == TYPE_PRECISION (qualified_type)
10530 && TYPE_UNSIGNED (t)
10531 == TYPE_UNSIGNED (qualified_type));
10532 qualified_type = t;
10533 }
10534
10535 /* If we do, then we can just use its DIE, if it exists. */
10536 if (qualified_type)
10537 {
10538 mod_type_die = lookup_type_die (qualified_type);
10539 if (mod_type_die)
10540 return mod_type_die;
10541 }
10542
10543 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10544
10545 /* Handle C typedef types. */
10546 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10547 && !DECL_ARTIFICIAL (name))
10548 {
10549 tree dtype = TREE_TYPE (name);
10550
10551 if (qualified_type == dtype)
10552 {
10553 /* For a named type, use the typedef. */
10554 gen_type_die (qualified_type, context_die);
10555 return lookup_type_die (qualified_type);
10556 }
10557 else if (is_const_type < TYPE_READONLY (dtype)
10558 || is_volatile_type < TYPE_VOLATILE (dtype)
10559 || (is_const_type <= TYPE_READONLY (dtype)
10560 && is_volatile_type <= TYPE_VOLATILE (dtype)
10561 && DECL_ORIGINAL_TYPE (name) != type))
10562 /* cv-unqualified version of named type. Just use the unnamed
10563 type to which it refers. */
10564 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10565 is_const_type, is_volatile_type,
10566 context_die);
10567 /* Else cv-qualified version of named type; fall through. */
10568 }
10569
10570 mod_scope = scope_die_for (type, context_die);
10571
10572 if (is_const_type
10573 /* If both is_const_type and is_volatile_type, prefer the path
10574 which leads to a qualified type. */
10575 && (!is_volatile_type
10576 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10577 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10578 {
10579 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10580 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10581 }
10582 else if (is_volatile_type)
10583 {
10584 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10585 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10586 }
10587 else if (code == POINTER_TYPE)
10588 {
10589 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10590 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10591 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10592 item_type = TREE_TYPE (type);
10593 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10594 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10595 TYPE_ADDR_SPACE (item_type));
10596 }
10597 else if (code == REFERENCE_TYPE)
10598 {
10599 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10600 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10601 type);
10602 else
10603 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10604 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10605 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10606 item_type = TREE_TYPE (type);
10607 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10608 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10609 TYPE_ADDR_SPACE (item_type));
10610 }
10611 else if (code == INTEGER_TYPE
10612 && TREE_TYPE (type) != NULL_TREE
10613 && subrange_type_for_debug_p (type, &low, &high))
10614 {
10615 mod_type_die = subrange_type_die (type, low, high, context_die);
10616 item_type = TREE_TYPE (type);
10617 }
10618 else if (is_base_type (type))
10619 mod_type_die = base_type_die (type);
10620 else
10621 {
10622 gen_type_die (type, context_die);
10623
10624 /* We have to get the type_main_variant here (and pass that to the
10625 `lookup_type_die' routine) because the ..._TYPE node we have
10626 might simply be a *copy* of some original type node (where the
10627 copy was created to help us keep track of typedef names) and
10628 that copy might have a different TYPE_UID from the original
10629 ..._TYPE node. */
10630 if (TREE_CODE (type) != VECTOR_TYPE)
10631 return lookup_type_die (type_main_variant (type));
10632 else
10633 /* Vectors have the debugging information in the type,
10634 not the main variant. */
10635 return lookup_type_die (type);
10636 }
10637
10638 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10639 don't output a DW_TAG_typedef, since there isn't one in the
10640 user's program; just attach a DW_AT_name to the type.
10641 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10642 if the base type already has the same name. */
10643 if (name
10644 && ((TREE_CODE (name) != TYPE_DECL
10645 && (qualified_type == TYPE_MAIN_VARIANT (type)
10646 || (!is_const_type && !is_volatile_type)))
10647 || (TREE_CODE (name) == TYPE_DECL
10648 && TREE_TYPE (name) == qualified_type
10649 && DECL_NAME (name))))
10650 {
10651 if (TREE_CODE (name) == TYPE_DECL)
10652 /* Could just call add_name_and_src_coords_attributes here,
10653 but since this is a builtin type it doesn't have any
10654 useful source coordinates anyway. */
10655 name = DECL_NAME (name);
10656 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10657 }
10658 /* This probably indicates a bug. */
10659 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10660 {
10661 name = TYPE_IDENTIFIER (type);
10662 add_name_attribute (mod_type_die,
10663 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10664 }
10665
10666 if (qualified_type)
10667 equate_type_number_to_die (qualified_type, mod_type_die);
10668
10669 if (item_type)
10670 /* We must do this after the equate_type_number_to_die call, in case
10671 this is a recursive type. This ensures that the modified_type_die
10672 recursion will terminate even if the type is recursive. Recursive
10673 types are possible in Ada. */
10674 sub_die = modified_type_die (item_type,
10675 TYPE_READONLY (item_type),
10676 TYPE_VOLATILE (item_type),
10677 context_die);
10678
10679 if (sub_die != NULL)
10680 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10681
10682 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10683 if (TYPE_ARTIFICIAL (type))
10684 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10685
10686 return mod_type_die;
10687 }
10688
10689 /* Generate DIEs for the generic parameters of T.
10690 T must be either a generic type or a generic function.
10691 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10692
10693 static void
10694 gen_generic_params_dies (tree t)
10695 {
10696 tree parms, args;
10697 int parms_num, i;
10698 dw_die_ref die = NULL;
10699 int non_default;
10700
10701 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10702 return;
10703
10704 if (TYPE_P (t))
10705 die = lookup_type_die (t);
10706 else if (DECL_P (t))
10707 die = lookup_decl_die (t);
10708
10709 gcc_assert (die);
10710
10711 parms = lang_hooks.get_innermost_generic_parms (t);
10712 if (!parms)
10713 /* T has no generic parameter. It means T is neither a generic type
10714 or function. End of story. */
10715 return;
10716
10717 parms_num = TREE_VEC_LENGTH (parms);
10718 args = lang_hooks.get_innermost_generic_args (t);
10719 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10720 non_default = int_cst_value (TREE_CHAIN (args));
10721 else
10722 non_default = TREE_VEC_LENGTH (args);
10723 for (i = 0; i < parms_num; i++)
10724 {
10725 tree parm, arg, arg_pack_elems;
10726 dw_die_ref parm_die;
10727
10728 parm = TREE_VEC_ELT (parms, i);
10729 arg = TREE_VEC_ELT (args, i);
10730 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10731 gcc_assert (parm && TREE_VALUE (parm) && arg);
10732
10733 if (parm && TREE_VALUE (parm) && arg)
10734 {
10735 /* If PARM represents a template parameter pack,
10736 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10737 by DW_TAG_template_*_parameter DIEs for the argument
10738 pack elements of ARG. Note that ARG would then be
10739 an argument pack. */
10740 if (arg_pack_elems)
10741 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10742 arg_pack_elems,
10743 die);
10744 else
10745 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10746 true /* emit name */, die);
10747 if (i >= non_default)
10748 add_AT_flag (parm_die, DW_AT_default_value, 1);
10749 }
10750 }
10751 }
10752
10753 /* Create and return a DIE for PARM which should be
10754 the representation of a generic type parameter.
10755 For instance, in the C++ front end, PARM would be a template parameter.
10756 ARG is the argument to PARM.
10757 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10758 name of the PARM.
10759 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10760 as a child node. */
10761
10762 static dw_die_ref
10763 generic_parameter_die (tree parm, tree arg,
10764 bool emit_name_p,
10765 dw_die_ref parent_die)
10766 {
10767 dw_die_ref tmpl_die = NULL;
10768 const char *name = NULL;
10769
10770 if (!parm || !DECL_NAME (parm) || !arg)
10771 return NULL;
10772
10773 /* We support non-type generic parameters and arguments,
10774 type generic parameters and arguments, as well as
10775 generic generic parameters (a.k.a. template template parameters in C++)
10776 and arguments. */
10777 if (TREE_CODE (parm) == PARM_DECL)
10778 /* PARM is a nontype generic parameter */
10779 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10780 else if (TREE_CODE (parm) == TYPE_DECL)
10781 /* PARM is a type generic parameter. */
10782 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10783 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10784 /* PARM is a generic generic parameter.
10785 Its DIE is a GNU extension. It shall have a
10786 DW_AT_name attribute to represent the name of the template template
10787 parameter, and a DW_AT_GNU_template_name attribute to represent the
10788 name of the template template argument. */
10789 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10790 parent_die, parm);
10791 else
10792 gcc_unreachable ();
10793
10794 if (tmpl_die)
10795 {
10796 tree tmpl_type;
10797
10798 /* If PARM is a generic parameter pack, it means we are
10799 emitting debug info for a template argument pack element.
10800 In other terms, ARG is a template argument pack element.
10801 In that case, we don't emit any DW_AT_name attribute for
10802 the die. */
10803 if (emit_name_p)
10804 {
10805 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10806 gcc_assert (name);
10807 add_AT_string (tmpl_die, DW_AT_name, name);
10808 }
10809
10810 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10811 {
10812 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10813 TMPL_DIE should have a child DW_AT_type attribute that is set
10814 to the type of the argument to PARM, which is ARG.
10815 If PARM is a type generic parameter, TMPL_DIE should have a
10816 child DW_AT_type that is set to ARG. */
10817 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10818 add_type_attribute (tmpl_die, tmpl_type, 0,
10819 TREE_THIS_VOLATILE (tmpl_type),
10820 parent_die);
10821 }
10822 else
10823 {
10824 /* So TMPL_DIE is a DIE representing a
10825 a generic generic template parameter, a.k.a template template
10826 parameter in C++ and arg is a template. */
10827
10828 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10829 to the name of the argument. */
10830 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10831 if (name)
10832 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10833 }
10834
10835 if (TREE_CODE (parm) == PARM_DECL)
10836 /* So PARM is a non-type generic parameter.
10837 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10838 attribute of TMPL_DIE which value represents the value
10839 of ARG.
10840 We must be careful here:
10841 The value of ARG might reference some function decls.
10842 We might currently be emitting debug info for a generic
10843 type and types are emitted before function decls, we don't
10844 know if the function decls referenced by ARG will actually be
10845 emitted after cgraph computations.
10846 So must defer the generation of the DW_AT_const_value to
10847 after cgraph is ready. */
10848 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10849 }
10850
10851 return tmpl_die;
10852 }
10853
10854 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10855 PARM_PACK must be a template parameter pack. The returned DIE
10856 will be child DIE of PARENT_DIE. */
10857
10858 static dw_die_ref
10859 template_parameter_pack_die (tree parm_pack,
10860 tree parm_pack_args,
10861 dw_die_ref parent_die)
10862 {
10863 dw_die_ref die;
10864 int j;
10865
10866 gcc_assert (parent_die && parm_pack);
10867
10868 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10869 add_name_and_src_coords_attributes (die, parm_pack);
10870 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10871 generic_parameter_die (parm_pack,
10872 TREE_VEC_ELT (parm_pack_args, j),
10873 false /* Don't emit DW_AT_name */,
10874 die);
10875 return die;
10876 }
10877
10878 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10879 an enumerated type. */
10880
10881 static inline int
10882 type_is_enum (const_tree type)
10883 {
10884 return TREE_CODE (type) == ENUMERAL_TYPE;
10885 }
10886
10887 /* Return the DBX register number described by a given RTL node. */
10888
10889 static unsigned int
10890 dbx_reg_number (const_rtx rtl)
10891 {
10892 unsigned regno = REGNO (rtl);
10893
10894 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10895
10896 #ifdef LEAF_REG_REMAP
10897 if (crtl->uses_only_leaf_regs)
10898 {
10899 int leaf_reg = LEAF_REG_REMAP (regno);
10900 if (leaf_reg != -1)
10901 regno = (unsigned) leaf_reg;
10902 }
10903 #endif
10904
10905 regno = DBX_REGISTER_NUMBER (regno);
10906 gcc_assert (regno != INVALID_REGNUM);
10907 return regno;
10908 }
10909
10910 /* Optionally add a DW_OP_piece term to a location description expression.
10911 DW_OP_piece is only added if the location description expression already
10912 doesn't end with DW_OP_piece. */
10913
10914 static void
10915 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10916 {
10917 dw_loc_descr_ref loc;
10918
10919 if (*list_head != NULL)
10920 {
10921 /* Find the end of the chain. */
10922 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10923 ;
10924
10925 if (loc->dw_loc_opc != DW_OP_piece)
10926 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10927 }
10928 }
10929
10930 /* Return a location descriptor that designates a machine register or
10931 zero if there is none. */
10932
10933 static dw_loc_descr_ref
10934 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10935 {
10936 rtx regs;
10937
10938 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10939 return 0;
10940
10941 /* We only use "frame base" when we're sure we're talking about the
10942 post-prologue local stack frame. We do this by *not* running
10943 register elimination until this point, and recognizing the special
10944 argument pointer and soft frame pointer rtx's.
10945 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10946 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10947 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10948 {
10949 dw_loc_descr_ref result = NULL;
10950
10951 if (dwarf_version >= 4 || !dwarf_strict)
10952 {
10953 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10954 initialized);
10955 if (result)
10956 add_loc_descr (&result,
10957 new_loc_descr (DW_OP_stack_value, 0, 0));
10958 }
10959 return result;
10960 }
10961
10962 regs = targetm.dwarf_register_span (rtl);
10963
10964 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10965 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10966 else
10967 {
10968 unsigned int dbx_regnum = dbx_reg_number (rtl);
10969 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10970 return 0;
10971 return one_reg_loc_descriptor (dbx_regnum, initialized);
10972 }
10973 }
10974
10975 /* Return a location descriptor that designates a machine register for
10976 a given hard register number. */
10977
10978 static dw_loc_descr_ref
10979 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10980 {
10981 dw_loc_descr_ref reg_loc_descr;
10982
10983 if (regno <= 31)
10984 reg_loc_descr
10985 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10986 else
10987 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10988
10989 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10990 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10991
10992 return reg_loc_descr;
10993 }
10994
10995 /* Given an RTL of a register, return a location descriptor that
10996 designates a value that spans more than one register. */
10997
10998 static dw_loc_descr_ref
10999 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11000 enum var_init_status initialized)
11001 {
11002 int size, i;
11003 dw_loc_descr_ref loc_result = NULL;
11004
11005 /* Simple, contiguous registers. */
11006 if (regs == NULL_RTX)
11007 {
11008 unsigned reg = REGNO (rtl);
11009 int nregs;
11010
11011 #ifdef LEAF_REG_REMAP
11012 if (crtl->uses_only_leaf_regs)
11013 {
11014 int leaf_reg = LEAF_REG_REMAP (reg);
11015 if (leaf_reg != -1)
11016 reg = (unsigned) leaf_reg;
11017 }
11018 #endif
11019
11020 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11021 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11022
11023 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11024
11025 loc_result = NULL;
11026 while (nregs--)
11027 {
11028 dw_loc_descr_ref t;
11029
11030 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11031 VAR_INIT_STATUS_INITIALIZED);
11032 add_loc_descr (&loc_result, t);
11033 add_loc_descr_op_piece (&loc_result, size);
11034 ++reg;
11035 }
11036 return loc_result;
11037 }
11038
11039 /* Now onto stupid register sets in non contiguous locations. */
11040
11041 gcc_assert (GET_CODE (regs) == PARALLEL);
11042
11043 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11044 loc_result = NULL;
11045
11046 for (i = 0; i < XVECLEN (regs, 0); ++i)
11047 {
11048 dw_loc_descr_ref t;
11049
11050 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11051 VAR_INIT_STATUS_INITIALIZED);
11052 add_loc_descr (&loc_result, t);
11053 add_loc_descr_op_piece (&loc_result, size);
11054 }
11055
11056 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11057 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11058 return loc_result;
11059 }
11060
11061 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11062
11063 /* Return a location descriptor that designates a constant i,
11064 as a compound operation from constant (i >> shift), constant shift
11065 and DW_OP_shl. */
11066
11067 static dw_loc_descr_ref
11068 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11069 {
11070 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11071 add_loc_descr (&ret, int_loc_descriptor (shift));
11072 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11073 return ret;
11074 }
11075
11076 /* Return a location descriptor that designates a constant. */
11077
11078 static dw_loc_descr_ref
11079 int_loc_descriptor (HOST_WIDE_INT i)
11080 {
11081 enum dwarf_location_atom op;
11082
11083 /* Pick the smallest representation of a constant, rather than just
11084 defaulting to the LEB encoding. */
11085 if (i >= 0)
11086 {
11087 int clz = clz_hwi (i);
11088 int ctz = ctz_hwi (i);
11089 if (i <= 31)
11090 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11091 else if (i <= 0xff)
11092 op = DW_OP_const1u;
11093 else if (i <= 0xffff)
11094 op = DW_OP_const2u;
11095 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11096 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11097 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11098 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11099 while DW_OP_const4u is 5 bytes. */
11100 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11101 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11102 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11103 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11104 while DW_OP_const4u is 5 bytes. */
11105 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11106 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11107 op = DW_OP_const4u;
11108 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11109 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11110 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11111 while DW_OP_constu of constant >= 0x100000000 takes at least
11112 6 bytes. */
11113 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11114 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11115 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11116 >= HOST_BITS_PER_WIDE_INT)
11117 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11118 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11119 while DW_OP_constu takes in this case at least 6 bytes. */
11120 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11121 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11122 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11123 && size_of_uleb128 (i) > 6)
11124 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11125 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11126 else
11127 op = DW_OP_constu;
11128 }
11129 else
11130 {
11131 if (i >= -0x80)
11132 op = DW_OP_const1s;
11133 else if (i >= -0x8000)
11134 op = DW_OP_const2s;
11135 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11136 {
11137 if (size_of_int_loc_descriptor (i) < 5)
11138 {
11139 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11140 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11141 return ret;
11142 }
11143 op = DW_OP_const4s;
11144 }
11145 else
11146 {
11147 if (size_of_int_loc_descriptor (i)
11148 < (unsigned long) 1 + size_of_sleb128 (i))
11149 {
11150 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11151 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11152 return ret;
11153 }
11154 op = DW_OP_consts;
11155 }
11156 }
11157
11158 return new_loc_descr (op, i, 0);
11159 }
11160
11161 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11162 without actually allocating it. */
11163
11164 static unsigned long
11165 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11166 {
11167 return size_of_int_loc_descriptor (i >> shift)
11168 + size_of_int_loc_descriptor (shift)
11169 + 1;
11170 }
11171
11172 /* Return size_of_locs (int_loc_descriptor (i)) without
11173 actually allocating it. */
11174
11175 static unsigned long
11176 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11177 {
11178 unsigned long s;
11179
11180 if (i >= 0)
11181 {
11182 int clz, ctz;
11183 if (i <= 31)
11184 return 1;
11185 else if (i <= 0xff)
11186 return 2;
11187 else if (i <= 0xffff)
11188 return 3;
11189 clz = clz_hwi (i);
11190 ctz = ctz_hwi (i);
11191 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11192 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11193 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11194 - clz - 5);
11195 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11196 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11197 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11198 - clz - 8);
11199 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11200 return 5;
11201 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11202 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11203 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11204 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11205 - clz - 8);
11206 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11207 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11208 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11209 - clz - 16);
11210 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11211 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11212 && s > 6)
11213 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11214 - clz - 32);
11215 else
11216 return 1 + s;
11217 }
11218 else
11219 {
11220 if (i >= -0x80)
11221 return 2;
11222 else if (i >= -0x8000)
11223 return 3;
11224 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11225 {
11226 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11227 {
11228 s = size_of_int_loc_descriptor (-i) + 1;
11229 if (s < 5)
11230 return s;
11231 }
11232 return 5;
11233 }
11234 else
11235 {
11236 unsigned long r = 1 + size_of_sleb128 (i);
11237 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11238 {
11239 s = size_of_int_loc_descriptor (-i) + 1;
11240 if (s < r)
11241 return s;
11242 }
11243 return r;
11244 }
11245 }
11246 }
11247
11248 /* Return loc description representing "address" of integer value.
11249 This can appear only as toplevel expression. */
11250
11251 static dw_loc_descr_ref
11252 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11253 {
11254 int litsize;
11255 dw_loc_descr_ref loc_result = NULL;
11256
11257 if (!(dwarf_version >= 4 || !dwarf_strict))
11258 return NULL;
11259
11260 litsize = size_of_int_loc_descriptor (i);
11261 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11262 is more compact. For DW_OP_stack_value we need:
11263 litsize + 1 (DW_OP_stack_value)
11264 and for DW_OP_implicit_value:
11265 1 (DW_OP_implicit_value) + 1 (length) + size. */
11266 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11267 {
11268 loc_result = int_loc_descriptor (i);
11269 add_loc_descr (&loc_result,
11270 new_loc_descr (DW_OP_stack_value, 0, 0));
11271 return loc_result;
11272 }
11273
11274 loc_result = new_loc_descr (DW_OP_implicit_value,
11275 size, 0);
11276 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11277 loc_result->dw_loc_oprnd2.v.val_int = i;
11278 return loc_result;
11279 }
11280
11281 /* Return a location descriptor that designates a base+offset location. */
11282
11283 static dw_loc_descr_ref
11284 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11285 enum var_init_status initialized)
11286 {
11287 unsigned int regno;
11288 dw_loc_descr_ref result;
11289 dw_fde_ref fde = cfun->fde;
11290
11291 /* We only use "frame base" when we're sure we're talking about the
11292 post-prologue local stack frame. We do this by *not* running
11293 register elimination until this point, and recognizing the special
11294 argument pointer and soft frame pointer rtx's. */
11295 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11296 {
11297 rtx elim = (ira_use_lra_p
11298 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11299 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11300
11301 if (elim != reg)
11302 {
11303 if (GET_CODE (elim) == PLUS)
11304 {
11305 offset += INTVAL (XEXP (elim, 1));
11306 elim = XEXP (elim, 0);
11307 }
11308 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11309 && (elim == hard_frame_pointer_rtx
11310 || elim == stack_pointer_rtx))
11311 || elim == (frame_pointer_needed
11312 ? hard_frame_pointer_rtx
11313 : stack_pointer_rtx));
11314
11315 /* If drap register is used to align stack, use frame
11316 pointer + offset to access stack variables. If stack
11317 is aligned without drap, use stack pointer + offset to
11318 access stack variables. */
11319 if (crtl->stack_realign_tried
11320 && reg == frame_pointer_rtx)
11321 {
11322 int base_reg
11323 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11324 ? HARD_FRAME_POINTER_REGNUM
11325 : REGNO (elim));
11326 return new_reg_loc_descr (base_reg, offset);
11327 }
11328
11329 gcc_assert (frame_pointer_fb_offset_valid);
11330 offset += frame_pointer_fb_offset;
11331 return new_loc_descr (DW_OP_fbreg, offset, 0);
11332 }
11333 }
11334
11335 regno = REGNO (reg);
11336 #ifdef LEAF_REG_REMAP
11337 if (crtl->uses_only_leaf_regs)
11338 {
11339 int leaf_reg = LEAF_REG_REMAP (regno);
11340 if (leaf_reg != -1)
11341 regno = (unsigned) leaf_reg;
11342 }
11343 #endif
11344 regno = DWARF_FRAME_REGNUM (regno);
11345
11346 if (!optimize && fde
11347 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11348 {
11349 /* Use cfa+offset to represent the location of arguments passed
11350 on the stack when drap is used to align stack.
11351 Only do this when not optimizing, for optimized code var-tracking
11352 is supposed to track where the arguments live and the register
11353 used as vdrap or drap in some spot might be used for something
11354 else in other part of the routine. */
11355 return new_loc_descr (DW_OP_fbreg, offset, 0);
11356 }
11357
11358 if (regno <= 31)
11359 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11360 offset, 0);
11361 else
11362 result = new_loc_descr (DW_OP_bregx, regno, offset);
11363
11364 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11365 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11366
11367 return result;
11368 }
11369
11370 /* Return true if this RTL expression describes a base+offset calculation. */
11371
11372 static inline int
11373 is_based_loc (const_rtx rtl)
11374 {
11375 return (GET_CODE (rtl) == PLUS
11376 && ((REG_P (XEXP (rtl, 0))
11377 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11378 && CONST_INT_P (XEXP (rtl, 1)))));
11379 }
11380
11381 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11382 failed. */
11383
11384 static dw_loc_descr_ref
11385 tls_mem_loc_descriptor (rtx mem)
11386 {
11387 tree base;
11388 dw_loc_descr_ref loc_result;
11389
11390 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11391 return NULL;
11392
11393 base = get_base_address (MEM_EXPR (mem));
11394 if (base == NULL
11395 || TREE_CODE (base) != VAR_DECL
11396 || !DECL_THREAD_LOCAL_P (base))
11397 return NULL;
11398
11399 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11400 if (loc_result == NULL)
11401 return NULL;
11402
11403 if (MEM_OFFSET (mem))
11404 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11405
11406 return loc_result;
11407 }
11408
11409 /* Output debug info about reason why we failed to expand expression as dwarf
11410 expression. */
11411
11412 static void
11413 expansion_failed (tree expr, rtx rtl, char const *reason)
11414 {
11415 if (dump_file && (dump_flags & TDF_DETAILS))
11416 {
11417 fprintf (dump_file, "Failed to expand as dwarf: ");
11418 if (expr)
11419 print_generic_expr (dump_file, expr, dump_flags);
11420 if (rtl)
11421 {
11422 fprintf (dump_file, "\n");
11423 print_rtl (dump_file, rtl);
11424 }
11425 fprintf (dump_file, "\nReason: %s\n", reason);
11426 }
11427 }
11428
11429 /* Helper function for const_ok_for_output, called either directly
11430 or via for_each_rtx. */
11431
11432 static int
11433 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11434 {
11435 rtx rtl = *rtlp;
11436
11437 if (GET_CODE (rtl) == UNSPEC)
11438 {
11439 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11440 we can't express it in the debug info. */
11441 #ifdef ENABLE_CHECKING
11442 /* Don't complain about TLS UNSPECs, those are just too hard to
11443 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11444 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11445 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11446 if (XVECLEN (rtl, 0) == 0
11447 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11448 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11449 inform (current_function_decl
11450 ? DECL_SOURCE_LOCATION (current_function_decl)
11451 : UNKNOWN_LOCATION,
11452 #if NUM_UNSPEC_VALUES > 0
11453 "non-delegitimized UNSPEC %s (%d) found in variable location",
11454 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11455 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11456 XINT (rtl, 1));
11457 #else
11458 "non-delegitimized UNSPEC %d found in variable location",
11459 XINT (rtl, 1));
11460 #endif
11461 #endif
11462 expansion_failed (NULL_TREE, rtl,
11463 "UNSPEC hasn't been delegitimized.\n");
11464 return 1;
11465 }
11466
11467 if (targetm.const_not_ok_for_debug_p (rtl))
11468 {
11469 expansion_failed (NULL_TREE, rtl,
11470 "Expression rejected for debug by the backend.\n");
11471 return 1;
11472 }
11473
11474 /* FIXME: Refer to PR60655. It is possible for simplification
11475 of rtl expressions in var tracking to produce such expressions.
11476 We should really identify / validate expressions
11477 enclosed in CONST that can be handled by assemblers on various
11478 targets and only handle legitimate cases here. */
11479 if (GET_CODE (rtl) != SYMBOL_REF)
11480 {
11481 if (GET_CODE (rtl) == NOT)
11482 return 1;
11483
11484 return 0;
11485 }
11486
11487 if (CONSTANT_POOL_ADDRESS_P (rtl))
11488 {
11489 bool marked;
11490 get_pool_constant_mark (rtl, &marked);
11491 /* If all references to this pool constant were optimized away,
11492 it was not output and thus we can't represent it. */
11493 if (!marked)
11494 {
11495 expansion_failed (NULL_TREE, rtl,
11496 "Constant was removed from constant pool.\n");
11497 return 1;
11498 }
11499 }
11500
11501 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11502 return 1;
11503
11504 /* Avoid references to external symbols in debug info, on several targets
11505 the linker might even refuse to link when linking a shared library,
11506 and in many other cases the relocations for .debug_info/.debug_loc are
11507 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11508 to be defined within the same shared library or executable are fine. */
11509 if (SYMBOL_REF_EXTERNAL_P (rtl))
11510 {
11511 tree decl = SYMBOL_REF_DECL (rtl);
11512
11513 if (decl == NULL || !targetm.binds_local_p (decl))
11514 {
11515 expansion_failed (NULL_TREE, rtl,
11516 "Symbol not defined in current TU.\n");
11517 return 1;
11518 }
11519 }
11520
11521 return 0;
11522 }
11523
11524 /* Return true if constant RTL can be emitted in DW_OP_addr or
11525 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11526 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11527
11528 static bool
11529 const_ok_for_output (rtx rtl)
11530 {
11531 if (GET_CODE (rtl) == SYMBOL_REF)
11532 return const_ok_for_output_1 (&rtl, NULL) == 0;
11533
11534 if (GET_CODE (rtl) == CONST)
11535 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11536
11537 return true;
11538 }
11539
11540 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11541 if possible, NULL otherwise. */
11542
11543 static dw_die_ref
11544 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11545 {
11546 dw_die_ref type_die;
11547 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11548
11549 if (type == NULL)
11550 return NULL;
11551 switch (TREE_CODE (type))
11552 {
11553 case INTEGER_TYPE:
11554 case REAL_TYPE:
11555 break;
11556 default:
11557 return NULL;
11558 }
11559 type_die = lookup_type_die (type);
11560 if (!type_die)
11561 type_die = modified_type_die (type, false, false, comp_unit_die ());
11562 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11563 return NULL;
11564 return type_die;
11565 }
11566
11567 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11568 type matching MODE, or, if MODE is narrower than or as wide as
11569 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11570 possible. */
11571
11572 static dw_loc_descr_ref
11573 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11574 {
11575 enum machine_mode outer_mode = mode;
11576 dw_die_ref type_die;
11577 dw_loc_descr_ref cvt;
11578
11579 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11580 {
11581 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11582 return op;
11583 }
11584 type_die = base_type_for_mode (outer_mode, 1);
11585 if (type_die == NULL)
11586 return NULL;
11587 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11588 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11589 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11590 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11591 add_loc_descr (&op, cvt);
11592 return op;
11593 }
11594
11595 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11596
11597 static dw_loc_descr_ref
11598 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11599 dw_loc_descr_ref op1)
11600 {
11601 dw_loc_descr_ref ret = op0;
11602 add_loc_descr (&ret, op1);
11603 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11604 if (STORE_FLAG_VALUE != 1)
11605 {
11606 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11607 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11608 }
11609 return ret;
11610 }
11611
11612 /* Return location descriptor for signed comparison OP RTL. */
11613
11614 static dw_loc_descr_ref
11615 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11616 enum machine_mode mem_mode)
11617 {
11618 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11619 dw_loc_descr_ref op0, op1;
11620 int shift;
11621
11622 if (op_mode == VOIDmode)
11623 op_mode = GET_MODE (XEXP (rtl, 1));
11624 if (op_mode == VOIDmode)
11625 return NULL;
11626
11627 if (dwarf_strict
11628 && (GET_MODE_CLASS (op_mode) != MODE_INT
11629 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11630 return NULL;
11631
11632 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11633 VAR_INIT_STATUS_INITIALIZED);
11634 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11635 VAR_INIT_STATUS_INITIALIZED);
11636
11637 if (op0 == NULL || op1 == NULL)
11638 return NULL;
11639
11640 if (GET_MODE_CLASS (op_mode) != MODE_INT
11641 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11642 return compare_loc_descriptor (op, op0, op1);
11643
11644 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11645 {
11646 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11647 dw_loc_descr_ref cvt;
11648
11649 if (type_die == NULL)
11650 return NULL;
11651 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11652 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11653 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11654 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11655 add_loc_descr (&op0, cvt);
11656 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11657 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11658 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11659 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11660 add_loc_descr (&op1, cvt);
11661 return compare_loc_descriptor (op, op0, op1);
11662 }
11663
11664 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11665 /* For eq/ne, if the operands are known to be zero-extended,
11666 there is no need to do the fancy shifting up. */
11667 if (op == DW_OP_eq || op == DW_OP_ne)
11668 {
11669 dw_loc_descr_ref last0, last1;
11670 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11671 ;
11672 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11673 ;
11674 /* deref_size zero extends, and for constants we can check
11675 whether they are zero extended or not. */
11676 if (((last0->dw_loc_opc == DW_OP_deref_size
11677 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11678 || (CONST_INT_P (XEXP (rtl, 0))
11679 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11680 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11681 && ((last1->dw_loc_opc == DW_OP_deref_size
11682 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11683 || (CONST_INT_P (XEXP (rtl, 1))
11684 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11685 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11686 return compare_loc_descriptor (op, op0, op1);
11687
11688 /* EQ/NE comparison against constant in narrower type than
11689 DWARF2_ADDR_SIZE can be performed either as
11690 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11691 DW_OP_{eq,ne}
11692 or
11693 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11694 DW_OP_{eq,ne}. Pick whatever is shorter. */
11695 if (CONST_INT_P (XEXP (rtl, 1))
11696 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11697 && (size_of_int_loc_descriptor (shift) + 1
11698 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11699 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11700 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11701 & GET_MODE_MASK (op_mode))))
11702 {
11703 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11704 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11705 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11706 & GET_MODE_MASK (op_mode));
11707 return compare_loc_descriptor (op, op0, op1);
11708 }
11709 }
11710 add_loc_descr (&op0, int_loc_descriptor (shift));
11711 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11712 if (CONST_INT_P (XEXP (rtl, 1)))
11713 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11714 else
11715 {
11716 add_loc_descr (&op1, int_loc_descriptor (shift));
11717 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11718 }
11719 return compare_loc_descriptor (op, op0, op1);
11720 }
11721
11722 /* Return location descriptor for unsigned comparison OP RTL. */
11723
11724 static dw_loc_descr_ref
11725 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11726 enum machine_mode mem_mode)
11727 {
11728 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11729 dw_loc_descr_ref op0, op1;
11730
11731 if (op_mode == VOIDmode)
11732 op_mode = GET_MODE (XEXP (rtl, 1));
11733 if (op_mode == VOIDmode)
11734 return NULL;
11735 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11736 return NULL;
11737
11738 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11739 return NULL;
11740
11741 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11742 VAR_INIT_STATUS_INITIALIZED);
11743 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11744 VAR_INIT_STATUS_INITIALIZED);
11745
11746 if (op0 == NULL || op1 == NULL)
11747 return NULL;
11748
11749 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11750 {
11751 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11752 dw_loc_descr_ref last0, last1;
11753 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11754 ;
11755 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11756 ;
11757 if (CONST_INT_P (XEXP (rtl, 0)))
11758 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11759 /* deref_size zero extends, so no need to mask it again. */
11760 else if (last0->dw_loc_opc != DW_OP_deref_size
11761 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11762 {
11763 add_loc_descr (&op0, int_loc_descriptor (mask));
11764 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11765 }
11766 if (CONST_INT_P (XEXP (rtl, 1)))
11767 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11768 /* deref_size zero extends, so no need to mask it again. */
11769 else if (last1->dw_loc_opc != DW_OP_deref_size
11770 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11771 {
11772 add_loc_descr (&op1, int_loc_descriptor (mask));
11773 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11774 }
11775 }
11776 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11777 {
11778 HOST_WIDE_INT bias = 1;
11779 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11780 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11781 if (CONST_INT_P (XEXP (rtl, 1)))
11782 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11783 + INTVAL (XEXP (rtl, 1)));
11784 else
11785 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11786 bias, 0));
11787 }
11788 return compare_loc_descriptor (op, op0, op1);
11789 }
11790
11791 /* Return location descriptor for {U,S}{MIN,MAX}. */
11792
11793 static dw_loc_descr_ref
11794 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11795 enum machine_mode mem_mode)
11796 {
11797 enum dwarf_location_atom op;
11798 dw_loc_descr_ref op0, op1, ret;
11799 dw_loc_descr_ref bra_node, drop_node;
11800
11801 if (dwarf_strict
11802 && (GET_MODE_CLASS (mode) != MODE_INT
11803 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11804 return NULL;
11805
11806 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11807 VAR_INIT_STATUS_INITIALIZED);
11808 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11809 VAR_INIT_STATUS_INITIALIZED);
11810
11811 if (op0 == NULL || op1 == NULL)
11812 return NULL;
11813
11814 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11815 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11816 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11817 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11818 {
11819 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11820 {
11821 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11822 add_loc_descr (&op0, int_loc_descriptor (mask));
11823 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11824 add_loc_descr (&op1, int_loc_descriptor (mask));
11825 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11826 }
11827 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11828 {
11829 HOST_WIDE_INT bias = 1;
11830 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11831 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11832 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11833 }
11834 }
11835 else if (GET_MODE_CLASS (mode) == MODE_INT
11836 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11837 {
11838 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11839 add_loc_descr (&op0, int_loc_descriptor (shift));
11840 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11841 add_loc_descr (&op1, int_loc_descriptor (shift));
11842 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11843 }
11844 else if (GET_MODE_CLASS (mode) == MODE_INT
11845 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11846 {
11847 dw_die_ref type_die = base_type_for_mode (mode, 0);
11848 dw_loc_descr_ref cvt;
11849 if (type_die == NULL)
11850 return NULL;
11851 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11852 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11853 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11854 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11855 add_loc_descr (&op0, cvt);
11856 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11857 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11858 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11859 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11860 add_loc_descr (&op1, cvt);
11861 }
11862
11863 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11864 op = DW_OP_lt;
11865 else
11866 op = DW_OP_gt;
11867 ret = op0;
11868 add_loc_descr (&ret, op1);
11869 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11870 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11871 add_loc_descr (&ret, bra_node);
11872 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11873 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11874 add_loc_descr (&ret, drop_node);
11875 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11876 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11877 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11878 && GET_MODE_CLASS (mode) == MODE_INT
11879 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11880 ret = convert_descriptor_to_mode (mode, ret);
11881 return ret;
11882 }
11883
11884 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11885 but after converting arguments to type_die, afterwards
11886 convert back to unsigned. */
11887
11888 static dw_loc_descr_ref
11889 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11890 enum machine_mode mode, enum machine_mode mem_mode)
11891 {
11892 dw_loc_descr_ref cvt, op0, op1;
11893
11894 if (type_die == NULL)
11895 return NULL;
11896 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11897 VAR_INIT_STATUS_INITIALIZED);
11898 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11899 VAR_INIT_STATUS_INITIALIZED);
11900 if (op0 == NULL || op1 == NULL)
11901 return NULL;
11902 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11903 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11904 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11905 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11906 add_loc_descr (&op0, cvt);
11907 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11908 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11909 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11910 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11911 add_loc_descr (&op1, cvt);
11912 add_loc_descr (&op0, op1);
11913 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11914 return convert_descriptor_to_mode (mode, op0);
11915 }
11916
11917 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11918 const0 is DW_OP_lit0 or corresponding typed constant,
11919 const1 is DW_OP_lit1 or corresponding typed constant
11920 and constMSB is constant with just the MSB bit set
11921 for the mode):
11922 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11923 L1: const0 DW_OP_swap
11924 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11925 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11926 L3: DW_OP_drop
11927 L4: DW_OP_nop
11928
11929 CTZ is similar:
11930 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11931 L1: const0 DW_OP_swap
11932 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11933 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11934 L3: DW_OP_drop
11935 L4: DW_OP_nop
11936
11937 FFS is similar:
11938 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11939 L1: const1 DW_OP_swap
11940 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11941 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11942 L3: DW_OP_drop
11943 L4: DW_OP_nop */
11944
11945 static dw_loc_descr_ref
11946 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11947 enum machine_mode mem_mode)
11948 {
11949 dw_loc_descr_ref op0, ret, tmp;
11950 HOST_WIDE_INT valv;
11951 dw_loc_descr_ref l1jump, l1label;
11952 dw_loc_descr_ref l2jump, l2label;
11953 dw_loc_descr_ref l3jump, l3label;
11954 dw_loc_descr_ref l4jump, l4label;
11955 rtx msb;
11956
11957 if (GET_MODE_CLASS (mode) != MODE_INT
11958 || GET_MODE (XEXP (rtl, 0)) != mode)
11959 return NULL;
11960
11961 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11962 VAR_INIT_STATUS_INITIALIZED);
11963 if (op0 == NULL)
11964 return NULL;
11965 ret = op0;
11966 if (GET_CODE (rtl) == CLZ)
11967 {
11968 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11969 valv = GET_MODE_BITSIZE (mode);
11970 }
11971 else if (GET_CODE (rtl) == FFS)
11972 valv = 0;
11973 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11974 valv = GET_MODE_BITSIZE (mode);
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11976 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11977 add_loc_descr (&ret, l1jump);
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11979 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11980 VAR_INIT_STATUS_INITIALIZED);
11981 if (tmp == NULL)
11982 return NULL;
11983 add_loc_descr (&ret, tmp);
11984 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11985 add_loc_descr (&ret, l4jump);
11986 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11987 ? const1_rtx : const0_rtx,
11988 mode, mem_mode,
11989 VAR_INIT_STATUS_INITIALIZED);
11990 if (l1label == NULL)
11991 return NULL;
11992 add_loc_descr (&ret, l1label);
11993 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11994 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11995 add_loc_descr (&ret, l2label);
11996 if (GET_CODE (rtl) != CLZ)
11997 msb = const1_rtx;
11998 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11999 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12000 << (GET_MODE_BITSIZE (mode) - 1));
12001 else
12002 msb = immed_wide_int_const
12003 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12004 GET_MODE_PRECISION (mode)), mode);
12005 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12006 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12007 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12008 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12009 else
12010 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12011 VAR_INIT_STATUS_INITIALIZED);
12012 if (tmp == NULL)
12013 return NULL;
12014 add_loc_descr (&ret, tmp);
12015 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12016 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12017 add_loc_descr (&ret, l3jump);
12018 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12019 VAR_INIT_STATUS_INITIALIZED);
12020 if (tmp == NULL)
12021 return NULL;
12022 add_loc_descr (&ret, tmp);
12023 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12024 ? DW_OP_shl : DW_OP_shr, 0, 0));
12025 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12026 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12027 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12028 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12029 add_loc_descr (&ret, l2jump);
12030 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12031 add_loc_descr (&ret, l3label);
12032 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12033 add_loc_descr (&ret, l4label);
12034 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12035 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12036 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12037 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12038 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12039 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12040 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12041 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12042 return ret;
12043 }
12044
12045 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12046 const1 is DW_OP_lit1 or corresponding typed constant):
12047 const0 DW_OP_swap
12048 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12049 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12050 L2: DW_OP_drop
12051
12052 PARITY is similar:
12053 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12054 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12055 L2: DW_OP_drop */
12056
12057 static dw_loc_descr_ref
12058 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12059 enum machine_mode mem_mode)
12060 {
12061 dw_loc_descr_ref op0, ret, tmp;
12062 dw_loc_descr_ref l1jump, l1label;
12063 dw_loc_descr_ref l2jump, l2label;
12064
12065 if (GET_MODE_CLASS (mode) != MODE_INT
12066 || GET_MODE (XEXP (rtl, 0)) != mode)
12067 return NULL;
12068
12069 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12070 VAR_INIT_STATUS_INITIALIZED);
12071 if (op0 == NULL)
12072 return NULL;
12073 ret = op0;
12074 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12075 VAR_INIT_STATUS_INITIALIZED);
12076 if (tmp == NULL)
12077 return NULL;
12078 add_loc_descr (&ret, tmp);
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12080 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12081 add_loc_descr (&ret, l1label);
12082 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12083 add_loc_descr (&ret, l2jump);
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12086 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12087 VAR_INIT_STATUS_INITIALIZED);
12088 if (tmp == NULL)
12089 return NULL;
12090 add_loc_descr (&ret, tmp);
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12092 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12093 ? DW_OP_plus : DW_OP_xor, 0, 0));
12094 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12095 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12096 VAR_INIT_STATUS_INITIALIZED);
12097 add_loc_descr (&ret, tmp);
12098 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12099 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12100 add_loc_descr (&ret, l1jump);
12101 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12102 add_loc_descr (&ret, l2label);
12103 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12104 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12105 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12106 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12107 return ret;
12108 }
12109
12110 /* BSWAP (constS is initial shift count, either 56 or 24):
12111 constS const0
12112 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12113 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12114 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12115 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12116 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12117
12118 static dw_loc_descr_ref
12119 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12120 enum machine_mode mem_mode)
12121 {
12122 dw_loc_descr_ref op0, ret, tmp;
12123 dw_loc_descr_ref l1jump, l1label;
12124 dw_loc_descr_ref l2jump, l2label;
12125
12126 if (GET_MODE_CLASS (mode) != MODE_INT
12127 || BITS_PER_UNIT != 8
12128 || (GET_MODE_BITSIZE (mode) != 32
12129 && GET_MODE_BITSIZE (mode) != 64))
12130 return NULL;
12131
12132 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 if (op0 == NULL)
12135 return NULL;
12136
12137 ret = op0;
12138 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12139 mode, mem_mode,
12140 VAR_INIT_STATUS_INITIALIZED);
12141 if (tmp == NULL)
12142 return NULL;
12143 add_loc_descr (&ret, tmp);
12144 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12145 VAR_INIT_STATUS_INITIALIZED);
12146 if (tmp == NULL)
12147 return NULL;
12148 add_loc_descr (&ret, tmp);
12149 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12150 add_loc_descr (&ret, l1label);
12151 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12152 mode, mem_mode,
12153 VAR_INIT_STATUS_INITIALIZED);
12154 add_loc_descr (&ret, tmp);
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12157 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12158 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12159 VAR_INIT_STATUS_INITIALIZED);
12160 if (tmp == NULL)
12161 return NULL;
12162 add_loc_descr (&ret, tmp);
12163 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12164 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12165 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12166 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12167 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12168 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12169 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12170 VAR_INIT_STATUS_INITIALIZED);
12171 add_loc_descr (&ret, tmp);
12172 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12173 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12174 add_loc_descr (&ret, l2jump);
12175 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12176 VAR_INIT_STATUS_INITIALIZED);
12177 add_loc_descr (&ret, tmp);
12178 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12179 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12180 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12181 add_loc_descr (&ret, l1jump);
12182 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12183 add_loc_descr (&ret, l2label);
12184 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12185 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12186 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12187 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12188 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12189 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12190 return ret;
12191 }
12192
12193 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12194 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12195 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12196 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12197
12198 ROTATERT is similar:
12199 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12200 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12201 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12202
12203 static dw_loc_descr_ref
12204 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12205 enum machine_mode mem_mode)
12206 {
12207 rtx rtlop1 = XEXP (rtl, 1);
12208 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12209 int i;
12210
12211 if (GET_MODE_CLASS (mode) != MODE_INT)
12212 return NULL;
12213
12214 if (GET_MODE (rtlop1) != VOIDmode
12215 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12216 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12217 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12218 VAR_INIT_STATUS_INITIALIZED);
12219 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12220 VAR_INIT_STATUS_INITIALIZED);
12221 if (op0 == NULL || op1 == NULL)
12222 return NULL;
12223 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12224 for (i = 0; i < 2; i++)
12225 {
12226 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12227 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12228 mode, mem_mode,
12229 VAR_INIT_STATUS_INITIALIZED);
12230 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12231 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12232 ? DW_OP_const4u
12233 : HOST_BITS_PER_WIDE_INT == 64
12234 ? DW_OP_const8u : DW_OP_constu,
12235 GET_MODE_MASK (mode), 0);
12236 else
12237 mask[i] = NULL;
12238 if (mask[i] == NULL)
12239 return NULL;
12240 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12241 }
12242 ret = op0;
12243 add_loc_descr (&ret, op1);
12244 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12246 if (GET_CODE (rtl) == ROTATERT)
12247 {
12248 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12249 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12250 GET_MODE_BITSIZE (mode), 0));
12251 }
12252 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12253 if (mask[0] != NULL)
12254 add_loc_descr (&ret, mask[0]);
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12256 if (mask[1] != NULL)
12257 {
12258 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12259 add_loc_descr (&ret, mask[1]);
12260 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12261 }
12262 if (GET_CODE (rtl) == ROTATE)
12263 {
12264 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12265 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12266 GET_MODE_BITSIZE (mode), 0));
12267 }
12268 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12269 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12270 return ret;
12271 }
12272
12273 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12274 for DEBUG_PARAMETER_REF RTL. */
12275
12276 static dw_loc_descr_ref
12277 parameter_ref_descriptor (rtx rtl)
12278 {
12279 dw_loc_descr_ref ret;
12280 dw_die_ref ref;
12281
12282 if (dwarf_strict)
12283 return NULL;
12284 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12285 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12286 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12287 if (ref)
12288 {
12289 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12290 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12291 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12292 }
12293 else
12294 {
12295 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12296 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12297 }
12298 return ret;
12299 }
12300
12301 /* The following routine converts the RTL for a variable or parameter
12302 (resident in memory) into an equivalent Dwarf representation of a
12303 mechanism for getting the address of that same variable onto the top of a
12304 hypothetical "address evaluation" stack.
12305
12306 When creating memory location descriptors, we are effectively transforming
12307 the RTL for a memory-resident object into its Dwarf postfix expression
12308 equivalent. This routine recursively descends an RTL tree, turning
12309 it into Dwarf postfix code as it goes.
12310
12311 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12312
12313 MEM_MODE is the mode of the memory reference, needed to handle some
12314 autoincrement addressing modes.
12315
12316 Return 0 if we can't represent the location. */
12317
12318 dw_loc_descr_ref
12319 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12320 enum machine_mode mem_mode,
12321 enum var_init_status initialized)
12322 {
12323 dw_loc_descr_ref mem_loc_result = NULL;
12324 enum dwarf_location_atom op;
12325 dw_loc_descr_ref op0, op1;
12326 rtx inner = NULL_RTX;
12327
12328 if (mode == VOIDmode)
12329 mode = GET_MODE (rtl);
12330
12331 /* Note that for a dynamically sized array, the location we will generate a
12332 description of here will be the lowest numbered location which is
12333 actually within the array. That's *not* necessarily the same as the
12334 zeroth element of the array. */
12335
12336 rtl = targetm.delegitimize_address (rtl);
12337
12338 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12339 return NULL;
12340
12341 switch (GET_CODE (rtl))
12342 {
12343 case POST_INC:
12344 case POST_DEC:
12345 case POST_MODIFY:
12346 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12347
12348 case SUBREG:
12349 /* The case of a subreg may arise when we have a local (register)
12350 variable or a formal (register) parameter which doesn't quite fill
12351 up an entire register. For now, just assume that it is
12352 legitimate to make the Dwarf info refer to the whole register which
12353 contains the given subreg. */
12354 if (!subreg_lowpart_p (rtl))
12355 break;
12356 inner = SUBREG_REG (rtl);
12357 case TRUNCATE:
12358 if (inner == NULL_RTX)
12359 inner = XEXP (rtl, 0);
12360 if (GET_MODE_CLASS (mode) == MODE_INT
12361 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12362 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12363 #ifdef POINTERS_EXTEND_UNSIGNED
12364 || (mode == Pmode && mem_mode != VOIDmode)
12365 #endif
12366 )
12367 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12368 {
12369 mem_loc_result = mem_loc_descriptor (inner,
12370 GET_MODE (inner),
12371 mem_mode, initialized);
12372 break;
12373 }
12374 if (dwarf_strict)
12375 break;
12376 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12377 break;
12378 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12379 && (GET_MODE_CLASS (mode) != MODE_INT
12380 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12381 break;
12382 else
12383 {
12384 dw_die_ref type_die;
12385 dw_loc_descr_ref cvt;
12386
12387 mem_loc_result = mem_loc_descriptor (inner,
12388 GET_MODE (inner),
12389 mem_mode, initialized);
12390 if (mem_loc_result == NULL)
12391 break;
12392 type_die = base_type_for_mode (mode,
12393 GET_MODE_CLASS (mode) == MODE_INT);
12394 if (type_die == NULL)
12395 {
12396 mem_loc_result = NULL;
12397 break;
12398 }
12399 if (GET_MODE_SIZE (mode)
12400 != GET_MODE_SIZE (GET_MODE (inner)))
12401 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12402 else
12403 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12404 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12405 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12406 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12407 add_loc_descr (&mem_loc_result, cvt);
12408 }
12409 break;
12410
12411 case REG:
12412 if (GET_MODE_CLASS (mode) != MODE_INT
12413 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12414 && rtl != arg_pointer_rtx
12415 && rtl != frame_pointer_rtx
12416 #ifdef POINTERS_EXTEND_UNSIGNED
12417 && (mode != Pmode || mem_mode == VOIDmode)
12418 #endif
12419 ))
12420 {
12421 dw_die_ref type_die;
12422 unsigned int dbx_regnum;
12423
12424 if (dwarf_strict)
12425 break;
12426 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12427 break;
12428 type_die = base_type_for_mode (mode,
12429 GET_MODE_CLASS (mode) == MODE_INT);
12430 if (type_die == NULL)
12431 break;
12432
12433 dbx_regnum = dbx_reg_number (rtl);
12434 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12435 break;
12436 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12437 dbx_regnum, 0);
12438 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12439 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12440 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12441 break;
12442 }
12443 /* Whenever a register number forms a part of the description of the
12444 method for calculating the (dynamic) address of a memory resident
12445 object, DWARF rules require the register number be referred to as
12446 a "base register". This distinction is not based in any way upon
12447 what category of register the hardware believes the given register
12448 belongs to. This is strictly DWARF terminology we're dealing with
12449 here. Note that in cases where the location of a memory-resident
12450 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12451 OP_CONST (0)) the actual DWARF location descriptor that we generate
12452 may just be OP_BASEREG (basereg). This may look deceptively like
12453 the object in question was allocated to a register (rather than in
12454 memory) so DWARF consumers need to be aware of the subtle
12455 distinction between OP_REG and OP_BASEREG. */
12456 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12457 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12458 else if (stack_realign_drap
12459 && crtl->drap_reg
12460 && crtl->args.internal_arg_pointer == rtl
12461 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12462 {
12463 /* If RTL is internal_arg_pointer, which has been optimized
12464 out, use DRAP instead. */
12465 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12466 VAR_INIT_STATUS_INITIALIZED);
12467 }
12468 break;
12469
12470 case SIGN_EXTEND:
12471 case ZERO_EXTEND:
12472 if (GET_MODE_CLASS (mode) != MODE_INT)
12473 break;
12474 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12475 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12476 if (op0 == 0)
12477 break;
12478 else if (GET_CODE (rtl) == ZERO_EXTEND
12479 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12480 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12481 < HOST_BITS_PER_WIDE_INT
12482 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12483 to expand zero extend as two shifts instead of
12484 masking. */
12485 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12486 {
12487 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12488 mem_loc_result = op0;
12489 add_loc_descr (&mem_loc_result,
12490 int_loc_descriptor (GET_MODE_MASK (imode)));
12491 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12492 }
12493 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12494 {
12495 int shift = DWARF2_ADDR_SIZE
12496 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12497 shift *= BITS_PER_UNIT;
12498 if (GET_CODE (rtl) == SIGN_EXTEND)
12499 op = DW_OP_shra;
12500 else
12501 op = DW_OP_shr;
12502 mem_loc_result = op0;
12503 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12504 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12505 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12506 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12507 }
12508 else if (!dwarf_strict)
12509 {
12510 dw_die_ref type_die1, type_die2;
12511 dw_loc_descr_ref cvt;
12512
12513 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12514 GET_CODE (rtl) == ZERO_EXTEND);
12515 if (type_die1 == NULL)
12516 break;
12517 type_die2 = base_type_for_mode (mode, 1);
12518 if (type_die2 == NULL)
12519 break;
12520 mem_loc_result = op0;
12521 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12522 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12523 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12524 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12525 add_loc_descr (&mem_loc_result, cvt);
12526 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12527 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12528 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12529 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12530 add_loc_descr (&mem_loc_result, cvt);
12531 }
12532 break;
12533
12534 case MEM:
12535 {
12536 rtx new_rtl = avoid_constant_pool_reference (rtl);
12537 if (new_rtl != rtl)
12538 {
12539 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12540 initialized);
12541 if (mem_loc_result != NULL)
12542 return mem_loc_result;
12543 }
12544 }
12545 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12546 get_address_mode (rtl), mode,
12547 VAR_INIT_STATUS_INITIALIZED);
12548 if (mem_loc_result == NULL)
12549 mem_loc_result = tls_mem_loc_descriptor (rtl);
12550 if (mem_loc_result != NULL)
12551 {
12552 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12553 || GET_MODE_CLASS (mode) != MODE_INT)
12554 {
12555 dw_die_ref type_die;
12556 dw_loc_descr_ref deref;
12557
12558 if (dwarf_strict)
12559 return NULL;
12560 type_die
12561 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12562 if (type_die == NULL)
12563 return NULL;
12564 deref = new_loc_descr (DW_OP_GNU_deref_type,
12565 GET_MODE_SIZE (mode), 0);
12566 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12567 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12568 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12569 add_loc_descr (&mem_loc_result, deref);
12570 }
12571 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12572 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12573 else
12574 add_loc_descr (&mem_loc_result,
12575 new_loc_descr (DW_OP_deref_size,
12576 GET_MODE_SIZE (mode), 0));
12577 }
12578 break;
12579
12580 case LO_SUM:
12581 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12582
12583 case LABEL_REF:
12584 /* Some ports can transform a symbol ref into a label ref, because
12585 the symbol ref is too far away and has to be dumped into a constant
12586 pool. */
12587 case CONST:
12588 case SYMBOL_REF:
12589 if (GET_MODE_CLASS (mode) != MODE_INT
12590 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12591 #ifdef POINTERS_EXTEND_UNSIGNED
12592 && (mode != Pmode || mem_mode == VOIDmode)
12593 #endif
12594 ))
12595 break;
12596 if (GET_CODE (rtl) == SYMBOL_REF
12597 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12598 {
12599 dw_loc_descr_ref temp;
12600
12601 /* If this is not defined, we have no way to emit the data. */
12602 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12603 break;
12604
12605 temp = new_addr_loc_descr (rtl, dtprel_true);
12606
12607 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12608 add_loc_descr (&mem_loc_result, temp);
12609
12610 break;
12611 }
12612
12613 if (!const_ok_for_output (rtl))
12614 break;
12615
12616 symref:
12617 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12618 vec_safe_push (used_rtx_array, rtl);
12619 break;
12620
12621 case CONCAT:
12622 case CONCATN:
12623 case VAR_LOCATION:
12624 case DEBUG_IMPLICIT_PTR:
12625 expansion_failed (NULL_TREE, rtl,
12626 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12627 return 0;
12628
12629 case ENTRY_VALUE:
12630 if (dwarf_strict)
12631 return NULL;
12632 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12633 {
12634 if (GET_MODE_CLASS (mode) != MODE_INT
12635 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12636 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12637 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12638 else
12639 {
12640 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12641 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12642 return NULL;
12643 op0 = one_reg_loc_descriptor (dbx_regnum,
12644 VAR_INIT_STATUS_INITIALIZED);
12645 }
12646 }
12647 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12648 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12649 {
12650 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12651 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12652 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12653 return NULL;
12654 }
12655 else
12656 gcc_unreachable ();
12657 if (op0 == NULL)
12658 return NULL;
12659 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12660 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12661 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12662 break;
12663
12664 case DEBUG_PARAMETER_REF:
12665 mem_loc_result = parameter_ref_descriptor (rtl);
12666 break;
12667
12668 case PRE_MODIFY:
12669 /* Extract the PLUS expression nested inside and fall into
12670 PLUS code below. */
12671 rtl = XEXP (rtl, 1);
12672 goto plus;
12673
12674 case PRE_INC:
12675 case PRE_DEC:
12676 /* Turn these into a PLUS expression and fall into the PLUS code
12677 below. */
12678 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12679 gen_int_mode (GET_CODE (rtl) == PRE_INC
12680 ? GET_MODE_UNIT_SIZE (mem_mode)
12681 : -GET_MODE_UNIT_SIZE (mem_mode),
12682 mode));
12683
12684 /* ... fall through ... */
12685
12686 case PLUS:
12687 plus:
12688 if (is_based_loc (rtl)
12689 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12690 || XEXP (rtl, 0) == arg_pointer_rtx
12691 || XEXP (rtl, 0) == frame_pointer_rtx)
12692 && GET_MODE_CLASS (mode) == MODE_INT)
12693 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12694 INTVAL (XEXP (rtl, 1)),
12695 VAR_INIT_STATUS_INITIALIZED);
12696 else
12697 {
12698 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12699 VAR_INIT_STATUS_INITIALIZED);
12700 if (mem_loc_result == 0)
12701 break;
12702
12703 if (CONST_INT_P (XEXP (rtl, 1))
12704 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12705 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12706 else
12707 {
12708 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12709 VAR_INIT_STATUS_INITIALIZED);
12710 if (op1 == 0)
12711 break;
12712 add_loc_descr (&mem_loc_result, op1);
12713 add_loc_descr (&mem_loc_result,
12714 new_loc_descr (DW_OP_plus, 0, 0));
12715 }
12716 }
12717 break;
12718
12719 /* If a pseudo-reg is optimized away, it is possible for it to
12720 be replaced with a MEM containing a multiply or shift. */
12721 case MINUS:
12722 op = DW_OP_minus;
12723 goto do_binop;
12724
12725 case MULT:
12726 op = DW_OP_mul;
12727 goto do_binop;
12728
12729 case DIV:
12730 if (!dwarf_strict
12731 && GET_MODE_CLASS (mode) == MODE_INT
12732 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12733 {
12734 mem_loc_result = typed_binop (DW_OP_div, rtl,
12735 base_type_for_mode (mode, 0),
12736 mode, mem_mode);
12737 break;
12738 }
12739 op = DW_OP_div;
12740 goto do_binop;
12741
12742 case UMOD:
12743 op = DW_OP_mod;
12744 goto do_binop;
12745
12746 case ASHIFT:
12747 op = DW_OP_shl;
12748 goto do_shift;
12749
12750 case ASHIFTRT:
12751 op = DW_OP_shra;
12752 goto do_shift;
12753
12754 case LSHIFTRT:
12755 op = DW_OP_shr;
12756 goto do_shift;
12757
12758 do_shift:
12759 if (GET_MODE_CLASS (mode) != MODE_INT)
12760 break;
12761 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12762 VAR_INIT_STATUS_INITIALIZED);
12763 {
12764 rtx rtlop1 = XEXP (rtl, 1);
12765 if (GET_MODE (rtlop1) != VOIDmode
12766 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12767 < GET_MODE_BITSIZE (mode))
12768 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12769 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12770 VAR_INIT_STATUS_INITIALIZED);
12771 }
12772
12773 if (op0 == 0 || op1 == 0)
12774 break;
12775
12776 mem_loc_result = op0;
12777 add_loc_descr (&mem_loc_result, op1);
12778 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12779 break;
12780
12781 case AND:
12782 op = DW_OP_and;
12783 goto do_binop;
12784
12785 case IOR:
12786 op = DW_OP_or;
12787 goto do_binop;
12788
12789 case XOR:
12790 op = DW_OP_xor;
12791 goto do_binop;
12792
12793 do_binop:
12794 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12795 VAR_INIT_STATUS_INITIALIZED);
12796 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12797 VAR_INIT_STATUS_INITIALIZED);
12798
12799 if (op0 == 0 || op1 == 0)
12800 break;
12801
12802 mem_loc_result = op0;
12803 add_loc_descr (&mem_loc_result, op1);
12804 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12805 break;
12806
12807 case MOD:
12808 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12809 {
12810 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12811 base_type_for_mode (mode, 0),
12812 mode, mem_mode);
12813 break;
12814 }
12815
12816 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12817 VAR_INIT_STATUS_INITIALIZED);
12818 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12819 VAR_INIT_STATUS_INITIALIZED);
12820
12821 if (op0 == 0 || op1 == 0)
12822 break;
12823
12824 mem_loc_result = op0;
12825 add_loc_descr (&mem_loc_result, op1);
12826 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12827 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12828 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12829 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12830 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12831 break;
12832
12833 case UDIV:
12834 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12835 {
12836 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12837 {
12838 op = DW_OP_div;
12839 goto do_binop;
12840 }
12841 mem_loc_result = typed_binop (DW_OP_div, rtl,
12842 base_type_for_mode (mode, 1),
12843 mode, mem_mode);
12844 }
12845 break;
12846
12847 case NOT:
12848 op = DW_OP_not;
12849 goto do_unop;
12850
12851 case ABS:
12852 op = DW_OP_abs;
12853 goto do_unop;
12854
12855 case NEG:
12856 op = DW_OP_neg;
12857 goto do_unop;
12858
12859 do_unop:
12860 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12861 VAR_INIT_STATUS_INITIALIZED);
12862
12863 if (op0 == 0)
12864 break;
12865
12866 mem_loc_result = op0;
12867 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12868 break;
12869
12870 case CONST_INT:
12871 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12872 #ifdef POINTERS_EXTEND_UNSIGNED
12873 || (mode == Pmode
12874 && mem_mode != VOIDmode
12875 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12876 #endif
12877 )
12878 {
12879 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12880 break;
12881 }
12882 if (!dwarf_strict
12883 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12884 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12885 {
12886 dw_die_ref type_die = base_type_for_mode (mode, 1);
12887 enum machine_mode amode;
12888 if (type_die == NULL)
12889 return NULL;
12890 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12891 MODE_INT, 0);
12892 if (INTVAL (rtl) >= 0
12893 && amode != BLKmode
12894 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12895 /* const DW_OP_GNU_convert <XXX> vs.
12896 DW_OP_GNU_const_type <XXX, 1, const>. */
12897 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12898 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12899 {
12900 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12901 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12902 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12903 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12904 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12905 add_loc_descr (&mem_loc_result, op0);
12906 return mem_loc_result;
12907 }
12908 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12909 INTVAL (rtl));
12910 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12911 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12912 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12913 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12914 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12915 else
12916 {
12917 mem_loc_result->dw_loc_oprnd2.val_class
12918 = dw_val_class_const_double;
12919 mem_loc_result->dw_loc_oprnd2.v.val_double
12920 = double_int::from_shwi (INTVAL (rtl));
12921 }
12922 }
12923 break;
12924
12925 case CONST_DOUBLE:
12926 if (!dwarf_strict)
12927 {
12928 dw_die_ref type_die;
12929
12930 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12931 CONST_DOUBLE rtx could represent either a large integer
12932 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12933 the value is always a floating point constant.
12934
12935 When it is an integer, a CONST_DOUBLE is used whenever
12936 the constant requires 2 HWIs to be adequately represented.
12937 We output CONST_DOUBLEs as blocks. */
12938 if (mode == VOIDmode
12939 || (GET_MODE (rtl) == VOIDmode
12940 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12941 break;
12942 type_die = base_type_for_mode (mode,
12943 GET_MODE_CLASS (mode) == MODE_INT);
12944 if (type_die == NULL)
12945 return NULL;
12946 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12947 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12948 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12949 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12950 #if TARGET_SUPPORTS_WIDE_INT == 0
12951 if (!SCALAR_FLOAT_MODE_P (mode))
12952 {
12953 mem_loc_result->dw_loc_oprnd2.val_class
12954 = dw_val_class_const_double;
12955 mem_loc_result->dw_loc_oprnd2.v.val_double
12956 = rtx_to_double_int (rtl);
12957 }
12958 else
12959 #endif
12960 {
12961 unsigned int length = GET_MODE_SIZE (mode);
12962 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12963
12964 insert_float (rtl, array);
12965 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12966 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12967 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12968 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12969 }
12970 }
12971 break;
12972
12973 case CONST_WIDE_INT:
12974 if (!dwarf_strict)
12975 {
12976 dw_die_ref type_die;
12977
12978 type_die = base_type_for_mode (mode,
12979 GET_MODE_CLASS (mode) == MODE_INT);
12980 if (type_die == NULL)
12981 return NULL;
12982 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12983 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12984 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12985 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12986 mem_loc_result->dw_loc_oprnd2.val_class
12987 = dw_val_class_wide_int;
12988 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
12989 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12990 }
12991 break;
12992
12993 case EQ:
12994 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12995 break;
12996
12997 case GE:
12998 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12999 break;
13000
13001 case GT:
13002 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13003 break;
13004
13005 case LE:
13006 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13007 break;
13008
13009 case LT:
13010 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13011 break;
13012
13013 case NE:
13014 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13015 break;
13016
13017 case GEU:
13018 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13019 break;
13020
13021 case GTU:
13022 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13023 break;
13024
13025 case LEU:
13026 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13027 break;
13028
13029 case LTU:
13030 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13031 break;
13032
13033 case UMIN:
13034 case UMAX:
13035 if (GET_MODE_CLASS (mode) != MODE_INT)
13036 break;
13037 /* FALLTHRU */
13038 case SMIN:
13039 case SMAX:
13040 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13041 break;
13042
13043 case ZERO_EXTRACT:
13044 case SIGN_EXTRACT:
13045 if (CONST_INT_P (XEXP (rtl, 1))
13046 && CONST_INT_P (XEXP (rtl, 2))
13047 && ((unsigned) INTVAL (XEXP (rtl, 1))
13048 + (unsigned) INTVAL (XEXP (rtl, 2))
13049 <= GET_MODE_BITSIZE (mode))
13050 && GET_MODE_CLASS (mode) == MODE_INT
13051 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13052 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13053 {
13054 int shift, size;
13055 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13056 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13057 if (op0 == 0)
13058 break;
13059 if (GET_CODE (rtl) == SIGN_EXTRACT)
13060 op = DW_OP_shra;
13061 else
13062 op = DW_OP_shr;
13063 mem_loc_result = op0;
13064 size = INTVAL (XEXP (rtl, 1));
13065 shift = INTVAL (XEXP (rtl, 2));
13066 if (BITS_BIG_ENDIAN)
13067 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13068 - shift - size;
13069 if (shift + size != (int) DWARF2_ADDR_SIZE)
13070 {
13071 add_loc_descr (&mem_loc_result,
13072 int_loc_descriptor (DWARF2_ADDR_SIZE
13073 - shift - size));
13074 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13075 }
13076 if (size != (int) DWARF2_ADDR_SIZE)
13077 {
13078 add_loc_descr (&mem_loc_result,
13079 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13080 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13081 }
13082 }
13083 break;
13084
13085 case IF_THEN_ELSE:
13086 {
13087 dw_loc_descr_ref op2, bra_node, drop_node;
13088 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13089 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13090 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13091 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13092 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13093 VAR_INIT_STATUS_INITIALIZED);
13094 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13095 VAR_INIT_STATUS_INITIALIZED);
13096 if (op0 == NULL || op1 == NULL || op2 == NULL)
13097 break;
13098
13099 mem_loc_result = op1;
13100 add_loc_descr (&mem_loc_result, op2);
13101 add_loc_descr (&mem_loc_result, op0);
13102 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13103 add_loc_descr (&mem_loc_result, bra_node);
13104 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13105 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13106 add_loc_descr (&mem_loc_result, drop_node);
13107 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13108 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13109 }
13110 break;
13111
13112 case FLOAT_EXTEND:
13113 case FLOAT_TRUNCATE:
13114 case FLOAT:
13115 case UNSIGNED_FLOAT:
13116 case FIX:
13117 case UNSIGNED_FIX:
13118 if (!dwarf_strict)
13119 {
13120 dw_die_ref type_die;
13121 dw_loc_descr_ref cvt;
13122
13123 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13124 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13125 if (op0 == NULL)
13126 break;
13127 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13128 && (GET_CODE (rtl) == FLOAT
13129 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13130 <= DWARF2_ADDR_SIZE))
13131 {
13132 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13133 GET_CODE (rtl) == UNSIGNED_FLOAT);
13134 if (type_die == NULL)
13135 break;
13136 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13137 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13138 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13139 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13140 add_loc_descr (&op0, cvt);
13141 }
13142 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13143 if (type_die == NULL)
13144 break;
13145 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13146 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13147 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13148 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13149 add_loc_descr (&op0, cvt);
13150 if (GET_MODE_CLASS (mode) == MODE_INT
13151 && (GET_CODE (rtl) == FIX
13152 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13153 {
13154 op0 = convert_descriptor_to_mode (mode, op0);
13155 if (op0 == NULL)
13156 break;
13157 }
13158 mem_loc_result = op0;
13159 }
13160 break;
13161
13162 case CLZ:
13163 case CTZ:
13164 case FFS:
13165 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13166 break;
13167
13168 case POPCOUNT:
13169 case PARITY:
13170 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13171 break;
13172
13173 case BSWAP:
13174 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13175 break;
13176
13177 case ROTATE:
13178 case ROTATERT:
13179 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13180 break;
13181
13182 case COMPARE:
13183 /* In theory, we could implement the above. */
13184 /* DWARF cannot represent the unsigned compare operations
13185 natively. */
13186 case SS_MULT:
13187 case US_MULT:
13188 case SS_DIV:
13189 case US_DIV:
13190 case SS_PLUS:
13191 case US_PLUS:
13192 case SS_MINUS:
13193 case US_MINUS:
13194 case SS_NEG:
13195 case US_NEG:
13196 case SS_ABS:
13197 case SS_ASHIFT:
13198 case US_ASHIFT:
13199 case SS_TRUNCATE:
13200 case US_TRUNCATE:
13201 case UNORDERED:
13202 case ORDERED:
13203 case UNEQ:
13204 case UNGE:
13205 case UNGT:
13206 case UNLE:
13207 case UNLT:
13208 case LTGT:
13209 case FRACT_CONVERT:
13210 case UNSIGNED_FRACT_CONVERT:
13211 case SAT_FRACT:
13212 case UNSIGNED_SAT_FRACT:
13213 case SQRT:
13214 case ASM_OPERANDS:
13215 case VEC_MERGE:
13216 case VEC_SELECT:
13217 case VEC_CONCAT:
13218 case VEC_DUPLICATE:
13219 case UNSPEC:
13220 case HIGH:
13221 case FMA:
13222 case STRICT_LOW_PART:
13223 case CONST_VECTOR:
13224 case CONST_FIXED:
13225 case CLRSB:
13226 case CLOBBER:
13227 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13228 can't express it in the debug info. This can happen e.g. with some
13229 TLS UNSPECs. */
13230 break;
13231
13232 case CONST_STRING:
13233 resolve_one_addr (&rtl, NULL);
13234 goto symref;
13235
13236 default:
13237 #ifdef ENABLE_CHECKING
13238 print_rtl (stderr, rtl);
13239 gcc_unreachable ();
13240 #else
13241 break;
13242 #endif
13243 }
13244
13245 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13246 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13247
13248 return mem_loc_result;
13249 }
13250
13251 /* Return a descriptor that describes the concatenation of two locations.
13252 This is typically a complex variable. */
13253
13254 static dw_loc_descr_ref
13255 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13256 {
13257 dw_loc_descr_ref cc_loc_result = NULL;
13258 dw_loc_descr_ref x0_ref
13259 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13260 dw_loc_descr_ref x1_ref
13261 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13262
13263 if (x0_ref == 0 || x1_ref == 0)
13264 return 0;
13265
13266 cc_loc_result = x0_ref;
13267 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13268
13269 add_loc_descr (&cc_loc_result, x1_ref);
13270 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13271
13272 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13273 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13274
13275 return cc_loc_result;
13276 }
13277
13278 /* Return a descriptor that describes the concatenation of N
13279 locations. */
13280
13281 static dw_loc_descr_ref
13282 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13283 {
13284 unsigned int i;
13285 dw_loc_descr_ref cc_loc_result = NULL;
13286 unsigned int n = XVECLEN (concatn, 0);
13287
13288 for (i = 0; i < n; ++i)
13289 {
13290 dw_loc_descr_ref ref;
13291 rtx x = XVECEXP (concatn, 0, i);
13292
13293 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13294 if (ref == NULL)
13295 return NULL;
13296
13297 add_loc_descr (&cc_loc_result, ref);
13298 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13299 }
13300
13301 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13302 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13303
13304 return cc_loc_result;
13305 }
13306
13307 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13308 for DEBUG_IMPLICIT_PTR RTL. */
13309
13310 static dw_loc_descr_ref
13311 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13312 {
13313 dw_loc_descr_ref ret;
13314 dw_die_ref ref;
13315
13316 if (dwarf_strict)
13317 return NULL;
13318 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13319 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13320 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13321 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13322 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13323 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13324 if (ref)
13325 {
13326 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13327 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13328 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13329 }
13330 else
13331 {
13332 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13333 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13334 }
13335 return ret;
13336 }
13337
13338 /* Output a proper Dwarf location descriptor for a variable or parameter
13339 which is either allocated in a register or in a memory location. For a
13340 register, we just generate an OP_REG and the register number. For a
13341 memory location we provide a Dwarf postfix expression describing how to
13342 generate the (dynamic) address of the object onto the address stack.
13343
13344 MODE is mode of the decl if this loc_descriptor is going to be used in
13345 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13346 allowed, VOIDmode otherwise.
13347
13348 If we don't know how to describe it, return 0. */
13349
13350 static dw_loc_descr_ref
13351 loc_descriptor (rtx rtl, enum machine_mode mode,
13352 enum var_init_status initialized)
13353 {
13354 dw_loc_descr_ref loc_result = NULL;
13355
13356 switch (GET_CODE (rtl))
13357 {
13358 case SUBREG:
13359 /* The case of a subreg may arise when we have a local (register)
13360 variable or a formal (register) parameter which doesn't quite fill
13361 up an entire register. For now, just assume that it is
13362 legitimate to make the Dwarf info refer to the whole register which
13363 contains the given subreg. */
13364 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13365 loc_result = loc_descriptor (SUBREG_REG (rtl),
13366 GET_MODE (SUBREG_REG (rtl)), initialized);
13367 else
13368 goto do_default;
13369 break;
13370
13371 case REG:
13372 loc_result = reg_loc_descriptor (rtl, initialized);
13373 break;
13374
13375 case MEM:
13376 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13377 GET_MODE (rtl), initialized);
13378 if (loc_result == NULL)
13379 loc_result = tls_mem_loc_descriptor (rtl);
13380 if (loc_result == NULL)
13381 {
13382 rtx new_rtl = avoid_constant_pool_reference (rtl);
13383 if (new_rtl != rtl)
13384 loc_result = loc_descriptor (new_rtl, mode, initialized);
13385 }
13386 break;
13387
13388 case CONCAT:
13389 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13390 initialized);
13391 break;
13392
13393 case CONCATN:
13394 loc_result = concatn_loc_descriptor (rtl, initialized);
13395 break;
13396
13397 case VAR_LOCATION:
13398 /* Single part. */
13399 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13400 {
13401 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13402 if (GET_CODE (loc) == EXPR_LIST)
13403 loc = XEXP (loc, 0);
13404 loc_result = loc_descriptor (loc, mode, initialized);
13405 break;
13406 }
13407
13408 rtl = XEXP (rtl, 1);
13409 /* FALLTHRU */
13410
13411 case PARALLEL:
13412 {
13413 rtvec par_elems = XVEC (rtl, 0);
13414 int num_elem = GET_NUM_ELEM (par_elems);
13415 enum machine_mode mode;
13416 int i;
13417
13418 /* Create the first one, so we have something to add to. */
13419 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13420 VOIDmode, initialized);
13421 if (loc_result == NULL)
13422 return NULL;
13423 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13424 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13425 for (i = 1; i < num_elem; i++)
13426 {
13427 dw_loc_descr_ref temp;
13428
13429 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13430 VOIDmode, initialized);
13431 if (temp == NULL)
13432 return NULL;
13433 add_loc_descr (&loc_result, temp);
13434 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13435 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13436 }
13437 }
13438 break;
13439
13440 case CONST_INT:
13441 if (mode != VOIDmode && mode != BLKmode)
13442 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13443 INTVAL (rtl));
13444 break;
13445
13446 case CONST_DOUBLE:
13447 if (mode == VOIDmode)
13448 mode = GET_MODE (rtl);
13449
13450 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13451 {
13452 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13453
13454 /* Note that a CONST_DOUBLE rtx could represent either an integer
13455 or a floating-point constant. A CONST_DOUBLE is used whenever
13456 the constant requires more than one word in order to be
13457 adequately represented. We output CONST_DOUBLEs as blocks. */
13458 loc_result = new_loc_descr (DW_OP_implicit_value,
13459 GET_MODE_SIZE (mode), 0);
13460 #if TARGET_SUPPORTS_WIDE_INT == 0
13461 if (!SCALAR_FLOAT_MODE_P (mode))
13462 {
13463 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13464 loc_result->dw_loc_oprnd2.v.val_double
13465 = rtx_to_double_int (rtl);
13466 }
13467 else
13468 #endif
13469 {
13470 unsigned int length = GET_MODE_SIZE (mode);
13471 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13472
13473 insert_float (rtl, array);
13474 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13475 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13476 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13477 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13478 }
13479 }
13480 break;
13481
13482 case CONST_WIDE_INT:
13483 if (mode == VOIDmode)
13484 mode = GET_MODE (rtl);
13485
13486 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13487 {
13488 loc_result = new_loc_descr (DW_OP_implicit_value,
13489 GET_MODE_SIZE (mode), 0);
13490 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13491 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13492 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13493 }
13494 break;
13495
13496 case CONST_VECTOR:
13497 if (mode == VOIDmode)
13498 mode = GET_MODE (rtl);
13499
13500 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13501 {
13502 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13503 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13504 unsigned char *array
13505 = ggc_vec_alloc<unsigned char> (length * elt_size);
13506 unsigned int i;
13507 unsigned char *p;
13508 enum machine_mode imode = GET_MODE_INNER (mode);
13509
13510 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13511 switch (GET_MODE_CLASS (mode))
13512 {
13513 case MODE_VECTOR_INT:
13514 for (i = 0, p = array; i < length; i++, p += elt_size)
13515 {
13516 rtx elt = CONST_VECTOR_ELT (rtl, i);
13517 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13518 }
13519 break;
13520
13521 case MODE_VECTOR_FLOAT:
13522 for (i = 0, p = array; i < length; i++, p += elt_size)
13523 {
13524 rtx elt = CONST_VECTOR_ELT (rtl, i);
13525 insert_float (elt, p);
13526 }
13527 break;
13528
13529 default:
13530 gcc_unreachable ();
13531 }
13532
13533 loc_result = new_loc_descr (DW_OP_implicit_value,
13534 length * elt_size, 0);
13535 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13536 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13537 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13538 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13539 }
13540 break;
13541
13542 case CONST:
13543 if (mode == VOIDmode
13544 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13545 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13546 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13547 {
13548 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13549 break;
13550 }
13551 /* FALLTHROUGH */
13552 case SYMBOL_REF:
13553 if (!const_ok_for_output (rtl))
13554 break;
13555 case LABEL_REF:
13556 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13557 && (dwarf_version >= 4 || !dwarf_strict))
13558 {
13559 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13560 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13561 vec_safe_push (used_rtx_array, rtl);
13562 }
13563 break;
13564
13565 case DEBUG_IMPLICIT_PTR:
13566 loc_result = implicit_ptr_descriptor (rtl, 0);
13567 break;
13568
13569 case PLUS:
13570 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13571 && CONST_INT_P (XEXP (rtl, 1)))
13572 {
13573 loc_result
13574 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13575 break;
13576 }
13577 /* FALLTHRU */
13578 do_default:
13579 default:
13580 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13581 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13582 && dwarf_version >= 4)
13583 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13584 {
13585 /* Value expression. */
13586 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13587 if (loc_result)
13588 add_loc_descr (&loc_result,
13589 new_loc_descr (DW_OP_stack_value, 0, 0));
13590 }
13591 break;
13592 }
13593
13594 return loc_result;
13595 }
13596
13597 /* We need to figure out what section we should use as the base for the
13598 address ranges where a given location is valid.
13599 1. If this particular DECL has a section associated with it, use that.
13600 2. If this function has a section associated with it, use that.
13601 3. Otherwise, use the text section.
13602 XXX: If you split a variable across multiple sections, we won't notice. */
13603
13604 static const char *
13605 secname_for_decl (const_tree decl)
13606 {
13607 const char *secname;
13608
13609 if (VAR_OR_FUNCTION_DECL_P (decl)
13610 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13611 && DECL_SECTION_NAME (decl))
13612 secname = DECL_SECTION_NAME (decl);
13613 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13614 secname = DECL_SECTION_NAME (current_function_decl);
13615 else if (cfun && in_cold_section_p)
13616 secname = crtl->subsections.cold_section_label;
13617 else
13618 secname = text_section_label;
13619
13620 return secname;
13621 }
13622
13623 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13624
13625 static bool
13626 decl_by_reference_p (tree decl)
13627 {
13628 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13629 || TREE_CODE (decl) == VAR_DECL)
13630 && DECL_BY_REFERENCE (decl));
13631 }
13632
13633 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13634 for VARLOC. */
13635
13636 static dw_loc_descr_ref
13637 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13638 enum var_init_status initialized)
13639 {
13640 int have_address = 0;
13641 dw_loc_descr_ref descr;
13642 enum machine_mode mode;
13643
13644 if (want_address != 2)
13645 {
13646 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13647 /* Single part. */
13648 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13649 {
13650 varloc = PAT_VAR_LOCATION_LOC (varloc);
13651 if (GET_CODE (varloc) == EXPR_LIST)
13652 varloc = XEXP (varloc, 0);
13653 mode = GET_MODE (varloc);
13654 if (MEM_P (varloc))
13655 {
13656 rtx addr = XEXP (varloc, 0);
13657 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13658 mode, initialized);
13659 if (descr)
13660 have_address = 1;
13661 else
13662 {
13663 rtx x = avoid_constant_pool_reference (varloc);
13664 if (x != varloc)
13665 descr = mem_loc_descriptor (x, mode, VOIDmode,
13666 initialized);
13667 }
13668 }
13669 else
13670 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13671 }
13672 else
13673 return 0;
13674 }
13675 else
13676 {
13677 if (GET_CODE (varloc) == VAR_LOCATION)
13678 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13679 else
13680 mode = DECL_MODE (loc);
13681 descr = loc_descriptor (varloc, mode, initialized);
13682 have_address = 1;
13683 }
13684
13685 if (!descr)
13686 return 0;
13687
13688 if (want_address == 2 && !have_address
13689 && (dwarf_version >= 4 || !dwarf_strict))
13690 {
13691 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13692 {
13693 expansion_failed (loc, NULL_RTX,
13694 "DWARF address size mismatch");
13695 return 0;
13696 }
13697 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13698 have_address = 1;
13699 }
13700 /* Show if we can't fill the request for an address. */
13701 if (want_address && !have_address)
13702 {
13703 expansion_failed (loc, NULL_RTX,
13704 "Want address and only have value");
13705 return 0;
13706 }
13707
13708 /* If we've got an address and don't want one, dereference. */
13709 if (!want_address && have_address)
13710 {
13711 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13712 enum dwarf_location_atom op;
13713
13714 if (size > DWARF2_ADDR_SIZE || size == -1)
13715 {
13716 expansion_failed (loc, NULL_RTX,
13717 "DWARF address size mismatch");
13718 return 0;
13719 }
13720 else if (size == DWARF2_ADDR_SIZE)
13721 op = DW_OP_deref;
13722 else
13723 op = DW_OP_deref_size;
13724
13725 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13726 }
13727
13728 return descr;
13729 }
13730
13731 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13732 if it is not possible. */
13733
13734 static dw_loc_descr_ref
13735 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13736 {
13737 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13738 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13739 else if (dwarf_version >= 3 || !dwarf_strict)
13740 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13741 else
13742 return NULL;
13743 }
13744
13745 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13746 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13747
13748 static dw_loc_descr_ref
13749 dw_sra_loc_expr (tree decl, rtx loc)
13750 {
13751 rtx p;
13752 unsigned int padsize = 0;
13753 dw_loc_descr_ref descr, *descr_tail;
13754 unsigned HOST_WIDE_INT decl_size;
13755 rtx varloc;
13756 enum var_init_status initialized;
13757
13758 if (DECL_SIZE (decl) == NULL
13759 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13760 return NULL;
13761
13762 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13763 descr = NULL;
13764 descr_tail = &descr;
13765
13766 for (p = loc; p; p = XEXP (p, 1))
13767 {
13768 unsigned int bitsize = decl_piece_bitsize (p);
13769 rtx loc_note = *decl_piece_varloc_ptr (p);
13770 dw_loc_descr_ref cur_descr;
13771 dw_loc_descr_ref *tail, last = NULL;
13772 unsigned int opsize = 0;
13773
13774 if (loc_note == NULL_RTX
13775 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13776 {
13777 padsize += bitsize;
13778 continue;
13779 }
13780 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13781 varloc = NOTE_VAR_LOCATION (loc_note);
13782 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13783 if (cur_descr == NULL)
13784 {
13785 padsize += bitsize;
13786 continue;
13787 }
13788
13789 /* Check that cur_descr either doesn't use
13790 DW_OP_*piece operations, or their sum is equal
13791 to bitsize. Otherwise we can't embed it. */
13792 for (tail = &cur_descr; *tail != NULL;
13793 tail = &(*tail)->dw_loc_next)
13794 if ((*tail)->dw_loc_opc == DW_OP_piece)
13795 {
13796 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13797 * BITS_PER_UNIT;
13798 last = *tail;
13799 }
13800 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13801 {
13802 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13803 last = *tail;
13804 }
13805
13806 if (last != NULL && opsize != bitsize)
13807 {
13808 padsize += bitsize;
13809 /* Discard the current piece of the descriptor and release any
13810 addr_table entries it uses. */
13811 remove_loc_list_addr_table_entries (cur_descr);
13812 continue;
13813 }
13814
13815 /* If there is a hole, add DW_OP_*piece after empty DWARF
13816 expression, which means that those bits are optimized out. */
13817 if (padsize)
13818 {
13819 if (padsize > decl_size)
13820 {
13821 remove_loc_list_addr_table_entries (cur_descr);
13822 goto discard_descr;
13823 }
13824 decl_size -= padsize;
13825 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13826 if (*descr_tail == NULL)
13827 {
13828 remove_loc_list_addr_table_entries (cur_descr);
13829 goto discard_descr;
13830 }
13831 descr_tail = &(*descr_tail)->dw_loc_next;
13832 padsize = 0;
13833 }
13834 *descr_tail = cur_descr;
13835 descr_tail = tail;
13836 if (bitsize > decl_size)
13837 goto discard_descr;
13838 decl_size -= bitsize;
13839 if (last == NULL)
13840 {
13841 HOST_WIDE_INT offset = 0;
13842 if (GET_CODE (varloc) == VAR_LOCATION
13843 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13844 {
13845 varloc = PAT_VAR_LOCATION_LOC (varloc);
13846 if (GET_CODE (varloc) == EXPR_LIST)
13847 varloc = XEXP (varloc, 0);
13848 }
13849 do
13850 {
13851 if (GET_CODE (varloc) == CONST
13852 || GET_CODE (varloc) == SIGN_EXTEND
13853 || GET_CODE (varloc) == ZERO_EXTEND)
13854 varloc = XEXP (varloc, 0);
13855 else if (GET_CODE (varloc) == SUBREG)
13856 varloc = SUBREG_REG (varloc);
13857 else
13858 break;
13859 }
13860 while (1);
13861 /* DW_OP_bit_size offset should be zero for register
13862 or implicit location descriptions and empty location
13863 descriptions, but for memory addresses needs big endian
13864 adjustment. */
13865 if (MEM_P (varloc))
13866 {
13867 unsigned HOST_WIDE_INT memsize
13868 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13869 if (memsize != bitsize)
13870 {
13871 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13872 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13873 goto discard_descr;
13874 if (memsize < bitsize)
13875 goto discard_descr;
13876 if (BITS_BIG_ENDIAN)
13877 offset = memsize - bitsize;
13878 }
13879 }
13880
13881 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13882 if (*descr_tail == NULL)
13883 goto discard_descr;
13884 descr_tail = &(*descr_tail)->dw_loc_next;
13885 }
13886 }
13887
13888 /* If there were any non-empty expressions, add padding till the end of
13889 the decl. */
13890 if (descr != NULL && decl_size != 0)
13891 {
13892 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13893 if (*descr_tail == NULL)
13894 goto discard_descr;
13895 }
13896 return descr;
13897
13898 discard_descr:
13899 /* Discard the descriptor and release any addr_table entries it uses. */
13900 remove_loc_list_addr_table_entries (descr);
13901 return NULL;
13902 }
13903
13904 /* Return the dwarf representation of the location list LOC_LIST of
13905 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13906 function. */
13907
13908 static dw_loc_list_ref
13909 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13910 {
13911 const char *endname, *secname;
13912 rtx varloc;
13913 enum var_init_status initialized;
13914 struct var_loc_node *node;
13915 dw_loc_descr_ref descr;
13916 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13917 dw_loc_list_ref list = NULL;
13918 dw_loc_list_ref *listp = &list;
13919
13920 /* Now that we know what section we are using for a base,
13921 actually construct the list of locations.
13922 The first location information is what is passed to the
13923 function that creates the location list, and the remaining
13924 locations just get added on to that list.
13925 Note that we only know the start address for a location
13926 (IE location changes), so to build the range, we use
13927 the range [current location start, next location start].
13928 This means we have to special case the last node, and generate
13929 a range of [last location start, end of function label]. */
13930
13931 secname = secname_for_decl (decl);
13932
13933 for (node = loc_list->first; node; node = node->next)
13934 if (GET_CODE (node->loc) == EXPR_LIST
13935 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13936 {
13937 if (GET_CODE (node->loc) == EXPR_LIST)
13938 {
13939 /* This requires DW_OP_{,bit_}piece, which is not usable
13940 inside DWARF expressions. */
13941 if (want_address != 2)
13942 continue;
13943 descr = dw_sra_loc_expr (decl, node->loc);
13944 if (descr == NULL)
13945 continue;
13946 }
13947 else
13948 {
13949 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13950 varloc = NOTE_VAR_LOCATION (node->loc);
13951 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13952 }
13953 if (descr)
13954 {
13955 bool range_across_switch = false;
13956 /* If section switch happens in between node->label
13957 and node->next->label (or end of function) and
13958 we can't emit it as a single entry list,
13959 emit two ranges, first one ending at the end
13960 of first partition and second one starting at the
13961 beginning of second partition. */
13962 if (node == loc_list->last_before_switch
13963 && (node != loc_list->first || loc_list->first->next)
13964 && current_function_decl)
13965 {
13966 endname = cfun->fde->dw_fde_end;
13967 range_across_switch = true;
13968 }
13969 /* The variable has a location between NODE->LABEL and
13970 NODE->NEXT->LABEL. */
13971 else if (node->next)
13972 endname = node->next->label;
13973 /* If the variable has a location at the last label
13974 it keeps its location until the end of function. */
13975 else if (!current_function_decl)
13976 endname = text_end_label;
13977 else
13978 {
13979 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13980 current_function_funcdef_no);
13981 endname = ggc_strdup (label_id);
13982 }
13983
13984 *listp = new_loc_list (descr, node->label, endname, secname);
13985 if (TREE_CODE (decl) == PARM_DECL
13986 && node == loc_list->first
13987 && NOTE_P (node->loc)
13988 && strcmp (node->label, endname) == 0)
13989 (*listp)->force = true;
13990 listp = &(*listp)->dw_loc_next;
13991
13992 if (range_across_switch)
13993 {
13994 if (GET_CODE (node->loc) == EXPR_LIST)
13995 descr = dw_sra_loc_expr (decl, node->loc);
13996 else
13997 {
13998 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13999 varloc = NOTE_VAR_LOCATION (node->loc);
14000 descr = dw_loc_list_1 (decl, varloc, want_address,
14001 initialized);
14002 }
14003 gcc_assert (descr);
14004 /* The variable has a location between NODE->LABEL and
14005 NODE->NEXT->LABEL. */
14006 if (node->next)
14007 endname = node->next->label;
14008 else
14009 endname = cfun->fde->dw_fde_second_end;
14010 *listp = new_loc_list (descr,
14011 cfun->fde->dw_fde_second_begin,
14012 endname, secname);
14013 listp = &(*listp)->dw_loc_next;
14014 }
14015 }
14016 }
14017
14018 /* Try to avoid the overhead of a location list emitting a location
14019 expression instead, but only if we didn't have more than one
14020 location entry in the first place. If some entries were not
14021 representable, we don't want to pretend a single entry that was
14022 applies to the entire scope in which the variable is
14023 available. */
14024 if (list && loc_list->first->next)
14025 gen_llsym (list);
14026
14027 return list;
14028 }
14029
14030 /* Return if the loc_list has only single element and thus can be represented
14031 as location description. */
14032
14033 static bool
14034 single_element_loc_list_p (dw_loc_list_ref list)
14035 {
14036 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14037 return !list->ll_symbol;
14038 }
14039
14040 /* To each location in list LIST add loc descr REF. */
14041
14042 static void
14043 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14044 {
14045 dw_loc_descr_ref copy;
14046 add_loc_descr (&list->expr, ref);
14047 list = list->dw_loc_next;
14048 while (list)
14049 {
14050 copy = ggc_alloc<dw_loc_descr_node> ();
14051 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14052 add_loc_descr (&list->expr, copy);
14053 while (copy->dw_loc_next)
14054 {
14055 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14056 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14057 copy->dw_loc_next = new_copy;
14058 copy = new_copy;
14059 }
14060 list = list->dw_loc_next;
14061 }
14062 }
14063
14064 /* Given two lists RET and LIST
14065 produce location list that is result of adding expression in LIST
14066 to expression in RET on each position in program.
14067 Might be destructive on both RET and LIST.
14068
14069 TODO: We handle only simple cases of RET or LIST having at most one
14070 element. General case would inolve sorting the lists in program order
14071 and merging them that will need some additional work.
14072 Adding that will improve quality of debug info especially for SRA-ed
14073 structures. */
14074
14075 static void
14076 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14077 {
14078 if (!list)
14079 return;
14080 if (!*ret)
14081 {
14082 *ret = list;
14083 return;
14084 }
14085 if (!list->dw_loc_next)
14086 {
14087 add_loc_descr_to_each (*ret, list->expr);
14088 return;
14089 }
14090 if (!(*ret)->dw_loc_next)
14091 {
14092 add_loc_descr_to_each (list, (*ret)->expr);
14093 *ret = list;
14094 return;
14095 }
14096 expansion_failed (NULL_TREE, NULL_RTX,
14097 "Don't know how to merge two non-trivial"
14098 " location lists.\n");
14099 *ret = NULL;
14100 return;
14101 }
14102
14103 /* LOC is constant expression. Try a luck, look it up in constant
14104 pool and return its loc_descr of its address. */
14105
14106 static dw_loc_descr_ref
14107 cst_pool_loc_descr (tree loc)
14108 {
14109 /* Get an RTL for this, if something has been emitted. */
14110 rtx rtl = lookup_constant_def (loc);
14111
14112 if (!rtl || !MEM_P (rtl))
14113 {
14114 gcc_assert (!rtl);
14115 return 0;
14116 }
14117 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14118
14119 /* TODO: We might get more coverage if we was actually delaying expansion
14120 of all expressions till end of compilation when constant pools are fully
14121 populated. */
14122 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14123 {
14124 expansion_failed (loc, NULL_RTX,
14125 "CST value in contant pool but not marked.");
14126 return 0;
14127 }
14128 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14129 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14130 }
14131
14132 /* Return dw_loc_list representing address of addr_expr LOC
14133 by looking for inner INDIRECT_REF expression and turning
14134 it into simple arithmetics. */
14135
14136 static dw_loc_list_ref
14137 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14138 {
14139 tree obj, offset;
14140 HOST_WIDE_INT bitsize, bitpos, bytepos;
14141 enum machine_mode mode;
14142 int unsignedp, volatilep = 0;
14143 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14144
14145 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14146 &bitsize, &bitpos, &offset, &mode,
14147 &unsignedp, &volatilep, false);
14148 STRIP_NOPS (obj);
14149 if (bitpos % BITS_PER_UNIT)
14150 {
14151 expansion_failed (loc, NULL_RTX, "bitfield access");
14152 return 0;
14153 }
14154 if (!INDIRECT_REF_P (obj))
14155 {
14156 expansion_failed (obj,
14157 NULL_RTX, "no indirect ref in inner refrence");
14158 return 0;
14159 }
14160 if (!offset && !bitpos)
14161 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14162 else if (toplev
14163 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14164 && (dwarf_version >= 4 || !dwarf_strict))
14165 {
14166 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14167 if (!list_ret)
14168 return 0;
14169 if (offset)
14170 {
14171 /* Variable offset. */
14172 list_ret1 = loc_list_from_tree (offset, 0);
14173 if (list_ret1 == 0)
14174 return 0;
14175 add_loc_list (&list_ret, list_ret1);
14176 if (!list_ret)
14177 return 0;
14178 add_loc_descr_to_each (list_ret,
14179 new_loc_descr (DW_OP_plus, 0, 0));
14180 }
14181 bytepos = bitpos / BITS_PER_UNIT;
14182 if (bytepos > 0)
14183 add_loc_descr_to_each (list_ret,
14184 new_loc_descr (DW_OP_plus_uconst,
14185 bytepos, 0));
14186 else if (bytepos < 0)
14187 loc_list_plus_const (list_ret, bytepos);
14188 add_loc_descr_to_each (list_ret,
14189 new_loc_descr (DW_OP_stack_value, 0, 0));
14190 }
14191 return list_ret;
14192 }
14193
14194
14195 /* Generate Dwarf location list representing LOC.
14196 If WANT_ADDRESS is false, expression computing LOC will be computed
14197 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14198 if WANT_ADDRESS is 2, expression computing address useable in location
14199 will be returned (i.e. DW_OP_reg can be used
14200 to refer to register values). */
14201
14202 static dw_loc_list_ref
14203 loc_list_from_tree (tree loc, int want_address)
14204 {
14205 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14206 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14207 int have_address = 0;
14208 enum dwarf_location_atom op;
14209
14210 /* ??? Most of the time we do not take proper care for sign/zero
14211 extending the values properly. Hopefully this won't be a real
14212 problem... */
14213
14214 switch (TREE_CODE (loc))
14215 {
14216 case ERROR_MARK:
14217 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14218 return 0;
14219
14220 case PLACEHOLDER_EXPR:
14221 /* This case involves extracting fields from an object to determine the
14222 position of other fields. We don't try to encode this here. The
14223 only user of this is Ada, which encodes the needed information using
14224 the names of types. */
14225 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14226 return 0;
14227
14228 case CALL_EXPR:
14229 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14230 /* There are no opcodes for these operations. */
14231 return 0;
14232
14233 case PREINCREMENT_EXPR:
14234 case PREDECREMENT_EXPR:
14235 case POSTINCREMENT_EXPR:
14236 case POSTDECREMENT_EXPR:
14237 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14238 /* There are no opcodes for these operations. */
14239 return 0;
14240
14241 case ADDR_EXPR:
14242 /* If we already want an address, see if there is INDIRECT_REF inside
14243 e.g. for &this->field. */
14244 if (want_address)
14245 {
14246 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14247 (loc, want_address == 2);
14248 if (list_ret)
14249 have_address = 1;
14250 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14251 && (ret = cst_pool_loc_descr (loc)))
14252 have_address = 1;
14253 }
14254 /* Otherwise, process the argument and look for the address. */
14255 if (!list_ret && !ret)
14256 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14257 else
14258 {
14259 if (want_address)
14260 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14261 return NULL;
14262 }
14263 break;
14264
14265 case VAR_DECL:
14266 if (DECL_THREAD_LOCAL_P (loc))
14267 {
14268 rtx rtl;
14269 enum dwarf_location_atom tls_op;
14270 enum dtprel_bool dtprel = dtprel_false;
14271
14272 if (targetm.have_tls)
14273 {
14274 /* If this is not defined, we have no way to emit the
14275 data. */
14276 if (!targetm.asm_out.output_dwarf_dtprel)
14277 return 0;
14278
14279 /* The way DW_OP_GNU_push_tls_address is specified, we
14280 can only look up addresses of objects in the current
14281 module. We used DW_OP_addr as first op, but that's
14282 wrong, because DW_OP_addr is relocated by the debug
14283 info consumer, while DW_OP_GNU_push_tls_address
14284 operand shouldn't be. */
14285 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14286 return 0;
14287 dtprel = dtprel_true;
14288 tls_op = DW_OP_GNU_push_tls_address;
14289 }
14290 else
14291 {
14292 if (!targetm.emutls.debug_form_tls_address
14293 || !(dwarf_version >= 3 || !dwarf_strict))
14294 return 0;
14295 /* We stuffed the control variable into the DECL_VALUE_EXPR
14296 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14297 no longer appear in gimple code. We used the control
14298 variable in specific so that we could pick it up here. */
14299 loc = DECL_VALUE_EXPR (loc);
14300 tls_op = DW_OP_form_tls_address;
14301 }
14302
14303 rtl = rtl_for_decl_location (loc);
14304 if (rtl == NULL_RTX)
14305 return 0;
14306
14307 if (!MEM_P (rtl))
14308 return 0;
14309 rtl = XEXP (rtl, 0);
14310 if (! CONSTANT_P (rtl))
14311 return 0;
14312
14313 ret = new_addr_loc_descr (rtl, dtprel);
14314 ret1 = new_loc_descr (tls_op, 0, 0);
14315 add_loc_descr (&ret, ret1);
14316
14317 have_address = 1;
14318 break;
14319 }
14320 /* FALLTHRU */
14321
14322 case PARM_DECL:
14323 case RESULT_DECL:
14324 if (DECL_HAS_VALUE_EXPR_P (loc))
14325 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14326 want_address);
14327 /* FALLTHRU */
14328
14329 case FUNCTION_DECL:
14330 {
14331 rtx rtl;
14332 var_loc_list *loc_list = lookup_decl_loc (loc);
14333
14334 if (loc_list && loc_list->first)
14335 {
14336 list_ret = dw_loc_list (loc_list, loc, want_address);
14337 have_address = want_address != 0;
14338 break;
14339 }
14340 rtl = rtl_for_decl_location (loc);
14341 if (rtl == NULL_RTX)
14342 {
14343 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14344 return 0;
14345 }
14346 else if (CONST_INT_P (rtl))
14347 {
14348 HOST_WIDE_INT val = INTVAL (rtl);
14349 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14350 val &= GET_MODE_MASK (DECL_MODE (loc));
14351 ret = int_loc_descriptor (val);
14352 }
14353 else if (GET_CODE (rtl) == CONST_STRING)
14354 {
14355 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14356 return 0;
14357 }
14358 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14359 ret = new_addr_loc_descr (rtl, dtprel_false);
14360 else
14361 {
14362 enum machine_mode mode, mem_mode;
14363
14364 /* Certain constructs can only be represented at top-level. */
14365 if (want_address == 2)
14366 {
14367 ret = loc_descriptor (rtl, VOIDmode,
14368 VAR_INIT_STATUS_INITIALIZED);
14369 have_address = 1;
14370 }
14371 else
14372 {
14373 mode = GET_MODE (rtl);
14374 mem_mode = VOIDmode;
14375 if (MEM_P (rtl))
14376 {
14377 mem_mode = mode;
14378 mode = get_address_mode (rtl);
14379 rtl = XEXP (rtl, 0);
14380 have_address = 1;
14381 }
14382 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14383 VAR_INIT_STATUS_INITIALIZED);
14384 }
14385 if (!ret)
14386 expansion_failed (loc, rtl,
14387 "failed to produce loc descriptor for rtl");
14388 }
14389 }
14390 break;
14391
14392 case MEM_REF:
14393 /* ??? FIXME. */
14394 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14395 return 0;
14396 /* Fallthru. */
14397 case INDIRECT_REF:
14398 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14399 have_address = 1;
14400 break;
14401
14402 case COMPOUND_EXPR:
14403 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14404
14405 CASE_CONVERT:
14406 case VIEW_CONVERT_EXPR:
14407 case SAVE_EXPR:
14408 case MODIFY_EXPR:
14409 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14410
14411 case COMPONENT_REF:
14412 case BIT_FIELD_REF:
14413 case ARRAY_REF:
14414 case ARRAY_RANGE_REF:
14415 case REALPART_EXPR:
14416 case IMAGPART_EXPR:
14417 {
14418 tree obj, offset;
14419 HOST_WIDE_INT bitsize, bitpos, bytepos;
14420 enum machine_mode mode;
14421 int unsignedp, volatilep = 0;
14422
14423 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14424 &unsignedp, &volatilep, false);
14425
14426 gcc_assert (obj != loc);
14427
14428 list_ret = loc_list_from_tree (obj,
14429 want_address == 2
14430 && !bitpos && !offset ? 2 : 1);
14431 /* TODO: We can extract value of the small expression via shifting even
14432 for nonzero bitpos. */
14433 if (list_ret == 0)
14434 return 0;
14435 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14436 {
14437 expansion_failed (loc, NULL_RTX,
14438 "bitfield access");
14439 return 0;
14440 }
14441
14442 if (offset != NULL_TREE)
14443 {
14444 /* Variable offset. */
14445 list_ret1 = loc_list_from_tree (offset, 0);
14446 if (list_ret1 == 0)
14447 return 0;
14448 add_loc_list (&list_ret, list_ret1);
14449 if (!list_ret)
14450 return 0;
14451 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14452 }
14453
14454 bytepos = bitpos / BITS_PER_UNIT;
14455 if (bytepos > 0)
14456 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14457 else if (bytepos < 0)
14458 loc_list_plus_const (list_ret, bytepos);
14459
14460 have_address = 1;
14461 break;
14462 }
14463
14464 case INTEGER_CST:
14465 if ((want_address || !tree_fits_shwi_p (loc))
14466 && (ret = cst_pool_loc_descr (loc)))
14467 have_address = 1;
14468 else if (want_address == 2
14469 && tree_fits_shwi_p (loc)
14470 && (ret = address_of_int_loc_descriptor
14471 (int_size_in_bytes (TREE_TYPE (loc)),
14472 tree_to_shwi (loc))))
14473 have_address = 1;
14474 else if (tree_fits_shwi_p (loc))
14475 ret = int_loc_descriptor (tree_to_shwi (loc));
14476 else
14477 {
14478 expansion_failed (loc, NULL_RTX,
14479 "Integer operand is not host integer");
14480 return 0;
14481 }
14482 break;
14483
14484 case CONSTRUCTOR:
14485 case REAL_CST:
14486 case STRING_CST:
14487 case COMPLEX_CST:
14488 if ((ret = cst_pool_loc_descr (loc)))
14489 have_address = 1;
14490 else
14491 /* We can construct small constants here using int_loc_descriptor. */
14492 expansion_failed (loc, NULL_RTX,
14493 "constructor or constant not in constant pool");
14494 break;
14495
14496 case TRUTH_AND_EXPR:
14497 case TRUTH_ANDIF_EXPR:
14498 case BIT_AND_EXPR:
14499 op = DW_OP_and;
14500 goto do_binop;
14501
14502 case TRUTH_XOR_EXPR:
14503 case BIT_XOR_EXPR:
14504 op = DW_OP_xor;
14505 goto do_binop;
14506
14507 case TRUTH_OR_EXPR:
14508 case TRUTH_ORIF_EXPR:
14509 case BIT_IOR_EXPR:
14510 op = DW_OP_or;
14511 goto do_binop;
14512
14513 case FLOOR_DIV_EXPR:
14514 case CEIL_DIV_EXPR:
14515 case ROUND_DIV_EXPR:
14516 case TRUNC_DIV_EXPR:
14517 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14518 return 0;
14519 op = DW_OP_div;
14520 goto do_binop;
14521
14522 case MINUS_EXPR:
14523 op = DW_OP_minus;
14524 goto do_binop;
14525
14526 case FLOOR_MOD_EXPR:
14527 case CEIL_MOD_EXPR:
14528 case ROUND_MOD_EXPR:
14529 case TRUNC_MOD_EXPR:
14530 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14531 {
14532 op = DW_OP_mod;
14533 goto do_binop;
14534 }
14535 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14536 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14537 if (list_ret == 0 || list_ret1 == 0)
14538 return 0;
14539
14540 add_loc_list (&list_ret, list_ret1);
14541 if (list_ret == 0)
14542 return 0;
14543 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14544 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14545 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14546 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14547 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14548 break;
14549
14550 case MULT_EXPR:
14551 op = DW_OP_mul;
14552 goto do_binop;
14553
14554 case LSHIFT_EXPR:
14555 op = DW_OP_shl;
14556 goto do_binop;
14557
14558 case RSHIFT_EXPR:
14559 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14560 goto do_binop;
14561
14562 case POINTER_PLUS_EXPR:
14563 case PLUS_EXPR:
14564 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14565 {
14566 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14567 if (list_ret == 0)
14568 return 0;
14569
14570 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14571 break;
14572 }
14573
14574 op = DW_OP_plus;
14575 goto do_binop;
14576
14577 case LE_EXPR:
14578 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14579 return 0;
14580
14581 op = DW_OP_le;
14582 goto do_binop;
14583
14584 case GE_EXPR:
14585 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14586 return 0;
14587
14588 op = DW_OP_ge;
14589 goto do_binop;
14590
14591 case LT_EXPR:
14592 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14593 return 0;
14594
14595 op = DW_OP_lt;
14596 goto do_binop;
14597
14598 case GT_EXPR:
14599 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14600 return 0;
14601
14602 op = DW_OP_gt;
14603 goto do_binop;
14604
14605 case EQ_EXPR:
14606 op = DW_OP_eq;
14607 goto do_binop;
14608
14609 case NE_EXPR:
14610 op = DW_OP_ne;
14611 goto do_binop;
14612
14613 do_binop:
14614 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14615 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14616 if (list_ret == 0 || list_ret1 == 0)
14617 return 0;
14618
14619 add_loc_list (&list_ret, list_ret1);
14620 if (list_ret == 0)
14621 return 0;
14622 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14623 break;
14624
14625 case TRUTH_NOT_EXPR:
14626 case BIT_NOT_EXPR:
14627 op = DW_OP_not;
14628 goto do_unop;
14629
14630 case ABS_EXPR:
14631 op = DW_OP_abs;
14632 goto do_unop;
14633
14634 case NEGATE_EXPR:
14635 op = DW_OP_neg;
14636 goto do_unop;
14637
14638 do_unop:
14639 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14640 if (list_ret == 0)
14641 return 0;
14642
14643 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14644 break;
14645
14646 case MIN_EXPR:
14647 case MAX_EXPR:
14648 {
14649 const enum tree_code code =
14650 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14651
14652 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14653 build2 (code, integer_type_node,
14654 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14655 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14656 }
14657
14658 /* ... fall through ... */
14659
14660 case COND_EXPR:
14661 {
14662 dw_loc_descr_ref lhs
14663 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14664 dw_loc_list_ref rhs
14665 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14666 dw_loc_descr_ref bra_node, jump_node, tmp;
14667
14668 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14669 if (list_ret == 0 || lhs == 0 || rhs == 0)
14670 return 0;
14671
14672 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14673 add_loc_descr_to_each (list_ret, bra_node);
14674
14675 add_loc_list (&list_ret, rhs);
14676 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14677 add_loc_descr_to_each (list_ret, jump_node);
14678
14679 add_loc_descr_to_each (list_ret, lhs);
14680 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14681 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14682
14683 /* ??? Need a node to point the skip at. Use a nop. */
14684 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14685 add_loc_descr_to_each (list_ret, tmp);
14686 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14687 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14688 }
14689 break;
14690
14691 case FIX_TRUNC_EXPR:
14692 return 0;
14693
14694 default:
14695 /* Leave front-end specific codes as simply unknown. This comes
14696 up, for instance, with the C STMT_EXPR. */
14697 if ((unsigned int) TREE_CODE (loc)
14698 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14699 {
14700 expansion_failed (loc, NULL_RTX,
14701 "language specific tree node");
14702 return 0;
14703 }
14704
14705 #ifdef ENABLE_CHECKING
14706 /* Otherwise this is a generic code; we should just lists all of
14707 these explicitly. We forgot one. */
14708 gcc_unreachable ();
14709 #else
14710 /* In a release build, we want to degrade gracefully: better to
14711 generate incomplete debugging information than to crash. */
14712 return NULL;
14713 #endif
14714 }
14715
14716 if (!ret && !list_ret)
14717 return 0;
14718
14719 if (want_address == 2 && !have_address
14720 && (dwarf_version >= 4 || !dwarf_strict))
14721 {
14722 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14723 {
14724 expansion_failed (loc, NULL_RTX,
14725 "DWARF address size mismatch");
14726 return 0;
14727 }
14728 if (ret)
14729 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14730 else
14731 add_loc_descr_to_each (list_ret,
14732 new_loc_descr (DW_OP_stack_value, 0, 0));
14733 have_address = 1;
14734 }
14735 /* Show if we can't fill the request for an address. */
14736 if (want_address && !have_address)
14737 {
14738 expansion_failed (loc, NULL_RTX,
14739 "Want address and only have value");
14740 return 0;
14741 }
14742
14743 gcc_assert (!ret || !list_ret);
14744
14745 /* If we've got an address and don't want one, dereference. */
14746 if (!want_address && have_address)
14747 {
14748 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14749
14750 if (size > DWARF2_ADDR_SIZE || size == -1)
14751 {
14752 expansion_failed (loc, NULL_RTX,
14753 "DWARF address size mismatch");
14754 return 0;
14755 }
14756 else if (size == DWARF2_ADDR_SIZE)
14757 op = DW_OP_deref;
14758 else
14759 op = DW_OP_deref_size;
14760
14761 if (ret)
14762 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14763 else
14764 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14765 }
14766 if (ret)
14767 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14768
14769 return list_ret;
14770 }
14771
14772 /* Same as above but return only single location expression. */
14773 static dw_loc_descr_ref
14774 loc_descriptor_from_tree (tree loc, int want_address)
14775 {
14776 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14777 if (!ret)
14778 return NULL;
14779 if (ret->dw_loc_next)
14780 {
14781 expansion_failed (loc, NULL_RTX,
14782 "Location list where only loc descriptor needed");
14783 return NULL;
14784 }
14785 return ret->expr;
14786 }
14787
14788 /* Given a value, round it up to the lowest multiple of `boundary'
14789 which is not less than the value itself. */
14790
14791 static inline HOST_WIDE_INT
14792 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14793 {
14794 return (((value + boundary - 1) / boundary) * boundary);
14795 }
14796
14797 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14798 pointer to the declared type for the relevant field variable, or return
14799 `integer_type_node' if the given node turns out to be an
14800 ERROR_MARK node. */
14801
14802 static inline tree
14803 field_type (const_tree decl)
14804 {
14805 tree type;
14806
14807 if (TREE_CODE (decl) == ERROR_MARK)
14808 return integer_type_node;
14809
14810 type = DECL_BIT_FIELD_TYPE (decl);
14811 if (type == NULL_TREE)
14812 type = TREE_TYPE (decl);
14813
14814 return type;
14815 }
14816
14817 /* Given a pointer to a tree node, return the alignment in bits for
14818 it, or else return BITS_PER_WORD if the node actually turns out to
14819 be an ERROR_MARK node. */
14820
14821 static inline unsigned
14822 simple_type_align_in_bits (const_tree type)
14823 {
14824 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14825 }
14826
14827 static inline unsigned
14828 simple_decl_align_in_bits (const_tree decl)
14829 {
14830 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14831 }
14832
14833 /* Return the result of rounding T up to ALIGN. */
14834
14835 static inline offset_int
14836 round_up_to_align (const offset_int &t, unsigned int align)
14837 {
14838 return wi::udiv_trunc (t + align - 1, align) * align;
14839 }
14840
14841 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14842 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14843 or return 0 if we are unable to determine what that offset is, either
14844 because the argument turns out to be a pointer to an ERROR_MARK node, or
14845 because the offset is actually variable. (We can't handle the latter case
14846 just yet). */
14847
14848 static HOST_WIDE_INT
14849 field_byte_offset (const_tree decl)
14850 {
14851 offset_int object_offset_in_bits;
14852 offset_int object_offset_in_bytes;
14853 offset_int bitpos_int;
14854
14855 if (TREE_CODE (decl) == ERROR_MARK)
14856 return 0;
14857
14858 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14859
14860 /* We cannot yet cope with fields whose positions are variable, so
14861 for now, when we see such things, we simply return 0. Someday, we may
14862 be able to handle such cases, but it will be damn difficult. */
14863 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14864 return 0;
14865
14866 bitpos_int = wi::to_offset (bit_position (decl));
14867
14868 #ifdef PCC_BITFIELD_TYPE_MATTERS
14869 if (PCC_BITFIELD_TYPE_MATTERS)
14870 {
14871 tree type;
14872 tree field_size_tree;
14873 offset_int deepest_bitpos;
14874 offset_int field_size_in_bits;
14875 unsigned int type_align_in_bits;
14876 unsigned int decl_align_in_bits;
14877 offset_int type_size_in_bits;
14878
14879 type = field_type (decl);
14880 type_size_in_bits = offset_int_type_size_in_bits (type);
14881 type_align_in_bits = simple_type_align_in_bits (type);
14882
14883 field_size_tree = DECL_SIZE (decl);
14884
14885 /* The size could be unspecified if there was an error, or for
14886 a flexible array member. */
14887 if (!field_size_tree)
14888 field_size_tree = bitsize_zero_node;
14889
14890 /* If the size of the field is not constant, use the type size. */
14891 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14892 field_size_in_bits = wi::to_offset (field_size_tree);
14893 else
14894 field_size_in_bits = type_size_in_bits;
14895
14896 decl_align_in_bits = simple_decl_align_in_bits (decl);
14897
14898 /* The GCC front-end doesn't make any attempt to keep track of the
14899 starting bit offset (relative to the start of the containing
14900 structure type) of the hypothetical "containing object" for a
14901 bit-field. Thus, when computing the byte offset value for the
14902 start of the "containing object" of a bit-field, we must deduce
14903 this information on our own. This can be rather tricky to do in
14904 some cases. For example, handling the following structure type
14905 definition when compiling for an i386/i486 target (which only
14906 aligns long long's to 32-bit boundaries) can be very tricky:
14907
14908 struct S { int field1; long long field2:31; };
14909
14910 Fortunately, there is a simple rule-of-thumb which can be used
14911 in such cases. When compiling for an i386/i486, GCC will
14912 allocate 8 bytes for the structure shown above. It decides to
14913 do this based upon one simple rule for bit-field allocation.
14914 GCC allocates each "containing object" for each bit-field at
14915 the first (i.e. lowest addressed) legitimate alignment boundary
14916 (based upon the required minimum alignment for the declared
14917 type of the field) which it can possibly use, subject to the
14918 condition that there is still enough available space remaining
14919 in the containing object (when allocated at the selected point)
14920 to fully accommodate all of the bits of the bit-field itself.
14921
14922 This simple rule makes it obvious why GCC allocates 8 bytes for
14923 each object of the structure type shown above. When looking
14924 for a place to allocate the "containing object" for `field2',
14925 the compiler simply tries to allocate a 64-bit "containing
14926 object" at each successive 32-bit boundary (starting at zero)
14927 until it finds a place to allocate that 64- bit field such that
14928 at least 31 contiguous (and previously unallocated) bits remain
14929 within that selected 64 bit field. (As it turns out, for the
14930 example above, the compiler finds it is OK to allocate the
14931 "containing object" 64-bit field at bit-offset zero within the
14932 structure type.)
14933
14934 Here we attempt to work backwards from the limited set of facts
14935 we're given, and we try to deduce from those facts, where GCC
14936 must have believed that the containing object started (within
14937 the structure type). The value we deduce is then used (by the
14938 callers of this routine) to generate DW_AT_location and
14939 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14940 the case of DW_AT_location, regular fields as well). */
14941
14942 /* Figure out the bit-distance from the start of the structure to
14943 the "deepest" bit of the bit-field. */
14944 deepest_bitpos = bitpos_int + field_size_in_bits;
14945
14946 /* This is the tricky part. Use some fancy footwork to deduce
14947 where the lowest addressed bit of the containing object must
14948 be. */
14949 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14950
14951 /* Round up to type_align by default. This works best for
14952 bitfields. */
14953 object_offset_in_bits
14954 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14955
14956 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14957 {
14958 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14959
14960 /* Round up to decl_align instead. */
14961 object_offset_in_bits
14962 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14963 }
14964 }
14965 else
14966 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14967 object_offset_in_bits = bitpos_int;
14968
14969 object_offset_in_bytes
14970 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14971 return object_offset_in_bytes.to_shwi ();
14972 }
14973 \f
14974 /* The following routines define various Dwarf attributes and any data
14975 associated with them. */
14976
14977 /* Add a location description attribute value to a DIE.
14978
14979 This emits location attributes suitable for whole variables and
14980 whole parameters. Note that the location attributes for struct fields are
14981 generated by the routine `data_member_location_attribute' below. */
14982
14983 static inline void
14984 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14985 dw_loc_list_ref descr)
14986 {
14987 if (descr == 0)
14988 return;
14989 if (single_element_loc_list_p (descr))
14990 add_AT_loc (die, attr_kind, descr->expr);
14991 else
14992 add_AT_loc_list (die, attr_kind, descr);
14993 }
14994
14995 /* Add DW_AT_accessibility attribute to DIE if needed. */
14996
14997 static void
14998 add_accessibility_attribute (dw_die_ref die, tree decl)
14999 {
15000 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15001 children, otherwise the default is DW_ACCESS_public. In DWARF2
15002 the default has always been DW_ACCESS_public. */
15003 if (TREE_PROTECTED (decl))
15004 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15005 else if (TREE_PRIVATE (decl))
15006 {
15007 if (dwarf_version == 2
15008 || die->die_parent == NULL
15009 || die->die_parent->die_tag != DW_TAG_class_type)
15010 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15011 }
15012 else if (dwarf_version > 2
15013 && die->die_parent
15014 && die->die_parent->die_tag == DW_TAG_class_type)
15015 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15016 }
15017
15018 /* Attach the specialized form of location attribute used for data members of
15019 struct and union types. In the special case of a FIELD_DECL node which
15020 represents a bit-field, the "offset" part of this special location
15021 descriptor must indicate the distance in bytes from the lowest-addressed
15022 byte of the containing struct or union type to the lowest-addressed byte of
15023 the "containing object" for the bit-field. (See the `field_byte_offset'
15024 function above).
15025
15026 For any given bit-field, the "containing object" is a hypothetical object
15027 (of some integral or enum type) within which the given bit-field lives. The
15028 type of this hypothetical "containing object" is always the same as the
15029 declared type of the individual bit-field itself (for GCC anyway... the
15030 DWARF spec doesn't actually mandate this). Note that it is the size (in
15031 bytes) of the hypothetical "containing object" which will be given in the
15032 DW_AT_byte_size attribute for this bit-field. (See the
15033 `byte_size_attribute' function below.) It is also used when calculating the
15034 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15035 function below.) */
15036
15037 static void
15038 add_data_member_location_attribute (dw_die_ref die, tree decl)
15039 {
15040 HOST_WIDE_INT offset;
15041 dw_loc_descr_ref loc_descr = 0;
15042
15043 if (TREE_CODE (decl) == TREE_BINFO)
15044 {
15045 /* We're working on the TAG_inheritance for a base class. */
15046 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15047 {
15048 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15049 aren't at a fixed offset from all (sub)objects of the same
15050 type. We need to extract the appropriate offset from our
15051 vtable. The following dwarf expression means
15052
15053 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15054
15055 This is specific to the V3 ABI, of course. */
15056
15057 dw_loc_descr_ref tmp;
15058
15059 /* Make a copy of the object address. */
15060 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15061 add_loc_descr (&loc_descr, tmp);
15062
15063 /* Extract the vtable address. */
15064 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15065 add_loc_descr (&loc_descr, tmp);
15066
15067 /* Calculate the address of the offset. */
15068 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15069 gcc_assert (offset < 0);
15070
15071 tmp = int_loc_descriptor (-offset);
15072 add_loc_descr (&loc_descr, tmp);
15073 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15074 add_loc_descr (&loc_descr, tmp);
15075
15076 /* Extract the offset. */
15077 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15078 add_loc_descr (&loc_descr, tmp);
15079
15080 /* Add it to the object address. */
15081 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15082 add_loc_descr (&loc_descr, tmp);
15083 }
15084 else
15085 offset = tree_to_shwi (BINFO_OFFSET (decl));
15086 }
15087 else
15088 offset = field_byte_offset (decl);
15089
15090 if (! loc_descr)
15091 {
15092 if (dwarf_version > 2)
15093 {
15094 /* Don't need to output a location expression, just the constant. */
15095 if (offset < 0)
15096 add_AT_int (die, DW_AT_data_member_location, offset);
15097 else
15098 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15099 return;
15100 }
15101 else
15102 {
15103 enum dwarf_location_atom op;
15104
15105 /* The DWARF2 standard says that we should assume that the structure
15106 address is already on the stack, so we can specify a structure
15107 field address by using DW_OP_plus_uconst. */
15108 op = DW_OP_plus_uconst;
15109 loc_descr = new_loc_descr (op, offset, 0);
15110 }
15111 }
15112
15113 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15114 }
15115
15116 /* Writes integer values to dw_vec_const array. */
15117
15118 static void
15119 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15120 {
15121 while (size != 0)
15122 {
15123 *dest++ = val & 0xff;
15124 val >>= 8;
15125 --size;
15126 }
15127 }
15128
15129 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15130
15131 static HOST_WIDE_INT
15132 extract_int (const unsigned char *src, unsigned int size)
15133 {
15134 HOST_WIDE_INT val = 0;
15135
15136 src += size;
15137 while (size != 0)
15138 {
15139 val <<= 8;
15140 val |= *--src & 0xff;
15141 --size;
15142 }
15143 return val;
15144 }
15145
15146 /* Writes wide_int values to dw_vec_const array. */
15147
15148 static void
15149 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15150 {
15151 int i;
15152
15153 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15154 {
15155 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15156 return;
15157 }
15158
15159 /* We'd have to extend this code to support odd sizes. */
15160 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15161
15162 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15163
15164 if (WORDS_BIG_ENDIAN)
15165 for (i = n - 1; i >= 0; i--)
15166 {
15167 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15168 dest += sizeof (HOST_WIDE_INT);
15169 }
15170 else
15171 for (i = 0; i < n; i++)
15172 {
15173 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15174 dest += sizeof (HOST_WIDE_INT);
15175 }
15176 }
15177
15178 /* Writes floating point values to dw_vec_const array. */
15179
15180 static void
15181 insert_float (const_rtx rtl, unsigned char *array)
15182 {
15183 REAL_VALUE_TYPE rv;
15184 long val[4];
15185 int i;
15186
15187 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15188 real_to_target (val, &rv, GET_MODE (rtl));
15189
15190 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15191 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15192 {
15193 insert_int (val[i], 4, array);
15194 array += 4;
15195 }
15196 }
15197
15198 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15199 does not have a "location" either in memory or in a register. These
15200 things can arise in GNU C when a constant is passed as an actual parameter
15201 to an inlined function. They can also arise in C++ where declared
15202 constants do not necessarily get memory "homes". */
15203
15204 static bool
15205 add_const_value_attribute (dw_die_ref die, rtx rtl)
15206 {
15207 switch (GET_CODE (rtl))
15208 {
15209 case CONST_INT:
15210 {
15211 HOST_WIDE_INT val = INTVAL (rtl);
15212
15213 if (val < 0)
15214 add_AT_int (die, DW_AT_const_value, val);
15215 else
15216 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15217 }
15218 return true;
15219
15220 case CONST_WIDE_INT:
15221 add_AT_wide (die, DW_AT_const_value,
15222 std::make_pair (rtl, GET_MODE (rtl)));
15223 return true;
15224
15225 case CONST_DOUBLE:
15226 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15227 floating-point constant. A CONST_DOUBLE is used whenever the
15228 constant requires more than one word in order to be adequately
15229 represented. */
15230 {
15231 enum machine_mode mode = GET_MODE (rtl);
15232
15233 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15234 add_AT_double (die, DW_AT_const_value,
15235 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15236 else
15237 {
15238 unsigned int length = GET_MODE_SIZE (mode);
15239 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15240
15241 insert_float (rtl, array);
15242 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15243 }
15244 }
15245 return true;
15246
15247 case CONST_VECTOR:
15248 {
15249 enum machine_mode mode = GET_MODE (rtl);
15250 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15251 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15252 unsigned char *array
15253 = ggc_vec_alloc<unsigned char> (length * elt_size);
15254 unsigned int i;
15255 unsigned char *p;
15256 enum machine_mode imode = GET_MODE_INNER (mode);
15257
15258 switch (GET_MODE_CLASS (mode))
15259 {
15260 case MODE_VECTOR_INT:
15261 for (i = 0, p = array; i < length; i++, p += elt_size)
15262 {
15263 rtx elt = CONST_VECTOR_ELT (rtl, i);
15264 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15265 }
15266 break;
15267
15268 case MODE_VECTOR_FLOAT:
15269 for (i = 0, p = array; i < length; i++, p += elt_size)
15270 {
15271 rtx elt = CONST_VECTOR_ELT (rtl, i);
15272 insert_float (elt, p);
15273 }
15274 break;
15275
15276 default:
15277 gcc_unreachable ();
15278 }
15279
15280 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15281 }
15282 return true;
15283
15284 case CONST_STRING:
15285 if (dwarf_version >= 4 || !dwarf_strict)
15286 {
15287 dw_loc_descr_ref loc_result;
15288 resolve_one_addr (&rtl, NULL);
15289 rtl_addr:
15290 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15291 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15292 add_AT_loc (die, DW_AT_location, loc_result);
15293 vec_safe_push (used_rtx_array, rtl);
15294 return true;
15295 }
15296 return false;
15297
15298 case CONST:
15299 if (CONSTANT_P (XEXP (rtl, 0)))
15300 return add_const_value_attribute (die, XEXP (rtl, 0));
15301 /* FALLTHROUGH */
15302 case SYMBOL_REF:
15303 if (!const_ok_for_output (rtl))
15304 return false;
15305 case LABEL_REF:
15306 if (dwarf_version >= 4 || !dwarf_strict)
15307 goto rtl_addr;
15308 return false;
15309
15310 case PLUS:
15311 /* In cases where an inlined instance of an inline function is passed
15312 the address of an `auto' variable (which is local to the caller) we
15313 can get a situation where the DECL_RTL of the artificial local
15314 variable (for the inlining) which acts as a stand-in for the
15315 corresponding formal parameter (of the inline function) will look
15316 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15317 exactly a compile-time constant expression, but it isn't the address
15318 of the (artificial) local variable either. Rather, it represents the
15319 *value* which the artificial local variable always has during its
15320 lifetime. We currently have no way to represent such quasi-constant
15321 values in Dwarf, so for now we just punt and generate nothing. */
15322 return false;
15323
15324 case HIGH:
15325 case CONST_FIXED:
15326 return false;
15327
15328 case MEM:
15329 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15330 && MEM_READONLY_P (rtl)
15331 && GET_MODE (rtl) == BLKmode)
15332 {
15333 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15334 return true;
15335 }
15336 return false;
15337
15338 default:
15339 /* No other kinds of rtx should be possible here. */
15340 gcc_unreachable ();
15341 }
15342 return false;
15343 }
15344
15345 /* Determine whether the evaluation of EXPR references any variables
15346 or functions which aren't otherwise used (and therefore may not be
15347 output). */
15348 static tree
15349 reference_to_unused (tree * tp, int * walk_subtrees,
15350 void * data ATTRIBUTE_UNUSED)
15351 {
15352 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15353 *walk_subtrees = 0;
15354
15355 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15356 && ! TREE_ASM_WRITTEN (*tp))
15357 return *tp;
15358 /* ??? The C++ FE emits debug information for using decls, so
15359 putting gcc_unreachable here falls over. See PR31899. For now
15360 be conservative. */
15361 else if (!cgraph_global_info_ready
15362 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15363 return *tp;
15364 else if (TREE_CODE (*tp) == VAR_DECL)
15365 {
15366 varpool_node *node = varpool_get_node (*tp);
15367 if (!node || !node->definition)
15368 return *tp;
15369 }
15370 else if (TREE_CODE (*tp) == FUNCTION_DECL
15371 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15372 {
15373 /* The call graph machinery must have finished analyzing,
15374 optimizing and gimplifying the CU by now.
15375 So if *TP has no call graph node associated
15376 to it, it means *TP will not be emitted. */
15377 if (!cgraph_get_node (*tp))
15378 return *tp;
15379 }
15380 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15381 return *tp;
15382
15383 return NULL_TREE;
15384 }
15385
15386 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15387 for use in a later add_const_value_attribute call. */
15388
15389 static rtx
15390 rtl_for_decl_init (tree init, tree type)
15391 {
15392 rtx rtl = NULL_RTX;
15393
15394 STRIP_NOPS (init);
15395
15396 /* If a variable is initialized with a string constant without embedded
15397 zeros, build CONST_STRING. */
15398 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15399 {
15400 tree enttype = TREE_TYPE (type);
15401 tree domain = TYPE_DOMAIN (type);
15402 enum machine_mode mode = TYPE_MODE (enttype);
15403
15404 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15405 && domain
15406 && integer_zerop (TYPE_MIN_VALUE (domain))
15407 && compare_tree_int (TYPE_MAX_VALUE (domain),
15408 TREE_STRING_LENGTH (init) - 1) == 0
15409 && ((size_t) TREE_STRING_LENGTH (init)
15410 == strlen (TREE_STRING_POINTER (init)) + 1))
15411 {
15412 rtl = gen_rtx_CONST_STRING (VOIDmode,
15413 ggc_strdup (TREE_STRING_POINTER (init)));
15414 rtl = gen_rtx_MEM (BLKmode, rtl);
15415 MEM_READONLY_P (rtl) = 1;
15416 }
15417 }
15418 /* Other aggregates, and complex values, could be represented using
15419 CONCAT: FIXME! */
15420 else if (AGGREGATE_TYPE_P (type)
15421 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15422 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15423 || TREE_CODE (type) == COMPLEX_TYPE)
15424 ;
15425 /* Vectors only work if their mode is supported by the target.
15426 FIXME: generic vectors ought to work too. */
15427 else if (TREE_CODE (type) == VECTOR_TYPE
15428 && !VECTOR_MODE_P (TYPE_MODE (type)))
15429 ;
15430 /* If the initializer is something that we know will expand into an
15431 immediate RTL constant, expand it now. We must be careful not to
15432 reference variables which won't be output. */
15433 else if (initializer_constant_valid_p (init, type)
15434 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15435 {
15436 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15437 possible. */
15438 if (TREE_CODE (type) == VECTOR_TYPE)
15439 switch (TREE_CODE (init))
15440 {
15441 case VECTOR_CST:
15442 break;
15443 case CONSTRUCTOR:
15444 if (TREE_CONSTANT (init))
15445 {
15446 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15447 bool constant_p = true;
15448 tree value;
15449 unsigned HOST_WIDE_INT ix;
15450
15451 /* Even when ctor is constant, it might contain non-*_CST
15452 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15453 belong into VECTOR_CST nodes. */
15454 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15455 if (!CONSTANT_CLASS_P (value))
15456 {
15457 constant_p = false;
15458 break;
15459 }
15460
15461 if (constant_p)
15462 {
15463 init = build_vector_from_ctor (type, elts);
15464 break;
15465 }
15466 }
15467 /* FALLTHRU */
15468
15469 default:
15470 return NULL;
15471 }
15472
15473 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15474
15475 /* If expand_expr returns a MEM, it wasn't immediate. */
15476 gcc_assert (!rtl || !MEM_P (rtl));
15477 }
15478
15479 return rtl;
15480 }
15481
15482 /* Generate RTL for the variable DECL to represent its location. */
15483
15484 static rtx
15485 rtl_for_decl_location (tree decl)
15486 {
15487 rtx rtl;
15488
15489 /* Here we have to decide where we are going to say the parameter "lives"
15490 (as far as the debugger is concerned). We only have a couple of
15491 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15492
15493 DECL_RTL normally indicates where the parameter lives during most of the
15494 activation of the function. If optimization is enabled however, this
15495 could be either NULL or else a pseudo-reg. Both of those cases indicate
15496 that the parameter doesn't really live anywhere (as far as the code
15497 generation parts of GCC are concerned) during most of the function's
15498 activation. That will happen (for example) if the parameter is never
15499 referenced within the function.
15500
15501 We could just generate a location descriptor here for all non-NULL
15502 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15503 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15504 where DECL_RTL is NULL or is a pseudo-reg.
15505
15506 Note however that we can only get away with using DECL_INCOMING_RTL as
15507 a backup substitute for DECL_RTL in certain limited cases. In cases
15508 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15509 we can be sure that the parameter was passed using the same type as it is
15510 declared to have within the function, and that its DECL_INCOMING_RTL
15511 points us to a place where a value of that type is passed.
15512
15513 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15514 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15515 because in these cases DECL_INCOMING_RTL points us to a value of some
15516 type which is *different* from the type of the parameter itself. Thus,
15517 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15518 such cases, the debugger would end up (for example) trying to fetch a
15519 `float' from a place which actually contains the first part of a
15520 `double'. That would lead to really incorrect and confusing
15521 output at debug-time.
15522
15523 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15524 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15525 are a couple of exceptions however. On little-endian machines we can
15526 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15527 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15528 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15529 when (on a little-endian machine) a non-prototyped function has a
15530 parameter declared to be of type `short' or `char'. In such cases,
15531 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15532 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15533 passed `int' value. If the debugger then uses that address to fetch
15534 a `short' or a `char' (on a little-endian machine) the result will be
15535 the correct data, so we allow for such exceptional cases below.
15536
15537 Note that our goal here is to describe the place where the given formal
15538 parameter lives during most of the function's activation (i.e. between the
15539 end of the prologue and the start of the epilogue). We'll do that as best
15540 as we can. Note however that if the given formal parameter is modified
15541 sometime during the execution of the function, then a stack backtrace (at
15542 debug-time) will show the function as having been called with the *new*
15543 value rather than the value which was originally passed in. This happens
15544 rarely enough that it is not a major problem, but it *is* a problem, and
15545 I'd like to fix it.
15546
15547 A future version of dwarf2out.c may generate two additional attributes for
15548 any given DW_TAG_formal_parameter DIE which will describe the "passed
15549 type" and the "passed location" for the given formal parameter in addition
15550 to the attributes we now generate to indicate the "declared type" and the
15551 "active location" for each parameter. This additional set of attributes
15552 could be used by debuggers for stack backtraces. Separately, note that
15553 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15554 This happens (for example) for inlined-instances of inline function formal
15555 parameters which are never referenced. This really shouldn't be
15556 happening. All PARM_DECL nodes should get valid non-NULL
15557 DECL_INCOMING_RTL values. FIXME. */
15558
15559 /* Use DECL_RTL as the "location" unless we find something better. */
15560 rtl = DECL_RTL_IF_SET (decl);
15561
15562 /* When generating abstract instances, ignore everything except
15563 constants, symbols living in memory, and symbols living in
15564 fixed registers. */
15565 if (! reload_completed)
15566 {
15567 if (rtl
15568 && (CONSTANT_P (rtl)
15569 || (MEM_P (rtl)
15570 && CONSTANT_P (XEXP (rtl, 0)))
15571 || (REG_P (rtl)
15572 && TREE_CODE (decl) == VAR_DECL
15573 && TREE_STATIC (decl))))
15574 {
15575 rtl = targetm.delegitimize_address (rtl);
15576 return rtl;
15577 }
15578 rtl = NULL_RTX;
15579 }
15580 else if (TREE_CODE (decl) == PARM_DECL)
15581 {
15582 if (rtl == NULL_RTX
15583 || is_pseudo_reg (rtl)
15584 || (MEM_P (rtl)
15585 && is_pseudo_reg (XEXP (rtl, 0))
15586 && DECL_INCOMING_RTL (decl)
15587 && MEM_P (DECL_INCOMING_RTL (decl))
15588 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15589 {
15590 tree declared_type = TREE_TYPE (decl);
15591 tree passed_type = DECL_ARG_TYPE (decl);
15592 enum machine_mode dmode = TYPE_MODE (declared_type);
15593 enum machine_mode pmode = TYPE_MODE (passed_type);
15594
15595 /* This decl represents a formal parameter which was optimized out.
15596 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15597 all cases where (rtl == NULL_RTX) just below. */
15598 if (dmode == pmode)
15599 rtl = DECL_INCOMING_RTL (decl);
15600 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15601 && SCALAR_INT_MODE_P (dmode)
15602 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15603 && DECL_INCOMING_RTL (decl))
15604 {
15605 rtx inc = DECL_INCOMING_RTL (decl);
15606 if (REG_P (inc))
15607 rtl = inc;
15608 else if (MEM_P (inc))
15609 {
15610 if (BYTES_BIG_ENDIAN)
15611 rtl = adjust_address_nv (inc, dmode,
15612 GET_MODE_SIZE (pmode)
15613 - GET_MODE_SIZE (dmode));
15614 else
15615 rtl = inc;
15616 }
15617 }
15618 }
15619
15620 /* If the parm was passed in registers, but lives on the stack, then
15621 make a big endian correction if the mode of the type of the
15622 parameter is not the same as the mode of the rtl. */
15623 /* ??? This is the same series of checks that are made in dbxout.c before
15624 we reach the big endian correction code there. It isn't clear if all
15625 of these checks are necessary here, but keeping them all is the safe
15626 thing to do. */
15627 else if (MEM_P (rtl)
15628 && XEXP (rtl, 0) != const0_rtx
15629 && ! CONSTANT_P (XEXP (rtl, 0))
15630 /* Not passed in memory. */
15631 && !MEM_P (DECL_INCOMING_RTL (decl))
15632 /* Not passed by invisible reference. */
15633 && (!REG_P (XEXP (rtl, 0))
15634 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15635 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15636 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15637 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15638 #endif
15639 )
15640 /* Big endian correction check. */
15641 && BYTES_BIG_ENDIAN
15642 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15643 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15644 < UNITS_PER_WORD))
15645 {
15646 enum machine_mode addr_mode = get_address_mode (rtl);
15647 int offset = (UNITS_PER_WORD
15648 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15649
15650 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15651 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15652 }
15653 }
15654 else if (TREE_CODE (decl) == VAR_DECL
15655 && rtl
15656 && MEM_P (rtl)
15657 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15658 && BYTES_BIG_ENDIAN)
15659 {
15660 enum machine_mode addr_mode = get_address_mode (rtl);
15661 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15662 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15663
15664 /* If a variable is declared "register" yet is smaller than
15665 a register, then if we store the variable to memory, it
15666 looks like we're storing a register-sized value, when in
15667 fact we are not. We need to adjust the offset of the
15668 storage location to reflect the actual value's bytes,
15669 else gdb will not be able to display it. */
15670 if (rsize > dsize)
15671 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15672 plus_constant (addr_mode, XEXP (rtl, 0),
15673 rsize - dsize));
15674 }
15675
15676 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15677 and will have been substituted directly into all expressions that use it.
15678 C does not have such a concept, but C++ and other languages do. */
15679 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15680 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15681
15682 if (rtl)
15683 rtl = targetm.delegitimize_address (rtl);
15684
15685 /* If we don't look past the constant pool, we risk emitting a
15686 reference to a constant pool entry that isn't referenced from
15687 code, and thus is not emitted. */
15688 if (rtl)
15689 rtl = avoid_constant_pool_reference (rtl);
15690
15691 /* Try harder to get a rtl. If this symbol ends up not being emitted
15692 in the current CU, resolve_addr will remove the expression referencing
15693 it. */
15694 if (rtl == NULL_RTX
15695 && TREE_CODE (decl) == VAR_DECL
15696 && !DECL_EXTERNAL (decl)
15697 && TREE_STATIC (decl)
15698 && DECL_NAME (decl)
15699 && !DECL_HARD_REGISTER (decl)
15700 && DECL_MODE (decl) != VOIDmode)
15701 {
15702 rtl = make_decl_rtl_for_debug (decl);
15703 if (!MEM_P (rtl)
15704 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15705 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15706 rtl = NULL_RTX;
15707 }
15708
15709 return rtl;
15710 }
15711
15712 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15713 returned. If so, the decl for the COMMON block is returned, and the
15714 value is the offset into the common block for the symbol. */
15715
15716 static tree
15717 fortran_common (tree decl, HOST_WIDE_INT *value)
15718 {
15719 tree val_expr, cvar;
15720 enum machine_mode mode;
15721 HOST_WIDE_INT bitsize, bitpos;
15722 tree offset;
15723 int unsignedp, volatilep = 0;
15724
15725 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15726 it does not have a value (the offset into the common area), or if it
15727 is thread local (as opposed to global) then it isn't common, and shouldn't
15728 be handled as such. */
15729 if (TREE_CODE (decl) != VAR_DECL
15730 || !TREE_STATIC (decl)
15731 || !DECL_HAS_VALUE_EXPR_P (decl)
15732 || !is_fortran ())
15733 return NULL_TREE;
15734
15735 val_expr = DECL_VALUE_EXPR (decl);
15736 if (TREE_CODE (val_expr) != COMPONENT_REF)
15737 return NULL_TREE;
15738
15739 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15740 &mode, &unsignedp, &volatilep, true);
15741
15742 if (cvar == NULL_TREE
15743 || TREE_CODE (cvar) != VAR_DECL
15744 || DECL_ARTIFICIAL (cvar)
15745 || !TREE_PUBLIC (cvar))
15746 return NULL_TREE;
15747
15748 *value = 0;
15749 if (offset != NULL)
15750 {
15751 if (!tree_fits_shwi_p (offset))
15752 return NULL_TREE;
15753 *value = tree_to_shwi (offset);
15754 }
15755 if (bitpos != 0)
15756 *value += bitpos / BITS_PER_UNIT;
15757
15758 return cvar;
15759 }
15760
15761 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15762 data attribute for a variable or a parameter. We generate the
15763 DW_AT_const_value attribute only in those cases where the given variable
15764 or parameter does not have a true "location" either in memory or in a
15765 register. This can happen (for example) when a constant is passed as an
15766 actual argument in a call to an inline function. (It's possible that
15767 these things can crop up in other ways also.) Note that one type of
15768 constant value which can be passed into an inlined function is a constant
15769 pointer. This can happen for example if an actual argument in an inlined
15770 function call evaluates to a compile-time constant address.
15771
15772 CACHE_P is true if it is worth caching the location list for DECL,
15773 so that future calls can reuse it rather than regenerate it from scratch.
15774 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15775 since we will need to refer to them each time the function is inlined. */
15776
15777 static bool
15778 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15779 enum dwarf_attribute attr)
15780 {
15781 rtx rtl;
15782 dw_loc_list_ref list;
15783 var_loc_list *loc_list;
15784 cached_dw_loc_list *cache;
15785 void **slot;
15786
15787 if (TREE_CODE (decl) == ERROR_MARK)
15788 return false;
15789
15790 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15791 || TREE_CODE (decl) == RESULT_DECL);
15792
15793 /* Try to get some constant RTL for this decl, and use that as the value of
15794 the location. */
15795
15796 rtl = rtl_for_decl_location (decl);
15797 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15798 && add_const_value_attribute (die, rtl))
15799 return true;
15800
15801 /* See if we have single element location list that is equivalent to
15802 a constant value. That way we are better to use add_const_value_attribute
15803 rather than expanding constant value equivalent. */
15804 loc_list = lookup_decl_loc (decl);
15805 if (loc_list
15806 && loc_list->first
15807 && loc_list->first->next == NULL
15808 && NOTE_P (loc_list->first->loc)
15809 && NOTE_VAR_LOCATION (loc_list->first->loc)
15810 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15811 {
15812 struct var_loc_node *node;
15813
15814 node = loc_list->first;
15815 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15816 if (GET_CODE (rtl) == EXPR_LIST)
15817 rtl = XEXP (rtl, 0);
15818 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15819 && add_const_value_attribute (die, rtl))
15820 return true;
15821 }
15822 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15823 list several times. See if we've already cached the contents. */
15824 list = NULL;
15825 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15826 cache_p = false;
15827 if (cache_p)
15828 {
15829 cache = (cached_dw_loc_list *)
15830 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15831 if (cache)
15832 list = cache->loc_list;
15833 }
15834 if (list == NULL)
15835 {
15836 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15837 /* It is usually worth caching this result if the decl is from
15838 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15839 if (cache_p && list && list->dw_loc_next)
15840 {
15841 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15842 DECL_UID (decl), INSERT);
15843 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15844 cache->decl_id = DECL_UID (decl);
15845 cache->loc_list = list;
15846 *slot = cache;
15847 }
15848 }
15849 if (list)
15850 {
15851 add_AT_location_description (die, attr, list);
15852 return true;
15853 }
15854 /* None of that worked, so it must not really have a location;
15855 try adding a constant value attribute from the DECL_INITIAL. */
15856 return tree_add_const_value_attribute_for_decl (die, decl);
15857 }
15858
15859 /* Add VARIABLE and DIE into deferred locations list. */
15860
15861 static void
15862 defer_location (tree variable, dw_die_ref die)
15863 {
15864 deferred_locations entry;
15865 entry.variable = variable;
15866 entry.die = die;
15867 vec_safe_push (deferred_locations_list, entry);
15868 }
15869
15870 /* Helper function for tree_add_const_value_attribute. Natively encode
15871 initializer INIT into an array. Return true if successful. */
15872
15873 static bool
15874 native_encode_initializer (tree init, unsigned char *array, int size)
15875 {
15876 tree type;
15877
15878 if (init == NULL_TREE)
15879 return false;
15880
15881 STRIP_NOPS (init);
15882 switch (TREE_CODE (init))
15883 {
15884 case STRING_CST:
15885 type = TREE_TYPE (init);
15886 if (TREE_CODE (type) == ARRAY_TYPE)
15887 {
15888 tree enttype = TREE_TYPE (type);
15889 enum machine_mode mode = TYPE_MODE (enttype);
15890
15891 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15892 return false;
15893 if (int_size_in_bytes (type) != size)
15894 return false;
15895 if (size > TREE_STRING_LENGTH (init))
15896 {
15897 memcpy (array, TREE_STRING_POINTER (init),
15898 TREE_STRING_LENGTH (init));
15899 memset (array + TREE_STRING_LENGTH (init),
15900 '\0', size - TREE_STRING_LENGTH (init));
15901 }
15902 else
15903 memcpy (array, TREE_STRING_POINTER (init), size);
15904 return true;
15905 }
15906 return false;
15907 case CONSTRUCTOR:
15908 type = TREE_TYPE (init);
15909 if (int_size_in_bytes (type) != size)
15910 return false;
15911 if (TREE_CODE (type) == ARRAY_TYPE)
15912 {
15913 HOST_WIDE_INT min_index;
15914 unsigned HOST_WIDE_INT cnt;
15915 int curpos = 0, fieldsize;
15916 constructor_elt *ce;
15917
15918 if (TYPE_DOMAIN (type) == NULL_TREE
15919 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15920 return false;
15921
15922 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15923 if (fieldsize <= 0)
15924 return false;
15925
15926 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15927 memset (array, '\0', size);
15928 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15929 {
15930 tree val = ce->value;
15931 tree index = ce->index;
15932 int pos = curpos;
15933 if (index && TREE_CODE (index) == RANGE_EXPR)
15934 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15935 * fieldsize;
15936 else if (index)
15937 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15938
15939 if (val)
15940 {
15941 STRIP_NOPS (val);
15942 if (!native_encode_initializer (val, array + pos, fieldsize))
15943 return false;
15944 }
15945 curpos = pos + fieldsize;
15946 if (index && TREE_CODE (index) == RANGE_EXPR)
15947 {
15948 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15949 - tree_to_shwi (TREE_OPERAND (index, 0));
15950 while (count-- > 0)
15951 {
15952 if (val)
15953 memcpy (array + curpos, array + pos, fieldsize);
15954 curpos += fieldsize;
15955 }
15956 }
15957 gcc_assert (curpos <= size);
15958 }
15959 return true;
15960 }
15961 else if (TREE_CODE (type) == RECORD_TYPE
15962 || TREE_CODE (type) == UNION_TYPE)
15963 {
15964 tree field = NULL_TREE;
15965 unsigned HOST_WIDE_INT cnt;
15966 constructor_elt *ce;
15967
15968 if (int_size_in_bytes (type) != size)
15969 return false;
15970
15971 if (TREE_CODE (type) == RECORD_TYPE)
15972 field = TYPE_FIELDS (type);
15973
15974 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15975 {
15976 tree val = ce->value;
15977 int pos, fieldsize;
15978
15979 if (ce->index != 0)
15980 field = ce->index;
15981
15982 if (val)
15983 STRIP_NOPS (val);
15984
15985 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15986 return false;
15987
15988 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15989 && TYPE_DOMAIN (TREE_TYPE (field))
15990 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15991 return false;
15992 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15993 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15994 return false;
15995 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15996 pos = int_byte_position (field);
15997 gcc_assert (pos + fieldsize <= size);
15998 if (val
15999 && !native_encode_initializer (val, array + pos, fieldsize))
16000 return false;
16001 }
16002 return true;
16003 }
16004 return false;
16005 case VIEW_CONVERT_EXPR:
16006 case NON_LVALUE_EXPR:
16007 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16008 default:
16009 return native_encode_expr (init, array, size) == size;
16010 }
16011 }
16012
16013 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16014 attribute is the const value T. */
16015
16016 static bool
16017 tree_add_const_value_attribute (dw_die_ref die, tree t)
16018 {
16019 tree init;
16020 tree type = TREE_TYPE (t);
16021 rtx rtl;
16022
16023 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16024 return false;
16025
16026 init = t;
16027 gcc_assert (!DECL_P (init));
16028
16029 rtl = rtl_for_decl_init (init, type);
16030 if (rtl)
16031 return add_const_value_attribute (die, rtl);
16032 /* If the host and target are sane, try harder. */
16033 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16034 && initializer_constant_valid_p (init, type))
16035 {
16036 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16037 if (size > 0 && (int) size == size)
16038 {
16039 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16040
16041 if (native_encode_initializer (init, array, size))
16042 {
16043 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16044 return true;
16045 }
16046 ggc_free (array);
16047 }
16048 }
16049 return false;
16050 }
16051
16052 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16053 attribute is the const value of T, where T is an integral constant
16054 variable with static storage duration
16055 (so it can't be a PARM_DECL or a RESULT_DECL). */
16056
16057 static bool
16058 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16059 {
16060
16061 if (!decl
16062 || (TREE_CODE (decl) != VAR_DECL
16063 && TREE_CODE (decl) != CONST_DECL)
16064 || (TREE_CODE (decl) == VAR_DECL
16065 && !TREE_STATIC (decl)))
16066 return false;
16067
16068 if (TREE_READONLY (decl)
16069 && ! TREE_THIS_VOLATILE (decl)
16070 && DECL_INITIAL (decl))
16071 /* OK */;
16072 else
16073 return false;
16074
16075 /* Don't add DW_AT_const_value if abstract origin already has one. */
16076 if (get_AT (var_die, DW_AT_const_value))
16077 return false;
16078
16079 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16080 }
16081
16082 /* Convert the CFI instructions for the current function into a
16083 location list. This is used for DW_AT_frame_base when we targeting
16084 a dwarf2 consumer that does not support the dwarf3
16085 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16086 expressions. */
16087
16088 static dw_loc_list_ref
16089 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16090 {
16091 int ix;
16092 dw_fde_ref fde;
16093 dw_loc_list_ref list, *list_tail;
16094 dw_cfi_ref cfi;
16095 dw_cfa_location last_cfa, next_cfa;
16096 const char *start_label, *last_label, *section;
16097 dw_cfa_location remember;
16098
16099 fde = cfun->fde;
16100 gcc_assert (fde != NULL);
16101
16102 section = secname_for_decl (current_function_decl);
16103 list_tail = &list;
16104 list = NULL;
16105
16106 memset (&next_cfa, 0, sizeof (next_cfa));
16107 next_cfa.reg = INVALID_REGNUM;
16108 remember = next_cfa;
16109
16110 start_label = fde->dw_fde_begin;
16111
16112 /* ??? Bald assumption that the CIE opcode list does not contain
16113 advance opcodes. */
16114 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16115 lookup_cfa_1 (cfi, &next_cfa, &remember);
16116
16117 last_cfa = next_cfa;
16118 last_label = start_label;
16119
16120 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16121 {
16122 /* If the first partition contained no CFI adjustments, the
16123 CIE opcodes apply to the whole first partition. */
16124 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16125 fde->dw_fde_begin, fde->dw_fde_end, section);
16126 list_tail =&(*list_tail)->dw_loc_next;
16127 start_label = last_label = fde->dw_fde_second_begin;
16128 }
16129
16130 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16131 {
16132 switch (cfi->dw_cfi_opc)
16133 {
16134 case DW_CFA_set_loc:
16135 case DW_CFA_advance_loc1:
16136 case DW_CFA_advance_loc2:
16137 case DW_CFA_advance_loc4:
16138 if (!cfa_equal_p (&last_cfa, &next_cfa))
16139 {
16140 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16141 start_label, last_label, section);
16142
16143 list_tail = &(*list_tail)->dw_loc_next;
16144 last_cfa = next_cfa;
16145 start_label = last_label;
16146 }
16147 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16148 break;
16149
16150 case DW_CFA_advance_loc:
16151 /* The encoding is complex enough that we should never emit this. */
16152 gcc_unreachable ();
16153
16154 default:
16155 lookup_cfa_1 (cfi, &next_cfa, &remember);
16156 break;
16157 }
16158 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16159 {
16160 if (!cfa_equal_p (&last_cfa, &next_cfa))
16161 {
16162 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16163 start_label, last_label, section);
16164
16165 list_tail = &(*list_tail)->dw_loc_next;
16166 last_cfa = next_cfa;
16167 start_label = last_label;
16168 }
16169 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16170 start_label, fde->dw_fde_end, section);
16171 list_tail = &(*list_tail)->dw_loc_next;
16172 start_label = last_label = fde->dw_fde_second_begin;
16173 }
16174 }
16175
16176 if (!cfa_equal_p (&last_cfa, &next_cfa))
16177 {
16178 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16179 start_label, last_label, section);
16180 list_tail = &(*list_tail)->dw_loc_next;
16181 start_label = last_label;
16182 }
16183
16184 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16185 start_label,
16186 fde->dw_fde_second_begin
16187 ? fde->dw_fde_second_end : fde->dw_fde_end,
16188 section);
16189
16190 if (list && list->dw_loc_next)
16191 gen_llsym (list);
16192
16193 return list;
16194 }
16195
16196 /* Compute a displacement from the "steady-state frame pointer" to the
16197 frame base (often the same as the CFA), and store it in
16198 frame_pointer_fb_offset. OFFSET is added to the displacement
16199 before the latter is negated. */
16200
16201 static void
16202 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16203 {
16204 rtx reg, elim;
16205
16206 #ifdef FRAME_POINTER_CFA_OFFSET
16207 reg = frame_pointer_rtx;
16208 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16209 #else
16210 reg = arg_pointer_rtx;
16211 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16212 #endif
16213
16214 elim = (ira_use_lra_p
16215 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16216 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16217 if (GET_CODE (elim) == PLUS)
16218 {
16219 offset += INTVAL (XEXP (elim, 1));
16220 elim = XEXP (elim, 0);
16221 }
16222
16223 frame_pointer_fb_offset = -offset;
16224
16225 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16226 in which to eliminate. This is because it's stack pointer isn't
16227 directly accessible as a register within the ISA. To work around
16228 this, assume that while we cannot provide a proper value for
16229 frame_pointer_fb_offset, we won't need one either. */
16230 frame_pointer_fb_offset_valid
16231 = ((SUPPORTS_STACK_ALIGNMENT
16232 && (elim == hard_frame_pointer_rtx
16233 || elim == stack_pointer_rtx))
16234 || elim == (frame_pointer_needed
16235 ? hard_frame_pointer_rtx
16236 : stack_pointer_rtx));
16237 }
16238
16239 /* Generate a DW_AT_name attribute given some string value to be included as
16240 the value of the attribute. */
16241
16242 static void
16243 add_name_attribute (dw_die_ref die, const char *name_string)
16244 {
16245 if (name_string != NULL && *name_string != 0)
16246 {
16247 if (demangle_name_func)
16248 name_string = (*demangle_name_func) (name_string);
16249
16250 add_AT_string (die, DW_AT_name, name_string);
16251 }
16252 }
16253
16254 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16255 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16256 of TYPE accordingly.
16257
16258 ??? This is a temporary measure until after we're able to generate
16259 regular DWARF for the complex Ada type system. */
16260
16261 static void
16262 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16263 dw_die_ref context_die)
16264 {
16265 tree dtype;
16266 dw_die_ref dtype_die;
16267
16268 if (!lang_hooks.types.descriptive_type)
16269 return;
16270
16271 dtype = lang_hooks.types.descriptive_type (type);
16272 if (!dtype)
16273 return;
16274
16275 dtype_die = lookup_type_die (dtype);
16276 if (!dtype_die)
16277 {
16278 gen_type_die (dtype, context_die);
16279 dtype_die = lookup_type_die (dtype);
16280 gcc_assert (dtype_die);
16281 }
16282
16283 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16284 }
16285
16286 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16287
16288 static const char *
16289 comp_dir_string (void)
16290 {
16291 const char *wd;
16292 char *wd1;
16293 static const char *cached_wd = NULL;
16294
16295 if (cached_wd != NULL)
16296 return cached_wd;
16297
16298 wd = get_src_pwd ();
16299 if (wd == NULL)
16300 return NULL;
16301
16302 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16303 {
16304 int wdlen;
16305
16306 wdlen = strlen (wd);
16307 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16308 strcpy (wd1, wd);
16309 wd1 [wdlen] = DIR_SEPARATOR;
16310 wd1 [wdlen + 1] = 0;
16311 wd = wd1;
16312 }
16313
16314 cached_wd = remap_debug_filename (wd);
16315 return cached_wd;
16316 }
16317
16318 /* Generate a DW_AT_comp_dir attribute for DIE. */
16319
16320 static void
16321 add_comp_dir_attribute (dw_die_ref die)
16322 {
16323 const char * wd = comp_dir_string ();
16324 if (wd != NULL)
16325 add_AT_string (die, DW_AT_comp_dir, wd);
16326 }
16327
16328 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16329 default. */
16330
16331 static int
16332 lower_bound_default (void)
16333 {
16334 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16335 {
16336 case DW_LANG_C:
16337 case DW_LANG_C89:
16338 case DW_LANG_C99:
16339 case DW_LANG_C_plus_plus:
16340 case DW_LANG_ObjC:
16341 case DW_LANG_ObjC_plus_plus:
16342 case DW_LANG_Java:
16343 return 0;
16344 case DW_LANG_Fortran77:
16345 case DW_LANG_Fortran90:
16346 case DW_LANG_Fortran95:
16347 return 1;
16348 case DW_LANG_UPC:
16349 case DW_LANG_D:
16350 case DW_LANG_Python:
16351 return dwarf_version >= 4 ? 0 : -1;
16352 case DW_LANG_Ada95:
16353 case DW_LANG_Ada83:
16354 case DW_LANG_Cobol74:
16355 case DW_LANG_Cobol85:
16356 case DW_LANG_Pascal83:
16357 case DW_LANG_Modula2:
16358 case DW_LANG_PLI:
16359 return dwarf_version >= 4 ? 1 : -1;
16360 default:
16361 return -1;
16362 }
16363 }
16364
16365 /* Given a tree node describing an array bound (either lower or upper) output
16366 a representation for that bound. */
16367
16368 static void
16369 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16370 {
16371 switch (TREE_CODE (bound))
16372 {
16373 case ERROR_MARK:
16374 return;
16375
16376 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16377 case INTEGER_CST:
16378 {
16379 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16380 int dflt;
16381
16382 /* Use the default if possible. */
16383 if (bound_attr == DW_AT_lower_bound
16384 && tree_fits_shwi_p (bound)
16385 && (dflt = lower_bound_default ()) != -1
16386 && tree_to_shwi (bound) == dflt)
16387 ;
16388
16389 /* If HOST_WIDE_INT is big enough then represent the bound as
16390 a constant value. We need to choose a form based on
16391 whether the type is signed or unsigned. We cannot just
16392 call add_AT_unsigned if the value itself is positive
16393 (add_AT_unsigned might add the unsigned value encoded as
16394 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16395 bounds type and then sign extend any unsigned values found
16396 for signed types. This is needed only for
16397 DW_AT_{lower,upper}_bound, since for most other attributes,
16398 consumers will treat DW_FORM_data[1248] as unsigned values,
16399 regardless of the underlying type. */
16400 else if (prec <= HOST_BITS_PER_WIDE_INT
16401 || tree_fits_uhwi_p (bound))
16402 {
16403 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16404 add_AT_unsigned (subrange_die, bound_attr,
16405 TREE_INT_CST_LOW (bound));
16406 else
16407 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16408 }
16409 else
16410 /* Otherwise represent the bound as an unsigned value with
16411 the precision of its type. The precision and signedness
16412 of the type will be necessary to re-interpret it
16413 unambiguously. */
16414 add_AT_wide (subrange_die, bound_attr, bound);
16415 }
16416 break;
16417
16418 CASE_CONVERT:
16419 case VIEW_CONVERT_EXPR:
16420 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16421 break;
16422
16423 case SAVE_EXPR:
16424 break;
16425
16426 case VAR_DECL:
16427 case PARM_DECL:
16428 case RESULT_DECL:
16429 {
16430 dw_die_ref decl_die = lookup_decl_die (bound);
16431
16432 /* ??? Can this happen, or should the variable have been bound
16433 first? Probably it can, since I imagine that we try to create
16434 the types of parameters in the order in which they exist in
16435 the list, and won't have created a forward reference to a
16436 later parameter. */
16437 if (decl_die != NULL)
16438 {
16439 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16440 break;
16441 }
16442 }
16443 /* FALLTHRU */
16444
16445 default:
16446 {
16447 /* Otherwise try to create a stack operation procedure to
16448 evaluate the value of the array bound. */
16449
16450 dw_die_ref ctx, decl_die;
16451 dw_loc_list_ref list;
16452
16453 list = loc_list_from_tree (bound, 2);
16454 if (list == NULL || single_element_loc_list_p (list))
16455 {
16456 /* If DW_AT_*bound is not a reference nor constant, it is
16457 a DWARF expression rather than location description.
16458 For that loc_list_from_tree (bound, 0) is needed.
16459 If that fails to give a single element list,
16460 fall back to outputting this as a reference anyway. */
16461 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16462 if (list2 && single_element_loc_list_p (list2))
16463 {
16464 add_AT_loc (subrange_die, bound_attr, list2->expr);
16465 break;
16466 }
16467 }
16468 if (list == NULL)
16469 break;
16470
16471 if (current_function_decl == 0)
16472 ctx = comp_unit_die ();
16473 else
16474 ctx = lookup_decl_die (current_function_decl);
16475
16476 decl_die = new_die (DW_TAG_variable, ctx, bound);
16477 add_AT_flag (decl_die, DW_AT_artificial, 1);
16478 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16479 add_AT_location_description (decl_die, DW_AT_location, list);
16480 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16481 break;
16482 }
16483 }
16484 }
16485
16486 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16487 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16488 Note that the block of subscript information for an array type also
16489 includes information about the element type of the given array type. */
16490
16491 static void
16492 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16493 {
16494 unsigned dimension_number;
16495 tree lower, upper;
16496 dw_die_ref subrange_die;
16497
16498 for (dimension_number = 0;
16499 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16500 type = TREE_TYPE (type), dimension_number++)
16501 {
16502 tree domain = TYPE_DOMAIN (type);
16503
16504 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16505 break;
16506
16507 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16508 and (in GNU C only) variable bounds. Handle all three forms
16509 here. */
16510 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16511 if (domain)
16512 {
16513 /* We have an array type with specified bounds. */
16514 lower = TYPE_MIN_VALUE (domain);
16515 upper = TYPE_MAX_VALUE (domain);
16516
16517 /* Define the index type. */
16518 if (TREE_TYPE (domain))
16519 {
16520 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16521 TREE_TYPE field. We can't emit debug info for this
16522 because it is an unnamed integral type. */
16523 if (TREE_CODE (domain) == INTEGER_TYPE
16524 && TYPE_NAME (domain) == NULL_TREE
16525 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16526 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16527 ;
16528 else
16529 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16530 type_die);
16531 }
16532
16533 /* ??? If upper is NULL, the array has unspecified length,
16534 but it does have a lower bound. This happens with Fortran
16535 dimension arr(N:*)
16536 Since the debugger is definitely going to need to know N
16537 to produce useful results, go ahead and output the lower
16538 bound solo, and hope the debugger can cope. */
16539
16540 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16541 if (upper)
16542 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16543 }
16544
16545 /* Otherwise we have an array type with an unspecified length. The
16546 DWARF-2 spec does not say how to handle this; let's just leave out the
16547 bounds. */
16548 }
16549 }
16550
16551 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16552
16553 static void
16554 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16555 {
16556 dw_die_ref decl_die;
16557 HOST_WIDE_INT size;
16558
16559 switch (TREE_CODE (tree_node))
16560 {
16561 case ERROR_MARK:
16562 size = 0;
16563 break;
16564 case ENUMERAL_TYPE:
16565 case RECORD_TYPE:
16566 case UNION_TYPE:
16567 case QUAL_UNION_TYPE:
16568 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16569 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16570 {
16571 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16572 return;
16573 }
16574 size = int_size_in_bytes (tree_node);
16575 break;
16576 case FIELD_DECL:
16577 /* For a data member of a struct or union, the DW_AT_byte_size is
16578 generally given as the number of bytes normally allocated for an
16579 object of the *declared* type of the member itself. This is true
16580 even for bit-fields. */
16581 size = int_size_in_bytes (field_type (tree_node));
16582 break;
16583 default:
16584 gcc_unreachable ();
16585 }
16586
16587 /* Note that `size' might be -1 when we get to this point. If it is, that
16588 indicates that the byte size of the entity in question is variable. We
16589 have no good way of expressing this fact in Dwarf at the present time,
16590 when location description was not used by the caller code instead. */
16591 if (size >= 0)
16592 add_AT_unsigned (die, DW_AT_byte_size, size);
16593 }
16594
16595 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16596 which specifies the distance in bits from the highest order bit of the
16597 "containing object" for the bit-field to the highest order bit of the
16598 bit-field itself.
16599
16600 For any given bit-field, the "containing object" is a hypothetical object
16601 (of some integral or enum type) within which the given bit-field lives. The
16602 type of this hypothetical "containing object" is always the same as the
16603 declared type of the individual bit-field itself. The determination of the
16604 exact location of the "containing object" for a bit-field is rather
16605 complicated. It's handled by the `field_byte_offset' function (above).
16606
16607 Note that it is the size (in bytes) of the hypothetical "containing object"
16608 which will be given in the DW_AT_byte_size attribute for this bit-field.
16609 (See `byte_size_attribute' above). */
16610
16611 static inline void
16612 add_bit_offset_attribute (dw_die_ref die, tree decl)
16613 {
16614 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16615 tree type = DECL_BIT_FIELD_TYPE (decl);
16616 HOST_WIDE_INT bitpos_int;
16617 HOST_WIDE_INT highest_order_object_bit_offset;
16618 HOST_WIDE_INT highest_order_field_bit_offset;
16619 HOST_WIDE_INT bit_offset;
16620
16621 /* Must be a field and a bit field. */
16622 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16623
16624 /* We can't yet handle bit-fields whose offsets are variable, so if we
16625 encounter such things, just return without generating any attribute
16626 whatsoever. Likewise for variable or too large size. */
16627 if (! tree_fits_shwi_p (bit_position (decl))
16628 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16629 return;
16630
16631 bitpos_int = int_bit_position (decl);
16632
16633 /* Note that the bit offset is always the distance (in bits) from the
16634 highest-order bit of the "containing object" to the highest-order bit of
16635 the bit-field itself. Since the "high-order end" of any object or field
16636 is different on big-endian and little-endian machines, the computation
16637 below must take account of these differences. */
16638 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16639 highest_order_field_bit_offset = bitpos_int;
16640
16641 if (! BYTES_BIG_ENDIAN)
16642 {
16643 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16644 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16645 }
16646
16647 bit_offset
16648 = (! BYTES_BIG_ENDIAN
16649 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16650 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16651
16652 if (bit_offset < 0)
16653 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16654 else
16655 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16656 }
16657
16658 /* For a FIELD_DECL node which represents a bit field, output an attribute
16659 which specifies the length in bits of the given field. */
16660
16661 static inline void
16662 add_bit_size_attribute (dw_die_ref die, tree decl)
16663 {
16664 /* Must be a field and a bit field. */
16665 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16666 && DECL_BIT_FIELD_TYPE (decl));
16667
16668 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16669 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16670 }
16671
16672 /* If the compiled language is ANSI C, then add a 'prototyped'
16673 attribute, if arg types are given for the parameters of a function. */
16674
16675 static inline void
16676 add_prototyped_attribute (dw_die_ref die, tree func_type)
16677 {
16678 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16679 && prototype_p (func_type))
16680 add_AT_flag (die, DW_AT_prototyped, 1);
16681 }
16682
16683 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16684 by looking in either the type declaration or object declaration
16685 equate table. */
16686
16687 static inline dw_die_ref
16688 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16689 {
16690 dw_die_ref origin_die = NULL;
16691
16692 if (TREE_CODE (origin) != FUNCTION_DECL)
16693 {
16694 /* We may have gotten separated from the block for the inlined
16695 function, if we're in an exception handler or some such; make
16696 sure that the abstract function has been written out.
16697
16698 Doing this for nested functions is wrong, however; functions are
16699 distinct units, and our context might not even be inline. */
16700 tree fn = origin;
16701
16702 if (TYPE_P (fn))
16703 fn = TYPE_STUB_DECL (fn);
16704
16705 fn = decl_function_context (fn);
16706 if (fn)
16707 dwarf2out_abstract_function (fn);
16708 }
16709
16710 if (DECL_P (origin))
16711 origin_die = lookup_decl_die (origin);
16712 else if (TYPE_P (origin))
16713 origin_die = lookup_type_die (origin);
16714
16715 /* XXX: Functions that are never lowered don't always have correct block
16716 trees (in the case of java, they simply have no block tree, in some other
16717 languages). For these functions, there is nothing we can really do to
16718 output correct debug info for inlined functions in all cases. Rather
16719 than die, we'll just produce deficient debug info now, in that we will
16720 have variables without a proper abstract origin. In the future, when all
16721 functions are lowered, we should re-add a gcc_assert (origin_die)
16722 here. */
16723
16724 if (origin_die)
16725 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16726 return origin_die;
16727 }
16728
16729 /* We do not currently support the pure_virtual attribute. */
16730
16731 static inline void
16732 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16733 {
16734 if (DECL_VINDEX (func_decl))
16735 {
16736 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16737
16738 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16739 add_AT_loc (die, DW_AT_vtable_elem_location,
16740 new_loc_descr (DW_OP_constu,
16741 tree_to_shwi (DECL_VINDEX (func_decl)),
16742 0));
16743
16744 /* GNU extension: Record what type this method came from originally. */
16745 if (debug_info_level > DINFO_LEVEL_TERSE
16746 && DECL_CONTEXT (func_decl))
16747 add_AT_die_ref (die, DW_AT_containing_type,
16748 lookup_type_die (DECL_CONTEXT (func_decl)));
16749 }
16750 }
16751 \f
16752 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16753 given decl. This used to be a vendor extension until after DWARF 4
16754 standardized it. */
16755
16756 static void
16757 add_linkage_attr (dw_die_ref die, tree decl)
16758 {
16759 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16760
16761 /* Mimic what assemble_name_raw does with a leading '*'. */
16762 if (name[0] == '*')
16763 name = &name[1];
16764
16765 if (dwarf_version >= 4)
16766 add_AT_string (die, DW_AT_linkage_name, name);
16767 else
16768 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16769 }
16770
16771 /* Add source coordinate attributes for the given decl. */
16772
16773 static void
16774 add_src_coords_attributes (dw_die_ref die, tree decl)
16775 {
16776 expanded_location s;
16777
16778 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16779 return;
16780 s = expand_location (DECL_SOURCE_LOCATION (decl));
16781 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16782 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16783 }
16784
16785 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16786
16787 static void
16788 add_linkage_name (dw_die_ref die, tree decl)
16789 {
16790 if (debug_info_level > DINFO_LEVEL_NONE
16791 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16792 && TREE_PUBLIC (decl)
16793 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16794 && die->die_tag != DW_TAG_member)
16795 {
16796 /* Defer until we have an assembler name set. */
16797 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16798 {
16799 limbo_die_node *asm_name;
16800
16801 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16802 asm_name->die = die;
16803 asm_name->created_for = decl;
16804 asm_name->next = deferred_asm_name;
16805 deferred_asm_name = asm_name;
16806 }
16807 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16808 add_linkage_attr (die, decl);
16809 }
16810 }
16811
16812 /* Add a DW_AT_name attribute and source coordinate attribute for the
16813 given decl, but only if it actually has a name. */
16814
16815 static void
16816 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16817 {
16818 tree decl_name;
16819
16820 decl_name = DECL_NAME (decl);
16821 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16822 {
16823 const char *name = dwarf2_name (decl, 0);
16824 if (name)
16825 add_name_attribute (die, name);
16826 if (! DECL_ARTIFICIAL (decl))
16827 add_src_coords_attributes (die, decl);
16828
16829 add_linkage_name (die, decl);
16830 }
16831
16832 #ifdef VMS_DEBUGGING_INFO
16833 /* Get the function's name, as described by its RTL. This may be different
16834 from the DECL_NAME name used in the source file. */
16835 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16836 {
16837 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16838 XEXP (DECL_RTL (decl), 0), false);
16839 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16840 }
16841 #endif /* VMS_DEBUGGING_INFO */
16842 }
16843
16844 #ifdef VMS_DEBUGGING_INFO
16845 /* Output the debug main pointer die for VMS */
16846
16847 void
16848 dwarf2out_vms_debug_main_pointer (void)
16849 {
16850 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16851 dw_die_ref die;
16852
16853 /* Allocate the VMS debug main subprogram die. */
16854 die = ggc_cleared_alloc<die_node> ();
16855 die->die_tag = DW_TAG_subprogram;
16856 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16857 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16858 current_function_funcdef_no);
16859 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16860
16861 /* Make it the first child of comp_unit_die (). */
16862 die->die_parent = comp_unit_die ();
16863 if (comp_unit_die ()->die_child)
16864 {
16865 die->die_sib = comp_unit_die ()->die_child->die_sib;
16866 comp_unit_die ()->die_child->die_sib = die;
16867 }
16868 else
16869 {
16870 die->die_sib = die;
16871 comp_unit_die ()->die_child = die;
16872 }
16873 }
16874 #endif /* VMS_DEBUGGING_INFO */
16875
16876 /* Push a new declaration scope. */
16877
16878 static void
16879 push_decl_scope (tree scope)
16880 {
16881 vec_safe_push (decl_scope_table, scope);
16882 }
16883
16884 /* Pop a declaration scope. */
16885
16886 static inline void
16887 pop_decl_scope (void)
16888 {
16889 decl_scope_table->pop ();
16890 }
16891
16892 /* walk_tree helper function for uses_local_type, below. */
16893
16894 static tree
16895 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16896 {
16897 if (!TYPE_P (*tp))
16898 *walk_subtrees = 0;
16899 else
16900 {
16901 tree name = TYPE_NAME (*tp);
16902 if (name && DECL_P (name) && decl_function_context (name))
16903 return *tp;
16904 }
16905 return NULL_TREE;
16906 }
16907
16908 /* If TYPE involves a function-local type (including a local typedef to a
16909 non-local type), returns that type; otherwise returns NULL_TREE. */
16910
16911 static tree
16912 uses_local_type (tree type)
16913 {
16914 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16915 return used;
16916 }
16917
16918 /* Return the DIE for the scope that immediately contains this type.
16919 Non-named types that do not involve a function-local type get global
16920 scope. Named types nested in namespaces or other types get their
16921 containing scope. All other types (i.e. function-local named types) get
16922 the current active scope. */
16923
16924 static dw_die_ref
16925 scope_die_for (tree t, dw_die_ref context_die)
16926 {
16927 dw_die_ref scope_die = NULL;
16928 tree containing_scope;
16929
16930 /* Non-types always go in the current scope. */
16931 gcc_assert (TYPE_P (t));
16932
16933 /* Use the scope of the typedef, rather than the scope of the type
16934 it refers to. */
16935 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16936 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16937 else
16938 containing_scope = TYPE_CONTEXT (t);
16939
16940 /* Use the containing namespace if there is one. */
16941 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16942 {
16943 if (context_die == lookup_decl_die (containing_scope))
16944 /* OK */;
16945 else if (debug_info_level > DINFO_LEVEL_TERSE)
16946 context_die = get_context_die (containing_scope);
16947 else
16948 containing_scope = NULL_TREE;
16949 }
16950
16951 /* Ignore function type "scopes" from the C frontend. They mean that
16952 a tagged type is local to a parmlist of a function declarator, but
16953 that isn't useful to DWARF. */
16954 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16955 containing_scope = NULL_TREE;
16956
16957 if (SCOPE_FILE_SCOPE_P (containing_scope))
16958 {
16959 /* If T uses a local type keep it local as well, to avoid references
16960 to function-local DIEs from outside the function. */
16961 if (current_function_decl && uses_local_type (t))
16962 scope_die = context_die;
16963 else
16964 scope_die = comp_unit_die ();
16965 }
16966 else if (TYPE_P (containing_scope))
16967 {
16968 /* For types, we can just look up the appropriate DIE. */
16969 if (debug_info_level > DINFO_LEVEL_TERSE)
16970 scope_die = get_context_die (containing_scope);
16971 else
16972 {
16973 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16974 if (scope_die == NULL)
16975 scope_die = comp_unit_die ();
16976 }
16977 }
16978 else
16979 scope_die = context_die;
16980
16981 return scope_die;
16982 }
16983
16984 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16985
16986 static inline int
16987 local_scope_p (dw_die_ref context_die)
16988 {
16989 for (; context_die; context_die = context_die->die_parent)
16990 if (context_die->die_tag == DW_TAG_inlined_subroutine
16991 || context_die->die_tag == DW_TAG_subprogram)
16992 return 1;
16993
16994 return 0;
16995 }
16996
16997 /* Returns nonzero if CONTEXT_DIE is a class. */
16998
16999 static inline int
17000 class_scope_p (dw_die_ref context_die)
17001 {
17002 return (context_die
17003 && (context_die->die_tag == DW_TAG_structure_type
17004 || context_die->die_tag == DW_TAG_class_type
17005 || context_die->die_tag == DW_TAG_interface_type
17006 || context_die->die_tag == DW_TAG_union_type));
17007 }
17008
17009 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17010 whether or not to treat a DIE in this context as a declaration. */
17011
17012 static inline int
17013 class_or_namespace_scope_p (dw_die_ref context_die)
17014 {
17015 return (class_scope_p (context_die)
17016 || (context_die && context_die->die_tag == DW_TAG_namespace));
17017 }
17018
17019 /* Many forms of DIEs require a "type description" attribute. This
17020 routine locates the proper "type descriptor" die for the type given
17021 by 'type', and adds a DW_AT_type attribute below the given die. */
17022
17023 static void
17024 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17025 int decl_volatile, dw_die_ref context_die)
17026 {
17027 enum tree_code code = TREE_CODE (type);
17028 dw_die_ref type_die = NULL;
17029
17030 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17031 or fixed-point type, use the inner type. This is because we have no
17032 support for unnamed types in base_type_die. This can happen if this is
17033 an Ada subrange type. Correct solution is emit a subrange type die. */
17034 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17035 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17036 type = TREE_TYPE (type), code = TREE_CODE (type);
17037
17038 if (code == ERROR_MARK
17039 /* Handle a special case. For functions whose return type is void, we
17040 generate *no* type attribute. (Note that no object may have type
17041 `void', so this only applies to function return types). */
17042 || code == VOID_TYPE)
17043 return;
17044
17045 type_die = modified_type_die (type,
17046 decl_const || TYPE_READONLY (type),
17047 decl_volatile || TYPE_VOLATILE (type),
17048 context_die);
17049
17050 if (type_die != NULL)
17051 add_AT_die_ref (object_die, DW_AT_type, type_die);
17052 }
17053
17054 /* Given an object die, add the calling convention attribute for the
17055 function call type. */
17056 static void
17057 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17058 {
17059 enum dwarf_calling_convention value = DW_CC_normal;
17060
17061 value = ((enum dwarf_calling_convention)
17062 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17063
17064 if (is_fortran ()
17065 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17066 {
17067 /* DWARF 2 doesn't provide a way to identify a program's source-level
17068 entry point. DW_AT_calling_convention attributes are only meant
17069 to describe functions' calling conventions. However, lacking a
17070 better way to signal the Fortran main program, we used this for
17071 a long time, following existing custom. Now, DWARF 4 has
17072 DW_AT_main_subprogram, which we add below, but some tools still
17073 rely on the old way, which we thus keep. */
17074 value = DW_CC_program;
17075
17076 if (dwarf_version >= 4 || !dwarf_strict)
17077 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17078 }
17079
17080 /* Only add the attribute if the backend requests it, and
17081 is not DW_CC_normal. */
17082 if (value && (value != DW_CC_normal))
17083 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17084 }
17085
17086 /* Given a tree pointer to a struct, class, union, or enum type node, return
17087 a pointer to the (string) tag name for the given type, or zero if the type
17088 was declared without a tag. */
17089
17090 static const char *
17091 type_tag (const_tree type)
17092 {
17093 const char *name = 0;
17094
17095 if (TYPE_NAME (type) != 0)
17096 {
17097 tree t = 0;
17098
17099 /* Find the IDENTIFIER_NODE for the type name. */
17100 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17101 && !TYPE_NAMELESS (type))
17102 t = TYPE_NAME (type);
17103
17104 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17105 a TYPE_DECL node, regardless of whether or not a `typedef' was
17106 involved. */
17107 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17108 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17109 {
17110 /* We want to be extra verbose. Don't call dwarf_name if
17111 DECL_NAME isn't set. The default hook for decl_printable_name
17112 doesn't like that, and in this context it's correct to return
17113 0, instead of "<anonymous>" or the like. */
17114 if (DECL_NAME (TYPE_NAME (type))
17115 && !DECL_NAMELESS (TYPE_NAME (type)))
17116 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17117 }
17118
17119 /* Now get the name as a string, or invent one. */
17120 if (!name && t != 0)
17121 name = IDENTIFIER_POINTER (t);
17122 }
17123
17124 return (name == 0 || *name == '\0') ? 0 : name;
17125 }
17126
17127 /* Return the type associated with a data member, make a special check
17128 for bit field types. */
17129
17130 static inline tree
17131 member_declared_type (const_tree member)
17132 {
17133 return (DECL_BIT_FIELD_TYPE (member)
17134 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17135 }
17136
17137 /* Get the decl's label, as described by its RTL. This may be different
17138 from the DECL_NAME name used in the source file. */
17139
17140 #if 0
17141 static const char *
17142 decl_start_label (tree decl)
17143 {
17144 rtx x;
17145 const char *fnname;
17146
17147 x = DECL_RTL (decl);
17148 gcc_assert (MEM_P (x));
17149
17150 x = XEXP (x, 0);
17151 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17152
17153 fnname = XSTR (x, 0);
17154 return fnname;
17155 }
17156 #endif
17157 \f
17158 /* These routines generate the internal representation of the DIE's for
17159 the compilation unit. Debugging information is collected by walking
17160 the declaration trees passed in from dwarf2out_decl(). */
17161
17162 static void
17163 gen_array_type_die (tree type, dw_die_ref context_die)
17164 {
17165 dw_die_ref scope_die = scope_die_for (type, context_die);
17166 dw_die_ref array_die;
17167
17168 /* GNU compilers represent multidimensional array types as sequences of one
17169 dimensional array types whose element types are themselves array types.
17170 We sometimes squish that down to a single array_type DIE with multiple
17171 subscripts in the Dwarf debugging info. The draft Dwarf specification
17172 say that we are allowed to do this kind of compression in C, because
17173 there is no difference between an array of arrays and a multidimensional
17174 array. We don't do this for Ada to remain as close as possible to the
17175 actual representation, which is especially important against the language
17176 flexibilty wrt arrays of variable size. */
17177
17178 bool collapse_nested_arrays = !is_ada ();
17179 tree element_type;
17180
17181 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17182 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17183 if (TYPE_STRING_FLAG (type)
17184 && TREE_CODE (type) == ARRAY_TYPE
17185 && is_fortran ()
17186 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17187 {
17188 HOST_WIDE_INT size;
17189
17190 array_die = new_die (DW_TAG_string_type, scope_die, type);
17191 add_name_attribute (array_die, type_tag (type));
17192 equate_type_number_to_die (type, array_die);
17193 size = int_size_in_bytes (type);
17194 if (size >= 0)
17195 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17196 else if (TYPE_DOMAIN (type) != NULL_TREE
17197 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17198 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17199 {
17200 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17201 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17202
17203 size = int_size_in_bytes (TREE_TYPE (szdecl));
17204 if (loc && size > 0)
17205 {
17206 add_AT_location_description (array_die, DW_AT_string_length, loc);
17207 if (size != DWARF2_ADDR_SIZE)
17208 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17209 }
17210 }
17211 return;
17212 }
17213
17214 array_die = new_die (DW_TAG_array_type, scope_die, type);
17215 add_name_attribute (array_die, type_tag (type));
17216 equate_type_number_to_die (type, array_die);
17217
17218 if (TREE_CODE (type) == VECTOR_TYPE)
17219 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17220
17221 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17222 if (is_fortran ()
17223 && TREE_CODE (type) == ARRAY_TYPE
17224 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17225 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17226 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17227
17228 #if 0
17229 /* We default the array ordering. SDB will probably do
17230 the right things even if DW_AT_ordering is not present. It's not even
17231 an issue until we start to get into multidimensional arrays anyway. If
17232 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17233 then we'll have to put the DW_AT_ordering attribute back in. (But if
17234 and when we find out that we need to put these in, we will only do so
17235 for multidimensional arrays. */
17236 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17237 #endif
17238
17239 if (TREE_CODE (type) == VECTOR_TYPE)
17240 {
17241 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17242 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17243 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17244 add_bound_info (subrange_die, DW_AT_upper_bound,
17245 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17246 }
17247 else
17248 add_subscript_info (array_die, type, collapse_nested_arrays);
17249
17250 /* Add representation of the type of the elements of this array type and
17251 emit the corresponding DIE if we haven't done it already. */
17252 element_type = TREE_TYPE (type);
17253 if (collapse_nested_arrays)
17254 while (TREE_CODE (element_type) == ARRAY_TYPE)
17255 {
17256 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17257 break;
17258 element_type = TREE_TYPE (element_type);
17259 }
17260
17261 add_type_attribute (array_die, element_type, 0, 0, context_die);
17262
17263 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17264 if (TYPE_ARTIFICIAL (type))
17265 add_AT_flag (array_die, DW_AT_artificial, 1);
17266
17267 if (get_AT (array_die, DW_AT_name))
17268 add_pubtype (type, array_die);
17269 }
17270
17271 static dw_loc_descr_ref
17272 descr_info_loc (tree val, tree base_decl)
17273 {
17274 HOST_WIDE_INT size;
17275 dw_loc_descr_ref loc, loc2;
17276 enum dwarf_location_atom op;
17277
17278 if (val == base_decl)
17279 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17280
17281 switch (TREE_CODE (val))
17282 {
17283 CASE_CONVERT:
17284 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17285 case VAR_DECL:
17286 return loc_descriptor_from_tree (val, 0);
17287 case INTEGER_CST:
17288 if (tree_fits_shwi_p (val))
17289 return int_loc_descriptor (tree_to_shwi (val));
17290 break;
17291 case INDIRECT_REF:
17292 size = int_size_in_bytes (TREE_TYPE (val));
17293 if (size < 0)
17294 break;
17295 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17296 if (!loc)
17297 break;
17298 if (size == DWARF2_ADDR_SIZE)
17299 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17300 else
17301 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17302 return loc;
17303 case POINTER_PLUS_EXPR:
17304 case PLUS_EXPR:
17305 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17306 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17307 {
17308 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17309 if (!loc)
17310 break;
17311 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17312 }
17313 else
17314 {
17315 op = DW_OP_plus;
17316 do_binop:
17317 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17318 if (!loc)
17319 break;
17320 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17321 if (!loc2)
17322 break;
17323 add_loc_descr (&loc, loc2);
17324 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17325 }
17326 return loc;
17327 case MINUS_EXPR:
17328 op = DW_OP_minus;
17329 goto do_binop;
17330 case MULT_EXPR:
17331 op = DW_OP_mul;
17332 goto do_binop;
17333 case EQ_EXPR:
17334 op = DW_OP_eq;
17335 goto do_binop;
17336 case NE_EXPR:
17337 op = DW_OP_ne;
17338 goto do_binop;
17339 default:
17340 break;
17341 }
17342 return NULL;
17343 }
17344
17345 static void
17346 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17347 tree val, tree base_decl)
17348 {
17349 dw_loc_descr_ref loc;
17350
17351 if (tree_fits_shwi_p (val))
17352 {
17353 add_AT_unsigned (die, attr, tree_to_shwi (val));
17354 return;
17355 }
17356
17357 loc = descr_info_loc (val, base_decl);
17358 if (!loc)
17359 return;
17360
17361 add_AT_loc (die, attr, loc);
17362 }
17363
17364 /* This routine generates DIE for array with hidden descriptor, details
17365 are filled into *info by a langhook. */
17366
17367 static void
17368 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17369 dw_die_ref context_die)
17370 {
17371 dw_die_ref scope_die = scope_die_for (type, context_die);
17372 dw_die_ref array_die;
17373 int dim;
17374
17375 array_die = new_die (DW_TAG_array_type, scope_die, type);
17376 add_name_attribute (array_die, type_tag (type));
17377 equate_type_number_to_die (type, array_die);
17378
17379 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17380 if (is_fortran ()
17381 && info->ndimensions >= 2)
17382 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17383
17384 if (info->data_location)
17385 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17386 info->base_decl);
17387 if (info->associated)
17388 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17389 info->base_decl);
17390 if (info->allocated)
17391 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17392 info->base_decl);
17393
17394 for (dim = 0; dim < info->ndimensions; dim++)
17395 {
17396 dw_die_ref subrange_die
17397 = new_die (DW_TAG_subrange_type, array_die, NULL);
17398
17399 if (info->dimen[dim].lower_bound)
17400 {
17401 /* If it is the default value, omit it. */
17402 int dflt;
17403
17404 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17405 && (dflt = lower_bound_default ()) != -1
17406 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17407 ;
17408 else
17409 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17410 info->dimen[dim].lower_bound,
17411 info->base_decl);
17412 }
17413 if (info->dimen[dim].upper_bound)
17414 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17415 info->dimen[dim].upper_bound,
17416 info->base_decl);
17417 if (info->dimen[dim].stride)
17418 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17419 info->dimen[dim].stride,
17420 info->base_decl);
17421 }
17422
17423 gen_type_die (info->element_type, context_die);
17424 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17425
17426 if (get_AT (array_die, DW_AT_name))
17427 add_pubtype (type, array_die);
17428 }
17429
17430 #if 0
17431 static void
17432 gen_entry_point_die (tree decl, dw_die_ref context_die)
17433 {
17434 tree origin = decl_ultimate_origin (decl);
17435 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17436
17437 if (origin != NULL)
17438 add_abstract_origin_attribute (decl_die, origin);
17439 else
17440 {
17441 add_name_and_src_coords_attributes (decl_die, decl);
17442 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17443 0, 0, context_die);
17444 }
17445
17446 if (DECL_ABSTRACT (decl))
17447 equate_decl_number_to_die (decl, decl_die);
17448 else
17449 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17450 }
17451 #endif
17452
17453 /* Walk through the list of incomplete types again, trying once more to
17454 emit full debugging info for them. */
17455
17456 static void
17457 retry_incomplete_types (void)
17458 {
17459 int i;
17460
17461 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17462 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17463 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17464 }
17465
17466 /* Determine what tag to use for a record type. */
17467
17468 static enum dwarf_tag
17469 record_type_tag (tree type)
17470 {
17471 if (! lang_hooks.types.classify_record)
17472 return DW_TAG_structure_type;
17473
17474 switch (lang_hooks.types.classify_record (type))
17475 {
17476 case RECORD_IS_STRUCT:
17477 return DW_TAG_structure_type;
17478
17479 case RECORD_IS_CLASS:
17480 return DW_TAG_class_type;
17481
17482 case RECORD_IS_INTERFACE:
17483 if (dwarf_version >= 3 || !dwarf_strict)
17484 return DW_TAG_interface_type;
17485 return DW_TAG_structure_type;
17486
17487 default:
17488 gcc_unreachable ();
17489 }
17490 }
17491
17492 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17493 include all of the information about the enumeration values also. Each
17494 enumerated type name/value is listed as a child of the enumerated type
17495 DIE. */
17496
17497 static dw_die_ref
17498 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17499 {
17500 dw_die_ref type_die = lookup_type_die (type);
17501
17502 if (type_die == NULL)
17503 {
17504 type_die = new_die (DW_TAG_enumeration_type,
17505 scope_die_for (type, context_die), type);
17506 equate_type_number_to_die (type, type_die);
17507 add_name_attribute (type_die, type_tag (type));
17508 if (dwarf_version >= 4 || !dwarf_strict)
17509 {
17510 if (ENUM_IS_SCOPED (type))
17511 add_AT_flag (type_die, DW_AT_enum_class, 1);
17512 if (ENUM_IS_OPAQUE (type))
17513 add_AT_flag (type_die, DW_AT_declaration, 1);
17514 }
17515 }
17516 else if (! TYPE_SIZE (type))
17517 return type_die;
17518 else
17519 remove_AT (type_die, DW_AT_declaration);
17520
17521 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17522 given enum type is incomplete, do not generate the DW_AT_byte_size
17523 attribute or the DW_AT_element_list attribute. */
17524 if (TYPE_SIZE (type))
17525 {
17526 tree link;
17527
17528 TREE_ASM_WRITTEN (type) = 1;
17529 add_byte_size_attribute (type_die, type);
17530 if (dwarf_version >= 3 || !dwarf_strict)
17531 {
17532 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17533 add_type_attribute (type_die, underlying, 0, 0, context_die);
17534 }
17535 if (TYPE_STUB_DECL (type) != NULL_TREE)
17536 {
17537 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17538 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17539 }
17540
17541 /* If the first reference to this type was as the return type of an
17542 inline function, then it may not have a parent. Fix this now. */
17543 if (type_die->die_parent == NULL)
17544 add_child_die (scope_die_for (type, context_die), type_die);
17545
17546 for (link = TYPE_VALUES (type);
17547 link != NULL; link = TREE_CHAIN (link))
17548 {
17549 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17550 tree value = TREE_VALUE (link);
17551
17552 add_name_attribute (enum_die,
17553 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17554
17555 if (TREE_CODE (value) == CONST_DECL)
17556 value = DECL_INITIAL (value);
17557
17558 if (simple_type_size_in_bits (TREE_TYPE (value))
17559 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17560 {
17561 /* For constant forms created by add_AT_unsigned DWARF
17562 consumers (GDB, elfutils, etc.) always zero extend
17563 the value. Only when the actual value is negative
17564 do we need to use add_AT_int to generate a constant
17565 form that can represent negative values. */
17566 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17567 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17568 add_AT_unsigned (enum_die, DW_AT_const_value,
17569 (unsigned HOST_WIDE_INT) val);
17570 else
17571 add_AT_int (enum_die, DW_AT_const_value, val);
17572 }
17573 else
17574 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17575 that here. TODO: This should be re-worked to use correct
17576 signed/unsigned double tags for all cases. */
17577 add_AT_wide (enum_die, DW_AT_const_value, value);
17578 }
17579
17580 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17581 if (TYPE_ARTIFICIAL (type))
17582 add_AT_flag (type_die, DW_AT_artificial, 1);
17583 }
17584 else
17585 add_AT_flag (type_die, DW_AT_declaration, 1);
17586
17587 add_pubtype (type, type_die);
17588
17589 return type_die;
17590 }
17591
17592 /* Generate a DIE to represent either a real live formal parameter decl or to
17593 represent just the type of some formal parameter position in some function
17594 type.
17595
17596 Note that this routine is a bit unusual because its argument may be a
17597 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17598 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17599 node. If it's the former then this function is being called to output a
17600 DIE to represent a formal parameter object (or some inlining thereof). If
17601 it's the latter, then this function is only being called to output a
17602 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17603 argument type of some subprogram type.
17604 If EMIT_NAME_P is true, name and source coordinate attributes
17605 are emitted. */
17606
17607 static dw_die_ref
17608 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17609 dw_die_ref context_die)
17610 {
17611 tree node_or_origin = node ? node : origin;
17612 tree ultimate_origin;
17613 dw_die_ref parm_die
17614 = new_die (DW_TAG_formal_parameter, context_die, node);
17615
17616 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17617 {
17618 case tcc_declaration:
17619 ultimate_origin = decl_ultimate_origin (node_or_origin);
17620 if (node || ultimate_origin)
17621 origin = ultimate_origin;
17622 if (origin != NULL)
17623 add_abstract_origin_attribute (parm_die, origin);
17624 else if (emit_name_p)
17625 add_name_and_src_coords_attributes (parm_die, node);
17626 if (origin == NULL
17627 || (! DECL_ABSTRACT (node_or_origin)
17628 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17629 decl_function_context
17630 (node_or_origin))))
17631 {
17632 tree type = TREE_TYPE (node_or_origin);
17633 if (decl_by_reference_p (node_or_origin))
17634 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17635 context_die);
17636 else
17637 add_type_attribute (parm_die, type,
17638 TREE_READONLY (node_or_origin),
17639 TREE_THIS_VOLATILE (node_or_origin),
17640 context_die);
17641 }
17642 if (origin == NULL && DECL_ARTIFICIAL (node))
17643 add_AT_flag (parm_die, DW_AT_artificial, 1);
17644
17645 if (node && node != origin)
17646 equate_decl_number_to_die (node, parm_die);
17647 if (! DECL_ABSTRACT (node_or_origin))
17648 add_location_or_const_value_attribute (parm_die, node_or_origin,
17649 node == NULL, DW_AT_location);
17650
17651 break;
17652
17653 case tcc_type:
17654 /* We were called with some kind of a ..._TYPE node. */
17655 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17656 break;
17657
17658 default:
17659 gcc_unreachable ();
17660 }
17661
17662 return parm_die;
17663 }
17664
17665 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17666 children DW_TAG_formal_parameter DIEs representing the arguments of the
17667 parameter pack.
17668
17669 PARM_PACK must be a function parameter pack.
17670 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17671 must point to the subsequent arguments of the function PACK_ARG belongs to.
17672 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17673 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17674 following the last one for which a DIE was generated. */
17675
17676 static dw_die_ref
17677 gen_formal_parameter_pack_die (tree parm_pack,
17678 tree pack_arg,
17679 dw_die_ref subr_die,
17680 tree *next_arg)
17681 {
17682 tree arg;
17683 dw_die_ref parm_pack_die;
17684
17685 gcc_assert (parm_pack
17686 && lang_hooks.function_parameter_pack_p (parm_pack)
17687 && subr_die);
17688
17689 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17690 add_src_coords_attributes (parm_pack_die, parm_pack);
17691
17692 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17693 {
17694 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17695 parm_pack))
17696 break;
17697 gen_formal_parameter_die (arg, NULL,
17698 false /* Don't emit name attribute. */,
17699 parm_pack_die);
17700 }
17701 if (next_arg)
17702 *next_arg = arg;
17703 return parm_pack_die;
17704 }
17705
17706 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17707 at the end of an (ANSI prototyped) formal parameters list. */
17708
17709 static void
17710 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17711 {
17712 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17713 }
17714
17715 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17716 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17717 parameters as specified in some function type specification (except for
17718 those which appear as part of a function *definition*). */
17719
17720 static void
17721 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17722 {
17723 tree link;
17724 tree formal_type = NULL;
17725 tree first_parm_type;
17726 tree arg;
17727
17728 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17729 {
17730 arg = DECL_ARGUMENTS (function_or_method_type);
17731 function_or_method_type = TREE_TYPE (function_or_method_type);
17732 }
17733 else
17734 arg = NULL_TREE;
17735
17736 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17737
17738 /* Make our first pass over the list of formal parameter types and output a
17739 DW_TAG_formal_parameter DIE for each one. */
17740 for (link = first_parm_type; link; )
17741 {
17742 dw_die_ref parm_die;
17743
17744 formal_type = TREE_VALUE (link);
17745 if (formal_type == void_type_node)
17746 break;
17747
17748 /* Output a (nameless) DIE to represent the formal parameter itself. */
17749 parm_die = gen_formal_parameter_die (formal_type, NULL,
17750 true /* Emit name attribute. */,
17751 context_die);
17752 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17753 && link == first_parm_type)
17754 {
17755 add_AT_flag (parm_die, DW_AT_artificial, 1);
17756 if (dwarf_version >= 3 || !dwarf_strict)
17757 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17758 }
17759 else if (arg && DECL_ARTIFICIAL (arg))
17760 add_AT_flag (parm_die, DW_AT_artificial, 1);
17761
17762 link = TREE_CHAIN (link);
17763 if (arg)
17764 arg = DECL_CHAIN (arg);
17765 }
17766
17767 /* If this function type has an ellipsis, add a
17768 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17769 if (formal_type != void_type_node)
17770 gen_unspecified_parameters_die (function_or_method_type, context_die);
17771
17772 /* Make our second (and final) pass over the list of formal parameter types
17773 and output DIEs to represent those types (as necessary). */
17774 for (link = TYPE_ARG_TYPES (function_or_method_type);
17775 link && TREE_VALUE (link);
17776 link = TREE_CHAIN (link))
17777 gen_type_die (TREE_VALUE (link), context_die);
17778 }
17779
17780 /* We want to generate the DIE for TYPE so that we can generate the
17781 die for MEMBER, which has been defined; we will need to refer back
17782 to the member declaration nested within TYPE. If we're trying to
17783 generate minimal debug info for TYPE, processing TYPE won't do the
17784 trick; we need to attach the member declaration by hand. */
17785
17786 static void
17787 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17788 {
17789 gen_type_die (type, context_die);
17790
17791 /* If we're trying to avoid duplicate debug info, we may not have
17792 emitted the member decl for this function. Emit it now. */
17793 if (TYPE_STUB_DECL (type)
17794 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17795 && ! lookup_decl_die (member))
17796 {
17797 dw_die_ref type_die;
17798 gcc_assert (!decl_ultimate_origin (member));
17799
17800 push_decl_scope (type);
17801 type_die = lookup_type_die_strip_naming_typedef (type);
17802 if (TREE_CODE (member) == FUNCTION_DECL)
17803 gen_subprogram_die (member, type_die);
17804 else if (TREE_CODE (member) == FIELD_DECL)
17805 {
17806 /* Ignore the nameless fields that are used to skip bits but handle
17807 C++ anonymous unions and structs. */
17808 if (DECL_NAME (member) != NULL_TREE
17809 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17810 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17811 {
17812 gen_type_die (member_declared_type (member), type_die);
17813 gen_field_die (member, type_die);
17814 }
17815 }
17816 else
17817 gen_variable_die (member, NULL_TREE, type_die);
17818
17819 pop_decl_scope ();
17820 }
17821 }
17822 \f
17823 /* Forward declare these functions, because they are mutually recursive
17824 with their set_block_* pairing functions. */
17825 static void set_decl_origin_self (tree);
17826 static void set_decl_abstract_flags (tree, int);
17827
17828 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17829 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17830 that it points to the node itself, thus indicating that the node is its
17831 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17832 the given node is NULL, recursively descend the decl/block tree which
17833 it is the root of, and for each other ..._DECL or BLOCK node contained
17834 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17835 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17836 values to point to themselves. */
17837
17838 static void
17839 set_block_origin_self (tree stmt)
17840 {
17841 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17842 {
17843 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17844
17845 {
17846 tree local_decl;
17847
17848 for (local_decl = BLOCK_VARS (stmt);
17849 local_decl != NULL_TREE;
17850 local_decl = DECL_CHAIN (local_decl))
17851 if (! DECL_EXTERNAL (local_decl))
17852 set_decl_origin_self (local_decl); /* Potential recursion. */
17853 }
17854
17855 {
17856 tree subblock;
17857
17858 for (subblock = BLOCK_SUBBLOCKS (stmt);
17859 subblock != NULL_TREE;
17860 subblock = BLOCK_CHAIN (subblock))
17861 set_block_origin_self (subblock); /* Recurse. */
17862 }
17863 }
17864 }
17865
17866 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17867 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17868 node to so that it points to the node itself, thus indicating that the
17869 node represents its own (abstract) origin. Additionally, if the
17870 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17871 the decl/block tree of which the given node is the root of, and for
17872 each other ..._DECL or BLOCK node contained therein whose
17873 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17874 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17875 point to themselves. */
17876
17877 static void
17878 set_decl_origin_self (tree decl)
17879 {
17880 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17881 {
17882 DECL_ABSTRACT_ORIGIN (decl) = decl;
17883 if (TREE_CODE (decl) == FUNCTION_DECL)
17884 {
17885 tree arg;
17886
17887 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17888 DECL_ABSTRACT_ORIGIN (arg) = arg;
17889 if (DECL_INITIAL (decl) != NULL_TREE
17890 && DECL_INITIAL (decl) != error_mark_node)
17891 set_block_origin_self (DECL_INITIAL (decl));
17892 }
17893 }
17894 }
17895 \f
17896 /* Given a pointer to some BLOCK node, and a boolean value to set the
17897 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17898 the given block, and for all local decls and all local sub-blocks
17899 (recursively) which are contained therein. */
17900
17901 static void
17902 set_block_abstract_flags (tree stmt, int setting)
17903 {
17904 tree local_decl;
17905 tree subblock;
17906 unsigned int i;
17907
17908 BLOCK_ABSTRACT (stmt) = setting;
17909
17910 for (local_decl = BLOCK_VARS (stmt);
17911 local_decl != NULL_TREE;
17912 local_decl = DECL_CHAIN (local_decl))
17913 if (! DECL_EXTERNAL (local_decl))
17914 set_decl_abstract_flags (local_decl, setting);
17915
17916 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17917 {
17918 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17919 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17920 || TREE_CODE (local_decl) == PARM_DECL)
17921 set_decl_abstract_flags (local_decl, setting);
17922 }
17923
17924 for (subblock = BLOCK_SUBBLOCKS (stmt);
17925 subblock != NULL_TREE;
17926 subblock = BLOCK_CHAIN (subblock))
17927 set_block_abstract_flags (subblock, setting);
17928 }
17929
17930 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17931 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17932 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17933 set the abstract flags for all of the parameters, local vars, local
17934 blocks and sub-blocks (recursively) to the same setting. */
17935
17936 static void
17937 set_decl_abstract_flags (tree decl, int setting)
17938 {
17939 DECL_ABSTRACT (decl) = setting;
17940 if (TREE_CODE (decl) == FUNCTION_DECL)
17941 {
17942 tree arg;
17943
17944 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17945 DECL_ABSTRACT (arg) = setting;
17946 if (DECL_INITIAL (decl) != NULL_TREE
17947 && DECL_INITIAL (decl) != error_mark_node)
17948 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17949 }
17950 }
17951
17952 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17953 may later generate inlined and/or out-of-line instances of. */
17954
17955 static void
17956 dwarf2out_abstract_function (tree decl)
17957 {
17958 dw_die_ref old_die;
17959 tree save_fn;
17960 tree context;
17961 int was_abstract;
17962 htab_t old_decl_loc_table;
17963 htab_t old_cached_dw_loc_list_table;
17964 int old_call_site_count, old_tail_call_site_count;
17965 struct call_arg_loc_node *old_call_arg_locations;
17966
17967 /* Make sure we have the actual abstract inline, not a clone. */
17968 decl = DECL_ORIGIN (decl);
17969
17970 old_die = lookup_decl_die (decl);
17971 if (old_die && get_AT (old_die, DW_AT_inline))
17972 /* We've already generated the abstract instance. */
17973 return;
17974
17975 /* We can be called while recursively when seeing block defining inlined subroutine
17976 DIE. Be sure to not clobber the outer location table nor use it or we would
17977 get locations in abstract instantces. */
17978 old_decl_loc_table = decl_loc_table;
17979 decl_loc_table = NULL;
17980 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17981 cached_dw_loc_list_table = NULL;
17982 old_call_arg_locations = call_arg_locations;
17983 call_arg_locations = NULL;
17984 old_call_site_count = call_site_count;
17985 call_site_count = -1;
17986 old_tail_call_site_count = tail_call_site_count;
17987 tail_call_site_count = -1;
17988
17989 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17990 we don't get confused by DECL_ABSTRACT. */
17991 if (debug_info_level > DINFO_LEVEL_TERSE)
17992 {
17993 context = decl_class_context (decl);
17994 if (context)
17995 gen_type_die_for_member
17996 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17997 }
17998
17999 /* Pretend we've just finished compiling this function. */
18000 save_fn = current_function_decl;
18001 current_function_decl = decl;
18002
18003 was_abstract = DECL_ABSTRACT (decl);
18004 set_decl_abstract_flags (decl, 1);
18005 dwarf2out_decl (decl);
18006 if (! was_abstract)
18007 set_decl_abstract_flags (decl, 0);
18008
18009 current_function_decl = save_fn;
18010 decl_loc_table = old_decl_loc_table;
18011 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18012 call_arg_locations = old_call_arg_locations;
18013 call_site_count = old_call_site_count;
18014 tail_call_site_count = old_tail_call_site_count;
18015 }
18016
18017 /* Helper function of premark_used_types() which gets called through
18018 htab_traverse.
18019
18020 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18021 marked as unused by prune_unused_types. */
18022
18023 static int
18024 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18025 {
18026 tree type;
18027 dw_die_ref die;
18028
18029 type = (tree) *slot;
18030 die = lookup_type_die (type);
18031 if (die != NULL)
18032 die->die_perennial_p = 1;
18033 return 1;
18034 }
18035
18036 /* Helper function of premark_types_used_by_global_vars which gets called
18037 through htab_traverse.
18038
18039 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18040 marked as unused by prune_unused_types. The DIE of the type is marked
18041 only if the global variable using the type will actually be emitted. */
18042
18043 static int
18044 premark_types_used_by_global_vars_helper (void **slot,
18045 void *data ATTRIBUTE_UNUSED)
18046 {
18047 struct types_used_by_vars_entry *entry;
18048 dw_die_ref die;
18049
18050 entry = (struct types_used_by_vars_entry *) *slot;
18051 gcc_assert (entry->type != NULL
18052 && entry->var_decl != NULL);
18053 die = lookup_type_die (entry->type);
18054 if (die)
18055 {
18056 /* Ask cgraph if the global variable really is to be emitted.
18057 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18058 varpool_node *node = varpool_get_node (entry->var_decl);
18059 if (node && node->definition)
18060 {
18061 die->die_perennial_p = 1;
18062 /* Keep the parent DIEs as well. */
18063 while ((die = die->die_parent) && die->die_perennial_p == 0)
18064 die->die_perennial_p = 1;
18065 }
18066 }
18067 return 1;
18068 }
18069
18070 /* Mark all members of used_types_hash as perennial. */
18071
18072 static void
18073 premark_used_types (struct function *fun)
18074 {
18075 if (fun && fun->used_types_hash)
18076 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18077 }
18078
18079 /* Mark all members of types_used_by_vars_entry as perennial. */
18080
18081 static void
18082 premark_types_used_by_global_vars (void)
18083 {
18084 if (types_used_by_vars_hash)
18085 htab_traverse (types_used_by_vars_hash,
18086 premark_types_used_by_global_vars_helper, NULL);
18087 }
18088
18089 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18090 for CA_LOC call arg loc node. */
18091
18092 static dw_die_ref
18093 gen_call_site_die (tree decl, dw_die_ref subr_die,
18094 struct call_arg_loc_node *ca_loc)
18095 {
18096 dw_die_ref stmt_die = NULL, die;
18097 tree block = ca_loc->block;
18098
18099 while (block
18100 && block != DECL_INITIAL (decl)
18101 && TREE_CODE (block) == BLOCK)
18102 {
18103 if (block_map.length () > BLOCK_NUMBER (block))
18104 stmt_die = block_map[BLOCK_NUMBER (block)];
18105 if (stmt_die)
18106 break;
18107 block = BLOCK_SUPERCONTEXT (block);
18108 }
18109 if (stmt_die == NULL)
18110 stmt_die = subr_die;
18111 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18112 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18113 if (ca_loc->tail_call_p)
18114 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18115 if (ca_loc->symbol_ref)
18116 {
18117 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18118 if (tdie)
18119 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18120 else
18121 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18122 }
18123 return die;
18124 }
18125
18126 /* Generate a DIE to represent a declared function (either file-scope or
18127 block-local). */
18128
18129 static void
18130 gen_subprogram_die (tree decl, dw_die_ref context_die)
18131 {
18132 tree origin = decl_ultimate_origin (decl);
18133 dw_die_ref subr_die;
18134 tree outer_scope;
18135 dw_die_ref old_die = lookup_decl_die (decl);
18136 int declaration = (current_function_decl != decl
18137 || class_or_namespace_scope_p (context_die));
18138
18139 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18140
18141 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18142 started to generate the abstract instance of an inline, decided to output
18143 its containing class, and proceeded to emit the declaration of the inline
18144 from the member list for the class. If so, DECLARATION takes priority;
18145 we'll get back to the abstract instance when done with the class. */
18146
18147 /* The class-scope declaration DIE must be the primary DIE. */
18148 if (origin && declaration && class_or_namespace_scope_p (context_die))
18149 {
18150 origin = NULL;
18151 gcc_assert (!old_die);
18152 }
18153
18154 /* Now that the C++ front end lazily declares artificial member fns, we
18155 might need to retrofit the declaration into its class. */
18156 if (!declaration && !origin && !old_die
18157 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18158 && !class_or_namespace_scope_p (context_die)
18159 && debug_info_level > DINFO_LEVEL_TERSE)
18160 old_die = force_decl_die (decl);
18161
18162 if (origin != NULL)
18163 {
18164 gcc_assert (!declaration || local_scope_p (context_die));
18165
18166 /* Fixup die_parent for the abstract instance of a nested
18167 inline function. */
18168 if (old_die && old_die->die_parent == NULL)
18169 add_child_die (context_die, old_die);
18170
18171 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18172 add_abstract_origin_attribute (subr_die, origin);
18173 /* This is where the actual code for a cloned function is.
18174 Let's emit linkage name attribute for it. This helps
18175 debuggers to e.g, set breakpoints into
18176 constructors/destructors when the user asks "break
18177 K::K". */
18178 add_linkage_name (subr_die, decl);
18179 }
18180 else if (old_die)
18181 {
18182 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18183 struct dwarf_file_data * file_index = lookup_filename (s.file);
18184
18185 if (!get_AT_flag (old_die, DW_AT_declaration)
18186 /* We can have a normal definition following an inline one in the
18187 case of redefinition of GNU C extern inlines.
18188 It seems reasonable to use AT_specification in this case. */
18189 && !get_AT (old_die, DW_AT_inline))
18190 {
18191 /* Detect and ignore this case, where we are trying to output
18192 something we have already output. */
18193 return;
18194 }
18195
18196 /* If the definition comes from the same place as the declaration,
18197 maybe use the old DIE. We always want the DIE for this function
18198 that has the *_pc attributes to be under comp_unit_die so the
18199 debugger can find it. We also need to do this for abstract
18200 instances of inlines, since the spec requires the out-of-line copy
18201 to have the same parent. For local class methods, this doesn't
18202 apply; we just use the old DIE. */
18203 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18204 && (DECL_ARTIFICIAL (decl)
18205 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18206 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18207 == (unsigned) s.line))))
18208 {
18209 subr_die = old_die;
18210
18211 /* Clear out the declaration attribute and the formal parameters.
18212 Do not remove all children, because it is possible that this
18213 declaration die was forced using force_decl_die(). In such
18214 cases die that forced declaration die (e.g. TAG_imported_module)
18215 is one of the children that we do not want to remove. */
18216 remove_AT (subr_die, DW_AT_declaration);
18217 remove_AT (subr_die, DW_AT_object_pointer);
18218 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18219 }
18220 else
18221 {
18222 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18223 add_AT_specification (subr_die, old_die);
18224 add_pubname (decl, subr_die);
18225 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18226 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18227 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18228 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18229
18230 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18231 emit the real type on the definition die. */
18232 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18233 {
18234 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18235 if (die == auto_die || die == decltype_auto_die)
18236 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18237 0, 0, context_die);
18238 }
18239 }
18240 }
18241 else
18242 {
18243 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18244
18245 if (TREE_PUBLIC (decl))
18246 add_AT_flag (subr_die, DW_AT_external, 1);
18247
18248 add_name_and_src_coords_attributes (subr_die, decl);
18249 add_pubname (decl, subr_die);
18250 if (debug_info_level > DINFO_LEVEL_TERSE)
18251 {
18252 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18253 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18254 0, 0, context_die);
18255 }
18256
18257 add_pure_or_virtual_attribute (subr_die, decl);
18258 if (DECL_ARTIFICIAL (decl))
18259 add_AT_flag (subr_die, DW_AT_artificial, 1);
18260
18261 add_accessibility_attribute (subr_die, decl);
18262 }
18263
18264 if (declaration)
18265 {
18266 if (!old_die || !get_AT (old_die, DW_AT_inline))
18267 {
18268 add_AT_flag (subr_die, DW_AT_declaration, 1);
18269
18270 /* If this is an explicit function declaration then generate
18271 a DW_AT_explicit attribute. */
18272 if (lang_hooks.decls.function_decl_explicit_p (decl)
18273 && (dwarf_version >= 3 || !dwarf_strict))
18274 add_AT_flag (subr_die, DW_AT_explicit, 1);
18275
18276 /* The first time we see a member function, it is in the context of
18277 the class to which it belongs. We make sure of this by emitting
18278 the class first. The next time is the definition, which is
18279 handled above. The two may come from the same source text.
18280
18281 Note that force_decl_die() forces function declaration die. It is
18282 later reused to represent definition. */
18283 equate_decl_number_to_die (decl, subr_die);
18284 }
18285 }
18286 else if (DECL_ABSTRACT (decl))
18287 {
18288 if (DECL_DECLARED_INLINE_P (decl))
18289 {
18290 if (cgraph_function_possibly_inlined_p (decl))
18291 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18292 else
18293 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18294 }
18295 else
18296 {
18297 if (cgraph_function_possibly_inlined_p (decl))
18298 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18299 else
18300 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18301 }
18302
18303 if (DECL_DECLARED_INLINE_P (decl)
18304 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18305 add_AT_flag (subr_die, DW_AT_artificial, 1);
18306
18307 equate_decl_number_to_die (decl, subr_die);
18308 }
18309 else if (!DECL_EXTERNAL (decl))
18310 {
18311 HOST_WIDE_INT cfa_fb_offset;
18312 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18313
18314 if (!old_die || !get_AT (old_die, DW_AT_inline))
18315 equate_decl_number_to_die (decl, subr_die);
18316
18317 gcc_checking_assert (fun);
18318 if (!flag_reorder_blocks_and_partition)
18319 {
18320 dw_fde_ref fde = fun->fde;
18321 if (fde->dw_fde_begin)
18322 {
18323 /* We have already generated the labels. */
18324 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18325 fde->dw_fde_end, false);
18326 }
18327 else
18328 {
18329 /* Create start/end labels and add the range. */
18330 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18331 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18332 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18333 current_function_funcdef_no);
18334 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18335 current_function_funcdef_no);
18336 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18337 false);
18338 }
18339
18340 #if VMS_DEBUGGING_INFO
18341 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18342 Section 2.3 Prologue and Epilogue Attributes:
18343 When a breakpoint is set on entry to a function, it is generally
18344 desirable for execution to be suspended, not on the very first
18345 instruction of the function, but rather at a point after the
18346 function's frame has been set up, after any language defined local
18347 declaration processing has been completed, and before execution of
18348 the first statement of the function begins. Debuggers generally
18349 cannot properly determine where this point is. Similarly for a
18350 breakpoint set on exit from a function. The prologue and epilogue
18351 attributes allow a compiler to communicate the location(s) to use. */
18352
18353 {
18354 if (fde->dw_fde_vms_end_prologue)
18355 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18356 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18357
18358 if (fde->dw_fde_vms_begin_epilogue)
18359 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18360 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18361 }
18362 #endif
18363
18364 }
18365 else
18366 {
18367 /* Generate pubnames entries for the split function code ranges. */
18368 dw_fde_ref fde = fun->fde;
18369
18370 if (fde->dw_fde_second_begin)
18371 {
18372 if (dwarf_version >= 3 || !dwarf_strict)
18373 {
18374 /* We should use ranges for non-contiguous code section
18375 addresses. Use the actual code range for the initial
18376 section, since the HOT/COLD labels might precede an
18377 alignment offset. */
18378 bool range_list_added = false;
18379 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18380 fde->dw_fde_end, &range_list_added,
18381 false);
18382 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18383 fde->dw_fde_second_end,
18384 &range_list_added, false);
18385 if (range_list_added)
18386 add_ranges (NULL);
18387 }
18388 else
18389 {
18390 /* There is no real support in DW2 for this .. so we make
18391 a work-around. First, emit the pub name for the segment
18392 containing the function label. Then make and emit a
18393 simplified subprogram DIE for the second segment with the
18394 name pre-fixed by __hot/cold_sect_of_. We use the same
18395 linkage name for the second die so that gdb will find both
18396 sections when given "b foo". */
18397 const char *name = NULL;
18398 tree decl_name = DECL_NAME (decl);
18399 dw_die_ref seg_die;
18400
18401 /* Do the 'primary' section. */
18402 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18403 fde->dw_fde_end, false);
18404
18405 /* Build a minimal DIE for the secondary section. */
18406 seg_die = new_die (DW_TAG_subprogram,
18407 subr_die->die_parent, decl);
18408
18409 if (TREE_PUBLIC (decl))
18410 add_AT_flag (seg_die, DW_AT_external, 1);
18411
18412 if (decl_name != NULL
18413 && IDENTIFIER_POINTER (decl_name) != NULL)
18414 {
18415 name = dwarf2_name (decl, 1);
18416 if (! DECL_ARTIFICIAL (decl))
18417 add_src_coords_attributes (seg_die, decl);
18418
18419 add_linkage_name (seg_die, decl);
18420 }
18421 gcc_assert (name != NULL);
18422 add_pure_or_virtual_attribute (seg_die, decl);
18423 if (DECL_ARTIFICIAL (decl))
18424 add_AT_flag (seg_die, DW_AT_artificial, 1);
18425
18426 name = concat ("__second_sect_of_", name, NULL);
18427 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18428 fde->dw_fde_second_end, false);
18429 add_name_attribute (seg_die, name);
18430 if (want_pubnames ())
18431 add_pubname_string (name, seg_die);
18432 }
18433 }
18434 else
18435 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18436 false);
18437 }
18438
18439 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18440
18441 /* We define the "frame base" as the function's CFA. This is more
18442 convenient for several reasons: (1) It's stable across the prologue
18443 and epilogue, which makes it better than just a frame pointer,
18444 (2) With dwarf3, there exists a one-byte encoding that allows us
18445 to reference the .debug_frame data by proxy, but failing that,
18446 (3) We can at least reuse the code inspection and interpretation
18447 code that determines the CFA position at various points in the
18448 function. */
18449 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18450 {
18451 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18452 add_AT_loc (subr_die, DW_AT_frame_base, op);
18453 }
18454 else
18455 {
18456 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18457 if (list->dw_loc_next)
18458 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18459 else
18460 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18461 }
18462
18463 /* Compute a displacement from the "steady-state frame pointer" to
18464 the CFA. The former is what all stack slots and argument slots
18465 will reference in the rtl; the latter is what we've told the
18466 debugger about. We'll need to adjust all frame_base references
18467 by this displacement. */
18468 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18469
18470 if (fun->static_chain_decl)
18471 add_AT_location_description (subr_die, DW_AT_static_link,
18472 loc_list_from_tree (fun->static_chain_decl, 2));
18473 }
18474
18475 /* Generate child dies for template paramaters. */
18476 if (debug_info_level > DINFO_LEVEL_TERSE)
18477 gen_generic_params_dies (decl);
18478
18479 /* Now output descriptions of the arguments for this function. This gets
18480 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18481 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18482 `...' at the end of the formal parameter list. In order to find out if
18483 there was a trailing ellipsis or not, we must instead look at the type
18484 associated with the FUNCTION_DECL. This will be a node of type
18485 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18486 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18487 an ellipsis at the end. */
18488
18489 /* In the case where we are describing a mere function declaration, all we
18490 need to do here (and all we *can* do here) is to describe the *types* of
18491 its formal parameters. */
18492 if (debug_info_level <= DINFO_LEVEL_TERSE)
18493 ;
18494 else if (declaration)
18495 gen_formal_types_die (decl, subr_die);
18496 else
18497 {
18498 /* Generate DIEs to represent all known formal parameters. */
18499 tree parm = DECL_ARGUMENTS (decl);
18500 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18501 tree generic_decl_parm = generic_decl
18502 ? DECL_ARGUMENTS (generic_decl)
18503 : NULL;
18504
18505 /* Now we want to walk the list of parameters of the function and
18506 emit their relevant DIEs.
18507
18508 We consider the case of DECL being an instance of a generic function
18509 as well as it being a normal function.
18510
18511 If DECL is an instance of a generic function we walk the
18512 parameters of the generic function declaration _and_ the parameters of
18513 DECL itself. This is useful because we want to emit specific DIEs for
18514 function parameter packs and those are declared as part of the
18515 generic function declaration. In that particular case,
18516 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18517 That DIE has children DIEs representing the set of arguments
18518 of the pack. Note that the set of pack arguments can be empty.
18519 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18520 children DIE.
18521
18522 Otherwise, we just consider the parameters of DECL. */
18523 while (generic_decl_parm || parm)
18524 {
18525 if (generic_decl_parm
18526 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18527 gen_formal_parameter_pack_die (generic_decl_parm,
18528 parm, subr_die,
18529 &parm);
18530 else if (parm)
18531 {
18532 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18533
18534 if (parm == DECL_ARGUMENTS (decl)
18535 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18536 && parm_die
18537 && (dwarf_version >= 3 || !dwarf_strict))
18538 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18539
18540 parm = DECL_CHAIN (parm);
18541 }
18542
18543 if (generic_decl_parm)
18544 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18545 }
18546
18547 /* Decide whether we need an unspecified_parameters DIE at the end.
18548 There are 2 more cases to do this for: 1) the ansi ... declaration -
18549 this is detectable when the end of the arg list is not a
18550 void_type_node 2) an unprototyped function declaration (not a
18551 definition). This just means that we have no info about the
18552 parameters at all. */
18553 if (prototype_p (TREE_TYPE (decl)))
18554 {
18555 /* This is the prototyped case, check for.... */
18556 if (stdarg_p (TREE_TYPE (decl)))
18557 gen_unspecified_parameters_die (decl, subr_die);
18558 }
18559 else if (DECL_INITIAL (decl) == NULL_TREE)
18560 gen_unspecified_parameters_die (decl, subr_die);
18561 }
18562
18563 /* Output Dwarf info for all of the stuff within the body of the function
18564 (if it has one - it may be just a declaration). */
18565 outer_scope = DECL_INITIAL (decl);
18566
18567 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18568 a function. This BLOCK actually represents the outermost binding contour
18569 for the function, i.e. the contour in which the function's formal
18570 parameters and labels get declared. Curiously, it appears that the front
18571 end doesn't actually put the PARM_DECL nodes for the current function onto
18572 the BLOCK_VARS list for this outer scope, but are strung off of the
18573 DECL_ARGUMENTS list for the function instead.
18574
18575 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18576 the LABEL_DECL nodes for the function however, and we output DWARF info
18577 for those in decls_for_scope. Just within the `outer_scope' there will be
18578 a BLOCK node representing the function's outermost pair of curly braces,
18579 and any blocks used for the base and member initializers of a C++
18580 constructor function. */
18581 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18582 {
18583 int call_site_note_count = 0;
18584 int tail_call_site_note_count = 0;
18585
18586 /* Emit a DW_TAG_variable DIE for a named return value. */
18587 if (DECL_NAME (DECL_RESULT (decl)))
18588 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18589
18590 current_function_has_inlines = 0;
18591 decls_for_scope (outer_scope, subr_die, 0);
18592
18593 if (call_arg_locations && !dwarf_strict)
18594 {
18595 struct call_arg_loc_node *ca_loc;
18596 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18597 {
18598 dw_die_ref die = NULL;
18599 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18600 rtx arg, next_arg;
18601
18602 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18603 arg; arg = next_arg)
18604 {
18605 dw_loc_descr_ref reg, val;
18606 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18607 dw_die_ref cdie, tdie = NULL;
18608
18609 next_arg = XEXP (arg, 1);
18610 if (REG_P (XEXP (XEXP (arg, 0), 0))
18611 && next_arg
18612 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18613 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18614 && REGNO (XEXP (XEXP (arg, 0), 0))
18615 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18616 next_arg = XEXP (next_arg, 1);
18617 if (mode == VOIDmode)
18618 {
18619 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18620 if (mode == VOIDmode)
18621 mode = GET_MODE (XEXP (arg, 0));
18622 }
18623 if (mode == VOIDmode || mode == BLKmode)
18624 continue;
18625 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18626 {
18627 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18628 tloc = XEXP (XEXP (arg, 0), 1);
18629 continue;
18630 }
18631 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18632 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18633 {
18634 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18635 tlocc = XEXP (XEXP (arg, 0), 1);
18636 continue;
18637 }
18638 reg = NULL;
18639 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18640 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18641 VAR_INIT_STATUS_INITIALIZED);
18642 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18643 {
18644 rtx mem = XEXP (XEXP (arg, 0), 0);
18645 reg = mem_loc_descriptor (XEXP (mem, 0),
18646 get_address_mode (mem),
18647 GET_MODE (mem),
18648 VAR_INIT_STATUS_INITIALIZED);
18649 }
18650 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18651 == DEBUG_PARAMETER_REF)
18652 {
18653 tree tdecl
18654 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18655 tdie = lookup_decl_die (tdecl);
18656 if (tdie == NULL)
18657 continue;
18658 }
18659 else
18660 continue;
18661 if (reg == NULL
18662 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18663 != DEBUG_PARAMETER_REF)
18664 continue;
18665 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18666 VOIDmode,
18667 VAR_INIT_STATUS_INITIALIZED);
18668 if (val == NULL)
18669 continue;
18670 if (die == NULL)
18671 die = gen_call_site_die (decl, subr_die, ca_loc);
18672 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18673 NULL_TREE);
18674 if (reg != NULL)
18675 add_AT_loc (cdie, DW_AT_location, reg);
18676 else if (tdie != NULL)
18677 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18678 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18679 if (next_arg != XEXP (arg, 1))
18680 {
18681 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18682 if (mode == VOIDmode)
18683 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18684 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18685 0), 1),
18686 mode, VOIDmode,
18687 VAR_INIT_STATUS_INITIALIZED);
18688 if (val != NULL)
18689 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18690 }
18691 }
18692 if (die == NULL
18693 && (ca_loc->symbol_ref || tloc))
18694 die = gen_call_site_die (decl, subr_die, ca_loc);
18695 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18696 {
18697 dw_loc_descr_ref tval = NULL;
18698
18699 if (tloc != NULL_RTX)
18700 tval = mem_loc_descriptor (tloc,
18701 GET_MODE (tloc) == VOIDmode
18702 ? Pmode : GET_MODE (tloc),
18703 VOIDmode,
18704 VAR_INIT_STATUS_INITIALIZED);
18705 if (tval)
18706 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18707 else if (tlocc != NULL_RTX)
18708 {
18709 tval = mem_loc_descriptor (tlocc,
18710 GET_MODE (tlocc) == VOIDmode
18711 ? Pmode : GET_MODE (tlocc),
18712 VOIDmode,
18713 VAR_INIT_STATUS_INITIALIZED);
18714 if (tval)
18715 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18716 tval);
18717 }
18718 }
18719 if (die != NULL)
18720 {
18721 call_site_note_count++;
18722 if (ca_loc->tail_call_p)
18723 tail_call_site_note_count++;
18724 }
18725 }
18726 }
18727 call_arg_locations = NULL;
18728 call_arg_loc_last = NULL;
18729 if (tail_call_site_count >= 0
18730 && tail_call_site_count == tail_call_site_note_count
18731 && !dwarf_strict)
18732 {
18733 if (call_site_count >= 0
18734 && call_site_count == call_site_note_count)
18735 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18736 else
18737 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18738 }
18739 call_site_count = -1;
18740 tail_call_site_count = -1;
18741 }
18742
18743 if (subr_die != old_die)
18744 /* Add the calling convention attribute if requested. */
18745 add_calling_convention_attribute (subr_die, decl);
18746 }
18747
18748 /* Returns a hash value for X (which really is a die_struct). */
18749
18750 static hashval_t
18751 common_block_die_table_hash (const void *x)
18752 {
18753 const_dw_die_ref d = (const_dw_die_ref) x;
18754 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18755 }
18756
18757 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18758 as decl_id and die_parent of die_struct Y. */
18759
18760 static int
18761 common_block_die_table_eq (const void *x, const void *y)
18762 {
18763 const_dw_die_ref d = (const_dw_die_ref) x;
18764 const_dw_die_ref e = (const_dw_die_ref) y;
18765 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18766 }
18767
18768 /* Generate a DIE to represent a declared data object.
18769 Either DECL or ORIGIN must be non-null. */
18770
18771 static void
18772 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18773 {
18774 HOST_WIDE_INT off = 0;
18775 tree com_decl;
18776 tree decl_or_origin = decl ? decl : origin;
18777 tree ultimate_origin;
18778 dw_die_ref var_die;
18779 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18780 dw_die_ref origin_die;
18781 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18782 || class_or_namespace_scope_p (context_die));
18783 bool specialization_p = false;
18784
18785 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18786 if (decl || ultimate_origin)
18787 origin = ultimate_origin;
18788 com_decl = fortran_common (decl_or_origin, &off);
18789
18790 /* Symbol in common gets emitted as a child of the common block, in the form
18791 of a data member. */
18792 if (com_decl)
18793 {
18794 dw_die_ref com_die;
18795 dw_loc_list_ref loc;
18796 die_node com_die_arg;
18797
18798 var_die = lookup_decl_die (decl_or_origin);
18799 if (var_die)
18800 {
18801 if (get_AT (var_die, DW_AT_location) == NULL)
18802 {
18803 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18804 if (loc)
18805 {
18806 if (off)
18807 {
18808 /* Optimize the common case. */
18809 if (single_element_loc_list_p (loc)
18810 && loc->expr->dw_loc_opc == DW_OP_addr
18811 && loc->expr->dw_loc_next == NULL
18812 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18813 == SYMBOL_REF)
18814 {
18815 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18816 loc->expr->dw_loc_oprnd1.v.val_addr
18817 = plus_constant (GET_MODE (x), x , off);
18818 }
18819 else
18820 loc_list_plus_const (loc, off);
18821 }
18822 add_AT_location_description (var_die, DW_AT_location, loc);
18823 remove_AT (var_die, DW_AT_declaration);
18824 }
18825 }
18826 return;
18827 }
18828
18829 if (common_block_die_table == NULL)
18830 common_block_die_table
18831 = htab_create_ggc (10, common_block_die_table_hash,
18832 common_block_die_table_eq, NULL);
18833
18834 com_die_arg.decl_id = DECL_UID (com_decl);
18835 com_die_arg.die_parent = context_die;
18836 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18837 loc = loc_list_from_tree (com_decl, 2);
18838 if (com_die == NULL)
18839 {
18840 const char *cnam
18841 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18842 void **slot;
18843
18844 com_die = new_die (DW_TAG_common_block, context_die, decl);
18845 add_name_and_src_coords_attributes (com_die, com_decl);
18846 if (loc)
18847 {
18848 add_AT_location_description (com_die, DW_AT_location, loc);
18849 /* Avoid sharing the same loc descriptor between
18850 DW_TAG_common_block and DW_TAG_variable. */
18851 loc = loc_list_from_tree (com_decl, 2);
18852 }
18853 else if (DECL_EXTERNAL (decl))
18854 add_AT_flag (com_die, DW_AT_declaration, 1);
18855 if (want_pubnames ())
18856 add_pubname_string (cnam, com_die); /* ??? needed? */
18857 com_die->decl_id = DECL_UID (com_decl);
18858 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18859 *slot = (void *) com_die;
18860 }
18861 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18862 {
18863 add_AT_location_description (com_die, DW_AT_location, loc);
18864 loc = loc_list_from_tree (com_decl, 2);
18865 remove_AT (com_die, DW_AT_declaration);
18866 }
18867 var_die = new_die (DW_TAG_variable, com_die, decl);
18868 add_name_and_src_coords_attributes (var_die, decl);
18869 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18870 TREE_THIS_VOLATILE (decl), context_die);
18871 add_AT_flag (var_die, DW_AT_external, 1);
18872 if (loc)
18873 {
18874 if (off)
18875 {
18876 /* Optimize the common case. */
18877 if (single_element_loc_list_p (loc)
18878 && loc->expr->dw_loc_opc == DW_OP_addr
18879 && loc->expr->dw_loc_next == NULL
18880 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18881 {
18882 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18883 loc->expr->dw_loc_oprnd1.v.val_addr
18884 = plus_constant (GET_MODE (x), x, off);
18885 }
18886 else
18887 loc_list_plus_const (loc, off);
18888 }
18889 add_AT_location_description (var_die, DW_AT_location, loc);
18890 }
18891 else if (DECL_EXTERNAL (decl))
18892 add_AT_flag (var_die, DW_AT_declaration, 1);
18893 equate_decl_number_to_die (decl, var_die);
18894 return;
18895 }
18896
18897 /* If the compiler emitted a definition for the DECL declaration
18898 and if we already emitted a DIE for it, don't emit a second
18899 DIE for it again. Allow re-declarations of DECLs that are
18900 inside functions, though. */
18901 if (old_die && declaration && !local_scope_p (context_die))
18902 return;
18903
18904 /* For static data members, the declaration in the class is supposed
18905 to have DW_TAG_member tag; the specification should still be
18906 DW_TAG_variable referencing the DW_TAG_member DIE. */
18907 if (declaration && class_scope_p (context_die))
18908 var_die = new_die (DW_TAG_member, context_die, decl);
18909 else
18910 var_die = new_die (DW_TAG_variable, context_die, decl);
18911
18912 origin_die = NULL;
18913 if (origin != NULL)
18914 origin_die = add_abstract_origin_attribute (var_die, origin);
18915
18916 /* Loop unrolling can create multiple blocks that refer to the same
18917 static variable, so we must test for the DW_AT_declaration flag.
18918
18919 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18920 copy decls and set the DECL_ABSTRACT flag on them instead of
18921 sharing them.
18922
18923 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18924
18925 ??? The declare_in_namespace support causes us to get two DIEs for one
18926 variable, both of which are declarations. We want to avoid considering
18927 one to be a specification, so we must test that this DIE is not a
18928 declaration. */
18929 else if (old_die && TREE_STATIC (decl) && ! declaration
18930 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18931 {
18932 /* This is a definition of a C++ class level static. */
18933 add_AT_specification (var_die, old_die);
18934 specialization_p = true;
18935 if (DECL_NAME (decl))
18936 {
18937 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18938 struct dwarf_file_data * file_index = lookup_filename (s.file);
18939
18940 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18941 add_AT_file (var_die, DW_AT_decl_file, file_index);
18942
18943 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18944 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18945
18946 if (old_die->die_tag == DW_TAG_member)
18947 add_linkage_name (var_die, decl);
18948 }
18949 }
18950 else
18951 add_name_and_src_coords_attributes (var_die, decl);
18952
18953 if ((origin == NULL && !specialization_p)
18954 || (origin != NULL
18955 && !DECL_ABSTRACT (decl_or_origin)
18956 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18957 decl_function_context
18958 (decl_or_origin))))
18959 {
18960 tree type = TREE_TYPE (decl_or_origin);
18961
18962 if (decl_by_reference_p (decl_or_origin))
18963 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18964 else
18965 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18966 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18967 }
18968
18969 if (origin == NULL && !specialization_p)
18970 {
18971 if (TREE_PUBLIC (decl))
18972 add_AT_flag (var_die, DW_AT_external, 1);
18973
18974 if (DECL_ARTIFICIAL (decl))
18975 add_AT_flag (var_die, DW_AT_artificial, 1);
18976
18977 add_accessibility_attribute (var_die, decl);
18978 }
18979
18980 if (declaration)
18981 add_AT_flag (var_die, DW_AT_declaration, 1);
18982
18983 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18984 equate_decl_number_to_die (decl, var_die);
18985
18986 if (! declaration
18987 && (! DECL_ABSTRACT (decl_or_origin)
18988 /* Local static vars are shared between all clones/inlines,
18989 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18990 already set. */
18991 || (TREE_CODE (decl_or_origin) == VAR_DECL
18992 && TREE_STATIC (decl_or_origin)
18993 && DECL_RTL_SET_P (decl_or_origin)))
18994 /* When abstract origin already has DW_AT_location attribute, no need
18995 to add it again. */
18996 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18997 {
18998 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18999 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19000 defer_location (decl_or_origin, var_die);
19001 else
19002 add_location_or_const_value_attribute (var_die, decl_or_origin,
19003 decl == NULL, DW_AT_location);
19004 add_pubname (decl_or_origin, var_die);
19005 }
19006 else
19007 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19008 }
19009
19010 /* Generate a DIE to represent a named constant. */
19011
19012 static void
19013 gen_const_die (tree decl, dw_die_ref context_die)
19014 {
19015 dw_die_ref const_die;
19016 tree type = TREE_TYPE (decl);
19017
19018 const_die = new_die (DW_TAG_constant, context_die, decl);
19019 add_name_and_src_coords_attributes (const_die, decl);
19020 add_type_attribute (const_die, type, 1, 0, context_die);
19021 if (TREE_PUBLIC (decl))
19022 add_AT_flag (const_die, DW_AT_external, 1);
19023 if (DECL_ARTIFICIAL (decl))
19024 add_AT_flag (const_die, DW_AT_artificial, 1);
19025 tree_add_const_value_attribute_for_decl (const_die, decl);
19026 }
19027
19028 /* Generate a DIE to represent a label identifier. */
19029
19030 static void
19031 gen_label_die (tree decl, dw_die_ref context_die)
19032 {
19033 tree origin = decl_ultimate_origin (decl);
19034 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19035 rtx insn;
19036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19037
19038 if (origin != NULL)
19039 add_abstract_origin_attribute (lbl_die, origin);
19040 else
19041 add_name_and_src_coords_attributes (lbl_die, decl);
19042
19043 if (DECL_ABSTRACT (decl))
19044 equate_decl_number_to_die (decl, lbl_die);
19045 else
19046 {
19047 insn = DECL_RTL_IF_SET (decl);
19048
19049 /* Deleted labels are programmer specified labels which have been
19050 eliminated because of various optimizations. We still emit them
19051 here so that it is possible to put breakpoints on them. */
19052 if (insn
19053 && (LABEL_P (insn)
19054 || ((NOTE_P (insn)
19055 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19056 {
19057 /* When optimization is enabled (via -O) some parts of the compiler
19058 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19059 represent source-level labels which were explicitly declared by
19060 the user. This really shouldn't be happening though, so catch
19061 it if it ever does happen. */
19062 gcc_assert (!INSN_DELETED_P (insn));
19063
19064 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19065 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19066 }
19067 else if (insn
19068 && NOTE_P (insn)
19069 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19070 && CODE_LABEL_NUMBER (insn) != -1)
19071 {
19072 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19073 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19074 }
19075 }
19076 }
19077
19078 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19079 attributes to the DIE for a block STMT, to describe where the inlined
19080 function was called from. This is similar to add_src_coords_attributes. */
19081
19082 static inline void
19083 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19084 {
19085 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19086
19087 if (dwarf_version >= 3 || !dwarf_strict)
19088 {
19089 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19090 add_AT_unsigned (die, DW_AT_call_line, s.line);
19091 }
19092 }
19093
19094
19095 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19096 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19097
19098 static inline void
19099 add_high_low_attributes (tree stmt, dw_die_ref die)
19100 {
19101 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19102
19103 if (BLOCK_FRAGMENT_CHAIN (stmt)
19104 && (dwarf_version >= 3 || !dwarf_strict))
19105 {
19106 tree chain, superblock = NULL_TREE;
19107 dw_die_ref pdie;
19108 dw_attr_ref attr = NULL;
19109
19110 if (inlined_function_outer_scope_p (stmt))
19111 {
19112 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19113 BLOCK_NUMBER (stmt));
19114 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19115 }
19116
19117 /* Optimize duplicate .debug_ranges lists or even tails of
19118 lists. If this BLOCK has same ranges as its supercontext,
19119 lookup DW_AT_ranges attribute in the supercontext (and
19120 recursively so), verify that the ranges_table contains the
19121 right values and use it instead of adding a new .debug_range. */
19122 for (chain = stmt, pdie = die;
19123 BLOCK_SAME_RANGE (chain);
19124 chain = BLOCK_SUPERCONTEXT (chain))
19125 {
19126 dw_attr_ref new_attr;
19127
19128 pdie = pdie->die_parent;
19129 if (pdie == NULL)
19130 break;
19131 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19132 break;
19133 new_attr = get_AT (pdie, DW_AT_ranges);
19134 if (new_attr == NULL
19135 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19136 break;
19137 attr = new_attr;
19138 superblock = BLOCK_SUPERCONTEXT (chain);
19139 }
19140 if (attr != NULL
19141 && (ranges_table[attr->dw_attr_val.v.val_offset
19142 / 2 / DWARF2_ADDR_SIZE].num
19143 == BLOCK_NUMBER (superblock))
19144 && BLOCK_FRAGMENT_CHAIN (superblock))
19145 {
19146 unsigned long off = attr->dw_attr_val.v.val_offset
19147 / 2 / DWARF2_ADDR_SIZE;
19148 unsigned long supercnt = 0, thiscnt = 0;
19149 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19150 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19151 {
19152 ++supercnt;
19153 gcc_checking_assert (ranges_table[off + supercnt].num
19154 == BLOCK_NUMBER (chain));
19155 }
19156 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19157 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19158 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19159 ++thiscnt;
19160 gcc_assert (supercnt >= thiscnt);
19161 add_AT_range_list (die, DW_AT_ranges,
19162 ((off + supercnt - thiscnt)
19163 * 2 * DWARF2_ADDR_SIZE),
19164 false);
19165 return;
19166 }
19167
19168 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19169
19170 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19171 do
19172 {
19173 add_ranges (chain);
19174 chain = BLOCK_FRAGMENT_CHAIN (chain);
19175 }
19176 while (chain);
19177 add_ranges (NULL);
19178 }
19179 else
19180 {
19181 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19182 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19183 BLOCK_NUMBER (stmt));
19184 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19185 BLOCK_NUMBER (stmt));
19186 add_AT_low_high_pc (die, label, label_high, false);
19187 }
19188 }
19189
19190 /* Generate a DIE for a lexical block. */
19191
19192 static void
19193 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19194 {
19195 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19196
19197 if (call_arg_locations)
19198 {
19199 if (block_map.length () <= BLOCK_NUMBER (stmt))
19200 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19201 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19202 }
19203
19204 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19205 add_high_low_attributes (stmt, stmt_die);
19206
19207 decls_for_scope (stmt, stmt_die, depth);
19208 }
19209
19210 /* Generate a DIE for an inlined subprogram. */
19211
19212 static void
19213 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19214 {
19215 tree decl;
19216
19217 /* The instance of function that is effectively being inlined shall not
19218 be abstract. */
19219 gcc_assert (! BLOCK_ABSTRACT (stmt));
19220
19221 decl = block_ultimate_origin (stmt);
19222
19223 /* Emit info for the abstract instance first, if we haven't yet. We
19224 must emit this even if the block is abstract, otherwise when we
19225 emit the block below (or elsewhere), we may end up trying to emit
19226 a die whose origin die hasn't been emitted, and crashing. */
19227 dwarf2out_abstract_function (decl);
19228
19229 if (! BLOCK_ABSTRACT (stmt))
19230 {
19231 dw_die_ref subr_die
19232 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19233
19234 if (call_arg_locations)
19235 {
19236 if (block_map.length () <= BLOCK_NUMBER (stmt))
19237 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19238 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19239 }
19240 add_abstract_origin_attribute (subr_die, decl);
19241 if (TREE_ASM_WRITTEN (stmt))
19242 add_high_low_attributes (stmt, subr_die);
19243 add_call_src_coords_attributes (stmt, subr_die);
19244
19245 decls_for_scope (stmt, subr_die, depth);
19246 current_function_has_inlines = 1;
19247 }
19248 }
19249
19250 /* Generate a DIE for a field in a record, or structure. */
19251
19252 static void
19253 gen_field_die (tree decl, dw_die_ref context_die)
19254 {
19255 dw_die_ref decl_die;
19256
19257 if (TREE_TYPE (decl) == error_mark_node)
19258 return;
19259
19260 decl_die = new_die (DW_TAG_member, context_die, decl);
19261 add_name_and_src_coords_attributes (decl_die, decl);
19262 add_type_attribute (decl_die, member_declared_type (decl),
19263 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19264 context_die);
19265
19266 if (DECL_BIT_FIELD_TYPE (decl))
19267 {
19268 add_byte_size_attribute (decl_die, decl);
19269 add_bit_size_attribute (decl_die, decl);
19270 add_bit_offset_attribute (decl_die, decl);
19271 }
19272
19273 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19274 add_data_member_location_attribute (decl_die, decl);
19275
19276 if (DECL_ARTIFICIAL (decl))
19277 add_AT_flag (decl_die, DW_AT_artificial, 1);
19278
19279 add_accessibility_attribute (decl_die, decl);
19280
19281 /* Equate decl number to die, so that we can look up this decl later on. */
19282 equate_decl_number_to_die (decl, decl_die);
19283 }
19284
19285 #if 0
19286 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19287 Use modified_type_die instead.
19288 We keep this code here just in case these types of DIEs may be needed to
19289 represent certain things in other languages (e.g. Pascal) someday. */
19290
19291 static void
19292 gen_pointer_type_die (tree type, dw_die_ref context_die)
19293 {
19294 dw_die_ref ptr_die
19295 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19296
19297 equate_type_number_to_die (type, ptr_die);
19298 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19299 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19300 }
19301
19302 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19303 Use modified_type_die instead.
19304 We keep this code here just in case these types of DIEs may be needed to
19305 represent certain things in other languages (e.g. Pascal) someday. */
19306
19307 static void
19308 gen_reference_type_die (tree type, dw_die_ref context_die)
19309 {
19310 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19311
19312 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19313 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19314 else
19315 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19316
19317 equate_type_number_to_die (type, ref_die);
19318 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19319 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19320 }
19321 #endif
19322
19323 /* Generate a DIE for a pointer to a member type. */
19324
19325 static void
19326 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19327 {
19328 dw_die_ref ptr_die
19329 = new_die (DW_TAG_ptr_to_member_type,
19330 scope_die_for (type, context_die), type);
19331
19332 equate_type_number_to_die (type, ptr_die);
19333 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19334 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19335 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19336 }
19337
19338 typedef const char *dchar_p; /* For DEF_VEC_P. */
19339
19340 static char *producer_string;
19341
19342 /* Return a heap allocated producer string including command line options
19343 if -grecord-gcc-switches. */
19344
19345 static char *
19346 gen_producer_string (void)
19347 {
19348 size_t j;
19349 auto_vec<dchar_p> switches;
19350 const char *language_string = lang_hooks.name;
19351 char *producer, *tail;
19352 const char *p;
19353 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19354 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19355
19356 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19357 switch (save_decoded_options[j].opt_index)
19358 {
19359 case OPT_o:
19360 case OPT_d:
19361 case OPT_dumpbase:
19362 case OPT_dumpdir:
19363 case OPT_auxbase:
19364 case OPT_auxbase_strip:
19365 case OPT_quiet:
19366 case OPT_version:
19367 case OPT_v:
19368 case OPT_w:
19369 case OPT_L:
19370 case OPT_D:
19371 case OPT_I:
19372 case OPT_U:
19373 case OPT_SPECIAL_unknown:
19374 case OPT_SPECIAL_ignore:
19375 case OPT_SPECIAL_program_name:
19376 case OPT_SPECIAL_input_file:
19377 case OPT_grecord_gcc_switches:
19378 case OPT_gno_record_gcc_switches:
19379 case OPT__output_pch_:
19380 case OPT_fdiagnostics_show_location_:
19381 case OPT_fdiagnostics_show_option:
19382 case OPT_fdiagnostics_show_caret:
19383 case OPT_fdiagnostics_color_:
19384 case OPT_fverbose_asm:
19385 case OPT____:
19386 case OPT__sysroot_:
19387 case OPT_nostdinc:
19388 case OPT_nostdinc__:
19389 /* Ignore these. */
19390 continue;
19391 default:
19392 if (cl_options[save_decoded_options[j].opt_index].flags
19393 & CL_NO_DWARF_RECORD)
19394 continue;
19395 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19396 == '-');
19397 switch (save_decoded_options[j].canonical_option[0][1])
19398 {
19399 case 'M':
19400 case 'i':
19401 case 'W':
19402 continue;
19403 case 'f':
19404 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19405 "dump", 4) == 0)
19406 continue;
19407 break;
19408 default:
19409 break;
19410 }
19411 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19412 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19413 break;
19414 }
19415
19416 producer = XNEWVEC (char, plen + 1 + len + 1);
19417 tail = producer;
19418 sprintf (tail, "%s %s", language_string, version_string);
19419 tail += plen;
19420
19421 FOR_EACH_VEC_ELT (switches, j, p)
19422 {
19423 len = strlen (p);
19424 *tail = ' ';
19425 memcpy (tail + 1, p, len);
19426 tail += len + 1;
19427 }
19428
19429 *tail = '\0';
19430 return producer;
19431 }
19432
19433 /* Generate the DIE for the compilation unit. */
19434
19435 static dw_die_ref
19436 gen_compile_unit_die (const char *filename)
19437 {
19438 dw_die_ref die;
19439 const char *language_string = lang_hooks.name;
19440 int language;
19441
19442 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19443
19444 if (filename)
19445 {
19446 add_name_attribute (die, filename);
19447 /* Don't add cwd for <built-in>. */
19448 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19449 add_comp_dir_attribute (die);
19450 }
19451
19452 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19453
19454 /* If our producer is LTO try to figure out a common language to use
19455 from the global list of translation units. */
19456 if (strcmp (language_string, "GNU GIMPLE") == 0)
19457 {
19458 unsigned i;
19459 tree t;
19460 const char *common_lang = NULL;
19461
19462 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19463 {
19464 if (!TRANSLATION_UNIT_LANGUAGE (t))
19465 continue;
19466 if (!common_lang)
19467 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19468 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19469 ;
19470 else if (strncmp (common_lang, "GNU C", 5) == 0
19471 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19472 /* Mixing C and C++ is ok, use C++ in that case. */
19473 common_lang = "GNU C++";
19474 else
19475 {
19476 /* Fall back to C. */
19477 common_lang = NULL;
19478 break;
19479 }
19480 }
19481
19482 if (common_lang)
19483 language_string = common_lang;
19484 }
19485
19486 language = DW_LANG_C89;
19487 if (strcmp (language_string, "GNU C++") == 0)
19488 language = DW_LANG_C_plus_plus;
19489 else if (strcmp (language_string, "GNU F77") == 0)
19490 language = DW_LANG_Fortran77;
19491 else if (strcmp (language_string, "GNU Pascal") == 0)
19492 language = DW_LANG_Pascal83;
19493 else if (dwarf_version >= 3 || !dwarf_strict)
19494 {
19495 if (strcmp (language_string, "GNU Ada") == 0)
19496 language = DW_LANG_Ada95;
19497 else if (strcmp (language_string, "GNU Fortran") == 0)
19498 language = DW_LANG_Fortran95;
19499 else if (strcmp (language_string, "GNU Java") == 0)
19500 language = DW_LANG_Java;
19501 else if (strcmp (language_string, "GNU Objective-C") == 0)
19502 language = DW_LANG_ObjC;
19503 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19504 language = DW_LANG_ObjC_plus_plus;
19505 else if (dwarf_version >= 5 || !dwarf_strict)
19506 {
19507 if (strcmp (language_string, "GNU Go") == 0)
19508 language = DW_LANG_Go;
19509 }
19510 }
19511 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19512 else if (strcmp (language_string, "GNU Fortran") == 0)
19513 language = DW_LANG_Fortran90;
19514
19515 add_AT_unsigned (die, DW_AT_language, language);
19516
19517 switch (language)
19518 {
19519 case DW_LANG_Fortran77:
19520 case DW_LANG_Fortran90:
19521 case DW_LANG_Fortran95:
19522 /* Fortran has case insensitive identifiers and the front-end
19523 lowercases everything. */
19524 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19525 break;
19526 default:
19527 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19528 break;
19529 }
19530 return die;
19531 }
19532
19533 /* Generate the DIE for a base class. */
19534
19535 static void
19536 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19537 {
19538 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19539
19540 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19541 add_data_member_location_attribute (die, binfo);
19542
19543 if (BINFO_VIRTUAL_P (binfo))
19544 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19545
19546 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19547 children, otherwise the default is DW_ACCESS_public. In DWARF2
19548 the default has always been DW_ACCESS_private. */
19549 if (access == access_public_node)
19550 {
19551 if (dwarf_version == 2
19552 || context_die->die_tag == DW_TAG_class_type)
19553 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19554 }
19555 else if (access == access_protected_node)
19556 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19557 else if (dwarf_version > 2
19558 && context_die->die_tag != DW_TAG_class_type)
19559 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19560 }
19561
19562 /* Generate a DIE for a class member. */
19563
19564 static void
19565 gen_member_die (tree type, dw_die_ref context_die)
19566 {
19567 tree member;
19568 tree binfo = TYPE_BINFO (type);
19569 dw_die_ref child;
19570
19571 /* If this is not an incomplete type, output descriptions of each of its
19572 members. Note that as we output the DIEs necessary to represent the
19573 members of this record or union type, we will also be trying to output
19574 DIEs to represent the *types* of those members. However the `type'
19575 function (above) will specifically avoid generating type DIEs for member
19576 types *within* the list of member DIEs for this (containing) type except
19577 for those types (of members) which are explicitly marked as also being
19578 members of this (containing) type themselves. The g++ front- end can
19579 force any given type to be treated as a member of some other (containing)
19580 type by setting the TYPE_CONTEXT of the given (member) type to point to
19581 the TREE node representing the appropriate (containing) type. */
19582
19583 /* First output info about the base classes. */
19584 if (binfo)
19585 {
19586 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19587 int i;
19588 tree base;
19589
19590 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19591 gen_inheritance_die (base,
19592 (accesses ? (*accesses)[i] : access_public_node),
19593 context_die);
19594 }
19595
19596 /* Now output info about the data members and type members. */
19597 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19598 {
19599 /* If we thought we were generating minimal debug info for TYPE
19600 and then changed our minds, some of the member declarations
19601 may have already been defined. Don't define them again, but
19602 do put them in the right order. */
19603
19604 child = lookup_decl_die (member);
19605 if (child)
19606 splice_child_die (context_die, child);
19607 else
19608 gen_decl_die (member, NULL, context_die);
19609 }
19610
19611 /* Now output info about the function members (if any). */
19612 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19613 {
19614 /* Don't include clones in the member list. */
19615 if (DECL_ABSTRACT_ORIGIN (member))
19616 continue;
19617
19618 child = lookup_decl_die (member);
19619 if (child)
19620 splice_child_die (context_die, child);
19621 else
19622 gen_decl_die (member, NULL, context_die);
19623 }
19624 }
19625
19626 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19627 is set, we pretend that the type was never defined, so we only get the
19628 member DIEs needed by later specification DIEs. */
19629
19630 static void
19631 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19632 enum debug_info_usage usage)
19633 {
19634 dw_die_ref type_die = lookup_type_die (type);
19635 dw_die_ref scope_die = 0;
19636 int nested = 0;
19637 int complete = (TYPE_SIZE (type)
19638 && (! TYPE_STUB_DECL (type)
19639 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19640 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19641 complete = complete && should_emit_struct_debug (type, usage);
19642
19643 if (type_die && ! complete)
19644 return;
19645
19646 if (TYPE_CONTEXT (type) != NULL_TREE
19647 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19648 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19649 nested = 1;
19650
19651 scope_die = scope_die_for (type, context_die);
19652
19653 /* Generate child dies for template paramaters. */
19654 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19655 schedule_generic_params_dies_gen (type);
19656
19657 if (! type_die || (nested && is_cu_die (scope_die)))
19658 /* First occurrence of type or toplevel definition of nested class. */
19659 {
19660 dw_die_ref old_die = type_die;
19661
19662 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19663 ? record_type_tag (type) : DW_TAG_union_type,
19664 scope_die, type);
19665 equate_type_number_to_die (type, type_die);
19666 if (old_die)
19667 add_AT_specification (type_die, old_die);
19668 else
19669 add_name_attribute (type_die, type_tag (type));
19670 }
19671 else
19672 remove_AT (type_die, DW_AT_declaration);
19673
19674 /* If this type has been completed, then give it a byte_size attribute and
19675 then give a list of members. */
19676 if (complete && !ns_decl)
19677 {
19678 /* Prevent infinite recursion in cases where the type of some member of
19679 this type is expressed in terms of this type itself. */
19680 TREE_ASM_WRITTEN (type) = 1;
19681 add_byte_size_attribute (type_die, type);
19682 if (TYPE_STUB_DECL (type) != NULL_TREE)
19683 {
19684 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19685 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19686 }
19687
19688 /* If the first reference to this type was as the return type of an
19689 inline function, then it may not have a parent. Fix this now. */
19690 if (type_die->die_parent == NULL)
19691 add_child_die (scope_die, type_die);
19692
19693 push_decl_scope (type);
19694 gen_member_die (type, type_die);
19695 pop_decl_scope ();
19696
19697 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19698 if (TYPE_ARTIFICIAL (type))
19699 add_AT_flag (type_die, DW_AT_artificial, 1);
19700
19701 /* GNU extension: Record what type our vtable lives in. */
19702 if (TYPE_VFIELD (type))
19703 {
19704 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19705
19706 gen_type_die (vtype, context_die);
19707 add_AT_die_ref (type_die, DW_AT_containing_type,
19708 lookup_type_die (vtype));
19709 }
19710 }
19711 else
19712 {
19713 add_AT_flag (type_die, DW_AT_declaration, 1);
19714
19715 /* We don't need to do this for function-local types. */
19716 if (TYPE_STUB_DECL (type)
19717 && ! decl_function_context (TYPE_STUB_DECL (type)))
19718 vec_safe_push (incomplete_types, type);
19719 }
19720
19721 if (get_AT (type_die, DW_AT_name))
19722 add_pubtype (type, type_die);
19723 }
19724
19725 /* Generate a DIE for a subroutine _type_. */
19726
19727 static void
19728 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19729 {
19730 tree return_type = TREE_TYPE (type);
19731 dw_die_ref subr_die
19732 = new_die (DW_TAG_subroutine_type,
19733 scope_die_for (type, context_die), type);
19734
19735 equate_type_number_to_die (type, subr_die);
19736 add_prototyped_attribute (subr_die, type);
19737 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19738 gen_formal_types_die (type, subr_die);
19739
19740 if (get_AT (subr_die, DW_AT_name))
19741 add_pubtype (type, subr_die);
19742 }
19743
19744 /* Generate a DIE for a type definition. */
19745
19746 static void
19747 gen_typedef_die (tree decl, dw_die_ref context_die)
19748 {
19749 dw_die_ref type_die;
19750 tree origin;
19751
19752 if (TREE_ASM_WRITTEN (decl))
19753 return;
19754
19755 TREE_ASM_WRITTEN (decl) = 1;
19756 type_die = new_die (DW_TAG_typedef, context_die, decl);
19757 origin = decl_ultimate_origin (decl);
19758 if (origin != NULL)
19759 add_abstract_origin_attribute (type_die, origin);
19760 else
19761 {
19762 tree type;
19763
19764 add_name_and_src_coords_attributes (type_die, decl);
19765 if (DECL_ORIGINAL_TYPE (decl))
19766 {
19767 type = DECL_ORIGINAL_TYPE (decl);
19768
19769 gcc_assert (type != TREE_TYPE (decl));
19770 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19771 }
19772 else
19773 {
19774 type = TREE_TYPE (decl);
19775
19776 if (is_naming_typedef_decl (TYPE_NAME (type)))
19777 {
19778 /* Here, we are in the case of decl being a typedef naming
19779 an anonymous type, e.g:
19780 typedef struct {...} foo;
19781 In that case TREE_TYPE (decl) is not a typedef variant
19782 type and TYPE_NAME of the anonymous type is set to the
19783 TYPE_DECL of the typedef. This construct is emitted by
19784 the C++ FE.
19785
19786 TYPE is the anonymous struct named by the typedef
19787 DECL. As we need the DW_AT_type attribute of the
19788 DW_TAG_typedef to point to the DIE of TYPE, let's
19789 generate that DIE right away. add_type_attribute
19790 called below will then pick (via lookup_type_die) that
19791 anonymous struct DIE. */
19792 if (!TREE_ASM_WRITTEN (type))
19793 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19794
19795 /* This is a GNU Extension. We are adding a
19796 DW_AT_linkage_name attribute to the DIE of the
19797 anonymous struct TYPE. The value of that attribute
19798 is the name of the typedef decl naming the anonymous
19799 struct. This greatly eases the work of consumers of
19800 this debug info. */
19801 add_linkage_attr (lookup_type_die (type), decl);
19802 }
19803 }
19804
19805 add_type_attribute (type_die, type, TREE_READONLY (decl),
19806 TREE_THIS_VOLATILE (decl), context_die);
19807
19808 if (is_naming_typedef_decl (decl))
19809 /* We want that all subsequent calls to lookup_type_die with
19810 TYPE in argument yield the DW_TAG_typedef we have just
19811 created. */
19812 equate_type_number_to_die (type, type_die);
19813
19814 add_accessibility_attribute (type_die, decl);
19815 }
19816
19817 if (DECL_ABSTRACT (decl))
19818 equate_decl_number_to_die (decl, type_die);
19819
19820 if (get_AT (type_die, DW_AT_name))
19821 add_pubtype (decl, type_die);
19822 }
19823
19824 /* Generate a DIE for a struct, class, enum or union type. */
19825
19826 static void
19827 gen_tagged_type_die (tree type,
19828 dw_die_ref context_die,
19829 enum debug_info_usage usage)
19830 {
19831 int need_pop;
19832
19833 if (type == NULL_TREE
19834 || !is_tagged_type (type))
19835 return;
19836
19837 /* If this is a nested type whose containing class hasn't been written
19838 out yet, writing it out will cover this one, too. This does not apply
19839 to instantiations of member class templates; they need to be added to
19840 the containing class as they are generated. FIXME: This hurts the
19841 idea of combining type decls from multiple TUs, since we can't predict
19842 what set of template instantiations we'll get. */
19843 if (TYPE_CONTEXT (type)
19844 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19845 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19846 {
19847 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19848
19849 if (TREE_ASM_WRITTEN (type))
19850 return;
19851
19852 /* If that failed, attach ourselves to the stub. */
19853 push_decl_scope (TYPE_CONTEXT (type));
19854 context_die = lookup_type_die (TYPE_CONTEXT (type));
19855 need_pop = 1;
19856 }
19857 else if (TYPE_CONTEXT (type) != NULL_TREE
19858 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19859 {
19860 /* If this type is local to a function that hasn't been written
19861 out yet, use a NULL context for now; it will be fixed up in
19862 decls_for_scope. */
19863 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19864 /* A declaration DIE doesn't count; nested types need to go in the
19865 specification. */
19866 if (context_die && is_declaration_die (context_die))
19867 context_die = NULL;
19868 need_pop = 0;
19869 }
19870 else
19871 {
19872 context_die = declare_in_namespace (type, context_die);
19873 need_pop = 0;
19874 }
19875
19876 if (TREE_CODE (type) == ENUMERAL_TYPE)
19877 {
19878 /* This might have been written out by the call to
19879 declare_in_namespace. */
19880 if (!TREE_ASM_WRITTEN (type))
19881 gen_enumeration_type_die (type, context_die);
19882 }
19883 else
19884 gen_struct_or_union_type_die (type, context_die, usage);
19885
19886 if (need_pop)
19887 pop_decl_scope ();
19888
19889 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19890 it up if it is ever completed. gen_*_type_die will set it for us
19891 when appropriate. */
19892 }
19893
19894 /* Generate a type description DIE. */
19895
19896 static void
19897 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19898 enum debug_info_usage usage)
19899 {
19900 struct array_descr_info info;
19901
19902 if (type == NULL_TREE || type == error_mark_node)
19903 return;
19904
19905 if (TYPE_NAME (type) != NULL_TREE
19906 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19907 && is_redundant_typedef (TYPE_NAME (type))
19908 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19909 /* The DECL of this type is a typedef we don't want to emit debug
19910 info for but we want debug info for its underlying typedef.
19911 This can happen for e.g, the injected-class-name of a C++
19912 type. */
19913 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19914
19915 /* If TYPE is a typedef type variant, let's generate debug info
19916 for the parent typedef which TYPE is a type of. */
19917 if (typedef_variant_p (type))
19918 {
19919 if (TREE_ASM_WRITTEN (type))
19920 return;
19921
19922 /* Prevent broken recursion; we can't hand off to the same type. */
19923 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19924
19925 /* Give typedefs the right scope. */
19926 context_die = scope_die_for (type, context_die);
19927
19928 TREE_ASM_WRITTEN (type) = 1;
19929
19930 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19931 return;
19932 }
19933
19934 /* If type is an anonymous tagged type named by a typedef, let's
19935 generate debug info for the typedef. */
19936 if (is_naming_typedef_decl (TYPE_NAME (type)))
19937 {
19938 /* Use the DIE of the containing namespace as the parent DIE of
19939 the type description DIE we want to generate. */
19940 if (DECL_CONTEXT (TYPE_NAME (type))
19941 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19942 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19943
19944 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19945 return;
19946 }
19947
19948 /* If this is an array type with hidden descriptor, handle it first. */
19949 if (!TREE_ASM_WRITTEN (type)
19950 && lang_hooks.types.get_array_descr_info
19951 && lang_hooks.types.get_array_descr_info (type, &info)
19952 && (dwarf_version >= 3 || !dwarf_strict))
19953 {
19954 gen_descr_array_type_die (type, &info, context_die);
19955 TREE_ASM_WRITTEN (type) = 1;
19956 return;
19957 }
19958
19959 /* We are going to output a DIE to represent the unqualified version
19960 of this type (i.e. without any const or volatile qualifiers) so
19961 get the main variant (i.e. the unqualified version) of this type
19962 now. (Vectors are special because the debugging info is in the
19963 cloned type itself). */
19964 if (TREE_CODE (type) != VECTOR_TYPE)
19965 type = type_main_variant (type);
19966
19967 if (TREE_ASM_WRITTEN (type))
19968 return;
19969
19970 switch (TREE_CODE (type))
19971 {
19972 case ERROR_MARK:
19973 break;
19974
19975 case POINTER_TYPE:
19976 case REFERENCE_TYPE:
19977 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19978 ensures that the gen_type_die recursion will terminate even if the
19979 type is recursive. Recursive types are possible in Ada. */
19980 /* ??? We could perhaps do this for all types before the switch
19981 statement. */
19982 TREE_ASM_WRITTEN (type) = 1;
19983
19984 /* For these types, all that is required is that we output a DIE (or a
19985 set of DIEs) to represent the "basis" type. */
19986 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19987 DINFO_USAGE_IND_USE);
19988 break;
19989
19990 case OFFSET_TYPE:
19991 /* This code is used for C++ pointer-to-data-member types.
19992 Output a description of the relevant class type. */
19993 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19994 DINFO_USAGE_IND_USE);
19995
19996 /* Output a description of the type of the object pointed to. */
19997 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19998 DINFO_USAGE_IND_USE);
19999
20000 /* Now output a DIE to represent this pointer-to-data-member type
20001 itself. */
20002 gen_ptr_to_mbr_type_die (type, context_die);
20003 break;
20004
20005 case FUNCTION_TYPE:
20006 /* Force out return type (in case it wasn't forced out already). */
20007 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20008 DINFO_USAGE_DIR_USE);
20009 gen_subroutine_type_die (type, context_die);
20010 break;
20011
20012 case METHOD_TYPE:
20013 /* Force out return type (in case it wasn't forced out already). */
20014 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20015 DINFO_USAGE_DIR_USE);
20016 gen_subroutine_type_die (type, context_die);
20017 break;
20018
20019 case ARRAY_TYPE:
20020 gen_array_type_die (type, context_die);
20021 break;
20022
20023 case VECTOR_TYPE:
20024 gen_array_type_die (type, context_die);
20025 break;
20026
20027 case ENUMERAL_TYPE:
20028 case RECORD_TYPE:
20029 case UNION_TYPE:
20030 case QUAL_UNION_TYPE:
20031 gen_tagged_type_die (type, context_die, usage);
20032 return;
20033
20034 case VOID_TYPE:
20035 case INTEGER_TYPE:
20036 case REAL_TYPE:
20037 case FIXED_POINT_TYPE:
20038 case COMPLEX_TYPE:
20039 case BOOLEAN_TYPE:
20040 /* No DIEs needed for fundamental types. */
20041 break;
20042
20043 case NULLPTR_TYPE:
20044 case LANG_TYPE:
20045 /* Just use DW_TAG_unspecified_type. */
20046 {
20047 dw_die_ref type_die = lookup_type_die (type);
20048 if (type_die == NULL)
20049 {
20050 tree name = TYPE_IDENTIFIER (type);
20051 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20052 type);
20053 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20054 equate_type_number_to_die (type, type_die);
20055 }
20056 }
20057 break;
20058
20059 default:
20060 if (is_cxx_auto (type))
20061 {
20062 tree name = TYPE_IDENTIFIER (type);
20063 dw_die_ref *die = (name == get_identifier ("auto")
20064 ? &auto_die : &decltype_auto_die);
20065 if (!*die)
20066 {
20067 *die = new_die (DW_TAG_unspecified_type,
20068 comp_unit_die (), NULL_TREE);
20069 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20070 }
20071 equate_type_number_to_die (type, *die);
20072 break;
20073 }
20074 gcc_unreachable ();
20075 }
20076
20077 TREE_ASM_WRITTEN (type) = 1;
20078 }
20079
20080 static void
20081 gen_type_die (tree type, dw_die_ref context_die)
20082 {
20083 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20084 }
20085
20086 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20087 things which are local to the given block. */
20088
20089 static void
20090 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20091 {
20092 int must_output_die = 0;
20093 bool inlined_func;
20094
20095 /* Ignore blocks that are NULL. */
20096 if (stmt == NULL_TREE)
20097 return;
20098
20099 inlined_func = inlined_function_outer_scope_p (stmt);
20100
20101 /* If the block is one fragment of a non-contiguous block, do not
20102 process the variables, since they will have been done by the
20103 origin block. Do process subblocks. */
20104 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20105 {
20106 tree sub;
20107
20108 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20109 gen_block_die (sub, context_die, depth + 1);
20110
20111 return;
20112 }
20113
20114 /* Determine if we need to output any Dwarf DIEs at all to represent this
20115 block. */
20116 if (inlined_func)
20117 /* The outer scopes for inlinings *must* always be represented. We
20118 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20119 must_output_die = 1;
20120 else
20121 {
20122 /* Determine if this block directly contains any "significant"
20123 local declarations which we will need to output DIEs for. */
20124 if (debug_info_level > DINFO_LEVEL_TERSE)
20125 /* We are not in terse mode so *any* local declaration counts
20126 as being a "significant" one. */
20127 must_output_die = ((BLOCK_VARS (stmt) != NULL
20128 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20129 && (TREE_USED (stmt)
20130 || TREE_ASM_WRITTEN (stmt)
20131 || BLOCK_ABSTRACT (stmt)));
20132 else if ((TREE_USED (stmt)
20133 || TREE_ASM_WRITTEN (stmt)
20134 || BLOCK_ABSTRACT (stmt))
20135 && !dwarf2out_ignore_block (stmt))
20136 must_output_die = 1;
20137 }
20138
20139 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20140 DIE for any block which contains no significant local declarations at
20141 all. Rather, in such cases we just call `decls_for_scope' so that any
20142 needed Dwarf info for any sub-blocks will get properly generated. Note
20143 that in terse mode, our definition of what constitutes a "significant"
20144 local declaration gets restricted to include only inlined function
20145 instances and local (nested) function definitions. */
20146 if (must_output_die)
20147 {
20148 if (inlined_func)
20149 {
20150 /* If STMT block is abstract, that means we have been called
20151 indirectly from dwarf2out_abstract_function.
20152 That function rightfully marks the descendent blocks (of
20153 the abstract function it is dealing with) as being abstract,
20154 precisely to prevent us from emitting any
20155 DW_TAG_inlined_subroutine DIE as a descendent
20156 of an abstract function instance. So in that case, we should
20157 not call gen_inlined_subroutine_die.
20158
20159 Later though, when cgraph asks dwarf2out to emit info
20160 for the concrete instance of the function decl into which
20161 the concrete instance of STMT got inlined, the later will lead
20162 to the generation of a DW_TAG_inlined_subroutine DIE. */
20163 if (! BLOCK_ABSTRACT (stmt))
20164 gen_inlined_subroutine_die (stmt, context_die, depth);
20165 }
20166 else
20167 gen_lexical_block_die (stmt, context_die, depth);
20168 }
20169 else
20170 decls_for_scope (stmt, context_die, depth);
20171 }
20172
20173 /* Process variable DECL (or variable with origin ORIGIN) within
20174 block STMT and add it to CONTEXT_DIE. */
20175 static void
20176 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20177 {
20178 dw_die_ref die;
20179 tree decl_or_origin = decl ? decl : origin;
20180
20181 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20182 die = lookup_decl_die (decl_or_origin);
20183 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20184 && TYPE_DECL_IS_STUB (decl_or_origin))
20185 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20186 else
20187 die = NULL;
20188
20189 if (die != NULL && die->die_parent == NULL)
20190 add_child_die (context_die, die);
20191 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20192 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20193 stmt, context_die);
20194 else
20195 gen_decl_die (decl, origin, context_die);
20196 }
20197
20198 /* Generate all of the decls declared within a given scope and (recursively)
20199 all of its sub-blocks. */
20200
20201 static void
20202 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20203 {
20204 tree decl;
20205 unsigned int i;
20206 tree subblocks;
20207
20208 /* Ignore NULL blocks. */
20209 if (stmt == NULL_TREE)
20210 return;
20211
20212 /* Output the DIEs to represent all of the data objects and typedefs
20213 declared directly within this block but not within any nested
20214 sub-blocks. Also, nested function and tag DIEs have been
20215 generated with a parent of NULL; fix that up now. We don't
20216 have to do this if we're at -g1. */
20217 if (debug_info_level > DINFO_LEVEL_TERSE)
20218 {
20219 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20220 process_scope_var (stmt, decl, NULL_TREE, context_die);
20221 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20222 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20223 context_die);
20224 }
20225
20226 /* Even if we're at -g1, we need to process the subblocks in order to get
20227 inlined call information. */
20228
20229 /* Output the DIEs to represent all sub-blocks (and the items declared
20230 therein) of this block. */
20231 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20232 subblocks != NULL;
20233 subblocks = BLOCK_CHAIN (subblocks))
20234 gen_block_die (subblocks, context_die, depth + 1);
20235 }
20236
20237 /* Is this a typedef we can avoid emitting? */
20238
20239 static inline int
20240 is_redundant_typedef (const_tree decl)
20241 {
20242 if (TYPE_DECL_IS_STUB (decl))
20243 return 1;
20244
20245 if (DECL_ARTIFICIAL (decl)
20246 && DECL_CONTEXT (decl)
20247 && is_tagged_type (DECL_CONTEXT (decl))
20248 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20249 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20250 /* Also ignore the artificial member typedef for the class name. */
20251 return 1;
20252
20253 return 0;
20254 }
20255
20256 /* Return TRUE if TYPE is a typedef that names a type for linkage
20257 purposes. This kind of typedefs is produced by the C++ FE for
20258 constructs like:
20259
20260 typedef struct {...} foo;
20261
20262 In that case, there is no typedef variant type produced for foo.
20263 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20264 struct type. */
20265
20266 static bool
20267 is_naming_typedef_decl (const_tree decl)
20268 {
20269 if (decl == NULL_TREE
20270 || TREE_CODE (decl) != TYPE_DECL
20271 || !is_tagged_type (TREE_TYPE (decl))
20272 || DECL_IS_BUILTIN (decl)
20273 || is_redundant_typedef (decl)
20274 /* It looks like Ada produces TYPE_DECLs that are very similar
20275 to C++ naming typedefs but that have different
20276 semantics. Let's be specific to c++ for now. */
20277 || !is_cxx ())
20278 return FALSE;
20279
20280 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20281 && TYPE_NAME (TREE_TYPE (decl)) == decl
20282 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20283 != TYPE_NAME (TREE_TYPE (decl))));
20284 }
20285
20286 /* Returns the DIE for a context. */
20287
20288 static inline dw_die_ref
20289 get_context_die (tree context)
20290 {
20291 if (context)
20292 {
20293 /* Find die that represents this context. */
20294 if (TYPE_P (context))
20295 {
20296 context = TYPE_MAIN_VARIANT (context);
20297 return strip_naming_typedef (context, force_type_die (context));
20298 }
20299 else
20300 return force_decl_die (context);
20301 }
20302 return comp_unit_die ();
20303 }
20304
20305 /* Returns the DIE for decl. A DIE will always be returned. */
20306
20307 static dw_die_ref
20308 force_decl_die (tree decl)
20309 {
20310 dw_die_ref decl_die;
20311 unsigned saved_external_flag;
20312 tree save_fn = NULL_TREE;
20313 decl_die = lookup_decl_die (decl);
20314 if (!decl_die)
20315 {
20316 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20317
20318 decl_die = lookup_decl_die (decl);
20319 if (decl_die)
20320 return decl_die;
20321
20322 switch (TREE_CODE (decl))
20323 {
20324 case FUNCTION_DECL:
20325 /* Clear current_function_decl, so that gen_subprogram_die thinks
20326 that this is a declaration. At this point, we just want to force
20327 declaration die. */
20328 save_fn = current_function_decl;
20329 current_function_decl = NULL_TREE;
20330 gen_subprogram_die (decl, context_die);
20331 current_function_decl = save_fn;
20332 break;
20333
20334 case VAR_DECL:
20335 /* Set external flag to force declaration die. Restore it after
20336 gen_decl_die() call. */
20337 saved_external_flag = DECL_EXTERNAL (decl);
20338 DECL_EXTERNAL (decl) = 1;
20339 gen_decl_die (decl, NULL, context_die);
20340 DECL_EXTERNAL (decl) = saved_external_flag;
20341 break;
20342
20343 case NAMESPACE_DECL:
20344 if (dwarf_version >= 3 || !dwarf_strict)
20345 dwarf2out_decl (decl);
20346 else
20347 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20348 decl_die = comp_unit_die ();
20349 break;
20350
20351 case TRANSLATION_UNIT_DECL:
20352 decl_die = comp_unit_die ();
20353 break;
20354
20355 default:
20356 gcc_unreachable ();
20357 }
20358
20359 /* We should be able to find the DIE now. */
20360 if (!decl_die)
20361 decl_die = lookup_decl_die (decl);
20362 gcc_assert (decl_die);
20363 }
20364
20365 return decl_die;
20366 }
20367
20368 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20369 always returned. */
20370
20371 static dw_die_ref
20372 force_type_die (tree type)
20373 {
20374 dw_die_ref type_die;
20375
20376 type_die = lookup_type_die (type);
20377 if (!type_die)
20378 {
20379 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20380
20381 type_die = modified_type_die (type, TYPE_READONLY (type),
20382 TYPE_VOLATILE (type), context_die);
20383 gcc_assert (type_die);
20384 }
20385 return type_die;
20386 }
20387
20388 /* Force out any required namespaces to be able to output DECL,
20389 and return the new context_die for it, if it's changed. */
20390
20391 static dw_die_ref
20392 setup_namespace_context (tree thing, dw_die_ref context_die)
20393 {
20394 tree context = (DECL_P (thing)
20395 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20396 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20397 /* Force out the namespace. */
20398 context_die = force_decl_die (context);
20399
20400 return context_die;
20401 }
20402
20403 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20404 type) within its namespace, if appropriate.
20405
20406 For compatibility with older debuggers, namespace DIEs only contain
20407 declarations; all definitions are emitted at CU scope. */
20408
20409 static dw_die_ref
20410 declare_in_namespace (tree thing, dw_die_ref context_die)
20411 {
20412 dw_die_ref ns_context;
20413
20414 if (debug_info_level <= DINFO_LEVEL_TERSE)
20415 return context_die;
20416
20417 /* If this decl is from an inlined function, then don't try to emit it in its
20418 namespace, as we will get confused. It would have already been emitted
20419 when the abstract instance of the inline function was emitted anyways. */
20420 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20421 return context_die;
20422
20423 ns_context = setup_namespace_context (thing, context_die);
20424
20425 if (ns_context != context_die)
20426 {
20427 if (is_fortran ())
20428 return ns_context;
20429 if (DECL_P (thing))
20430 gen_decl_die (thing, NULL, ns_context);
20431 else
20432 gen_type_die (thing, ns_context);
20433 }
20434 return context_die;
20435 }
20436
20437 /* Generate a DIE for a namespace or namespace alias. */
20438
20439 static void
20440 gen_namespace_die (tree decl, dw_die_ref context_die)
20441 {
20442 dw_die_ref namespace_die;
20443
20444 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20445 they are an alias of. */
20446 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20447 {
20448 /* Output a real namespace or module. */
20449 context_die = setup_namespace_context (decl, comp_unit_die ());
20450 namespace_die = new_die (is_fortran ()
20451 ? DW_TAG_module : DW_TAG_namespace,
20452 context_die, decl);
20453 /* For Fortran modules defined in different CU don't add src coords. */
20454 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20455 {
20456 const char *name = dwarf2_name (decl, 0);
20457 if (name)
20458 add_name_attribute (namespace_die, name);
20459 }
20460 else
20461 add_name_and_src_coords_attributes (namespace_die, decl);
20462 if (DECL_EXTERNAL (decl))
20463 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20464 equate_decl_number_to_die (decl, namespace_die);
20465 }
20466 else
20467 {
20468 /* Output a namespace alias. */
20469
20470 /* Force out the namespace we are an alias of, if necessary. */
20471 dw_die_ref origin_die
20472 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20473
20474 if (DECL_FILE_SCOPE_P (decl)
20475 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20476 context_die = setup_namespace_context (decl, comp_unit_die ());
20477 /* Now create the namespace alias DIE. */
20478 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20479 add_name_and_src_coords_attributes (namespace_die, decl);
20480 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20481 equate_decl_number_to_die (decl, namespace_die);
20482 }
20483 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20484 if (want_pubnames ())
20485 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20486 }
20487
20488 /* Generate Dwarf debug information for a decl described by DECL.
20489 The return value is currently only meaningful for PARM_DECLs,
20490 for all other decls it returns NULL. */
20491
20492 static dw_die_ref
20493 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20494 {
20495 tree decl_or_origin = decl ? decl : origin;
20496 tree class_origin = NULL, ultimate_origin;
20497
20498 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20499 return NULL;
20500
20501 switch (TREE_CODE (decl_or_origin))
20502 {
20503 case ERROR_MARK:
20504 break;
20505
20506 case CONST_DECL:
20507 if (!is_fortran () && !is_ada ())
20508 {
20509 /* The individual enumerators of an enum type get output when we output
20510 the Dwarf representation of the relevant enum type itself. */
20511 break;
20512 }
20513
20514 /* Emit its type. */
20515 gen_type_die (TREE_TYPE (decl), context_die);
20516
20517 /* And its containing namespace. */
20518 context_die = declare_in_namespace (decl, context_die);
20519
20520 gen_const_die (decl, context_die);
20521 break;
20522
20523 case FUNCTION_DECL:
20524 /* Don't output any DIEs to represent mere function declarations,
20525 unless they are class members or explicit block externs. */
20526 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20527 && DECL_FILE_SCOPE_P (decl_or_origin)
20528 && (current_function_decl == NULL_TREE
20529 || DECL_ARTIFICIAL (decl_or_origin)))
20530 break;
20531
20532 #if 0
20533 /* FIXME */
20534 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20535 on local redeclarations of global functions. That seems broken. */
20536 if (current_function_decl != decl)
20537 /* This is only a declaration. */;
20538 #endif
20539
20540 /* If we're emitting a clone, emit info for the abstract instance. */
20541 if (origin || DECL_ORIGIN (decl) != decl)
20542 dwarf2out_abstract_function (origin
20543 ? DECL_ORIGIN (origin)
20544 : DECL_ABSTRACT_ORIGIN (decl));
20545
20546 /* If we're emitting an out-of-line copy of an inline function,
20547 emit info for the abstract instance and set up to refer to it. */
20548 else if (cgraph_function_possibly_inlined_p (decl)
20549 && ! DECL_ABSTRACT (decl)
20550 && ! class_or_namespace_scope_p (context_die)
20551 /* dwarf2out_abstract_function won't emit a die if this is just
20552 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20553 that case, because that works only if we have a die. */
20554 && DECL_INITIAL (decl) != NULL_TREE)
20555 {
20556 dwarf2out_abstract_function (decl);
20557 set_decl_origin_self (decl);
20558 }
20559
20560 /* Otherwise we're emitting the primary DIE for this decl. */
20561 else if (debug_info_level > DINFO_LEVEL_TERSE)
20562 {
20563 /* Before we describe the FUNCTION_DECL itself, make sure that we
20564 have its containing type. */
20565 if (!origin)
20566 origin = decl_class_context (decl);
20567 if (origin != NULL_TREE)
20568 gen_type_die (origin, context_die);
20569
20570 /* And its return type. */
20571 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20572
20573 /* And its virtual context. */
20574 if (DECL_VINDEX (decl) != NULL_TREE)
20575 gen_type_die (DECL_CONTEXT (decl), context_die);
20576
20577 /* Make sure we have a member DIE for decl. */
20578 if (origin != NULL_TREE)
20579 gen_type_die_for_member (origin, decl, context_die);
20580
20581 /* And its containing namespace. */
20582 context_die = declare_in_namespace (decl, context_die);
20583 }
20584
20585 /* Now output a DIE to represent the function itself. */
20586 if (decl)
20587 gen_subprogram_die (decl, context_die);
20588 break;
20589
20590 case TYPE_DECL:
20591 /* If we are in terse mode, don't generate any DIEs to represent any
20592 actual typedefs. */
20593 if (debug_info_level <= DINFO_LEVEL_TERSE)
20594 break;
20595
20596 /* In the special case of a TYPE_DECL node representing the declaration
20597 of some type tag, if the given TYPE_DECL is marked as having been
20598 instantiated from some other (original) TYPE_DECL node (e.g. one which
20599 was generated within the original definition of an inline function) we
20600 used to generate a special (abbreviated) DW_TAG_structure_type,
20601 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20602 should be actually referencing those DIEs, as variable DIEs with that
20603 type would be emitted already in the abstract origin, so it was always
20604 removed during unused type prunning. Don't add anything in this
20605 case. */
20606 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20607 break;
20608
20609 if (is_redundant_typedef (decl))
20610 gen_type_die (TREE_TYPE (decl), context_die);
20611 else
20612 /* Output a DIE to represent the typedef itself. */
20613 gen_typedef_die (decl, context_die);
20614 break;
20615
20616 case LABEL_DECL:
20617 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20618 gen_label_die (decl, context_die);
20619 break;
20620
20621 case VAR_DECL:
20622 case RESULT_DECL:
20623 /* If we are in terse mode, don't generate any DIEs to represent any
20624 variable declarations or definitions. */
20625 if (debug_info_level <= DINFO_LEVEL_TERSE)
20626 break;
20627
20628 /* Output any DIEs that are needed to specify the type of this data
20629 object. */
20630 if (decl_by_reference_p (decl_or_origin))
20631 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20632 else
20633 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20634
20635 /* And its containing type. */
20636 class_origin = decl_class_context (decl_or_origin);
20637 if (class_origin != NULL_TREE)
20638 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20639
20640 /* And its containing namespace. */
20641 context_die = declare_in_namespace (decl_or_origin, context_die);
20642
20643 /* Now output the DIE to represent the data object itself. This gets
20644 complicated because of the possibility that the VAR_DECL really
20645 represents an inlined instance of a formal parameter for an inline
20646 function. */
20647 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20648 if (ultimate_origin != NULL_TREE
20649 && TREE_CODE (ultimate_origin) == PARM_DECL)
20650 gen_formal_parameter_die (decl, origin,
20651 true /* Emit name attribute. */,
20652 context_die);
20653 else
20654 gen_variable_die (decl, origin, context_die);
20655 break;
20656
20657 case FIELD_DECL:
20658 /* Ignore the nameless fields that are used to skip bits but handle C++
20659 anonymous unions and structs. */
20660 if (DECL_NAME (decl) != NULL_TREE
20661 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20662 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20663 {
20664 gen_type_die (member_declared_type (decl), context_die);
20665 gen_field_die (decl, context_die);
20666 }
20667 break;
20668
20669 case PARM_DECL:
20670 if (DECL_BY_REFERENCE (decl_or_origin))
20671 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20672 else
20673 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20674 return gen_formal_parameter_die (decl, origin,
20675 true /* Emit name attribute. */,
20676 context_die);
20677
20678 case NAMESPACE_DECL:
20679 case IMPORTED_DECL:
20680 if (dwarf_version >= 3 || !dwarf_strict)
20681 gen_namespace_die (decl, context_die);
20682 break;
20683
20684 case NAMELIST_DECL:
20685 gen_namelist_decl (DECL_NAME (decl), context_die,
20686 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20687 break;
20688
20689 default:
20690 /* Probably some frontend-internal decl. Assume we don't care. */
20691 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20692 break;
20693 }
20694
20695 return NULL;
20696 }
20697 \f
20698 /* Output debug information for global decl DECL. Called from toplev.c after
20699 compilation proper has finished. */
20700
20701 static void
20702 dwarf2out_global_decl (tree decl)
20703 {
20704 /* Output DWARF2 information for file-scope tentative data object
20705 declarations, file-scope (extern) function declarations (which
20706 had no corresponding body) and file-scope tagged type declarations
20707 and definitions which have not yet been forced out. */
20708 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20709 dwarf2out_decl (decl);
20710 }
20711
20712 /* Output debug information for type decl DECL. Called from toplev.c
20713 and from language front ends (to record built-in types). */
20714 static void
20715 dwarf2out_type_decl (tree decl, int local)
20716 {
20717 if (!local)
20718 dwarf2out_decl (decl);
20719 }
20720
20721 /* Output debug information for imported module or decl DECL.
20722 NAME is non-NULL name in the lexical block if the decl has been renamed.
20723 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20724 that DECL belongs to.
20725 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20726 static void
20727 dwarf2out_imported_module_or_decl_1 (tree decl,
20728 tree name,
20729 tree lexical_block,
20730 dw_die_ref lexical_block_die)
20731 {
20732 expanded_location xloc;
20733 dw_die_ref imported_die = NULL;
20734 dw_die_ref at_import_die;
20735
20736 if (TREE_CODE (decl) == IMPORTED_DECL)
20737 {
20738 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20739 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20740 gcc_assert (decl);
20741 }
20742 else
20743 xloc = expand_location (input_location);
20744
20745 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20746 {
20747 at_import_die = force_type_die (TREE_TYPE (decl));
20748 /* For namespace N { typedef void T; } using N::T; base_type_die
20749 returns NULL, but DW_TAG_imported_declaration requires
20750 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20751 if (!at_import_die)
20752 {
20753 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20754 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20755 at_import_die = lookup_type_die (TREE_TYPE (decl));
20756 gcc_assert (at_import_die);
20757 }
20758 }
20759 else
20760 {
20761 at_import_die = lookup_decl_die (decl);
20762 if (!at_import_die)
20763 {
20764 /* If we're trying to avoid duplicate debug info, we may not have
20765 emitted the member decl for this field. Emit it now. */
20766 if (TREE_CODE (decl) == FIELD_DECL)
20767 {
20768 tree type = DECL_CONTEXT (decl);
20769
20770 if (TYPE_CONTEXT (type)
20771 && TYPE_P (TYPE_CONTEXT (type))
20772 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20773 DINFO_USAGE_DIR_USE))
20774 return;
20775 gen_type_die_for_member (type, decl,
20776 get_context_die (TYPE_CONTEXT (type)));
20777 }
20778 if (TREE_CODE (decl) == NAMELIST_DECL)
20779 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20780 get_context_die (DECL_CONTEXT (decl)),
20781 NULL_TREE);
20782 else
20783 at_import_die = force_decl_die (decl);
20784 }
20785 }
20786
20787 if (TREE_CODE (decl) == NAMESPACE_DECL)
20788 {
20789 if (dwarf_version >= 3 || !dwarf_strict)
20790 imported_die = new_die (DW_TAG_imported_module,
20791 lexical_block_die,
20792 lexical_block);
20793 else
20794 return;
20795 }
20796 else
20797 imported_die = new_die (DW_TAG_imported_declaration,
20798 lexical_block_die,
20799 lexical_block);
20800
20801 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20802 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20803 if (name)
20804 add_AT_string (imported_die, DW_AT_name,
20805 IDENTIFIER_POINTER (name));
20806 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20807 }
20808
20809 /* Output debug information for imported module or decl DECL.
20810 NAME is non-NULL name in context if the decl has been renamed.
20811 CHILD is true if decl is one of the renamed decls as part of
20812 importing whole module. */
20813
20814 static void
20815 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20816 bool child)
20817 {
20818 /* dw_die_ref at_import_die; */
20819 dw_die_ref scope_die;
20820
20821 if (debug_info_level <= DINFO_LEVEL_TERSE)
20822 return;
20823
20824 gcc_assert (decl);
20825
20826 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20827 We need decl DIE for reference and scope die. First, get DIE for the decl
20828 itself. */
20829
20830 /* Get the scope die for decl context. Use comp_unit_die for global module
20831 or decl. If die is not found for non globals, force new die. */
20832 if (context
20833 && TYPE_P (context)
20834 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20835 return;
20836
20837 if (!(dwarf_version >= 3 || !dwarf_strict))
20838 return;
20839
20840 scope_die = get_context_die (context);
20841
20842 if (child)
20843 {
20844 gcc_assert (scope_die->die_child);
20845 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20846 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20847 scope_die = scope_die->die_child;
20848 }
20849
20850 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20851 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20852
20853 }
20854
20855 /* Output debug information for namelists. */
20856
20857 static dw_die_ref
20858 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20859 {
20860 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20861 tree value;
20862 unsigned i;
20863
20864 if (debug_info_level <= DINFO_LEVEL_TERSE)
20865 return NULL;
20866
20867 gcc_assert (scope_die != NULL);
20868 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20869 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20870
20871 /* If there are no item_decls, we have a nondefining namelist, e.g.
20872 with USE association; hence, set DW_AT_declaration. */
20873 if (item_decls == NULL_TREE)
20874 {
20875 add_AT_flag (nml_die, DW_AT_declaration, 1);
20876 return nml_die;
20877 }
20878
20879 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20880 {
20881 nml_item_ref_die = lookup_decl_die (value);
20882 if (!nml_item_ref_die)
20883 nml_item_ref_die = force_decl_die (value);
20884
20885 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20886 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20887 }
20888 return nml_die;
20889 }
20890
20891
20892 /* Write the debugging output for DECL. */
20893
20894 void
20895 dwarf2out_decl (tree decl)
20896 {
20897 dw_die_ref context_die = comp_unit_die ();
20898
20899 switch (TREE_CODE (decl))
20900 {
20901 case ERROR_MARK:
20902 return;
20903
20904 case FUNCTION_DECL:
20905 /* What we would really like to do here is to filter out all mere
20906 file-scope declarations of file-scope functions which are never
20907 referenced later within this translation unit (and keep all of ones
20908 that *are* referenced later on) but we aren't clairvoyant, so we have
20909 no idea which functions will be referenced in the future (i.e. later
20910 on within the current translation unit). So here we just ignore all
20911 file-scope function declarations which are not also definitions. If
20912 and when the debugger needs to know something about these functions,
20913 it will have to hunt around and find the DWARF information associated
20914 with the definition of the function.
20915
20916 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20917 nodes represent definitions and which ones represent mere
20918 declarations. We have to check DECL_INITIAL instead. That's because
20919 the C front-end supports some weird semantics for "extern inline"
20920 function definitions. These can get inlined within the current
20921 translation unit (and thus, we need to generate Dwarf info for their
20922 abstract instances so that the Dwarf info for the concrete inlined
20923 instances can have something to refer to) but the compiler never
20924 generates any out-of-lines instances of such things (despite the fact
20925 that they *are* definitions).
20926
20927 The important point is that the C front-end marks these "extern
20928 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20929 them anyway. Note that the C++ front-end also plays some similar games
20930 for inline function definitions appearing within include files which
20931 also contain `#pragma interface' pragmas.
20932
20933 If we are called from dwarf2out_abstract_function output a DIE
20934 anyway. We can end up here this way with early inlining and LTO
20935 where the inlined function is output in a different LTRANS unit
20936 or not at all. */
20937 if (DECL_INITIAL (decl) == NULL_TREE
20938 && ! DECL_ABSTRACT (decl))
20939 return;
20940
20941 /* If we're a nested function, initially use a parent of NULL; if we're
20942 a plain function, this will be fixed up in decls_for_scope. If
20943 we're a method, it will be ignored, since we already have a DIE. */
20944 if (decl_function_context (decl)
20945 /* But if we're in terse mode, we don't care about scope. */
20946 && debug_info_level > DINFO_LEVEL_TERSE)
20947 context_die = NULL;
20948 break;
20949
20950 case VAR_DECL:
20951 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20952 declaration and if the declaration was never even referenced from
20953 within this entire compilation unit. We suppress these DIEs in
20954 order to save space in the .debug section (by eliminating entries
20955 which are probably useless). Note that we must not suppress
20956 block-local extern declarations (whether used or not) because that
20957 would screw-up the debugger's name lookup mechanism and cause it to
20958 miss things which really ought to be in scope at a given point. */
20959 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20960 return;
20961
20962 /* For local statics lookup proper context die. */
20963 if (TREE_STATIC (decl)
20964 && DECL_CONTEXT (decl)
20965 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20966 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20967
20968 /* If we are in terse mode, don't generate any DIEs to represent any
20969 variable declarations or definitions. */
20970 if (debug_info_level <= DINFO_LEVEL_TERSE)
20971 return;
20972 break;
20973
20974 case CONST_DECL:
20975 if (debug_info_level <= DINFO_LEVEL_TERSE)
20976 return;
20977 if (!is_fortran () && !is_ada ())
20978 return;
20979 if (TREE_STATIC (decl) && decl_function_context (decl))
20980 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20981 break;
20982
20983 case NAMESPACE_DECL:
20984 case IMPORTED_DECL:
20985 if (debug_info_level <= DINFO_LEVEL_TERSE)
20986 return;
20987 if (lookup_decl_die (decl) != NULL)
20988 return;
20989 break;
20990
20991 case TYPE_DECL:
20992 /* Don't emit stubs for types unless they are needed by other DIEs. */
20993 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20994 return;
20995
20996 /* Don't bother trying to generate any DIEs to represent any of the
20997 normal built-in types for the language we are compiling. */
20998 if (DECL_IS_BUILTIN (decl))
20999 return;
21000
21001 /* If we are in terse mode, don't generate any DIEs for types. */
21002 if (debug_info_level <= DINFO_LEVEL_TERSE)
21003 return;
21004
21005 /* If we're a function-scope tag, initially use a parent of NULL;
21006 this will be fixed up in decls_for_scope. */
21007 if (decl_function_context (decl))
21008 context_die = NULL;
21009
21010 break;
21011
21012 case NAMELIST_DECL:
21013 break;
21014
21015 default:
21016 return;
21017 }
21018
21019 gen_decl_die (decl, NULL, context_die);
21020 }
21021
21022 /* Write the debugging output for DECL. */
21023
21024 static void
21025 dwarf2out_function_decl (tree decl)
21026 {
21027 dwarf2out_decl (decl);
21028 call_arg_locations = NULL;
21029 call_arg_loc_last = NULL;
21030 call_site_count = -1;
21031 tail_call_site_count = -1;
21032 block_map.release ();
21033 htab_empty (decl_loc_table);
21034 htab_empty (cached_dw_loc_list_table);
21035 }
21036
21037 /* Output a marker (i.e. a label) for the beginning of the generated code for
21038 a lexical block. */
21039
21040 static void
21041 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21042 unsigned int blocknum)
21043 {
21044 switch_to_section (current_function_section ());
21045 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21046 }
21047
21048 /* Output a marker (i.e. a label) for the end of the generated code for a
21049 lexical block. */
21050
21051 static void
21052 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21053 {
21054 switch_to_section (current_function_section ());
21055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21056 }
21057
21058 /* Returns nonzero if it is appropriate not to emit any debugging
21059 information for BLOCK, because it doesn't contain any instructions.
21060
21061 Don't allow this for blocks with nested functions or local classes
21062 as we would end up with orphans, and in the presence of scheduling
21063 we may end up calling them anyway. */
21064
21065 static bool
21066 dwarf2out_ignore_block (const_tree block)
21067 {
21068 tree decl;
21069 unsigned int i;
21070
21071 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21072 if (TREE_CODE (decl) == FUNCTION_DECL
21073 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21074 return 0;
21075 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21076 {
21077 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21078 if (TREE_CODE (decl) == FUNCTION_DECL
21079 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21080 return 0;
21081 }
21082
21083 return 1;
21084 }
21085
21086 /* Hash table routines for file_hash. */
21087
21088 static int
21089 file_table_eq (const void *p1_p, const void *p2_p)
21090 {
21091 const struct dwarf_file_data *const p1 =
21092 (const struct dwarf_file_data *) p1_p;
21093 const char *const p2 = (const char *) p2_p;
21094 return filename_cmp (p1->filename, p2) == 0;
21095 }
21096
21097 static hashval_t
21098 file_table_hash (const void *p_p)
21099 {
21100 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21101 return htab_hash_string (p->filename);
21102 }
21103
21104 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21105 dwarf2out.c) and return its "index". The index of each (known) filename is
21106 just a unique number which is associated with only that one filename. We
21107 need such numbers for the sake of generating labels (in the .debug_sfnames
21108 section) and references to those files numbers (in the .debug_srcinfo
21109 and.debug_macinfo sections). If the filename given as an argument is not
21110 found in our current list, add it to the list and assign it the next
21111 available unique index number. In order to speed up searches, we remember
21112 the index of the filename was looked up last. This handles the majority of
21113 all searches. */
21114
21115 static struct dwarf_file_data *
21116 lookup_filename (const char *file_name)
21117 {
21118 void ** slot;
21119 struct dwarf_file_data * created;
21120
21121 /* Check to see if the file name that was searched on the previous
21122 call matches this file name. If so, return the index. */
21123 if (file_table_last_lookup
21124 && (file_name == file_table_last_lookup->filename
21125 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21126 return file_table_last_lookup;
21127
21128 /* Didn't match the previous lookup, search the table. */
21129 slot = htab_find_slot_with_hash (file_table, file_name,
21130 htab_hash_string (file_name), INSERT);
21131 if (*slot)
21132 return (struct dwarf_file_data *) *slot;
21133
21134 created = ggc_alloc<dwarf_file_data> ();
21135 created->filename = file_name;
21136 created->emitted_number = 0;
21137 *slot = created;
21138 return created;
21139 }
21140
21141 /* If the assembler will construct the file table, then translate the compiler
21142 internal file table number into the assembler file table number, and emit
21143 a .file directive if we haven't already emitted one yet. The file table
21144 numbers are different because we prune debug info for unused variables and
21145 types, which may include filenames. */
21146
21147 static int
21148 maybe_emit_file (struct dwarf_file_data * fd)
21149 {
21150 if (! fd->emitted_number)
21151 {
21152 if (last_emitted_file)
21153 fd->emitted_number = last_emitted_file->emitted_number + 1;
21154 else
21155 fd->emitted_number = 1;
21156 last_emitted_file = fd;
21157
21158 if (DWARF2_ASM_LINE_DEBUG_INFO)
21159 {
21160 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21161 output_quoted_string (asm_out_file,
21162 remap_debug_filename (fd->filename));
21163 fputc ('\n', asm_out_file);
21164 }
21165 }
21166
21167 return fd->emitted_number;
21168 }
21169
21170 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21171 That generation should happen after function debug info has been
21172 generated. The value of the attribute is the constant value of ARG. */
21173
21174 static void
21175 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21176 {
21177 die_arg_entry entry;
21178
21179 if (!die || !arg)
21180 return;
21181
21182 if (!tmpl_value_parm_die_table)
21183 vec_alloc (tmpl_value_parm_die_table, 32);
21184
21185 entry.die = die;
21186 entry.arg = arg;
21187 vec_safe_push (tmpl_value_parm_die_table, entry);
21188 }
21189
21190 /* Return TRUE if T is an instance of generic type, FALSE
21191 otherwise. */
21192
21193 static bool
21194 generic_type_p (tree t)
21195 {
21196 if (t == NULL_TREE || !TYPE_P (t))
21197 return false;
21198 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21199 }
21200
21201 /* Schedule the generation of the generic parameter dies for the
21202 instance of generic type T. The proper generation itself is later
21203 done by gen_scheduled_generic_parms_dies. */
21204
21205 static void
21206 schedule_generic_params_dies_gen (tree t)
21207 {
21208 if (!generic_type_p (t))
21209 return;
21210
21211 if (!generic_type_instances)
21212 vec_alloc (generic_type_instances, 256);
21213
21214 vec_safe_push (generic_type_instances, t);
21215 }
21216
21217 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21218 by append_entry_to_tmpl_value_parm_die_table. This function must
21219 be called after function DIEs have been generated. */
21220
21221 static void
21222 gen_remaining_tmpl_value_param_die_attribute (void)
21223 {
21224 if (tmpl_value_parm_die_table)
21225 {
21226 unsigned i;
21227 die_arg_entry *e;
21228
21229 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21230 tree_add_const_value_attribute (e->die, e->arg);
21231 }
21232 }
21233
21234 /* Generate generic parameters DIEs for instances of generic types
21235 that have been previously scheduled by
21236 schedule_generic_params_dies_gen. This function must be called
21237 after all the types of the CU have been laid out. */
21238
21239 static void
21240 gen_scheduled_generic_parms_dies (void)
21241 {
21242 unsigned i;
21243 tree t;
21244
21245 if (!generic_type_instances)
21246 return;
21247
21248 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21249 if (COMPLETE_TYPE_P (t))
21250 gen_generic_params_dies (t);
21251 }
21252
21253
21254 /* Replace DW_AT_name for the decl with name. */
21255
21256 static void
21257 dwarf2out_set_name (tree decl, tree name)
21258 {
21259 dw_die_ref die;
21260 dw_attr_ref attr;
21261 const char *dname;
21262
21263 die = TYPE_SYMTAB_DIE (decl);
21264 if (!die)
21265 return;
21266
21267 dname = dwarf2_name (name, 0);
21268 if (!dname)
21269 return;
21270
21271 attr = get_AT (die, DW_AT_name);
21272 if (attr)
21273 {
21274 struct indirect_string_node *node;
21275
21276 node = find_AT_string (dname);
21277 /* replace the string. */
21278 attr->dw_attr_val.v.val_str = node;
21279 }
21280
21281 else
21282 add_name_attribute (die, dname);
21283 }
21284
21285 /* True if before or during processing of the first function being emitted. */
21286 static bool in_first_function_p = true;
21287 /* True if loc_note during dwarf2out_var_location call might still be
21288 before first real instruction at address equal to .Ltext0. */
21289 static bool maybe_at_text_label_p = true;
21290 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21291 static unsigned int first_loclabel_num_not_at_text_label;
21292
21293 /* Called by the final INSN scan whenever we see a var location. We
21294 use it to drop labels in the right places, and throw the location in
21295 our lookup table. */
21296
21297 static void
21298 dwarf2out_var_location (rtx loc_note)
21299 {
21300 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21301 struct var_loc_node *newloc;
21302 rtx next_real, next_note;
21303 static const char *last_label;
21304 static const char *last_postcall_label;
21305 static bool last_in_cold_section_p;
21306 static rtx expected_next_loc_note;
21307 tree decl;
21308 bool var_loc_p;
21309
21310 if (!NOTE_P (loc_note))
21311 {
21312 if (CALL_P (loc_note))
21313 {
21314 call_site_count++;
21315 if (SIBLING_CALL_P (loc_note))
21316 tail_call_site_count++;
21317 }
21318 return;
21319 }
21320
21321 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21322 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21323 return;
21324
21325 /* Optimize processing a large consecutive sequence of location
21326 notes so we don't spend too much time in next_real_insn. If the
21327 next insn is another location note, remember the next_real_insn
21328 calculation for next time. */
21329 next_real = cached_next_real_insn;
21330 if (next_real)
21331 {
21332 if (expected_next_loc_note != loc_note)
21333 next_real = NULL_RTX;
21334 }
21335
21336 next_note = NEXT_INSN (loc_note);
21337 if (! next_note
21338 || INSN_DELETED_P (next_note)
21339 || ! NOTE_P (next_note)
21340 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21341 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21342 next_note = NULL_RTX;
21343
21344 if (! next_real)
21345 next_real = next_real_insn (loc_note);
21346
21347 if (next_note)
21348 {
21349 expected_next_loc_note = next_note;
21350 cached_next_real_insn = next_real;
21351 }
21352 else
21353 cached_next_real_insn = NULL_RTX;
21354
21355 /* If there are no instructions which would be affected by this note,
21356 don't do anything. */
21357 if (var_loc_p
21358 && next_real == NULL_RTX
21359 && !NOTE_DURING_CALL_P (loc_note))
21360 return;
21361
21362 if (next_real == NULL_RTX)
21363 next_real = get_last_insn ();
21364
21365 /* If there were any real insns between note we processed last time
21366 and this note (or if it is the first note), clear
21367 last_{,postcall_}label so that they are not reused this time. */
21368 if (last_var_location_insn == NULL_RTX
21369 || last_var_location_insn != next_real
21370 || last_in_cold_section_p != in_cold_section_p)
21371 {
21372 last_label = NULL;
21373 last_postcall_label = NULL;
21374 }
21375
21376 if (var_loc_p)
21377 {
21378 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21379 newloc = add_var_loc_to_decl (decl, loc_note,
21380 NOTE_DURING_CALL_P (loc_note)
21381 ? last_postcall_label : last_label);
21382 if (newloc == NULL)
21383 return;
21384 }
21385 else
21386 {
21387 decl = NULL_TREE;
21388 newloc = NULL;
21389 }
21390
21391 /* If there were no real insns between note we processed last time
21392 and this note, use the label we emitted last time. Otherwise
21393 create a new label and emit it. */
21394 if (last_label == NULL)
21395 {
21396 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21397 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21398 loclabel_num++;
21399 last_label = ggc_strdup (loclabel);
21400 /* See if loclabel might be equal to .Ltext0. If yes,
21401 bump first_loclabel_num_not_at_text_label. */
21402 if (!have_multiple_function_sections
21403 && in_first_function_p
21404 && maybe_at_text_label_p)
21405 {
21406 static rtx last_start;
21407 rtx insn;
21408 for (insn = loc_note; insn; insn = previous_insn (insn))
21409 if (insn == last_start)
21410 break;
21411 else if (!NONDEBUG_INSN_P (insn))
21412 continue;
21413 else
21414 {
21415 rtx body = PATTERN (insn);
21416 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21417 continue;
21418 /* Inline asm could occupy zero bytes. */
21419 else if (GET_CODE (body) == ASM_INPUT
21420 || asm_noperands (body) >= 0)
21421 continue;
21422 #ifdef HAVE_attr_length
21423 else if (get_attr_min_length (insn) == 0)
21424 continue;
21425 #endif
21426 else
21427 {
21428 /* Assume insn has non-zero length. */
21429 maybe_at_text_label_p = false;
21430 break;
21431 }
21432 }
21433 if (maybe_at_text_label_p)
21434 {
21435 last_start = loc_note;
21436 first_loclabel_num_not_at_text_label = loclabel_num;
21437 }
21438 }
21439 }
21440
21441 if (!var_loc_p)
21442 {
21443 struct call_arg_loc_node *ca_loc
21444 = ggc_cleared_alloc<call_arg_loc_node> ();
21445 rtx prev = prev_real_insn (loc_note), x;
21446 ca_loc->call_arg_loc_note = loc_note;
21447 ca_loc->next = NULL;
21448 ca_loc->label = last_label;
21449 gcc_assert (prev
21450 && (CALL_P (prev)
21451 || (NONJUMP_INSN_P (prev)
21452 && GET_CODE (PATTERN (prev)) == SEQUENCE
21453 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21454 if (!CALL_P (prev))
21455 prev = XVECEXP (PATTERN (prev), 0, 0);
21456 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21457 x = get_call_rtx_from (PATTERN (prev));
21458 if (x)
21459 {
21460 x = XEXP (XEXP (x, 0), 0);
21461 if (GET_CODE (x) == SYMBOL_REF
21462 && SYMBOL_REF_DECL (x)
21463 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21464 ca_loc->symbol_ref = x;
21465 }
21466 ca_loc->block = insn_scope (prev);
21467 if (call_arg_locations)
21468 call_arg_loc_last->next = ca_loc;
21469 else
21470 call_arg_locations = ca_loc;
21471 call_arg_loc_last = ca_loc;
21472 }
21473 else if (!NOTE_DURING_CALL_P (loc_note))
21474 newloc->label = last_label;
21475 else
21476 {
21477 if (!last_postcall_label)
21478 {
21479 sprintf (loclabel, "%s-1", last_label);
21480 last_postcall_label = ggc_strdup (loclabel);
21481 }
21482 newloc->label = last_postcall_label;
21483 }
21484
21485 last_var_location_insn = next_real;
21486 last_in_cold_section_p = in_cold_section_p;
21487 }
21488
21489 /* Note in one location list that text section has changed. */
21490
21491 static int
21492 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21493 {
21494 var_loc_list *list = (var_loc_list *) *slot;
21495 if (list->first)
21496 list->last_before_switch
21497 = list->last->next ? list->last->next : list->last;
21498 return 1;
21499 }
21500
21501 /* Note in all location lists that text section has changed. */
21502
21503 static void
21504 var_location_switch_text_section (void)
21505 {
21506 if (decl_loc_table == NULL)
21507 return;
21508
21509 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21510 }
21511
21512 /* Create a new line number table. */
21513
21514 static dw_line_info_table *
21515 new_line_info_table (void)
21516 {
21517 dw_line_info_table *table;
21518
21519 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21520 table->file_num = 1;
21521 table->line_num = 1;
21522 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21523
21524 return table;
21525 }
21526
21527 /* Lookup the "current" table into which we emit line info, so
21528 that we don't have to do it for every source line. */
21529
21530 static void
21531 set_cur_line_info_table (section *sec)
21532 {
21533 dw_line_info_table *table;
21534
21535 if (sec == text_section)
21536 table = text_section_line_info;
21537 else if (sec == cold_text_section)
21538 {
21539 table = cold_text_section_line_info;
21540 if (!table)
21541 {
21542 cold_text_section_line_info = table = new_line_info_table ();
21543 table->end_label = cold_end_label;
21544 }
21545 }
21546 else
21547 {
21548 const char *end_label;
21549
21550 if (flag_reorder_blocks_and_partition)
21551 {
21552 if (in_cold_section_p)
21553 end_label = crtl->subsections.cold_section_end_label;
21554 else
21555 end_label = crtl->subsections.hot_section_end_label;
21556 }
21557 else
21558 {
21559 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21560 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21561 current_function_funcdef_no);
21562 end_label = ggc_strdup (label);
21563 }
21564
21565 table = new_line_info_table ();
21566 table->end_label = end_label;
21567
21568 vec_safe_push (separate_line_info, table);
21569 }
21570
21571 if (DWARF2_ASM_LINE_DEBUG_INFO)
21572 table->is_stmt = (cur_line_info_table
21573 ? cur_line_info_table->is_stmt
21574 : DWARF_LINE_DEFAULT_IS_STMT_START);
21575 cur_line_info_table = table;
21576 }
21577
21578
21579 /* We need to reset the locations at the beginning of each
21580 function. We can't do this in the end_function hook, because the
21581 declarations that use the locations won't have been output when
21582 that hook is called. Also compute have_multiple_function_sections here. */
21583
21584 static void
21585 dwarf2out_begin_function (tree fun)
21586 {
21587 section *sec = function_section (fun);
21588
21589 if (sec != text_section)
21590 have_multiple_function_sections = true;
21591
21592 if (flag_reorder_blocks_and_partition && !cold_text_section)
21593 {
21594 gcc_assert (current_function_decl == fun);
21595 cold_text_section = unlikely_text_section ();
21596 switch_to_section (cold_text_section);
21597 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21598 switch_to_section (sec);
21599 }
21600
21601 dwarf2out_note_section_used ();
21602 call_site_count = 0;
21603 tail_call_site_count = 0;
21604
21605 set_cur_line_info_table (sec);
21606 }
21607
21608 /* Helper function of dwarf2out_end_function, called only after emitting
21609 the very first function into assembly. Check if some .debug_loc range
21610 might end with a .LVL* label that could be equal to .Ltext0.
21611 In that case we must force using absolute addresses in .debug_loc ranges,
21612 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21613 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21614 list terminator.
21615 Set have_multiple_function_sections to true in that case and
21616 terminate htab traversal. */
21617
21618 static int
21619 find_empty_loc_ranges_at_text_label (void **slot, void *)
21620 {
21621 var_loc_list *entry;
21622 struct var_loc_node *node;
21623
21624 entry = (var_loc_list *) *slot;
21625 node = entry->first;
21626 if (node && node->next && node->next->label)
21627 {
21628 unsigned int i;
21629 const char *label = node->next->label;
21630 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21631
21632 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21633 {
21634 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21635 if (strcmp (label, loclabel) == 0)
21636 {
21637 have_multiple_function_sections = true;
21638 return 0;
21639 }
21640 }
21641 }
21642 return 1;
21643 }
21644
21645 /* Hook called after emitting a function into assembly.
21646 This does something only for the very first function emitted. */
21647
21648 static void
21649 dwarf2out_end_function (unsigned int)
21650 {
21651 if (in_first_function_p
21652 && !have_multiple_function_sections
21653 && first_loclabel_num_not_at_text_label
21654 && decl_loc_table)
21655 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21656 NULL);
21657 in_first_function_p = false;
21658 maybe_at_text_label_p = false;
21659 }
21660
21661 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21662
21663 static void
21664 push_dw_line_info_entry (dw_line_info_table *table,
21665 enum dw_line_info_opcode opcode, unsigned int val)
21666 {
21667 dw_line_info_entry e;
21668 e.opcode = opcode;
21669 e.val = val;
21670 vec_safe_push (table->entries, e);
21671 }
21672
21673 /* Output a label to mark the beginning of a source code line entry
21674 and record information relating to this source line, in
21675 'line_info_table' for later output of the .debug_line section. */
21676 /* ??? The discriminator parameter ought to be unsigned. */
21677
21678 static void
21679 dwarf2out_source_line (unsigned int line, const char *filename,
21680 int discriminator, bool is_stmt)
21681 {
21682 unsigned int file_num;
21683 dw_line_info_table *table;
21684
21685 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21686 return;
21687
21688 /* The discriminator column was added in dwarf4. Simplify the below
21689 by simply removing it if we're not supposed to output it. */
21690 if (dwarf_version < 4 && dwarf_strict)
21691 discriminator = 0;
21692
21693 table = cur_line_info_table;
21694 file_num = maybe_emit_file (lookup_filename (filename));
21695
21696 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21697 the debugger has used the second (possibly duplicate) line number
21698 at the beginning of the function to mark the end of the prologue.
21699 We could eliminate any other duplicates within the function. For
21700 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21701 that second line number entry. */
21702 /* Recall that this end-of-prologue indication is *not* the same thing
21703 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21704 to which the hook corresponds, follows the last insn that was
21705 emitted by gen_prologue. What we need is to precede the first insn
21706 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21707 insn that corresponds to something the user wrote. These may be
21708 very different locations once scheduling is enabled. */
21709
21710 if (0 && file_num == table->file_num
21711 && line == table->line_num
21712 && discriminator == table->discrim_num
21713 && is_stmt == table->is_stmt)
21714 return;
21715
21716 switch_to_section (current_function_section ());
21717
21718 /* If requested, emit something human-readable. */
21719 if (flag_debug_asm)
21720 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21721
21722 if (DWARF2_ASM_LINE_DEBUG_INFO)
21723 {
21724 /* Emit the .loc directive understood by GNU as. */
21725 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21726 file_num, line, is_stmt, discriminator */
21727 fputs ("\t.loc ", asm_out_file);
21728 fprint_ul (asm_out_file, file_num);
21729 putc (' ', asm_out_file);
21730 fprint_ul (asm_out_file, line);
21731 putc (' ', asm_out_file);
21732 putc ('0', asm_out_file);
21733
21734 if (is_stmt != table->is_stmt)
21735 {
21736 fputs (" is_stmt ", asm_out_file);
21737 putc (is_stmt ? '1' : '0', asm_out_file);
21738 }
21739 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21740 {
21741 gcc_assert (discriminator > 0);
21742 fputs (" discriminator ", asm_out_file);
21743 fprint_ul (asm_out_file, (unsigned long) discriminator);
21744 }
21745 putc ('\n', asm_out_file);
21746 }
21747 else
21748 {
21749 unsigned int label_num = ++line_info_label_num;
21750
21751 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21752
21753 push_dw_line_info_entry (table, LI_set_address, label_num);
21754 if (file_num != table->file_num)
21755 push_dw_line_info_entry (table, LI_set_file, file_num);
21756 if (discriminator != table->discrim_num)
21757 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21758 if (is_stmt != table->is_stmt)
21759 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21760 push_dw_line_info_entry (table, LI_set_line, line);
21761 }
21762
21763 table->file_num = file_num;
21764 table->line_num = line;
21765 table->discrim_num = discriminator;
21766 table->is_stmt = is_stmt;
21767 table->in_use = true;
21768 }
21769
21770 /* Record the beginning of a new source file. */
21771
21772 static void
21773 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21774 {
21775 if (flag_eliminate_dwarf2_dups)
21776 {
21777 /* Record the beginning of the file for break_out_includes. */
21778 dw_die_ref bincl_die;
21779
21780 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21781 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21782 }
21783
21784 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21785 {
21786 macinfo_entry e;
21787 e.code = DW_MACINFO_start_file;
21788 e.lineno = lineno;
21789 e.info = ggc_strdup (filename);
21790 vec_safe_push (macinfo_table, e);
21791 }
21792 }
21793
21794 /* Record the end of a source file. */
21795
21796 static void
21797 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21798 {
21799 if (flag_eliminate_dwarf2_dups)
21800 /* Record the end of the file for break_out_includes. */
21801 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21802
21803 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21804 {
21805 macinfo_entry e;
21806 e.code = DW_MACINFO_end_file;
21807 e.lineno = lineno;
21808 e.info = NULL;
21809 vec_safe_push (macinfo_table, e);
21810 }
21811 }
21812
21813 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21814 the tail part of the directive line, i.e. the part which is past the
21815 initial whitespace, #, whitespace, directive-name, whitespace part. */
21816
21817 static void
21818 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21819 const char *buffer ATTRIBUTE_UNUSED)
21820 {
21821 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21822 {
21823 macinfo_entry e;
21824 /* Insert a dummy first entry to be able to optimize the whole
21825 predefined macro block using DW_MACRO_GNU_transparent_include. */
21826 if (macinfo_table->is_empty () && lineno <= 1)
21827 {
21828 e.code = 0;
21829 e.lineno = 0;
21830 e.info = NULL;
21831 vec_safe_push (macinfo_table, e);
21832 }
21833 e.code = DW_MACINFO_define;
21834 e.lineno = lineno;
21835 e.info = ggc_strdup (buffer);
21836 vec_safe_push (macinfo_table, e);
21837 }
21838 }
21839
21840 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21841 the tail part of the directive line, i.e. the part which is past the
21842 initial whitespace, #, whitespace, directive-name, whitespace part. */
21843
21844 static void
21845 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21846 const char *buffer ATTRIBUTE_UNUSED)
21847 {
21848 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21849 {
21850 macinfo_entry e;
21851 /* Insert a dummy first entry to be able to optimize the whole
21852 predefined macro block using DW_MACRO_GNU_transparent_include. */
21853 if (macinfo_table->is_empty () && lineno <= 1)
21854 {
21855 e.code = 0;
21856 e.lineno = 0;
21857 e.info = NULL;
21858 vec_safe_push (macinfo_table, e);
21859 }
21860 e.code = DW_MACINFO_undef;
21861 e.lineno = lineno;
21862 e.info = ggc_strdup (buffer);
21863 vec_safe_push (macinfo_table, e);
21864 }
21865 }
21866
21867 /* Helpers to manipulate hash table of CUs. */
21868
21869 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21870 {
21871 typedef macinfo_entry value_type;
21872 typedef macinfo_entry compare_type;
21873 static inline hashval_t hash (const value_type *);
21874 static inline bool equal (const value_type *, const compare_type *);
21875 };
21876
21877 inline hashval_t
21878 macinfo_entry_hasher::hash (const value_type *entry)
21879 {
21880 return htab_hash_string (entry->info);
21881 }
21882
21883 inline bool
21884 macinfo_entry_hasher::equal (const value_type *entry1,
21885 const compare_type *entry2)
21886 {
21887 return !strcmp (entry1->info, entry2->info);
21888 }
21889
21890 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21891
21892 /* Output a single .debug_macinfo entry. */
21893
21894 static void
21895 output_macinfo_op (macinfo_entry *ref)
21896 {
21897 int file_num;
21898 size_t len;
21899 struct indirect_string_node *node;
21900 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21901 struct dwarf_file_data *fd;
21902
21903 switch (ref->code)
21904 {
21905 case DW_MACINFO_start_file:
21906 fd = lookup_filename (ref->info);
21907 file_num = maybe_emit_file (fd);
21908 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21909 dw2_asm_output_data_uleb128 (ref->lineno,
21910 "Included from line number %lu",
21911 (unsigned long) ref->lineno);
21912 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21913 break;
21914 case DW_MACINFO_end_file:
21915 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21916 break;
21917 case DW_MACINFO_define:
21918 case DW_MACINFO_undef:
21919 len = strlen (ref->info) + 1;
21920 if (!dwarf_strict
21921 && len > DWARF_OFFSET_SIZE
21922 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21923 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21924 {
21925 ref->code = ref->code == DW_MACINFO_define
21926 ? DW_MACRO_GNU_define_indirect
21927 : DW_MACRO_GNU_undef_indirect;
21928 output_macinfo_op (ref);
21929 return;
21930 }
21931 dw2_asm_output_data (1, ref->code,
21932 ref->code == DW_MACINFO_define
21933 ? "Define macro" : "Undefine macro");
21934 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21935 (unsigned long) ref->lineno);
21936 dw2_asm_output_nstring (ref->info, -1, "The macro");
21937 break;
21938 case DW_MACRO_GNU_define_indirect:
21939 case DW_MACRO_GNU_undef_indirect:
21940 node = find_AT_string (ref->info);
21941 gcc_assert (node
21942 && ((node->form == DW_FORM_strp)
21943 || (node->form == DW_FORM_GNU_str_index)));
21944 dw2_asm_output_data (1, ref->code,
21945 ref->code == DW_MACRO_GNU_define_indirect
21946 ? "Define macro indirect"
21947 : "Undefine macro indirect");
21948 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21949 (unsigned long) ref->lineno);
21950 if (node->form == DW_FORM_strp)
21951 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21952 debug_str_section, "The macro: \"%s\"",
21953 ref->info);
21954 else
21955 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21956 ref->info);
21957 break;
21958 case DW_MACRO_GNU_transparent_include:
21959 dw2_asm_output_data (1, ref->code, "Transparent include");
21960 ASM_GENERATE_INTERNAL_LABEL (label,
21961 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21962 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21963 break;
21964 default:
21965 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21966 ASM_COMMENT_START, (unsigned long) ref->code);
21967 break;
21968 }
21969 }
21970
21971 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21972 other compilation unit .debug_macinfo sections. IDX is the first
21973 index of a define/undef, return the number of ops that should be
21974 emitted in a comdat .debug_macinfo section and emit
21975 a DW_MACRO_GNU_transparent_include entry referencing it.
21976 If the define/undef entry should be emitted normally, return 0. */
21977
21978 static unsigned
21979 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21980 macinfo_hash_type **macinfo_htab)
21981 {
21982 macinfo_entry *first, *second, *cur, *inc;
21983 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21984 unsigned char checksum[16];
21985 struct md5_ctx ctx;
21986 char *grp_name, *tail;
21987 const char *base;
21988 unsigned int i, count, encoded_filename_len, linebuf_len;
21989 macinfo_entry **slot;
21990
21991 first = &(*macinfo_table)[idx];
21992 second = &(*macinfo_table)[idx + 1];
21993
21994 /* Optimize only if there are at least two consecutive define/undef ops,
21995 and either all of them are before first DW_MACINFO_start_file
21996 with lineno {0,1} (i.e. predefined macro block), or all of them are
21997 in some included header file. */
21998 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21999 return 0;
22000 if (vec_safe_is_empty (files))
22001 {
22002 if (first->lineno > 1 || second->lineno > 1)
22003 return 0;
22004 }
22005 else if (first->lineno == 0)
22006 return 0;
22007
22008 /* Find the last define/undef entry that can be grouped together
22009 with first and at the same time compute md5 checksum of their
22010 codes, linenumbers and strings. */
22011 md5_init_ctx (&ctx);
22012 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22013 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22014 break;
22015 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22016 break;
22017 else
22018 {
22019 unsigned char code = cur->code;
22020 md5_process_bytes (&code, 1, &ctx);
22021 checksum_uleb128 (cur->lineno, &ctx);
22022 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22023 }
22024 md5_finish_ctx (&ctx, checksum);
22025 count = i - idx;
22026
22027 /* From the containing include filename (if any) pick up just
22028 usable characters from its basename. */
22029 if (vec_safe_is_empty (files))
22030 base = "";
22031 else
22032 base = lbasename (files->last ().info);
22033 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22034 if (ISIDNUM (base[i]) || base[i] == '.')
22035 encoded_filename_len++;
22036 /* Count . at the end. */
22037 if (encoded_filename_len)
22038 encoded_filename_len++;
22039
22040 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22041 linebuf_len = strlen (linebuf);
22042
22043 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22044 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22045 + 16 * 2 + 1);
22046 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22047 tail = grp_name + 4;
22048 if (encoded_filename_len)
22049 {
22050 for (i = 0; base[i]; i++)
22051 if (ISIDNUM (base[i]) || base[i] == '.')
22052 *tail++ = base[i];
22053 *tail++ = '.';
22054 }
22055 memcpy (tail, linebuf, linebuf_len);
22056 tail += linebuf_len;
22057 *tail++ = '.';
22058 for (i = 0; i < 16; i++)
22059 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22060
22061 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22062 in the empty vector entry before the first define/undef. */
22063 inc = &(*macinfo_table)[idx - 1];
22064 inc->code = DW_MACRO_GNU_transparent_include;
22065 inc->lineno = 0;
22066 inc->info = ggc_strdup (grp_name);
22067 if (!*macinfo_htab)
22068 *macinfo_htab = new macinfo_hash_type (10);
22069 /* Avoid emitting duplicates. */
22070 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22071 if (*slot != NULL)
22072 {
22073 inc->code = 0;
22074 inc->info = NULL;
22075 /* If such an entry has been used before, just emit
22076 a DW_MACRO_GNU_transparent_include op. */
22077 inc = *slot;
22078 output_macinfo_op (inc);
22079 /* And clear all macinfo_entry in the range to avoid emitting them
22080 in the second pass. */
22081 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22082 {
22083 cur->code = 0;
22084 cur->info = NULL;
22085 }
22086 }
22087 else
22088 {
22089 *slot = inc;
22090 inc->lineno = (*macinfo_htab)->elements ();
22091 output_macinfo_op (inc);
22092 }
22093 return count;
22094 }
22095
22096 /* Save any strings needed by the macinfo table in the debug str
22097 table. All strings must be collected into the table by the time
22098 index_string is called. */
22099
22100 static void
22101 save_macinfo_strings (void)
22102 {
22103 unsigned len;
22104 unsigned i;
22105 macinfo_entry *ref;
22106
22107 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22108 {
22109 switch (ref->code)
22110 {
22111 /* Match the logic in output_macinfo_op to decide on
22112 indirect strings. */
22113 case DW_MACINFO_define:
22114 case DW_MACINFO_undef:
22115 len = strlen (ref->info) + 1;
22116 if (!dwarf_strict
22117 && len > DWARF_OFFSET_SIZE
22118 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22119 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22120 set_indirect_string (find_AT_string (ref->info));
22121 break;
22122 case DW_MACRO_GNU_define_indirect:
22123 case DW_MACRO_GNU_undef_indirect:
22124 set_indirect_string (find_AT_string (ref->info));
22125 break;
22126 default:
22127 break;
22128 }
22129 }
22130 }
22131
22132 /* Output macinfo section(s). */
22133
22134 static void
22135 output_macinfo (void)
22136 {
22137 unsigned i;
22138 unsigned long length = vec_safe_length (macinfo_table);
22139 macinfo_entry *ref;
22140 vec<macinfo_entry, va_gc> *files = NULL;
22141 macinfo_hash_type *macinfo_htab = NULL;
22142
22143 if (! length)
22144 return;
22145
22146 /* output_macinfo* uses these interchangeably. */
22147 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22148 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22149 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22150 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22151
22152 /* For .debug_macro emit the section header. */
22153 if (!dwarf_strict)
22154 {
22155 dw2_asm_output_data (2, 4, "DWARF macro version number");
22156 if (DWARF_OFFSET_SIZE == 8)
22157 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22158 else
22159 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22160 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22161 (!dwarf_split_debug_info ? debug_line_section_label
22162 : debug_skeleton_line_section_label),
22163 debug_line_section, NULL);
22164 }
22165
22166 /* In the first loop, it emits the primary .debug_macinfo section
22167 and after each emitted op the macinfo_entry is cleared.
22168 If a longer range of define/undef ops can be optimized using
22169 DW_MACRO_GNU_transparent_include, the
22170 DW_MACRO_GNU_transparent_include op is emitted and kept in
22171 the vector before the first define/undef in the range and the
22172 whole range of define/undef ops is not emitted and kept. */
22173 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22174 {
22175 switch (ref->code)
22176 {
22177 case DW_MACINFO_start_file:
22178 vec_safe_push (files, *ref);
22179 break;
22180 case DW_MACINFO_end_file:
22181 if (!vec_safe_is_empty (files))
22182 files->pop ();
22183 break;
22184 case DW_MACINFO_define:
22185 case DW_MACINFO_undef:
22186 if (!dwarf_strict
22187 && HAVE_COMDAT_GROUP
22188 && vec_safe_length (files) != 1
22189 && i > 0
22190 && i + 1 < length
22191 && (*macinfo_table)[i - 1].code == 0)
22192 {
22193 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22194 if (count)
22195 {
22196 i += count - 1;
22197 continue;
22198 }
22199 }
22200 break;
22201 case 0:
22202 /* A dummy entry may be inserted at the beginning to be able
22203 to optimize the whole block of predefined macros. */
22204 if (i == 0)
22205 continue;
22206 default:
22207 break;
22208 }
22209 output_macinfo_op (ref);
22210 ref->info = NULL;
22211 ref->code = 0;
22212 }
22213
22214 if (!macinfo_htab)
22215 return;
22216
22217 delete macinfo_htab;
22218 macinfo_htab = NULL;
22219
22220 /* If any DW_MACRO_GNU_transparent_include were used, on those
22221 DW_MACRO_GNU_transparent_include entries terminate the
22222 current chain and switch to a new comdat .debug_macinfo
22223 section and emit the define/undef entries within it. */
22224 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22225 switch (ref->code)
22226 {
22227 case 0:
22228 continue;
22229 case DW_MACRO_GNU_transparent_include:
22230 {
22231 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22232 tree comdat_key = get_identifier (ref->info);
22233 /* Terminate the previous .debug_macinfo section. */
22234 dw2_asm_output_data (1, 0, "End compilation unit");
22235 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22236 SECTION_DEBUG
22237 | SECTION_LINKONCE,
22238 comdat_key);
22239 ASM_GENERATE_INTERNAL_LABEL (label,
22240 DEBUG_MACRO_SECTION_LABEL,
22241 ref->lineno);
22242 ASM_OUTPUT_LABEL (asm_out_file, label);
22243 ref->code = 0;
22244 ref->info = NULL;
22245 dw2_asm_output_data (2, 4, "DWARF macro version number");
22246 if (DWARF_OFFSET_SIZE == 8)
22247 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22248 else
22249 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22250 }
22251 break;
22252 case DW_MACINFO_define:
22253 case DW_MACINFO_undef:
22254 output_macinfo_op (ref);
22255 ref->code = 0;
22256 ref->info = NULL;
22257 break;
22258 default:
22259 gcc_unreachable ();
22260 }
22261 }
22262
22263 /* Set up for Dwarf output at the start of compilation. */
22264
22265 static void
22266 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22267 {
22268 /* Allocate the file_table. */
22269 file_table = htab_create_ggc (50, file_table_hash,
22270 file_table_eq, NULL);
22271
22272 /* Allocate the decl_die_table. */
22273 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22274 decl_die_table_eq, NULL);
22275
22276 /* Allocate the decl_loc_table. */
22277 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22278 decl_loc_table_eq, NULL);
22279
22280 /* Allocate the cached_dw_loc_list_table. */
22281 cached_dw_loc_list_table
22282 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22283 cached_dw_loc_list_table_eq, NULL);
22284
22285 /* Allocate the initial hunk of the decl_scope_table. */
22286 vec_alloc (decl_scope_table, 256);
22287
22288 /* Allocate the initial hunk of the abbrev_die_table. */
22289 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22290 (ABBREV_DIE_TABLE_INCREMENT);
22291 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22292 /* Zero-th entry is allocated, but unused. */
22293 abbrev_die_table_in_use = 1;
22294
22295 /* Allocate the pubtypes and pubnames vectors. */
22296 vec_alloc (pubname_table, 32);
22297 vec_alloc (pubtype_table, 32);
22298
22299 vec_alloc (incomplete_types, 64);
22300
22301 vec_alloc (used_rtx_array, 32);
22302
22303 if (!dwarf_split_debug_info)
22304 {
22305 debug_info_section = get_section (DEBUG_INFO_SECTION,
22306 SECTION_DEBUG, NULL);
22307 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22308 SECTION_DEBUG, NULL);
22309 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22310 SECTION_DEBUG, NULL);
22311 }
22312 else
22313 {
22314 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22315 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22316 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22317 SECTION_DEBUG | SECTION_EXCLUDE,
22318 NULL);
22319 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22320 SECTION_DEBUG, NULL);
22321 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22322 SECTION_DEBUG, NULL);
22323 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22324 SECTION_DEBUG, NULL);
22325 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22326 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22327
22328 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22329 the main .o, but the skeleton_line goes into the split off dwo. */
22330 debug_skeleton_line_section
22331 = get_section (DEBUG_DWO_LINE_SECTION,
22332 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22333 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22334 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22335 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22336 SECTION_DEBUG | SECTION_EXCLUDE,
22337 NULL);
22338 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22339 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22340 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22341 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22342 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22343 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22344 }
22345 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22346 SECTION_DEBUG, NULL);
22347 debug_macinfo_section = get_section (dwarf_strict
22348 ? DEBUG_MACINFO_SECTION
22349 : DEBUG_MACRO_SECTION,
22350 DEBUG_MACRO_SECTION_FLAGS, NULL);
22351 debug_line_section = get_section (DEBUG_LINE_SECTION,
22352 SECTION_DEBUG, NULL);
22353 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22354 SECTION_DEBUG, NULL);
22355 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22356 SECTION_DEBUG, NULL);
22357 debug_str_section = get_section (DEBUG_STR_SECTION,
22358 DEBUG_STR_SECTION_FLAGS, NULL);
22359 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22360 SECTION_DEBUG, NULL);
22361 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22362 SECTION_DEBUG, NULL);
22363
22364 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22365 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22366 DEBUG_ABBREV_SECTION_LABEL, 0);
22367 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22368 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22369 COLD_TEXT_SECTION_LABEL, 0);
22370 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22371
22372 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22373 DEBUG_INFO_SECTION_LABEL, 0);
22374 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22375 DEBUG_LINE_SECTION_LABEL, 0);
22376 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22377 DEBUG_RANGES_SECTION_LABEL, 0);
22378 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22379 DEBUG_ADDR_SECTION_LABEL, 0);
22380 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22381 dwarf_strict
22382 ? DEBUG_MACINFO_SECTION_LABEL
22383 : DEBUG_MACRO_SECTION_LABEL, 0);
22384 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22385
22386 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22387 vec_alloc (macinfo_table, 64);
22388
22389 switch_to_section (text_section);
22390 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22391
22392 /* Make sure the line number table for .text always exists. */
22393 text_section_line_info = new_line_info_table ();
22394 text_section_line_info->end_label = text_end_label;
22395 }
22396
22397 /* Called before compile () starts outputtting functions, variables
22398 and toplevel asms into assembly. */
22399
22400 static void
22401 dwarf2out_assembly_start (void)
22402 {
22403 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22404 && dwarf2out_do_cfi_asm ()
22405 && (!(flag_unwind_tables || flag_exceptions)
22406 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22407 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22408 }
22409
22410 /* A helper function for dwarf2out_finish called through
22411 htab_traverse. Assign a string its index. All strings must be
22412 collected into the table by the time index_string is called,
22413 because the indexing code relies on htab_traverse to traverse nodes
22414 in the same order for each run. */
22415
22416 static int
22417 index_string (void **h, void *v)
22418 {
22419 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22420 unsigned int *index = (unsigned int *) v;
22421
22422 find_string_form (node);
22423 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22424 {
22425 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22426 node->index = *index;
22427 *index += 1;
22428 }
22429 return 1;
22430 }
22431
22432 /* A helper function for output_indirect_strings called through
22433 htab_traverse. Output the offset to a string and update the
22434 current offset. */
22435
22436 static int
22437 output_index_string_offset (void **h, void *v)
22438 {
22439 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22440 unsigned int *offset = (unsigned int *) v;
22441
22442 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22443 {
22444 /* Assert that this node has been assigned an index. */
22445 gcc_assert (node->index != NO_INDEX_ASSIGNED
22446 && node->index != NOT_INDEXED);
22447 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22448 "indexed string 0x%x: %s", node->index, node->str);
22449 *offset += strlen (node->str) + 1;
22450 }
22451 return 1;
22452 }
22453
22454 /* A helper function for dwarf2out_finish called through
22455 htab_traverse. Output the indexed string. */
22456
22457 static int
22458 output_index_string (void **h, void *v)
22459 {
22460 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22461 unsigned int *cur_idx = (unsigned int *) v;
22462
22463 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22464 {
22465 /* Assert that the strings are output in the same order as their
22466 indexes were assigned. */
22467 gcc_assert (*cur_idx == node->index);
22468 assemble_string (node->str, strlen (node->str) + 1);
22469 *cur_idx += 1;
22470 }
22471 return 1;
22472 }
22473
22474 /* A helper function for dwarf2out_finish called through
22475 htab_traverse. Emit one queued .debug_str string. */
22476
22477 static int
22478 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22479 {
22480 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22481
22482 node->form = find_string_form (node);
22483 if (node->form == DW_FORM_strp && node->refcount > 0)
22484 {
22485 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22486 assemble_string (node->str, strlen (node->str) + 1);
22487 }
22488
22489 return 1;
22490 }
22491
22492 /* Output the indexed string table. */
22493
22494 static void
22495 output_indirect_strings (void)
22496 {
22497 switch_to_section (debug_str_section);
22498 if (!dwarf_split_debug_info)
22499 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22500 else
22501 {
22502 unsigned int offset = 0;
22503 unsigned int cur_idx = 0;
22504
22505 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22506
22507 switch_to_section (debug_str_offsets_section);
22508 htab_traverse_noresize (debug_str_hash,
22509 output_index_string_offset,
22510 &offset);
22511 switch_to_section (debug_str_dwo_section);
22512 htab_traverse_noresize (debug_str_hash,
22513 output_index_string,
22514 &cur_idx);
22515 }
22516 }
22517
22518 /* Callback for htab_traverse to assign an index to an entry in the
22519 table, and to write that entry to the .debug_addr section. */
22520
22521 static int
22522 output_addr_table_entry (void **slot, void *data)
22523 {
22524 addr_table_entry *entry = (addr_table_entry *) *slot;
22525 unsigned int *cur_index = (unsigned int *)data;
22526
22527 if (entry->refcount == 0)
22528 {
22529 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22530 || entry->index == NOT_INDEXED);
22531 return 1;
22532 }
22533
22534 gcc_assert (entry->index == *cur_index);
22535 (*cur_index)++;
22536
22537 switch (entry->kind)
22538 {
22539 case ate_kind_rtx:
22540 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22541 "0x%x", entry->index);
22542 break;
22543 case ate_kind_rtx_dtprel:
22544 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22545 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22546 DWARF2_ADDR_SIZE,
22547 entry->addr.rtl);
22548 fputc ('\n', asm_out_file);
22549 break;
22550 case ate_kind_label:
22551 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22552 "0x%x", entry->index);
22553 break;
22554 default:
22555 gcc_unreachable ();
22556 }
22557 return 1;
22558 }
22559
22560 /* Produce the .debug_addr section. */
22561
22562 static void
22563 output_addr_table (void)
22564 {
22565 unsigned int index = 0;
22566 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22567 return;
22568
22569 switch_to_section (debug_addr_section);
22570 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22571 }
22572
22573 #if ENABLE_ASSERT_CHECKING
22574 /* Verify that all marks are clear. */
22575
22576 static void
22577 verify_marks_clear (dw_die_ref die)
22578 {
22579 dw_die_ref c;
22580
22581 gcc_assert (! die->die_mark);
22582 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22583 }
22584 #endif /* ENABLE_ASSERT_CHECKING */
22585
22586 /* Clear the marks for a die and its children.
22587 Be cool if the mark isn't set. */
22588
22589 static void
22590 prune_unmark_dies (dw_die_ref die)
22591 {
22592 dw_die_ref c;
22593
22594 if (die->die_mark)
22595 die->die_mark = 0;
22596 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22597 }
22598
22599 /* Given DIE that we're marking as used, find any other dies
22600 it references as attributes and mark them as used. */
22601
22602 static void
22603 prune_unused_types_walk_attribs (dw_die_ref die)
22604 {
22605 dw_attr_ref a;
22606 unsigned ix;
22607
22608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22609 {
22610 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22611 {
22612 /* A reference to another DIE.
22613 Make sure that it will get emitted.
22614 If it was broken out into a comdat group, don't follow it. */
22615 if (! AT_ref (a)->comdat_type_p
22616 || a->dw_attr == DW_AT_specification)
22617 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22618 }
22619 /* Set the string's refcount to 0 so that prune_unused_types_mark
22620 accounts properly for it. */
22621 if (AT_class (a) == dw_val_class_str)
22622 a->dw_attr_val.v.val_str->refcount = 0;
22623 }
22624 }
22625
22626 /* Mark the generic parameters and arguments children DIEs of DIE. */
22627
22628 static void
22629 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22630 {
22631 dw_die_ref c;
22632
22633 if (die == NULL || die->die_child == NULL)
22634 return;
22635 c = die->die_child;
22636 do
22637 {
22638 if (is_template_parameter (c))
22639 prune_unused_types_mark (c, 1);
22640 c = c->die_sib;
22641 } while (c && c != die->die_child);
22642 }
22643
22644 /* Mark DIE as being used. If DOKIDS is true, then walk down
22645 to DIE's children. */
22646
22647 static void
22648 prune_unused_types_mark (dw_die_ref die, int dokids)
22649 {
22650 dw_die_ref c;
22651
22652 if (die->die_mark == 0)
22653 {
22654 /* We haven't done this node yet. Mark it as used. */
22655 die->die_mark = 1;
22656 /* If this is the DIE of a generic type instantiation,
22657 mark the children DIEs that describe its generic parms and
22658 args. */
22659 prune_unused_types_mark_generic_parms_dies (die);
22660
22661 /* We also have to mark its parents as used.
22662 (But we don't want to mark our parent's kids due to this,
22663 unless it is a class.) */
22664 if (die->die_parent)
22665 prune_unused_types_mark (die->die_parent,
22666 class_scope_p (die->die_parent));
22667
22668 /* Mark any referenced nodes. */
22669 prune_unused_types_walk_attribs (die);
22670
22671 /* If this node is a specification,
22672 also mark the definition, if it exists. */
22673 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22674 prune_unused_types_mark (die->die_definition, 1);
22675 }
22676
22677 if (dokids && die->die_mark != 2)
22678 {
22679 /* We need to walk the children, but haven't done so yet.
22680 Remember that we've walked the kids. */
22681 die->die_mark = 2;
22682
22683 /* If this is an array type, we need to make sure our
22684 kids get marked, even if they're types. If we're
22685 breaking out types into comdat sections, do this
22686 for all type definitions. */
22687 if (die->die_tag == DW_TAG_array_type
22688 || (use_debug_types
22689 && is_type_die (die) && ! is_declaration_die (die)))
22690 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22691 else
22692 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22693 }
22694 }
22695
22696 /* For local classes, look if any static member functions were emitted
22697 and if so, mark them. */
22698
22699 static void
22700 prune_unused_types_walk_local_classes (dw_die_ref die)
22701 {
22702 dw_die_ref c;
22703
22704 if (die->die_mark == 2)
22705 return;
22706
22707 switch (die->die_tag)
22708 {
22709 case DW_TAG_structure_type:
22710 case DW_TAG_union_type:
22711 case DW_TAG_class_type:
22712 break;
22713
22714 case DW_TAG_subprogram:
22715 if (!get_AT_flag (die, DW_AT_declaration)
22716 || die->die_definition != NULL)
22717 prune_unused_types_mark (die, 1);
22718 return;
22719
22720 default:
22721 return;
22722 }
22723
22724 /* Mark children. */
22725 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22726 }
22727
22728 /* Walk the tree DIE and mark types that we actually use. */
22729
22730 static void
22731 prune_unused_types_walk (dw_die_ref die)
22732 {
22733 dw_die_ref c;
22734
22735 /* Don't do anything if this node is already marked and
22736 children have been marked as well. */
22737 if (die->die_mark == 2)
22738 return;
22739
22740 switch (die->die_tag)
22741 {
22742 case DW_TAG_structure_type:
22743 case DW_TAG_union_type:
22744 case DW_TAG_class_type:
22745 if (die->die_perennial_p)
22746 break;
22747
22748 for (c = die->die_parent; c; c = c->die_parent)
22749 if (c->die_tag == DW_TAG_subprogram)
22750 break;
22751
22752 /* Finding used static member functions inside of classes
22753 is needed just for local classes, because for other classes
22754 static member function DIEs with DW_AT_specification
22755 are emitted outside of the DW_TAG_*_type. If we ever change
22756 it, we'd need to call this even for non-local classes. */
22757 if (c)
22758 prune_unused_types_walk_local_classes (die);
22759
22760 /* It's a type node --- don't mark it. */
22761 return;
22762
22763 case DW_TAG_const_type:
22764 case DW_TAG_packed_type:
22765 case DW_TAG_pointer_type:
22766 case DW_TAG_reference_type:
22767 case DW_TAG_rvalue_reference_type:
22768 case DW_TAG_volatile_type:
22769 case DW_TAG_typedef:
22770 case DW_TAG_array_type:
22771 case DW_TAG_interface_type:
22772 case DW_TAG_friend:
22773 case DW_TAG_variant_part:
22774 case DW_TAG_enumeration_type:
22775 case DW_TAG_subroutine_type:
22776 case DW_TAG_string_type:
22777 case DW_TAG_set_type:
22778 case DW_TAG_subrange_type:
22779 case DW_TAG_ptr_to_member_type:
22780 case DW_TAG_file_type:
22781 if (die->die_perennial_p)
22782 break;
22783
22784 /* It's a type node --- don't mark it. */
22785 return;
22786
22787 default:
22788 /* Mark everything else. */
22789 break;
22790 }
22791
22792 if (die->die_mark == 0)
22793 {
22794 die->die_mark = 1;
22795
22796 /* Now, mark any dies referenced from here. */
22797 prune_unused_types_walk_attribs (die);
22798 }
22799
22800 die->die_mark = 2;
22801
22802 /* Mark children. */
22803 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22804 }
22805
22806 /* Increment the string counts on strings referred to from DIE's
22807 attributes. */
22808
22809 static void
22810 prune_unused_types_update_strings (dw_die_ref die)
22811 {
22812 dw_attr_ref a;
22813 unsigned ix;
22814
22815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22816 if (AT_class (a) == dw_val_class_str)
22817 {
22818 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22819 s->refcount++;
22820 /* Avoid unnecessarily putting strings that are used less than
22821 twice in the hash table. */
22822 if (s->refcount
22823 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22824 {
22825 void ** slot;
22826 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22827 htab_hash_string (s->str),
22828 INSERT);
22829 gcc_assert (*slot == NULL);
22830 *slot = s;
22831 }
22832 }
22833 }
22834
22835 /* Remove from the tree DIE any dies that aren't marked. */
22836
22837 static void
22838 prune_unused_types_prune (dw_die_ref die)
22839 {
22840 dw_die_ref c;
22841
22842 gcc_assert (die->die_mark);
22843 prune_unused_types_update_strings (die);
22844
22845 if (! die->die_child)
22846 return;
22847
22848 c = die->die_child;
22849 do {
22850 dw_die_ref prev = c;
22851 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22852 if (c == die->die_child)
22853 {
22854 /* No marked children between 'prev' and the end of the list. */
22855 if (prev == c)
22856 /* No marked children at all. */
22857 die->die_child = NULL;
22858 else
22859 {
22860 prev->die_sib = c->die_sib;
22861 die->die_child = prev;
22862 }
22863 return;
22864 }
22865
22866 if (c != prev->die_sib)
22867 prev->die_sib = c;
22868 prune_unused_types_prune (c);
22869 } while (c != die->die_child);
22870 }
22871
22872 /* Remove dies representing declarations that we never use. */
22873
22874 static void
22875 prune_unused_types (void)
22876 {
22877 unsigned int i;
22878 limbo_die_node *node;
22879 comdat_type_node *ctnode;
22880 pubname_ref pub;
22881 dw_die_ref base_type;
22882
22883 #if ENABLE_ASSERT_CHECKING
22884 /* All the marks should already be clear. */
22885 verify_marks_clear (comp_unit_die ());
22886 for (node = limbo_die_list; node; node = node->next)
22887 verify_marks_clear (node->die);
22888 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22889 verify_marks_clear (ctnode->root_die);
22890 #endif /* ENABLE_ASSERT_CHECKING */
22891
22892 /* Mark types that are used in global variables. */
22893 premark_types_used_by_global_vars ();
22894
22895 /* Set the mark on nodes that are actually used. */
22896 prune_unused_types_walk (comp_unit_die ());
22897 for (node = limbo_die_list; node; node = node->next)
22898 prune_unused_types_walk (node->die);
22899 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22900 {
22901 prune_unused_types_walk (ctnode->root_die);
22902 prune_unused_types_mark (ctnode->type_die, 1);
22903 }
22904
22905 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22906 are unusual in that they are pubnames that are the children of pubtypes.
22907 They should only be marked via their parent DW_TAG_enumeration_type die,
22908 not as roots in themselves. */
22909 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22910 if (pub->die->die_tag != DW_TAG_enumerator)
22911 prune_unused_types_mark (pub->die, 1);
22912 for (i = 0; base_types.iterate (i, &base_type); i++)
22913 prune_unused_types_mark (base_type, 1);
22914
22915 if (debug_str_hash)
22916 htab_empty (debug_str_hash);
22917 if (skeleton_debug_str_hash)
22918 htab_empty (skeleton_debug_str_hash);
22919 prune_unused_types_prune (comp_unit_die ());
22920 for (node = limbo_die_list; node; node = node->next)
22921 prune_unused_types_prune (node->die);
22922 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22923 prune_unused_types_prune (ctnode->root_die);
22924
22925 /* Leave the marks clear. */
22926 prune_unmark_dies (comp_unit_die ());
22927 for (node = limbo_die_list; node; node = node->next)
22928 prune_unmark_dies (node->die);
22929 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22930 prune_unmark_dies (ctnode->root_die);
22931 }
22932
22933 /* Set the parameter to true if there are any relative pathnames in
22934 the file table. */
22935 static int
22936 file_table_relative_p (void ** slot, void *param)
22937 {
22938 bool *p = (bool *) param;
22939 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22940 if (!IS_ABSOLUTE_PATH (d->filename))
22941 {
22942 *p = true;
22943 return 0;
22944 }
22945 return 1;
22946 }
22947
22948 /* Helpers to manipulate hash table of comdat type units. */
22949
22950 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22951 {
22952 typedef comdat_type_node value_type;
22953 typedef comdat_type_node compare_type;
22954 static inline hashval_t hash (const value_type *);
22955 static inline bool equal (const value_type *, const compare_type *);
22956 };
22957
22958 inline hashval_t
22959 comdat_type_hasher::hash (const value_type *type_node)
22960 {
22961 hashval_t h;
22962 memcpy (&h, type_node->signature, sizeof (h));
22963 return h;
22964 }
22965
22966 inline bool
22967 comdat_type_hasher::equal (const value_type *type_node_1,
22968 const compare_type *type_node_2)
22969 {
22970 return (! memcmp (type_node_1->signature, type_node_2->signature,
22971 DWARF_TYPE_SIGNATURE_SIZE));
22972 }
22973
22974 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22975 to the location it would have been added, should we know its
22976 DECL_ASSEMBLER_NAME when we added other attributes. This will
22977 probably improve compactness of debug info, removing equivalent
22978 abbrevs, and hide any differences caused by deferring the
22979 computation of the assembler name, triggered by e.g. PCH. */
22980
22981 static inline void
22982 move_linkage_attr (dw_die_ref die)
22983 {
22984 unsigned ix = vec_safe_length (die->die_attr);
22985 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22986
22987 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22988 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22989
22990 while (--ix > 0)
22991 {
22992 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22993
22994 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22995 break;
22996 }
22997
22998 if (ix != vec_safe_length (die->die_attr) - 1)
22999 {
23000 die->die_attr->pop ();
23001 die->die_attr->quick_insert (ix, linkage);
23002 }
23003 }
23004
23005 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23006 referenced from typed stack ops and count how often they are used. */
23007
23008 static void
23009 mark_base_types (dw_loc_descr_ref loc)
23010 {
23011 dw_die_ref base_type = NULL;
23012
23013 for (; loc; loc = loc->dw_loc_next)
23014 {
23015 switch (loc->dw_loc_opc)
23016 {
23017 case DW_OP_GNU_regval_type:
23018 case DW_OP_GNU_deref_type:
23019 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23020 break;
23021 case DW_OP_GNU_convert:
23022 case DW_OP_GNU_reinterpret:
23023 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23024 continue;
23025 /* FALLTHRU */
23026 case DW_OP_GNU_const_type:
23027 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23028 break;
23029 case DW_OP_GNU_entry_value:
23030 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23031 continue;
23032 default:
23033 continue;
23034 }
23035 gcc_assert (base_type->die_parent == comp_unit_die ());
23036 if (base_type->die_mark)
23037 base_type->die_mark++;
23038 else
23039 {
23040 base_types.safe_push (base_type);
23041 base_type->die_mark = 1;
23042 }
23043 }
23044 }
23045
23046 /* Comparison function for sorting marked base types. */
23047
23048 static int
23049 base_type_cmp (const void *x, const void *y)
23050 {
23051 dw_die_ref dx = *(const dw_die_ref *) x;
23052 dw_die_ref dy = *(const dw_die_ref *) y;
23053 unsigned int byte_size1, byte_size2;
23054 unsigned int encoding1, encoding2;
23055 if (dx->die_mark > dy->die_mark)
23056 return -1;
23057 if (dx->die_mark < dy->die_mark)
23058 return 1;
23059 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23060 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23061 if (byte_size1 < byte_size2)
23062 return 1;
23063 if (byte_size1 > byte_size2)
23064 return -1;
23065 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23066 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23067 if (encoding1 < encoding2)
23068 return 1;
23069 if (encoding1 > encoding2)
23070 return -1;
23071 return 0;
23072 }
23073
23074 /* Move base types marked by mark_base_types as early as possible
23075 in the CU, sorted by decreasing usage count both to make the
23076 uleb128 references as small as possible and to make sure they
23077 will have die_offset already computed by calc_die_sizes when
23078 sizes of typed stack loc ops is computed. */
23079
23080 static void
23081 move_marked_base_types (void)
23082 {
23083 unsigned int i;
23084 dw_die_ref base_type, die, c;
23085
23086 if (base_types.is_empty ())
23087 return;
23088
23089 /* Sort by decreasing usage count, they will be added again in that
23090 order later on. */
23091 base_types.qsort (base_type_cmp);
23092 die = comp_unit_die ();
23093 c = die->die_child;
23094 do
23095 {
23096 dw_die_ref prev = c;
23097 c = c->die_sib;
23098 while (c->die_mark)
23099 {
23100 remove_child_with_prev (c, prev);
23101 /* As base types got marked, there must be at least
23102 one node other than DW_TAG_base_type. */
23103 gcc_assert (c != c->die_sib);
23104 c = c->die_sib;
23105 }
23106 }
23107 while (c != die->die_child);
23108 gcc_assert (die->die_child);
23109 c = die->die_child;
23110 for (i = 0; base_types.iterate (i, &base_type); i++)
23111 {
23112 base_type->die_mark = 0;
23113 base_type->die_sib = c->die_sib;
23114 c->die_sib = base_type;
23115 c = base_type;
23116 }
23117 }
23118
23119 /* Helper function for resolve_addr, attempt to resolve
23120 one CONST_STRING, return non-zero if not successful. Similarly verify that
23121 SYMBOL_REFs refer to variables emitted in the current CU. */
23122
23123 static int
23124 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23125 {
23126 rtx rtl = *addr;
23127
23128 if (GET_CODE (rtl) == CONST_STRING)
23129 {
23130 size_t len = strlen (XSTR (rtl, 0)) + 1;
23131 tree t = build_string (len, XSTR (rtl, 0));
23132 tree tlen = size_int (len - 1);
23133 TREE_TYPE (t)
23134 = build_array_type (char_type_node, build_index_type (tlen));
23135 rtl = lookup_constant_def (t);
23136 if (!rtl || !MEM_P (rtl))
23137 return 1;
23138 rtl = XEXP (rtl, 0);
23139 if (GET_CODE (rtl) == SYMBOL_REF
23140 && SYMBOL_REF_DECL (rtl)
23141 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23142 return 1;
23143 vec_safe_push (used_rtx_array, rtl);
23144 *addr = rtl;
23145 return 0;
23146 }
23147
23148 if (GET_CODE (rtl) == SYMBOL_REF
23149 && SYMBOL_REF_DECL (rtl))
23150 {
23151 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23152 {
23153 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23154 return 1;
23155 }
23156 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23157 return 1;
23158 }
23159
23160 if (GET_CODE (rtl) == CONST
23161 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23162 return 1;
23163
23164 return 0;
23165 }
23166
23167 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23168 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23169 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23170
23171 static rtx
23172 string_cst_pool_decl (tree t)
23173 {
23174 rtx rtl = output_constant_def (t, 1);
23175 unsigned char *array;
23176 dw_loc_descr_ref l;
23177 tree decl;
23178 size_t len;
23179 dw_die_ref ref;
23180
23181 if (!rtl || !MEM_P (rtl))
23182 return NULL_RTX;
23183 rtl = XEXP (rtl, 0);
23184 if (GET_CODE (rtl) != SYMBOL_REF
23185 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23186 return NULL_RTX;
23187
23188 decl = SYMBOL_REF_DECL (rtl);
23189 if (!lookup_decl_die (decl))
23190 {
23191 len = TREE_STRING_LENGTH (t);
23192 vec_safe_push (used_rtx_array, rtl);
23193 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23194 array = ggc_vec_alloc<unsigned char> (len);
23195 memcpy (array, TREE_STRING_POINTER (t), len);
23196 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23197 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23198 l->dw_loc_oprnd2.v.val_vec.length = len;
23199 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23200 l->dw_loc_oprnd2.v.val_vec.array = array;
23201 add_AT_loc (ref, DW_AT_location, l);
23202 equate_decl_number_to_die (decl, ref);
23203 }
23204 return rtl;
23205 }
23206
23207 /* Helper function of resolve_addr_in_expr. LOC is
23208 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23209 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23210 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23211 with DW_OP_GNU_implicit_pointer if possible
23212 and return true, if unsuccessful, return false. */
23213
23214 static bool
23215 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23216 {
23217 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23218 HOST_WIDE_INT offset = 0;
23219 dw_die_ref ref = NULL;
23220 tree decl;
23221
23222 if (GET_CODE (rtl) == CONST
23223 && GET_CODE (XEXP (rtl, 0)) == PLUS
23224 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23225 {
23226 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23227 rtl = XEXP (XEXP (rtl, 0), 0);
23228 }
23229 if (GET_CODE (rtl) == CONST_STRING)
23230 {
23231 size_t len = strlen (XSTR (rtl, 0)) + 1;
23232 tree t = build_string (len, XSTR (rtl, 0));
23233 tree tlen = size_int (len - 1);
23234
23235 TREE_TYPE (t)
23236 = build_array_type (char_type_node, build_index_type (tlen));
23237 rtl = string_cst_pool_decl (t);
23238 if (!rtl)
23239 return false;
23240 }
23241 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23242 {
23243 decl = SYMBOL_REF_DECL (rtl);
23244 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23245 {
23246 ref = lookup_decl_die (decl);
23247 if (ref && (get_AT (ref, DW_AT_location)
23248 || get_AT (ref, DW_AT_const_value)))
23249 {
23250 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23251 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23252 loc->dw_loc_oprnd1.val_entry = NULL;
23253 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23254 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23255 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23256 loc->dw_loc_oprnd2.v.val_int = offset;
23257 return true;
23258 }
23259 }
23260 }
23261 return false;
23262 }
23263
23264 /* Helper function for resolve_addr, handle one location
23265 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23266 the location list couldn't be resolved. */
23267
23268 static bool
23269 resolve_addr_in_expr (dw_loc_descr_ref loc)
23270 {
23271 dw_loc_descr_ref keep = NULL;
23272 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23273 switch (loc->dw_loc_opc)
23274 {
23275 case DW_OP_addr:
23276 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23277 {
23278 if ((prev == NULL
23279 || prev->dw_loc_opc == DW_OP_piece
23280 || prev->dw_loc_opc == DW_OP_bit_piece)
23281 && loc->dw_loc_next
23282 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23283 && !dwarf_strict
23284 && optimize_one_addr_into_implicit_ptr (loc))
23285 break;
23286 return false;
23287 }
23288 break;
23289 case DW_OP_GNU_addr_index:
23290 case DW_OP_GNU_const_index:
23291 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23292 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23293 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23294 NULL))
23295 return false;
23296 break;
23297 case DW_OP_const4u:
23298 case DW_OP_const8u:
23299 if (loc->dtprel
23300 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23301 return false;
23302 break;
23303 case DW_OP_plus_uconst:
23304 if (size_of_loc_descr (loc)
23305 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23306 + 1
23307 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23308 {
23309 dw_loc_descr_ref repl
23310 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23311 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23312 add_loc_descr (&repl, loc->dw_loc_next);
23313 *loc = *repl;
23314 }
23315 break;
23316 case DW_OP_implicit_value:
23317 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23318 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23319 return false;
23320 break;
23321 case DW_OP_GNU_implicit_pointer:
23322 case DW_OP_GNU_parameter_ref:
23323 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23324 {
23325 dw_die_ref ref
23326 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23327 if (ref == NULL)
23328 return false;
23329 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23330 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23331 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23332 }
23333 break;
23334 case DW_OP_GNU_const_type:
23335 case DW_OP_GNU_regval_type:
23336 case DW_OP_GNU_deref_type:
23337 case DW_OP_GNU_convert:
23338 case DW_OP_GNU_reinterpret:
23339 while (loc->dw_loc_next
23340 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23341 {
23342 dw_die_ref base1, base2;
23343 unsigned enc1, enc2, size1, size2;
23344 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23345 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23346 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23347 else if (loc->dw_loc_oprnd1.val_class
23348 == dw_val_class_unsigned_const)
23349 break;
23350 else
23351 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23352 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23353 == dw_val_class_unsigned_const)
23354 break;
23355 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23356 gcc_assert (base1->die_tag == DW_TAG_base_type
23357 && base2->die_tag == DW_TAG_base_type);
23358 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23359 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23360 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23361 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23362 if (size1 == size2
23363 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23364 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23365 && loc != keep)
23366 || enc1 == enc2))
23367 {
23368 /* Optimize away next DW_OP_GNU_convert after
23369 adjusting LOC's base type die reference. */
23370 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23371 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23372 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23373 else
23374 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23375 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23376 continue;
23377 }
23378 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23379 point typed stack entry. */
23380 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23381 keep = loc->dw_loc_next;
23382 break;
23383 }
23384 break;
23385 default:
23386 break;
23387 }
23388 return true;
23389 }
23390
23391 /* Helper function of resolve_addr. DIE had DW_AT_location of
23392 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23393 and DW_OP_addr couldn't be resolved. resolve_addr has already
23394 removed the DW_AT_location attribute. This function attempts to
23395 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23396 to it or DW_AT_const_value attribute, if possible. */
23397
23398 static void
23399 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23400 {
23401 if (TREE_CODE (decl) != VAR_DECL
23402 || lookup_decl_die (decl) != die
23403 || DECL_EXTERNAL (decl)
23404 || !TREE_STATIC (decl)
23405 || DECL_INITIAL (decl) == NULL_TREE
23406 || DECL_P (DECL_INITIAL (decl))
23407 || get_AT (die, DW_AT_const_value))
23408 return;
23409
23410 tree init = DECL_INITIAL (decl);
23411 HOST_WIDE_INT offset = 0;
23412 /* For variables that have been optimized away and thus
23413 don't have a memory location, see if we can emit
23414 DW_AT_const_value instead. */
23415 if (tree_add_const_value_attribute (die, init))
23416 return;
23417 if (dwarf_strict)
23418 return;
23419 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23420 and ADDR_EXPR refers to a decl that has DW_AT_location or
23421 DW_AT_const_value (but isn't addressable, otherwise
23422 resolving the original DW_OP_addr wouldn't fail), see if
23423 we can add DW_OP_GNU_implicit_pointer. */
23424 STRIP_NOPS (init);
23425 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23426 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23427 {
23428 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23429 init = TREE_OPERAND (init, 0);
23430 STRIP_NOPS (init);
23431 }
23432 if (TREE_CODE (init) != ADDR_EXPR)
23433 return;
23434 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23435 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23436 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23437 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23438 && TREE_OPERAND (init, 0) != decl))
23439 {
23440 dw_die_ref ref;
23441 dw_loc_descr_ref l;
23442
23443 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23444 {
23445 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23446 if (!rtl)
23447 return;
23448 decl = SYMBOL_REF_DECL (rtl);
23449 }
23450 else
23451 decl = TREE_OPERAND (init, 0);
23452 ref = lookup_decl_die (decl);
23453 if (ref == NULL
23454 || (!get_AT (ref, DW_AT_location)
23455 && !get_AT (ref, DW_AT_const_value)))
23456 return;
23457 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23458 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23459 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23460 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23461 add_AT_loc (die, DW_AT_location, l);
23462 }
23463 }
23464
23465 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23466 an address in .rodata section if the string literal is emitted there,
23467 or remove the containing location list or replace DW_AT_const_value
23468 with DW_AT_location and empty location expression, if it isn't found
23469 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23470 to something that has been emitted in the current CU. */
23471
23472 static void
23473 resolve_addr (dw_die_ref die)
23474 {
23475 dw_die_ref c;
23476 dw_attr_ref a;
23477 dw_loc_list_ref *curr, *start, loc;
23478 unsigned ix;
23479
23480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23481 switch (AT_class (a))
23482 {
23483 case dw_val_class_loc_list:
23484 start = curr = AT_loc_list_ptr (a);
23485 loc = *curr;
23486 gcc_assert (loc);
23487 /* The same list can be referenced more than once. See if we have
23488 already recorded the result from a previous pass. */
23489 if (loc->replaced)
23490 *curr = loc->dw_loc_next;
23491 else if (!loc->resolved_addr)
23492 {
23493 /* As things stand, we do not expect or allow one die to
23494 reference a suffix of another die's location list chain.
23495 References must be identical or completely separate.
23496 There is therefore no need to cache the result of this
23497 pass on any list other than the first; doing so
23498 would lead to unnecessary writes. */
23499 while (*curr)
23500 {
23501 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23502 if (!resolve_addr_in_expr ((*curr)->expr))
23503 {
23504 dw_loc_list_ref next = (*curr)->dw_loc_next;
23505 dw_loc_descr_ref l = (*curr)->expr;
23506
23507 if (next && (*curr)->ll_symbol)
23508 {
23509 gcc_assert (!next->ll_symbol);
23510 next->ll_symbol = (*curr)->ll_symbol;
23511 }
23512 if (dwarf_split_debug_info)
23513 remove_loc_list_addr_table_entries (l);
23514 *curr = next;
23515 }
23516 else
23517 {
23518 mark_base_types ((*curr)->expr);
23519 curr = &(*curr)->dw_loc_next;
23520 }
23521 }
23522 if (loc == *start)
23523 loc->resolved_addr = 1;
23524 else
23525 {
23526 loc->replaced = 1;
23527 loc->dw_loc_next = *start;
23528 }
23529 }
23530 if (!*start)
23531 {
23532 remove_AT (die, a->dw_attr);
23533 ix--;
23534 }
23535 break;
23536 case dw_val_class_loc:
23537 {
23538 dw_loc_descr_ref l = AT_loc (a);
23539 /* For -gdwarf-2 don't attempt to optimize
23540 DW_AT_data_member_location containing
23541 DW_OP_plus_uconst - older consumers might
23542 rely on it being that op instead of a more complex,
23543 but shorter, location description. */
23544 if ((dwarf_version > 2
23545 || a->dw_attr != DW_AT_data_member_location
23546 || l == NULL
23547 || l->dw_loc_opc != DW_OP_plus_uconst
23548 || l->dw_loc_next != NULL)
23549 && !resolve_addr_in_expr (l))
23550 {
23551 if (dwarf_split_debug_info)
23552 remove_loc_list_addr_table_entries (l);
23553 if (l != NULL
23554 && l->dw_loc_next == NULL
23555 && l->dw_loc_opc == DW_OP_addr
23556 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23557 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23558 && a->dw_attr == DW_AT_location)
23559 {
23560 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23561 remove_AT (die, a->dw_attr);
23562 ix--;
23563 optimize_location_into_implicit_ptr (die, decl);
23564 break;
23565 }
23566 remove_AT (die, a->dw_attr);
23567 ix--;
23568 }
23569 else
23570 mark_base_types (l);
23571 }
23572 break;
23573 case dw_val_class_addr:
23574 if (a->dw_attr == DW_AT_const_value
23575 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23576 {
23577 if (AT_index (a) != NOT_INDEXED)
23578 remove_addr_table_entry (a->dw_attr_val.val_entry);
23579 remove_AT (die, a->dw_attr);
23580 ix--;
23581 }
23582 if (die->die_tag == DW_TAG_GNU_call_site
23583 && a->dw_attr == DW_AT_abstract_origin)
23584 {
23585 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23586 dw_die_ref tdie = lookup_decl_die (tdecl);
23587 if (tdie == NULL
23588 && DECL_EXTERNAL (tdecl)
23589 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23590 {
23591 force_decl_die (tdecl);
23592 tdie = lookup_decl_die (tdecl);
23593 }
23594 if (tdie)
23595 {
23596 a->dw_attr_val.val_class = dw_val_class_die_ref;
23597 a->dw_attr_val.v.val_die_ref.die = tdie;
23598 a->dw_attr_val.v.val_die_ref.external = 0;
23599 }
23600 else
23601 {
23602 if (AT_index (a) != NOT_INDEXED)
23603 remove_addr_table_entry (a->dw_attr_val.val_entry);
23604 remove_AT (die, a->dw_attr);
23605 ix--;
23606 }
23607 }
23608 break;
23609 default:
23610 break;
23611 }
23612
23613 FOR_EACH_CHILD (die, c, resolve_addr (c));
23614 }
23615 \f
23616 /* Helper routines for optimize_location_lists.
23617 This pass tries to share identical local lists in .debug_loc
23618 section. */
23619
23620 /* Iteratively hash operands of LOC opcode. */
23621
23622 static hashval_t
23623 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23624 {
23625 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23626 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23627
23628 switch (loc->dw_loc_opc)
23629 {
23630 case DW_OP_const4u:
23631 case DW_OP_const8u:
23632 if (loc->dtprel)
23633 goto hash_addr;
23634 /* FALLTHRU */
23635 case DW_OP_const1u:
23636 case DW_OP_const1s:
23637 case DW_OP_const2u:
23638 case DW_OP_const2s:
23639 case DW_OP_const4s:
23640 case DW_OP_const8s:
23641 case DW_OP_constu:
23642 case DW_OP_consts:
23643 case DW_OP_pick:
23644 case DW_OP_plus_uconst:
23645 case DW_OP_breg0:
23646 case DW_OP_breg1:
23647 case DW_OP_breg2:
23648 case DW_OP_breg3:
23649 case DW_OP_breg4:
23650 case DW_OP_breg5:
23651 case DW_OP_breg6:
23652 case DW_OP_breg7:
23653 case DW_OP_breg8:
23654 case DW_OP_breg9:
23655 case DW_OP_breg10:
23656 case DW_OP_breg11:
23657 case DW_OP_breg12:
23658 case DW_OP_breg13:
23659 case DW_OP_breg14:
23660 case DW_OP_breg15:
23661 case DW_OP_breg16:
23662 case DW_OP_breg17:
23663 case DW_OP_breg18:
23664 case DW_OP_breg19:
23665 case DW_OP_breg20:
23666 case DW_OP_breg21:
23667 case DW_OP_breg22:
23668 case DW_OP_breg23:
23669 case DW_OP_breg24:
23670 case DW_OP_breg25:
23671 case DW_OP_breg26:
23672 case DW_OP_breg27:
23673 case DW_OP_breg28:
23674 case DW_OP_breg29:
23675 case DW_OP_breg30:
23676 case DW_OP_breg31:
23677 case DW_OP_regx:
23678 case DW_OP_fbreg:
23679 case DW_OP_piece:
23680 case DW_OP_deref_size:
23681 case DW_OP_xderef_size:
23682 hash = iterative_hash_object (val1->v.val_int, hash);
23683 break;
23684 case DW_OP_skip:
23685 case DW_OP_bra:
23686 {
23687 int offset;
23688
23689 gcc_assert (val1->val_class == dw_val_class_loc);
23690 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23691 hash = iterative_hash_object (offset, hash);
23692 }
23693 break;
23694 case DW_OP_implicit_value:
23695 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23696 switch (val2->val_class)
23697 {
23698 case dw_val_class_const:
23699 hash = iterative_hash_object (val2->v.val_int, hash);
23700 break;
23701 case dw_val_class_vec:
23702 {
23703 unsigned int elt_size = val2->v.val_vec.elt_size;
23704 unsigned int len = val2->v.val_vec.length;
23705
23706 hash = iterative_hash_object (elt_size, hash);
23707 hash = iterative_hash_object (len, hash);
23708 hash = iterative_hash (val2->v.val_vec.array,
23709 len * elt_size, hash);
23710 }
23711 break;
23712 case dw_val_class_const_double:
23713 hash = iterative_hash_object (val2->v.val_double.low, hash);
23714 hash = iterative_hash_object (val2->v.val_double.high, hash);
23715 break;
23716 case dw_val_class_wide_int:
23717 hash = iterative_hash_object (*val2->v.val_wide, hash);
23718 break;
23719 case dw_val_class_addr:
23720 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23721 break;
23722 default:
23723 gcc_unreachable ();
23724 }
23725 break;
23726 case DW_OP_bregx:
23727 case DW_OP_bit_piece:
23728 hash = iterative_hash_object (val1->v.val_int, hash);
23729 hash = iterative_hash_object (val2->v.val_int, hash);
23730 break;
23731 case DW_OP_addr:
23732 hash_addr:
23733 if (loc->dtprel)
23734 {
23735 unsigned char dtprel = 0xd1;
23736 hash = iterative_hash_object (dtprel, hash);
23737 }
23738 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23739 break;
23740 case DW_OP_GNU_addr_index:
23741 case DW_OP_GNU_const_index:
23742 {
23743 if (loc->dtprel)
23744 {
23745 unsigned char dtprel = 0xd1;
23746 hash = iterative_hash_object (dtprel, hash);
23747 }
23748 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23749 }
23750 break;
23751 case DW_OP_GNU_implicit_pointer:
23752 hash = iterative_hash_object (val2->v.val_int, hash);
23753 break;
23754 case DW_OP_GNU_entry_value:
23755 hash = hash_loc_operands (val1->v.val_loc, hash);
23756 break;
23757 case DW_OP_GNU_regval_type:
23758 case DW_OP_GNU_deref_type:
23759 {
23760 unsigned int byte_size
23761 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23762 unsigned int encoding
23763 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23764 hash = iterative_hash_object (val1->v.val_int, hash);
23765 hash = iterative_hash_object (byte_size, hash);
23766 hash = iterative_hash_object (encoding, hash);
23767 }
23768 break;
23769 case DW_OP_GNU_convert:
23770 case DW_OP_GNU_reinterpret:
23771 if (val1->val_class == dw_val_class_unsigned_const)
23772 {
23773 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23774 break;
23775 }
23776 /* FALLTHRU */
23777 case DW_OP_GNU_const_type:
23778 {
23779 unsigned int byte_size
23780 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23781 unsigned int encoding
23782 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23783 hash = iterative_hash_object (byte_size, hash);
23784 hash = iterative_hash_object (encoding, hash);
23785 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23786 break;
23787 hash = iterative_hash_object (val2->val_class, hash);
23788 switch (val2->val_class)
23789 {
23790 case dw_val_class_const:
23791 hash = iterative_hash_object (val2->v.val_int, hash);
23792 break;
23793 case dw_val_class_vec:
23794 {
23795 unsigned int elt_size = val2->v.val_vec.elt_size;
23796 unsigned int len = val2->v.val_vec.length;
23797
23798 hash = iterative_hash_object (elt_size, hash);
23799 hash = iterative_hash_object (len, hash);
23800 hash = iterative_hash (val2->v.val_vec.array,
23801 len * elt_size, hash);
23802 }
23803 break;
23804 case dw_val_class_const_double:
23805 hash = iterative_hash_object (val2->v.val_double.low, hash);
23806 hash = iterative_hash_object (val2->v.val_double.high, hash);
23807 break;
23808 case dw_val_class_wide_int:
23809 hash = iterative_hash_object (*val2->v.val_wide, hash);
23810 break;
23811 default:
23812 gcc_unreachable ();
23813 }
23814 }
23815 break;
23816
23817 default:
23818 /* Other codes have no operands. */
23819 break;
23820 }
23821 return hash;
23822 }
23823
23824 /* Iteratively hash the whole DWARF location expression LOC. */
23825
23826 static inline hashval_t
23827 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23828 {
23829 dw_loc_descr_ref l;
23830 bool sizes_computed = false;
23831 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23832 size_of_locs (loc);
23833
23834 for (l = loc; l != NULL; l = l->dw_loc_next)
23835 {
23836 enum dwarf_location_atom opc = l->dw_loc_opc;
23837 hash = iterative_hash_object (opc, hash);
23838 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23839 {
23840 size_of_locs (loc);
23841 sizes_computed = true;
23842 }
23843 hash = hash_loc_operands (l, hash);
23844 }
23845 return hash;
23846 }
23847
23848 /* Compute hash of the whole location list LIST_HEAD. */
23849
23850 static inline void
23851 hash_loc_list (dw_loc_list_ref list_head)
23852 {
23853 dw_loc_list_ref curr = list_head;
23854 hashval_t hash = 0;
23855
23856 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23857 {
23858 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23859 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23860 if (curr->section)
23861 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23862 hash);
23863 hash = hash_locs (curr->expr, hash);
23864 }
23865 list_head->hash = hash;
23866 }
23867
23868 /* Return true if X and Y opcodes have the same operands. */
23869
23870 static inline bool
23871 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23872 {
23873 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23874 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23875 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23876 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23877
23878 switch (x->dw_loc_opc)
23879 {
23880 case DW_OP_const4u:
23881 case DW_OP_const8u:
23882 if (x->dtprel)
23883 goto hash_addr;
23884 /* FALLTHRU */
23885 case DW_OP_const1u:
23886 case DW_OP_const1s:
23887 case DW_OP_const2u:
23888 case DW_OP_const2s:
23889 case DW_OP_const4s:
23890 case DW_OP_const8s:
23891 case DW_OP_constu:
23892 case DW_OP_consts:
23893 case DW_OP_pick:
23894 case DW_OP_plus_uconst:
23895 case DW_OP_breg0:
23896 case DW_OP_breg1:
23897 case DW_OP_breg2:
23898 case DW_OP_breg3:
23899 case DW_OP_breg4:
23900 case DW_OP_breg5:
23901 case DW_OP_breg6:
23902 case DW_OP_breg7:
23903 case DW_OP_breg8:
23904 case DW_OP_breg9:
23905 case DW_OP_breg10:
23906 case DW_OP_breg11:
23907 case DW_OP_breg12:
23908 case DW_OP_breg13:
23909 case DW_OP_breg14:
23910 case DW_OP_breg15:
23911 case DW_OP_breg16:
23912 case DW_OP_breg17:
23913 case DW_OP_breg18:
23914 case DW_OP_breg19:
23915 case DW_OP_breg20:
23916 case DW_OP_breg21:
23917 case DW_OP_breg22:
23918 case DW_OP_breg23:
23919 case DW_OP_breg24:
23920 case DW_OP_breg25:
23921 case DW_OP_breg26:
23922 case DW_OP_breg27:
23923 case DW_OP_breg28:
23924 case DW_OP_breg29:
23925 case DW_OP_breg30:
23926 case DW_OP_breg31:
23927 case DW_OP_regx:
23928 case DW_OP_fbreg:
23929 case DW_OP_piece:
23930 case DW_OP_deref_size:
23931 case DW_OP_xderef_size:
23932 return valx1->v.val_int == valy1->v.val_int;
23933 case DW_OP_skip:
23934 case DW_OP_bra:
23935 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23936 can cause irrelevant differences in dw_loc_addr. */
23937 gcc_assert (valx1->val_class == dw_val_class_loc
23938 && valy1->val_class == dw_val_class_loc
23939 && (dwarf_split_debug_info
23940 || x->dw_loc_addr == y->dw_loc_addr));
23941 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23942 case DW_OP_implicit_value:
23943 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23944 || valx2->val_class != valy2->val_class)
23945 return false;
23946 switch (valx2->val_class)
23947 {
23948 case dw_val_class_const:
23949 return valx2->v.val_int == valy2->v.val_int;
23950 case dw_val_class_vec:
23951 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23952 && valx2->v.val_vec.length == valy2->v.val_vec.length
23953 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23954 valx2->v.val_vec.elt_size
23955 * valx2->v.val_vec.length) == 0;
23956 case dw_val_class_const_double:
23957 return valx2->v.val_double.low == valy2->v.val_double.low
23958 && valx2->v.val_double.high == valy2->v.val_double.high;
23959 case dw_val_class_wide_int:
23960 return *valx2->v.val_wide == *valy2->v.val_wide;
23961 case dw_val_class_addr:
23962 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23963 default:
23964 gcc_unreachable ();
23965 }
23966 case DW_OP_bregx:
23967 case DW_OP_bit_piece:
23968 return valx1->v.val_int == valy1->v.val_int
23969 && valx2->v.val_int == valy2->v.val_int;
23970 case DW_OP_addr:
23971 hash_addr:
23972 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23973 case DW_OP_GNU_addr_index:
23974 case DW_OP_GNU_const_index:
23975 {
23976 rtx ax1 = valx1->val_entry->addr.rtl;
23977 rtx ay1 = valy1->val_entry->addr.rtl;
23978 return rtx_equal_p (ax1, ay1);
23979 }
23980 case DW_OP_GNU_implicit_pointer:
23981 return valx1->val_class == dw_val_class_die_ref
23982 && valx1->val_class == valy1->val_class
23983 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23984 && valx2->v.val_int == valy2->v.val_int;
23985 case DW_OP_GNU_entry_value:
23986 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23987 case DW_OP_GNU_const_type:
23988 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23989 || valx2->val_class != valy2->val_class)
23990 return false;
23991 switch (valx2->val_class)
23992 {
23993 case dw_val_class_const:
23994 return valx2->v.val_int == valy2->v.val_int;
23995 case dw_val_class_vec:
23996 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23997 && valx2->v.val_vec.length == valy2->v.val_vec.length
23998 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23999 valx2->v.val_vec.elt_size
24000 * valx2->v.val_vec.length) == 0;
24001 case dw_val_class_const_double:
24002 return valx2->v.val_double.low == valy2->v.val_double.low
24003 && valx2->v.val_double.high == valy2->v.val_double.high;
24004 case dw_val_class_wide_int:
24005 return *valx2->v.val_wide == *valy2->v.val_wide;
24006 default:
24007 gcc_unreachable ();
24008 }
24009 case DW_OP_GNU_regval_type:
24010 case DW_OP_GNU_deref_type:
24011 return valx1->v.val_int == valy1->v.val_int
24012 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24013 case DW_OP_GNU_convert:
24014 case DW_OP_GNU_reinterpret:
24015 if (valx1->val_class != valy1->val_class)
24016 return false;
24017 if (valx1->val_class == dw_val_class_unsigned_const)
24018 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24019 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24020 case DW_OP_GNU_parameter_ref:
24021 return valx1->val_class == dw_val_class_die_ref
24022 && valx1->val_class == valy1->val_class
24023 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24024 default:
24025 /* Other codes have no operands. */
24026 return true;
24027 }
24028 }
24029
24030 /* Return true if DWARF location expressions X and Y are the same. */
24031
24032 static inline bool
24033 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24034 {
24035 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24036 if (x->dw_loc_opc != y->dw_loc_opc
24037 || x->dtprel != y->dtprel
24038 || !compare_loc_operands (x, y))
24039 break;
24040 return x == NULL && y == NULL;
24041 }
24042
24043 /* Hashtable helpers. */
24044
24045 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24046 {
24047 typedef dw_loc_list_struct value_type;
24048 typedef dw_loc_list_struct compare_type;
24049 static inline hashval_t hash (const value_type *);
24050 static inline bool equal (const value_type *, const compare_type *);
24051 };
24052
24053 /* Return precomputed hash of location list X. */
24054
24055 inline hashval_t
24056 loc_list_hasher::hash (const value_type *x)
24057 {
24058 return x->hash;
24059 }
24060
24061 /* Return true if location lists A and B are the same. */
24062
24063 inline bool
24064 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24065 {
24066 if (a == b)
24067 return 1;
24068 if (a->hash != b->hash)
24069 return 0;
24070 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24071 if (strcmp (a->begin, b->begin) != 0
24072 || strcmp (a->end, b->end) != 0
24073 || (a->section == NULL) != (b->section == NULL)
24074 || (a->section && strcmp (a->section, b->section) != 0)
24075 || !compare_locs (a->expr, b->expr))
24076 break;
24077 return a == NULL && b == NULL;
24078 }
24079
24080 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24081
24082
24083 /* Recursively optimize location lists referenced from DIE
24084 children and share them whenever possible. */
24085
24086 static void
24087 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24088 {
24089 dw_die_ref c;
24090 dw_attr_ref a;
24091 unsigned ix;
24092 dw_loc_list_struct **slot;
24093
24094 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24095 if (AT_class (a) == dw_val_class_loc_list)
24096 {
24097 dw_loc_list_ref list = AT_loc_list (a);
24098 /* TODO: perform some optimizations here, before hashing
24099 it and storing into the hash table. */
24100 hash_loc_list (list);
24101 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24102 if (*slot == NULL)
24103 *slot = list;
24104 else
24105 a->dw_attr_val.v.val_loc_list = *slot;
24106 }
24107
24108 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24109 }
24110
24111
24112 /* Recursively assign each location list a unique index into the debug_addr
24113 section. */
24114
24115 static void
24116 index_location_lists (dw_die_ref die)
24117 {
24118 dw_die_ref c;
24119 dw_attr_ref a;
24120 unsigned ix;
24121
24122 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24123 if (AT_class (a) == dw_val_class_loc_list)
24124 {
24125 dw_loc_list_ref list = AT_loc_list (a);
24126 dw_loc_list_ref curr;
24127 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24128 {
24129 /* Don't index an entry that has already been indexed
24130 or won't be output. */
24131 if (curr->begin_entry != NULL
24132 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24133 continue;
24134
24135 curr->begin_entry
24136 = add_addr_table_entry (xstrdup (curr->begin),
24137 ate_kind_label);
24138 }
24139 }
24140
24141 FOR_EACH_CHILD (die, c, index_location_lists (c));
24142 }
24143
24144 /* Optimize location lists referenced from DIE
24145 children and share them whenever possible. */
24146
24147 static void
24148 optimize_location_lists (dw_die_ref die)
24149 {
24150 loc_list_hash_type htab (500);
24151 optimize_location_lists_1 (die, &htab);
24152 }
24153 \f
24154 /* Output stuff that dwarf requires at the end of every file,
24155 and generate the DWARF-2 debugging info. */
24156
24157 static void
24158 dwarf2out_finish (const char *filename)
24159 {
24160 limbo_die_node *node, *next_node;
24161 comdat_type_node *ctnode;
24162 unsigned int i;
24163 dw_die_ref main_comp_unit_die;
24164
24165 /* PCH might result in DW_AT_producer string being restored from the
24166 header compilation, so always fill it with empty string initially
24167 and overwrite only here. */
24168 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24169 producer_string = gen_producer_string ();
24170 producer->dw_attr_val.v.val_str->refcount--;
24171 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24172
24173 gen_scheduled_generic_parms_dies ();
24174 gen_remaining_tmpl_value_param_die_attribute ();
24175
24176 /* Add the name for the main input file now. We delayed this from
24177 dwarf2out_init to avoid complications with PCH. */
24178 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24179 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24180 add_comp_dir_attribute (comp_unit_die ());
24181 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24182 {
24183 bool p = false;
24184 htab_traverse (file_table, file_table_relative_p, &p);
24185 if (p)
24186 add_comp_dir_attribute (comp_unit_die ());
24187 }
24188
24189 if (deferred_locations_list)
24190 for (i = 0; i < deferred_locations_list->length (); i++)
24191 {
24192 add_location_or_const_value_attribute (
24193 (*deferred_locations_list)[i].die,
24194 (*deferred_locations_list)[i].variable,
24195 false,
24196 DW_AT_location);
24197 }
24198
24199 /* Traverse the limbo die list, and add parent/child links. The only
24200 dies without parents that should be here are concrete instances of
24201 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24202 For concrete instances, we can get the parent die from the abstract
24203 instance. */
24204 for (node = limbo_die_list; node; node = next_node)
24205 {
24206 dw_die_ref die = node->die;
24207 next_node = node->next;
24208
24209 if (die->die_parent == NULL)
24210 {
24211 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24212
24213 if (origin && origin->die_parent)
24214 add_child_die (origin->die_parent, die);
24215 else if (is_cu_die (die))
24216 ;
24217 else if (seen_error ())
24218 /* It's OK to be confused by errors in the input. */
24219 add_child_die (comp_unit_die (), die);
24220 else
24221 {
24222 /* In certain situations, the lexical block containing a
24223 nested function can be optimized away, which results
24224 in the nested function die being orphaned. Likewise
24225 with the return type of that nested function. Force
24226 this to be a child of the containing function.
24227
24228 It may happen that even the containing function got fully
24229 inlined and optimized out. In that case we are lost and
24230 assign the empty child. This should not be big issue as
24231 the function is likely unreachable too. */
24232 gcc_assert (node->created_for);
24233
24234 if (DECL_P (node->created_for))
24235 origin = get_context_die (DECL_CONTEXT (node->created_for));
24236 else if (TYPE_P (node->created_for))
24237 origin = scope_die_for (node->created_for, comp_unit_die ());
24238 else
24239 origin = comp_unit_die ();
24240
24241 add_child_die (origin, die);
24242 }
24243 }
24244 }
24245
24246 limbo_die_list = NULL;
24247
24248 #if ENABLE_ASSERT_CHECKING
24249 {
24250 dw_die_ref die = comp_unit_die (), c;
24251 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24252 }
24253 #endif
24254 resolve_addr (comp_unit_die ());
24255 move_marked_base_types ();
24256
24257 for (node = deferred_asm_name; node; node = node->next)
24258 {
24259 tree decl = node->created_for;
24260 /* When generating LTO bytecode we can not generate new assembler
24261 names at this point and all important decls got theirs via
24262 free-lang-data. */
24263 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24264 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24265 {
24266 add_linkage_attr (node->die, decl);
24267 move_linkage_attr (node->die);
24268 }
24269 }
24270
24271 deferred_asm_name = NULL;
24272
24273 /* Walk through the list of incomplete types again, trying once more to
24274 emit full debugging info for them. */
24275 retry_incomplete_types ();
24276
24277 if (flag_eliminate_unused_debug_types)
24278 prune_unused_types ();
24279
24280 /* Generate separate COMDAT sections for type DIEs. */
24281 if (use_debug_types)
24282 {
24283 break_out_comdat_types (comp_unit_die ());
24284
24285 /* Each new type_unit DIE was added to the limbo die list when created.
24286 Since these have all been added to comdat_type_list, clear the
24287 limbo die list. */
24288 limbo_die_list = NULL;
24289
24290 /* For each new comdat type unit, copy declarations for incomplete
24291 types to make the new unit self-contained (i.e., no direct
24292 references to the main compile unit). */
24293 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24294 copy_decls_for_unworthy_types (ctnode->root_die);
24295 copy_decls_for_unworthy_types (comp_unit_die ());
24296
24297 /* In the process of copying declarations from one unit to another,
24298 we may have left some declarations behind that are no longer
24299 referenced. Prune them. */
24300 prune_unused_types ();
24301 }
24302
24303 /* Generate separate CUs for each of the include files we've seen.
24304 They will go into limbo_die_list. */
24305 if (flag_eliminate_dwarf2_dups)
24306 break_out_includes (comp_unit_die ());
24307
24308 /* Traverse the DIE's and add add sibling attributes to those DIE's
24309 that have children. */
24310 add_sibling_attributes (comp_unit_die ());
24311 for (node = limbo_die_list; node; node = node->next)
24312 add_sibling_attributes (node->die);
24313 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24314 add_sibling_attributes (ctnode->root_die);
24315
24316 /* When splitting DWARF info, we put some attributes in the
24317 skeleton compile_unit DIE that remains in the .o, while
24318 most attributes go in the DWO compile_unit_die. */
24319 if (dwarf_split_debug_info)
24320 main_comp_unit_die = gen_compile_unit_die (NULL);
24321 else
24322 main_comp_unit_die = comp_unit_die ();
24323
24324 /* Output a terminator label for the .text section. */
24325 switch_to_section (text_section);
24326 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24327 if (cold_text_section)
24328 {
24329 switch_to_section (cold_text_section);
24330 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24331 }
24332
24333 /* We can only use the low/high_pc attributes if all of the code was
24334 in .text. */
24335 if (!have_multiple_function_sections
24336 || (dwarf_version < 3 && dwarf_strict))
24337 {
24338 /* Don't add if the CU has no associated code. */
24339 if (text_section_used)
24340 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24341 text_end_label, true);
24342 }
24343 else
24344 {
24345 unsigned fde_idx;
24346 dw_fde_ref fde;
24347 bool range_list_added = false;
24348
24349 if (text_section_used)
24350 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24351 text_end_label, &range_list_added, true);
24352 if (cold_text_section_used)
24353 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24354 cold_end_label, &range_list_added, true);
24355
24356 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24357 {
24358 if (DECL_IGNORED_P (fde->decl))
24359 continue;
24360 if (!fde->in_std_section)
24361 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24362 fde->dw_fde_end, &range_list_added,
24363 true);
24364 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24365 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24366 fde->dw_fde_second_end, &range_list_added,
24367 true);
24368 }
24369
24370 if (range_list_added)
24371 {
24372 /* We need to give .debug_loc and .debug_ranges an appropriate
24373 "base address". Use zero so that these addresses become
24374 absolute. Historically, we've emitted the unexpected
24375 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24376 Emit both to give time for other tools to adapt. */
24377 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24378 if (! dwarf_strict && dwarf_version < 4)
24379 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24380
24381 add_ranges (NULL);
24382 }
24383 }
24384
24385 if (debug_info_level >= DINFO_LEVEL_TERSE)
24386 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24387 debug_line_section_label);
24388
24389 if (have_macinfo)
24390 add_AT_macptr (comp_unit_die (),
24391 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24392 macinfo_section_label);
24393
24394 if (dwarf_split_debug_info && addr_index_table != NULL)
24395 {
24396 /* optimize_location_lists calculates the size of the lists,
24397 so index them first, and assign indices to the entries.
24398 Although optimize_location_lists will remove entries from
24399 the table, it only does so for duplicates, and therefore
24400 only reduces ref_counts to 1. */
24401 unsigned int index = 0;
24402 index_location_lists (comp_unit_die ());
24403 htab_traverse_noresize (addr_index_table,
24404 index_addr_table_entry, &index);
24405 }
24406 if (have_location_lists)
24407 optimize_location_lists (comp_unit_die ());
24408
24409 save_macinfo_strings ();
24410
24411 if (dwarf_split_debug_info)
24412 {
24413 unsigned int index = 0;
24414
24415 /* Add attributes common to skeleton compile_units and
24416 type_units. Because these attributes include strings, it
24417 must be done before freezing the string table. Top-level
24418 skeleton die attrs are added when the skeleton type unit is
24419 created, so ensure it is created by this point. */
24420 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24421 (void) get_skeleton_type_unit ();
24422 htab_traverse_noresize (debug_str_hash, index_string, &index);
24423 }
24424
24425 /* Output all of the compilation units. We put the main one last so that
24426 the offsets are available to output_pubnames. */
24427 for (node = limbo_die_list; node; node = node->next)
24428 output_comp_unit (node->die, 0);
24429
24430 hash_table<comdat_type_hasher> comdat_type_table (100);
24431 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24432 {
24433 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24434
24435 /* Don't output duplicate types. */
24436 if (*slot != HTAB_EMPTY_ENTRY)
24437 continue;
24438
24439 /* Add a pointer to the line table for the main compilation unit
24440 so that the debugger can make sense of DW_AT_decl_file
24441 attributes. */
24442 if (debug_info_level >= DINFO_LEVEL_TERSE)
24443 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24444 (!dwarf_split_debug_info
24445 ? debug_line_section_label
24446 : debug_skeleton_line_section_label));
24447
24448 output_comdat_type_unit (ctnode);
24449 *slot = ctnode;
24450 }
24451
24452 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24453 both the main_cu and all skeleton TUs. Making this call unconditional
24454 would end up either adding a second copy of the AT_pubnames attribute, or
24455 requiring a special case in add_top_level_skeleton_die_attrs. */
24456 if (!dwarf_split_debug_info)
24457 add_AT_pubnames (comp_unit_die ());
24458
24459 if (dwarf_split_debug_info)
24460 {
24461 int mark;
24462 unsigned char checksum[16];
24463 struct md5_ctx ctx;
24464
24465 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24466 md5_init_ctx (&ctx);
24467 mark = 0;
24468 die_checksum (comp_unit_die (), &ctx, &mark);
24469 unmark_all_dies (comp_unit_die ());
24470 md5_finish_ctx (&ctx, checksum);
24471
24472 /* Use the first 8 bytes of the checksum as the dwo_id,
24473 and add it to both comp-unit DIEs. */
24474 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24475 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24476
24477 /* Add the base offset of the ranges table to the skeleton
24478 comp-unit DIE. */
24479 if (ranges_table_in_use)
24480 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24481 ranges_section_label);
24482
24483 switch_to_section (debug_addr_section);
24484 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24485 output_addr_table ();
24486 }
24487
24488 /* Output the main compilation unit if non-empty or if .debug_macinfo
24489 or .debug_macro will be emitted. */
24490 output_comp_unit (comp_unit_die (), have_macinfo);
24491
24492 if (dwarf_split_debug_info && info_section_emitted)
24493 output_skeleton_debug_sections (main_comp_unit_die);
24494
24495 /* Output the abbreviation table. */
24496 if (abbrev_die_table_in_use != 1)
24497 {
24498 switch_to_section (debug_abbrev_section);
24499 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24500 output_abbrev_section ();
24501 }
24502
24503 /* Output location list section if necessary. */
24504 if (have_location_lists)
24505 {
24506 /* Output the location lists info. */
24507 switch_to_section (debug_loc_section);
24508 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24509 output_location_lists (comp_unit_die ());
24510 }
24511
24512 output_pubtables ();
24513
24514 /* Output the address range information if a CU (.debug_info section)
24515 was emitted. We output an empty table even if we had no functions
24516 to put in it. This because the consumer has no way to tell the
24517 difference between an empty table that we omitted and failure to
24518 generate a table that would have contained data. */
24519 if (info_section_emitted)
24520 {
24521 unsigned long aranges_length = size_of_aranges ();
24522
24523 switch_to_section (debug_aranges_section);
24524 output_aranges (aranges_length);
24525 }
24526
24527 /* Output ranges section if necessary. */
24528 if (ranges_table_in_use)
24529 {
24530 switch_to_section (debug_ranges_section);
24531 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24532 output_ranges ();
24533 }
24534
24535 /* Have to end the macro section. */
24536 if (have_macinfo)
24537 {
24538 switch_to_section (debug_macinfo_section);
24539 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24540 output_macinfo ();
24541 dw2_asm_output_data (1, 0, "End compilation unit");
24542 }
24543
24544 /* Output the source line correspondence table. We must do this
24545 even if there is no line information. Otherwise, on an empty
24546 translation unit, we will generate a present, but empty,
24547 .debug_info section. IRIX 6.5 `nm' will then complain when
24548 examining the file. This is done late so that any filenames
24549 used by the debug_info section are marked as 'used'. */
24550 switch_to_section (debug_line_section);
24551 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24552 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24553 output_line_info (false);
24554
24555 if (dwarf_split_debug_info && info_section_emitted)
24556 {
24557 switch_to_section (debug_skeleton_line_section);
24558 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24559 output_line_info (true);
24560 }
24561
24562 /* If we emitted any indirect strings, output the string table too. */
24563 if (debug_str_hash || skeleton_debug_str_hash)
24564 output_indirect_strings ();
24565 }
24566
24567 #include "gt-dwarf2out.h"