expmed.c (struct init_expmed_rtl): Change all fields but pow2 and cint from struct...
[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 gcc_assert (dwarf_split_debug_info && addr_index_table);
4287 /* After an index is assigned, the table is frozen. */
4288 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4289 entry->refcount--;
4290 }
4291
4292 /* Given a location list, remove all addresses it refers to from the
4293 address_table. */
4294
4295 static void
4296 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4297 {
4298 for (; descr; descr = descr->dw_loc_next)
4299 if (descr->dw_loc_oprnd1.val_entry != NULL)
4300 {
4301 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4302 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4303 }
4304 }
4305
4306 /* A helper function for dwarf2out_finish called through
4307 htab_traverse. Assign an addr_table_entry its index. All entries
4308 must be collected into the table when this function is called,
4309 because the indexing code relies on htab_traverse to traverse nodes
4310 in the same order for each run. */
4311
4312 static int
4313 index_addr_table_entry (void **h, void *v)
4314 {
4315 addr_table_entry *node = (addr_table_entry *) *h;
4316 unsigned int *index = (unsigned int *) v;
4317
4318 /* Don't index unreferenced nodes. */
4319 if (node->refcount == 0)
4320 return 1;
4321
4322 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4323 node->index = *index;
4324 *index += 1;
4325
4326 return 1;
4327 }
4328
4329 /* Add an address constant attribute value to a DIE. When using
4330 dwarf_split_debug_info, address attributes in dies destined for the
4331 final executable should be direct references--setting the parameter
4332 force_direct ensures this behavior. */
4333
4334 static inline void
4335 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4336 bool force_direct)
4337 {
4338 dw_attr_node attr;
4339
4340 attr.dw_attr = attr_kind;
4341 attr.dw_attr_val.val_class = dw_val_class_addr;
4342 attr.dw_attr_val.v.val_addr = addr;
4343 if (dwarf_split_debug_info && !force_direct)
4344 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4345 else
4346 attr.dw_attr_val.val_entry = NULL;
4347 add_dwarf_attr (die, &attr);
4348 }
4349
4350 /* Get the RTX from to an address DIE attribute. */
4351
4352 static inline rtx
4353 AT_addr (dw_attr_ref a)
4354 {
4355 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4356 return a->dw_attr_val.v.val_addr;
4357 }
4358
4359 /* Add a file attribute value to a DIE. */
4360
4361 static inline void
4362 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4363 struct dwarf_file_data *fd)
4364 {
4365 dw_attr_node attr;
4366
4367 attr.dw_attr = attr_kind;
4368 attr.dw_attr_val.val_class = dw_val_class_file;
4369 attr.dw_attr_val.val_entry = NULL;
4370 attr.dw_attr_val.v.val_file = fd;
4371 add_dwarf_attr (die, &attr);
4372 }
4373
4374 /* Get the dwarf_file_data from a file DIE attribute. */
4375
4376 static inline struct dwarf_file_data *
4377 AT_file (dw_attr_ref a)
4378 {
4379 gcc_assert (a && AT_class (a) == dw_val_class_file);
4380 return a->dw_attr_val.v.val_file;
4381 }
4382
4383 /* Add a vms delta attribute value to a DIE. */
4384
4385 static inline void
4386 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4387 const char *lbl1, const char *lbl2)
4388 {
4389 dw_attr_node attr;
4390
4391 attr.dw_attr = attr_kind;
4392 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4393 attr.dw_attr_val.val_entry = NULL;
4394 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4395 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4396 add_dwarf_attr (die, &attr);
4397 }
4398
4399 /* Add a label identifier attribute value to a DIE. */
4400
4401 static inline void
4402 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4403 const char *lbl_id)
4404 {
4405 dw_attr_node attr;
4406
4407 attr.dw_attr = attr_kind;
4408 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4409 attr.dw_attr_val.val_entry = NULL;
4410 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4411 if (dwarf_split_debug_info)
4412 attr.dw_attr_val.val_entry
4413 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4414 ate_kind_label);
4415 add_dwarf_attr (die, &attr);
4416 }
4417
4418 /* Add a section offset attribute value to a DIE, an offset into the
4419 debug_line section. */
4420
4421 static inline void
4422 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4423 const char *label)
4424 {
4425 dw_attr_node attr;
4426
4427 attr.dw_attr = attr_kind;
4428 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4429 attr.dw_attr_val.val_entry = NULL;
4430 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4431 add_dwarf_attr (die, &attr);
4432 }
4433
4434 /* Add a section offset attribute value to a DIE, an offset into the
4435 debug_macinfo section. */
4436
4437 static inline void
4438 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4439 const char *label)
4440 {
4441 dw_attr_node attr;
4442
4443 attr.dw_attr = attr_kind;
4444 attr.dw_attr_val.val_class = dw_val_class_macptr;
4445 attr.dw_attr_val.val_entry = NULL;
4446 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4447 add_dwarf_attr (die, &attr);
4448 }
4449
4450 /* Add an offset attribute value to a DIE. */
4451
4452 static inline void
4453 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4454 unsigned HOST_WIDE_INT offset)
4455 {
4456 dw_attr_node attr;
4457
4458 attr.dw_attr = attr_kind;
4459 attr.dw_attr_val.val_class = dw_val_class_offset;
4460 attr.dw_attr_val.val_entry = NULL;
4461 attr.dw_attr_val.v.val_offset = offset;
4462 add_dwarf_attr (die, &attr);
4463 }
4464
4465 /* Add a range_list attribute value to a DIE. When using
4466 dwarf_split_debug_info, address attributes in dies destined for the
4467 final executable should be direct references--setting the parameter
4468 force_direct ensures this behavior. */
4469
4470 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4471 #define RELOCATED_OFFSET (NULL)
4472
4473 static void
4474 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4475 long unsigned int offset, bool force_direct)
4476 {
4477 dw_attr_node attr;
4478
4479 attr.dw_attr = attr_kind;
4480 attr.dw_attr_val.val_class = dw_val_class_range_list;
4481 /* For the range_list attribute, use val_entry to store whether the
4482 offset should follow split-debug-info or normal semantics. This
4483 value is read in output_range_list_offset. */
4484 if (dwarf_split_debug_info && !force_direct)
4485 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4486 else
4487 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4488 attr.dw_attr_val.v.val_offset = offset;
4489 add_dwarf_attr (die, &attr);
4490 }
4491
4492 /* Return the start label of a delta attribute. */
4493
4494 static inline const char *
4495 AT_vms_delta1 (dw_attr_ref a)
4496 {
4497 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4498 return a->dw_attr_val.v.val_vms_delta.lbl1;
4499 }
4500
4501 /* Return the end label of a delta attribute. */
4502
4503 static inline const char *
4504 AT_vms_delta2 (dw_attr_ref a)
4505 {
4506 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4507 return a->dw_attr_val.v.val_vms_delta.lbl2;
4508 }
4509
4510 static inline const char *
4511 AT_lbl (dw_attr_ref a)
4512 {
4513 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4514 || AT_class (a) == dw_val_class_lineptr
4515 || AT_class (a) == dw_val_class_macptr
4516 || AT_class (a) == dw_val_class_high_pc));
4517 return a->dw_attr_val.v.val_lbl_id;
4518 }
4519
4520 /* Get the attribute of type attr_kind. */
4521
4522 static dw_attr_ref
4523 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4524 {
4525 dw_attr_ref a;
4526 unsigned ix;
4527 dw_die_ref spec = NULL;
4528
4529 if (! die)
4530 return NULL;
4531
4532 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4533 if (a->dw_attr == attr_kind)
4534 return a;
4535 else if (a->dw_attr == DW_AT_specification
4536 || a->dw_attr == DW_AT_abstract_origin)
4537 spec = AT_ref (a);
4538
4539 if (spec)
4540 return get_AT (spec, attr_kind);
4541
4542 return NULL;
4543 }
4544
4545 /* Returns the parent of the declaration of DIE. */
4546
4547 static dw_die_ref
4548 get_die_parent (dw_die_ref die)
4549 {
4550 dw_die_ref t;
4551
4552 if (!die)
4553 return NULL;
4554
4555 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4556 || (t = get_AT_ref (die, DW_AT_specification)))
4557 die = t;
4558
4559 return die->die_parent;
4560 }
4561
4562 /* Return the "low pc" attribute value, typically associated with a subprogram
4563 DIE. Return null if the "low pc" attribute is either not present, or if it
4564 cannot be represented as an assembler label identifier. */
4565
4566 static inline const char *
4567 get_AT_low_pc (dw_die_ref die)
4568 {
4569 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4570
4571 return a ? AT_lbl (a) : NULL;
4572 }
4573
4574 /* Return the "high pc" attribute value, typically associated with a subprogram
4575 DIE. Return null if the "high pc" attribute is either not present, or if it
4576 cannot be represented as an assembler label identifier. */
4577
4578 static inline const char *
4579 get_AT_hi_pc (dw_die_ref die)
4580 {
4581 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4582
4583 return a ? AT_lbl (a) : NULL;
4584 }
4585
4586 /* Return the value of the string attribute designated by ATTR_KIND, or
4587 NULL if it is not present. */
4588
4589 static inline const char *
4590 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4591 {
4592 dw_attr_ref a = get_AT (die, attr_kind);
4593
4594 return a ? AT_string (a) : NULL;
4595 }
4596
4597 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4598 if it is not present. */
4599
4600 static inline int
4601 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4602 {
4603 dw_attr_ref a = get_AT (die, attr_kind);
4604
4605 return a ? AT_flag (a) : 0;
4606 }
4607
4608 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4609 if it is not present. */
4610
4611 static inline unsigned
4612 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4613 {
4614 dw_attr_ref a = get_AT (die, attr_kind);
4615
4616 return a ? AT_unsigned (a) : 0;
4617 }
4618
4619 static inline dw_die_ref
4620 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4621 {
4622 dw_attr_ref a = get_AT (die, attr_kind);
4623
4624 return a ? AT_ref (a) : NULL;
4625 }
4626
4627 static inline struct dwarf_file_data *
4628 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4629 {
4630 dw_attr_ref a = get_AT (die, attr_kind);
4631
4632 return a ? AT_file (a) : NULL;
4633 }
4634
4635 /* Return TRUE if the language is C++. */
4636
4637 static inline bool
4638 is_cxx (void)
4639 {
4640 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4641
4642 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4643 }
4644
4645 /* Return TRUE if the language is Java. */
4646
4647 static inline bool
4648 is_java (void)
4649 {
4650 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4651
4652 return lang == DW_LANG_Java;
4653 }
4654
4655 /* Return TRUE if the language is Fortran. */
4656
4657 static inline bool
4658 is_fortran (void)
4659 {
4660 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4661
4662 return (lang == DW_LANG_Fortran77
4663 || lang == DW_LANG_Fortran90
4664 || lang == DW_LANG_Fortran95);
4665 }
4666
4667 /* Return TRUE if the language is Ada. */
4668
4669 static inline bool
4670 is_ada (void)
4671 {
4672 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4673
4674 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4675 }
4676
4677 /* Remove the specified attribute if present. */
4678
4679 static void
4680 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4681 {
4682 dw_attr_ref a;
4683 unsigned ix;
4684
4685 if (! die)
4686 return;
4687
4688 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4689 if (a->dw_attr == attr_kind)
4690 {
4691 if (AT_class (a) == dw_val_class_str)
4692 if (a->dw_attr_val.v.val_str->refcount)
4693 a->dw_attr_val.v.val_str->refcount--;
4694
4695 /* vec::ordered_remove should help reduce the number of abbrevs
4696 that are needed. */
4697 die->die_attr->ordered_remove (ix);
4698 return;
4699 }
4700 }
4701
4702 /* Remove CHILD from its parent. PREV must have the property that
4703 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4704
4705 static void
4706 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4707 {
4708 gcc_assert (child->die_parent == prev->die_parent);
4709 gcc_assert (prev->die_sib == child);
4710 if (prev == child)
4711 {
4712 gcc_assert (child->die_parent->die_child == child);
4713 prev = NULL;
4714 }
4715 else
4716 prev->die_sib = child->die_sib;
4717 if (child->die_parent->die_child == child)
4718 child->die_parent->die_child = prev;
4719 }
4720
4721 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4722 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4723
4724 static void
4725 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4726 {
4727 dw_die_ref parent = old_child->die_parent;
4728
4729 gcc_assert (parent == prev->die_parent);
4730 gcc_assert (prev->die_sib == old_child);
4731
4732 new_child->die_parent = parent;
4733 if (prev == old_child)
4734 {
4735 gcc_assert (parent->die_child == old_child);
4736 new_child->die_sib = new_child;
4737 }
4738 else
4739 {
4740 prev->die_sib = new_child;
4741 new_child->die_sib = old_child->die_sib;
4742 }
4743 if (old_child->die_parent->die_child == old_child)
4744 old_child->die_parent->die_child = new_child;
4745 }
4746
4747 /* Move all children from OLD_PARENT to NEW_PARENT. */
4748
4749 static void
4750 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4751 {
4752 dw_die_ref c;
4753 new_parent->die_child = old_parent->die_child;
4754 old_parent->die_child = NULL;
4755 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4756 }
4757
4758 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4759 matches TAG. */
4760
4761 static void
4762 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4763 {
4764 dw_die_ref c;
4765
4766 c = die->die_child;
4767 if (c) do {
4768 dw_die_ref prev = c;
4769 c = c->die_sib;
4770 while (c->die_tag == tag)
4771 {
4772 remove_child_with_prev (c, prev);
4773 /* Might have removed every child. */
4774 if (c == c->die_sib)
4775 return;
4776 c = c->die_sib;
4777 }
4778 } while (c != die->die_child);
4779 }
4780
4781 /* Add a CHILD_DIE as the last child of DIE. */
4782
4783 static void
4784 add_child_die (dw_die_ref die, dw_die_ref child_die)
4785 {
4786 /* FIXME this should probably be an assert. */
4787 if (! die || ! child_die)
4788 return;
4789 gcc_assert (die != child_die);
4790
4791 child_die->die_parent = die;
4792 if (die->die_child)
4793 {
4794 child_die->die_sib = die->die_child->die_sib;
4795 die->die_child->die_sib = child_die;
4796 }
4797 else
4798 child_die->die_sib = child_die;
4799 die->die_child = child_die;
4800 }
4801
4802 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4803 is the specification, to the end of PARENT's list of children.
4804 This is done by removing and re-adding it. */
4805
4806 static void
4807 splice_child_die (dw_die_ref parent, dw_die_ref child)
4808 {
4809 dw_die_ref p;
4810
4811 /* We want the declaration DIE from inside the class, not the
4812 specification DIE at toplevel. */
4813 if (child->die_parent != parent)
4814 {
4815 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4816
4817 if (tmp)
4818 child = tmp;
4819 }
4820
4821 gcc_assert (child->die_parent == parent
4822 || (child->die_parent
4823 == get_AT_ref (parent, DW_AT_specification)));
4824
4825 for (p = child->die_parent->die_child; ; p = p->die_sib)
4826 if (p->die_sib == child)
4827 {
4828 remove_child_with_prev (child, p);
4829 break;
4830 }
4831
4832 add_child_die (parent, child);
4833 }
4834
4835 /* Return a pointer to a newly created DIE node. */
4836
4837 static inline dw_die_ref
4838 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4839 {
4840 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4841
4842 die->die_tag = tag_value;
4843
4844 if (parent_die != NULL)
4845 add_child_die (parent_die, die);
4846 else
4847 {
4848 limbo_die_node *limbo_node;
4849
4850 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4851 limbo_node->die = die;
4852 limbo_node->created_for = t;
4853 limbo_node->next = limbo_die_list;
4854 limbo_die_list = limbo_node;
4855 }
4856
4857 return die;
4858 }
4859
4860 /* Return the DIE associated with the given type specifier. */
4861
4862 static inline dw_die_ref
4863 lookup_type_die (tree type)
4864 {
4865 return TYPE_SYMTAB_DIE (type);
4866 }
4867
4868 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4869 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4870 anonymous type instead the one of the naming typedef. */
4871
4872 static inline dw_die_ref
4873 strip_naming_typedef (tree type, dw_die_ref type_die)
4874 {
4875 if (type
4876 && TREE_CODE (type) == RECORD_TYPE
4877 && type_die
4878 && type_die->die_tag == DW_TAG_typedef
4879 && is_naming_typedef_decl (TYPE_NAME (type)))
4880 type_die = get_AT_ref (type_die, DW_AT_type);
4881 return type_die;
4882 }
4883
4884 /* Like lookup_type_die, but if type is an anonymous type named by a
4885 typedef[1], return the DIE of the anonymous type instead the one of
4886 the naming typedef. This is because in gen_typedef_die, we did
4887 equate the anonymous struct named by the typedef with the DIE of
4888 the naming typedef. So by default, lookup_type_die on an anonymous
4889 struct yields the DIE of the naming typedef.
4890
4891 [1]: Read the comment of is_naming_typedef_decl to learn about what
4892 a naming typedef is. */
4893
4894 static inline dw_die_ref
4895 lookup_type_die_strip_naming_typedef (tree type)
4896 {
4897 dw_die_ref die = lookup_type_die (type);
4898 return strip_naming_typedef (type, die);
4899 }
4900
4901 /* Equate a DIE to a given type specifier. */
4902
4903 static inline void
4904 equate_type_number_to_die (tree type, dw_die_ref type_die)
4905 {
4906 TYPE_SYMTAB_DIE (type) = type_die;
4907 }
4908
4909 /* Returns a hash value for X (which really is a die_struct). */
4910
4911 static hashval_t
4912 decl_die_table_hash (const void *x)
4913 {
4914 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4915 }
4916
4917 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4918
4919 static int
4920 decl_die_table_eq (const void *x, const void *y)
4921 {
4922 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4923 }
4924
4925 /* Return the DIE associated with a given declaration. */
4926
4927 static inline dw_die_ref
4928 lookup_decl_die (tree decl)
4929 {
4930 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4931 }
4932
4933 /* Returns a hash value for X (which really is a var_loc_list). */
4934
4935 static hashval_t
4936 decl_loc_table_hash (const void *x)
4937 {
4938 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4939 }
4940
4941 /* Return nonzero if decl_id of var_loc_list X is the same as
4942 UID of decl *Y. */
4943
4944 static int
4945 decl_loc_table_eq (const void *x, const void *y)
4946 {
4947 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4948 }
4949
4950 /* Return the var_loc list associated with a given declaration. */
4951
4952 static inline var_loc_list *
4953 lookup_decl_loc (const_tree decl)
4954 {
4955 if (!decl_loc_table)
4956 return NULL;
4957 return (var_loc_list *)
4958 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4959 }
4960
4961 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4962
4963 static hashval_t
4964 cached_dw_loc_list_table_hash (const void *x)
4965 {
4966 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4967 }
4968
4969 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4970 UID of decl *Y. */
4971
4972 static int
4973 cached_dw_loc_list_table_eq (const void *x, const void *y)
4974 {
4975 return (((const cached_dw_loc_list *) x)->decl_id
4976 == DECL_UID ((const_tree) y));
4977 }
4978
4979 /* Equate a DIE to a particular declaration. */
4980
4981 static void
4982 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4983 {
4984 unsigned int decl_id = DECL_UID (decl);
4985 void **slot;
4986
4987 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4988 *slot = decl_die;
4989 decl_die->decl_id = decl_id;
4990 }
4991
4992 /* Return how many bits covers PIECE EXPR_LIST. */
4993
4994 static int
4995 decl_piece_bitsize (rtx piece)
4996 {
4997 int ret = (int) GET_MODE (piece);
4998 if (ret)
4999 return ret;
5000 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5001 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5002 return INTVAL (XEXP (XEXP (piece, 0), 0));
5003 }
5004
5005 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5006
5007 static rtx *
5008 decl_piece_varloc_ptr (rtx piece)
5009 {
5010 if ((int) GET_MODE (piece))
5011 return &XEXP (piece, 0);
5012 else
5013 return &XEXP (XEXP (piece, 0), 1);
5014 }
5015
5016 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5017 Next is the chain of following piece nodes. */
5018
5019 static rtx
5020 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5021 {
5022 if (bitsize <= (int) MAX_MACHINE_MODE)
5023 return alloc_EXPR_LIST (bitsize, loc_note, next);
5024 else
5025 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5026 GEN_INT (bitsize),
5027 loc_note), next);
5028 }
5029
5030 /* Return rtx that should be stored into loc field for
5031 LOC_NOTE and BITPOS/BITSIZE. */
5032
5033 static rtx
5034 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5035 HOST_WIDE_INT bitsize)
5036 {
5037 if (bitsize != -1)
5038 {
5039 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5040 if (bitpos != 0)
5041 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5042 }
5043 return loc_note;
5044 }
5045
5046 /* This function either modifies location piece list *DEST in
5047 place (if SRC and INNER is NULL), or copies location piece list
5048 *SRC to *DEST while modifying it. Location BITPOS is modified
5049 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5050 not copied and if needed some padding around it is added.
5051 When modifying in place, DEST should point to EXPR_LIST where
5052 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5053 to the start of the whole list and INNER points to the EXPR_LIST
5054 where earlier pieces cover PIECE_BITPOS bits. */
5055
5056 static void
5057 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5058 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5059 HOST_WIDE_INT bitsize, rtx loc_note)
5060 {
5061 int diff;
5062 bool copy = inner != NULL;
5063
5064 if (copy)
5065 {
5066 /* First copy all nodes preceding the current bitpos. */
5067 while (src != inner)
5068 {
5069 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5070 decl_piece_bitsize (*src), NULL_RTX);
5071 dest = &XEXP (*dest, 1);
5072 src = &XEXP (*src, 1);
5073 }
5074 }
5075 /* Add padding if needed. */
5076 if (bitpos != piece_bitpos)
5077 {
5078 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5079 copy ? NULL_RTX : *dest);
5080 dest = &XEXP (*dest, 1);
5081 }
5082 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5083 {
5084 gcc_assert (!copy);
5085 /* A piece with correct bitpos and bitsize already exist,
5086 just update the location for it and return. */
5087 *decl_piece_varloc_ptr (*dest) = loc_note;
5088 return;
5089 }
5090 /* Add the piece that changed. */
5091 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5092 dest = &XEXP (*dest, 1);
5093 /* Skip over pieces that overlap it. */
5094 diff = bitpos - piece_bitpos + bitsize;
5095 if (!copy)
5096 src = dest;
5097 while (diff > 0 && *src)
5098 {
5099 rtx piece = *src;
5100 diff -= decl_piece_bitsize (piece);
5101 if (copy)
5102 src = &XEXP (piece, 1);
5103 else
5104 {
5105 *src = XEXP (piece, 1);
5106 free_EXPR_LIST_node (piece);
5107 }
5108 }
5109 /* Add padding if needed. */
5110 if (diff < 0 && *src)
5111 {
5112 if (!copy)
5113 dest = src;
5114 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5115 dest = &XEXP (*dest, 1);
5116 }
5117 if (!copy)
5118 return;
5119 /* Finally copy all nodes following it. */
5120 while (*src)
5121 {
5122 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5123 decl_piece_bitsize (*src), NULL_RTX);
5124 dest = &XEXP (*dest, 1);
5125 src = &XEXP (*src, 1);
5126 }
5127 }
5128
5129 /* Add a variable location node to the linked list for DECL. */
5130
5131 static struct var_loc_node *
5132 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5133 {
5134 unsigned int decl_id;
5135 var_loc_list *temp;
5136 void **slot;
5137 struct var_loc_node *loc = NULL;
5138 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5139
5140 if (TREE_CODE (decl) == VAR_DECL
5141 && DECL_HAS_DEBUG_EXPR_P (decl))
5142 {
5143 tree realdecl = DECL_DEBUG_EXPR (decl);
5144 if (handled_component_p (realdecl)
5145 || (TREE_CODE (realdecl) == MEM_REF
5146 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5147 {
5148 HOST_WIDE_INT maxsize;
5149 tree innerdecl;
5150 innerdecl
5151 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5152 if (!DECL_P (innerdecl)
5153 || DECL_IGNORED_P (innerdecl)
5154 || TREE_STATIC (innerdecl)
5155 || bitsize <= 0
5156 || bitpos + bitsize > 256
5157 || bitsize != maxsize)
5158 return NULL;
5159 decl = innerdecl;
5160 }
5161 }
5162
5163 decl_id = DECL_UID (decl);
5164 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5165 if (*slot == NULL)
5166 {
5167 temp = ggc_cleared_alloc<var_loc_list> ();
5168 temp->decl_id = decl_id;
5169 *slot = temp;
5170 }
5171 else
5172 temp = (var_loc_list *) *slot;
5173
5174 /* For PARM_DECLs try to keep around the original incoming value,
5175 even if that means we'll emit a zero-range .debug_loc entry. */
5176 if (temp->last
5177 && temp->first == temp->last
5178 && TREE_CODE (decl) == PARM_DECL
5179 && NOTE_P (temp->first->loc)
5180 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5181 && DECL_INCOMING_RTL (decl)
5182 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5183 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5184 == GET_CODE (DECL_INCOMING_RTL (decl))
5185 && prev_real_insn (temp->first->loc) == NULL_RTX
5186 && (bitsize != -1
5187 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5188 NOTE_VAR_LOCATION_LOC (loc_note))
5189 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5190 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5191 {
5192 loc = ggc_cleared_alloc<var_loc_node> ();
5193 temp->first->next = loc;
5194 temp->last = loc;
5195 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5196 }
5197 else if (temp->last)
5198 {
5199 struct var_loc_node *last = temp->last, *unused = NULL;
5200 rtx *piece_loc = NULL, last_loc_note;
5201 int piece_bitpos = 0;
5202 if (last->next)
5203 {
5204 last = last->next;
5205 gcc_assert (last->next == NULL);
5206 }
5207 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5208 {
5209 piece_loc = &last->loc;
5210 do
5211 {
5212 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5213 if (piece_bitpos + cur_bitsize > bitpos)
5214 break;
5215 piece_bitpos += cur_bitsize;
5216 piece_loc = &XEXP (*piece_loc, 1);
5217 }
5218 while (*piece_loc);
5219 }
5220 /* TEMP->LAST here is either pointer to the last but one or
5221 last element in the chained list, LAST is pointer to the
5222 last element. */
5223 if (label && strcmp (last->label, label) == 0)
5224 {
5225 /* For SRA optimized variables if there weren't any real
5226 insns since last note, just modify the last node. */
5227 if (piece_loc != NULL)
5228 {
5229 adjust_piece_list (piece_loc, NULL, NULL,
5230 bitpos, piece_bitpos, bitsize, loc_note);
5231 return NULL;
5232 }
5233 /* If the last note doesn't cover any instructions, remove it. */
5234 if (temp->last != last)
5235 {
5236 temp->last->next = NULL;
5237 unused = last;
5238 last = temp->last;
5239 gcc_assert (strcmp (last->label, label) != 0);
5240 }
5241 else
5242 {
5243 gcc_assert (temp->first == temp->last
5244 || (temp->first->next == temp->last
5245 && TREE_CODE (decl) == PARM_DECL));
5246 memset (temp->last, '\0', sizeof (*temp->last));
5247 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5248 return temp->last;
5249 }
5250 }
5251 if (bitsize == -1 && NOTE_P (last->loc))
5252 last_loc_note = last->loc;
5253 else if (piece_loc != NULL
5254 && *piece_loc != NULL_RTX
5255 && piece_bitpos == bitpos
5256 && decl_piece_bitsize (*piece_loc) == bitsize)
5257 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5258 else
5259 last_loc_note = NULL_RTX;
5260 /* If the current location is the same as the end of the list,
5261 and either both or neither of the locations is uninitialized,
5262 we have nothing to do. */
5263 if (last_loc_note == NULL_RTX
5264 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5265 NOTE_VAR_LOCATION_LOC (loc_note)))
5266 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5267 != NOTE_VAR_LOCATION_STATUS (loc_note))
5268 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5269 == VAR_INIT_STATUS_UNINITIALIZED)
5270 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5271 == VAR_INIT_STATUS_UNINITIALIZED))))
5272 {
5273 /* Add LOC to the end of list and update LAST. If the last
5274 element of the list has been removed above, reuse its
5275 memory for the new node, otherwise allocate a new one. */
5276 if (unused)
5277 {
5278 loc = unused;
5279 memset (loc, '\0', sizeof (*loc));
5280 }
5281 else
5282 loc = ggc_cleared_alloc<var_loc_node> ();
5283 if (bitsize == -1 || piece_loc == NULL)
5284 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5285 else
5286 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5287 bitpos, piece_bitpos, bitsize, loc_note);
5288 last->next = loc;
5289 /* Ensure TEMP->LAST will point either to the new last but one
5290 element of the chain, or to the last element in it. */
5291 if (last != temp->last)
5292 temp->last = last;
5293 }
5294 else if (unused)
5295 ggc_free (unused);
5296 }
5297 else
5298 {
5299 loc = ggc_cleared_alloc<var_loc_node> ();
5300 temp->first = loc;
5301 temp->last = loc;
5302 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5303 }
5304 return loc;
5305 }
5306 \f
5307 /* Keep track of the number of spaces used to indent the
5308 output of the debugging routines that print the structure of
5309 the DIE internal representation. */
5310 static int print_indent;
5311
5312 /* Indent the line the number of spaces given by print_indent. */
5313
5314 static inline void
5315 print_spaces (FILE *outfile)
5316 {
5317 fprintf (outfile, "%*s", print_indent, "");
5318 }
5319
5320 /* Print a type signature in hex. */
5321
5322 static inline void
5323 print_signature (FILE *outfile, char *sig)
5324 {
5325 int i;
5326
5327 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5328 fprintf (outfile, "%02x", sig[i] & 0xff);
5329 }
5330
5331 /* Print the information associated with a given DIE, and its children.
5332 This routine is a debugging aid only. */
5333
5334 static void
5335 print_die (dw_die_ref die, FILE *outfile)
5336 {
5337 dw_attr_ref a;
5338 dw_die_ref c;
5339 unsigned ix;
5340
5341 print_spaces (outfile);
5342 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5343 die->die_offset, dwarf_tag_name (die->die_tag),
5344 (void*) die);
5345 print_spaces (outfile);
5346 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5347 fprintf (outfile, " offset: %ld", die->die_offset);
5348 fprintf (outfile, " mark: %d\n", die->die_mark);
5349
5350 if (die->comdat_type_p)
5351 {
5352 print_spaces (outfile);
5353 fprintf (outfile, " signature: ");
5354 print_signature (outfile, die->die_id.die_type_node->signature);
5355 fprintf (outfile, "\n");
5356 }
5357
5358 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5359 {
5360 print_spaces (outfile);
5361 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5362
5363 switch (AT_class (a))
5364 {
5365 case dw_val_class_addr:
5366 fprintf (outfile, "address");
5367 break;
5368 case dw_val_class_offset:
5369 fprintf (outfile, "offset");
5370 break;
5371 case dw_val_class_loc:
5372 fprintf (outfile, "location descriptor");
5373 break;
5374 case dw_val_class_loc_list:
5375 fprintf (outfile, "location list -> label:%s",
5376 AT_loc_list (a)->ll_symbol);
5377 break;
5378 case dw_val_class_range_list:
5379 fprintf (outfile, "range list");
5380 break;
5381 case dw_val_class_const:
5382 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5383 break;
5384 case dw_val_class_unsigned_const:
5385 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5386 break;
5387 case dw_val_class_const_double:
5388 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5389 HOST_WIDE_INT_PRINT_UNSIGNED")",
5390 a->dw_attr_val.v.val_double.high,
5391 a->dw_attr_val.v.val_double.low);
5392 break;
5393 case dw_val_class_wide_int:
5394 {
5395 int i = a->dw_attr_val.v.val_wide->get_len ();
5396 fprintf (outfile, "constant (");
5397 gcc_assert (i > 0);
5398 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5399 fprintf (outfile, "0x");
5400 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5401 a->dw_attr_val.v.val_wide->elt (--i));
5402 while (--i >= 0)
5403 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5404 a->dw_attr_val.v.val_wide->elt (i));
5405 fprintf (outfile, ")");
5406 break;
5407 }
5408 case dw_val_class_vec:
5409 fprintf (outfile, "floating-point or vector constant");
5410 break;
5411 case dw_val_class_flag:
5412 fprintf (outfile, "%u", AT_flag (a));
5413 break;
5414 case dw_val_class_die_ref:
5415 if (AT_ref (a) != NULL)
5416 {
5417 if (AT_ref (a)->comdat_type_p)
5418 {
5419 fprintf (outfile, "die -> signature: ");
5420 print_signature (outfile,
5421 AT_ref (a)->die_id.die_type_node->signature);
5422 }
5423 else if (AT_ref (a)->die_id.die_symbol)
5424 fprintf (outfile, "die -> label: %s",
5425 AT_ref (a)->die_id.die_symbol);
5426 else
5427 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5428 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5429 }
5430 else
5431 fprintf (outfile, "die -> <null>");
5432 break;
5433 case dw_val_class_vms_delta:
5434 fprintf (outfile, "delta: @slotcount(%s-%s)",
5435 AT_vms_delta2 (a), AT_vms_delta1 (a));
5436 break;
5437 case dw_val_class_lbl_id:
5438 case dw_val_class_lineptr:
5439 case dw_val_class_macptr:
5440 case dw_val_class_high_pc:
5441 fprintf (outfile, "label: %s", AT_lbl (a));
5442 break;
5443 case dw_val_class_str:
5444 if (AT_string (a) != NULL)
5445 fprintf (outfile, "\"%s\"", AT_string (a));
5446 else
5447 fprintf (outfile, "<null>");
5448 break;
5449 case dw_val_class_file:
5450 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5451 AT_file (a)->emitted_number);
5452 break;
5453 case dw_val_class_data8:
5454 {
5455 int i;
5456
5457 for (i = 0; i < 8; i++)
5458 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5459 break;
5460 }
5461 default:
5462 break;
5463 }
5464
5465 fprintf (outfile, "\n");
5466 }
5467
5468 if (die->die_child != NULL)
5469 {
5470 print_indent += 4;
5471 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5472 print_indent -= 4;
5473 }
5474 if (print_indent == 0)
5475 fprintf (outfile, "\n");
5476 }
5477
5478 /* Print the information collected for a given DIE. */
5479
5480 DEBUG_FUNCTION void
5481 debug_dwarf_die (dw_die_ref die)
5482 {
5483 print_die (die, stderr);
5484 }
5485
5486 DEBUG_FUNCTION void
5487 debug (die_struct &ref)
5488 {
5489 print_die (&ref, stderr);
5490 }
5491
5492 DEBUG_FUNCTION void
5493 debug (die_struct *ptr)
5494 {
5495 if (ptr)
5496 debug (*ptr);
5497 else
5498 fprintf (stderr, "<nil>\n");
5499 }
5500
5501
5502 /* Print all DWARF information collected for the compilation unit.
5503 This routine is a debugging aid only. */
5504
5505 DEBUG_FUNCTION void
5506 debug_dwarf (void)
5507 {
5508 print_indent = 0;
5509 print_die (comp_unit_die (), stderr);
5510 }
5511 \f
5512 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5513 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5514 DIE that marks the start of the DIEs for this include file. */
5515
5516 static dw_die_ref
5517 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5518 {
5519 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5520 dw_die_ref new_unit = gen_compile_unit_die (filename);
5521
5522 new_unit->die_sib = old_unit;
5523 return new_unit;
5524 }
5525
5526 /* Close an include-file CU and reopen the enclosing one. */
5527
5528 static dw_die_ref
5529 pop_compile_unit (dw_die_ref old_unit)
5530 {
5531 dw_die_ref new_unit = old_unit->die_sib;
5532
5533 old_unit->die_sib = NULL;
5534 return new_unit;
5535 }
5536
5537 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5538 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5539 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5540
5541 /* Calculate the checksum of a location expression. */
5542
5543 static inline void
5544 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5545 {
5546 int tem;
5547 hashval_t hash = 0;
5548
5549 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5550 CHECKSUM (tem);
5551 hash = hash_loc_operands (loc, hash);
5552 CHECKSUM (hash);
5553 }
5554
5555 /* Calculate the checksum of an attribute. */
5556
5557 static void
5558 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5559 {
5560 dw_loc_descr_ref loc;
5561 rtx r;
5562
5563 CHECKSUM (at->dw_attr);
5564
5565 /* We don't care that this was compiled with a different compiler
5566 snapshot; if the output is the same, that's what matters. */
5567 if (at->dw_attr == DW_AT_producer)
5568 return;
5569
5570 switch (AT_class (at))
5571 {
5572 case dw_val_class_const:
5573 CHECKSUM (at->dw_attr_val.v.val_int);
5574 break;
5575 case dw_val_class_unsigned_const:
5576 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5577 break;
5578 case dw_val_class_const_double:
5579 CHECKSUM (at->dw_attr_val.v.val_double);
5580 break;
5581 case dw_val_class_wide_int:
5582 CHECKSUM (*at->dw_attr_val.v.val_wide);
5583 break;
5584 case dw_val_class_vec:
5585 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5586 (at->dw_attr_val.v.val_vec.length
5587 * at->dw_attr_val.v.val_vec.elt_size));
5588 break;
5589 case dw_val_class_flag:
5590 CHECKSUM (at->dw_attr_val.v.val_flag);
5591 break;
5592 case dw_val_class_str:
5593 CHECKSUM_STRING (AT_string (at));
5594 break;
5595
5596 case dw_val_class_addr:
5597 r = AT_addr (at);
5598 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5599 CHECKSUM_STRING (XSTR (r, 0));
5600 break;
5601
5602 case dw_val_class_offset:
5603 CHECKSUM (at->dw_attr_val.v.val_offset);
5604 break;
5605
5606 case dw_val_class_loc:
5607 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5608 loc_checksum (loc, ctx);
5609 break;
5610
5611 case dw_val_class_die_ref:
5612 die_checksum (AT_ref (at), ctx, mark);
5613 break;
5614
5615 case dw_val_class_fde_ref:
5616 case dw_val_class_vms_delta:
5617 case dw_val_class_lbl_id:
5618 case dw_val_class_lineptr:
5619 case dw_val_class_macptr:
5620 case dw_val_class_high_pc:
5621 break;
5622
5623 case dw_val_class_file:
5624 CHECKSUM_STRING (AT_file (at)->filename);
5625 break;
5626
5627 case dw_val_class_data8:
5628 CHECKSUM (at->dw_attr_val.v.val_data8);
5629 break;
5630
5631 default:
5632 break;
5633 }
5634 }
5635
5636 /* Calculate the checksum of a DIE. */
5637
5638 static void
5639 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5640 {
5641 dw_die_ref c;
5642 dw_attr_ref a;
5643 unsigned ix;
5644
5645 /* To avoid infinite recursion. */
5646 if (die->die_mark)
5647 {
5648 CHECKSUM (die->die_mark);
5649 return;
5650 }
5651 die->die_mark = ++(*mark);
5652
5653 CHECKSUM (die->die_tag);
5654
5655 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5656 attr_checksum (a, ctx, mark);
5657
5658 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5659 }
5660
5661 #undef CHECKSUM
5662 #undef CHECKSUM_BLOCK
5663 #undef CHECKSUM_STRING
5664
5665 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5666 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5667 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5668 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5669 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5670 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5671 #define CHECKSUM_ATTR(FOO) \
5672 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5673
5674 /* Calculate the checksum of a number in signed LEB128 format. */
5675
5676 static void
5677 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5678 {
5679 unsigned char byte;
5680 bool more;
5681
5682 while (1)
5683 {
5684 byte = (value & 0x7f);
5685 value >>= 7;
5686 more = !((value == 0 && (byte & 0x40) == 0)
5687 || (value == -1 && (byte & 0x40) != 0));
5688 if (more)
5689 byte |= 0x80;
5690 CHECKSUM (byte);
5691 if (!more)
5692 break;
5693 }
5694 }
5695
5696 /* Calculate the checksum of a number in unsigned LEB128 format. */
5697
5698 static void
5699 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5700 {
5701 while (1)
5702 {
5703 unsigned char byte = (value & 0x7f);
5704 value >>= 7;
5705 if (value != 0)
5706 /* More bytes to follow. */
5707 byte |= 0x80;
5708 CHECKSUM (byte);
5709 if (value == 0)
5710 break;
5711 }
5712 }
5713
5714 /* Checksum the context of the DIE. This adds the names of any
5715 surrounding namespaces or structures to the checksum. */
5716
5717 static void
5718 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5719 {
5720 const char *name;
5721 dw_die_ref spec;
5722 int tag = die->die_tag;
5723
5724 if (tag != DW_TAG_namespace
5725 && tag != DW_TAG_structure_type
5726 && tag != DW_TAG_class_type)
5727 return;
5728
5729 name = get_AT_string (die, DW_AT_name);
5730
5731 spec = get_AT_ref (die, DW_AT_specification);
5732 if (spec != NULL)
5733 die = spec;
5734
5735 if (die->die_parent != NULL)
5736 checksum_die_context (die->die_parent, ctx);
5737
5738 CHECKSUM_ULEB128 ('C');
5739 CHECKSUM_ULEB128 (tag);
5740 if (name != NULL)
5741 CHECKSUM_STRING (name);
5742 }
5743
5744 /* Calculate the checksum of a location expression. */
5745
5746 static inline void
5747 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5748 {
5749 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5750 were emitted as a DW_FORM_sdata instead of a location expression. */
5751 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5752 {
5753 CHECKSUM_ULEB128 (DW_FORM_sdata);
5754 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5755 return;
5756 }
5757
5758 /* Otherwise, just checksum the raw location expression. */
5759 while (loc != NULL)
5760 {
5761 hashval_t hash = 0;
5762
5763 CHECKSUM_ULEB128 (loc->dtprel);
5764 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5765 hash = hash_loc_operands (loc, hash);
5766 CHECKSUM (hash);
5767 loc = loc->dw_loc_next;
5768 }
5769 }
5770
5771 /* Calculate the checksum of an attribute. */
5772
5773 static void
5774 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5775 struct md5_ctx *ctx, int *mark)
5776 {
5777 dw_loc_descr_ref loc;
5778 rtx r;
5779
5780 if (AT_class (at) == dw_val_class_die_ref)
5781 {
5782 dw_die_ref target_die = AT_ref (at);
5783
5784 /* For pointer and reference types, we checksum only the (qualified)
5785 name of the target type (if there is a name). For friend entries,
5786 we checksum only the (qualified) name of the target type or function.
5787 This allows the checksum to remain the same whether the target type
5788 is complete or not. */
5789 if ((at->dw_attr == DW_AT_type
5790 && (tag == DW_TAG_pointer_type
5791 || tag == DW_TAG_reference_type
5792 || tag == DW_TAG_rvalue_reference_type
5793 || tag == DW_TAG_ptr_to_member_type))
5794 || (at->dw_attr == DW_AT_friend
5795 && tag == DW_TAG_friend))
5796 {
5797 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5798
5799 if (name_attr != NULL)
5800 {
5801 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5802
5803 if (decl == NULL)
5804 decl = target_die;
5805 CHECKSUM_ULEB128 ('N');
5806 CHECKSUM_ULEB128 (at->dw_attr);
5807 if (decl->die_parent != NULL)
5808 checksum_die_context (decl->die_parent, ctx);
5809 CHECKSUM_ULEB128 ('E');
5810 CHECKSUM_STRING (AT_string (name_attr));
5811 return;
5812 }
5813 }
5814
5815 /* For all other references to another DIE, we check to see if the
5816 target DIE has already been visited. If it has, we emit a
5817 backward reference; if not, we descend recursively. */
5818 if (target_die->die_mark > 0)
5819 {
5820 CHECKSUM_ULEB128 ('R');
5821 CHECKSUM_ULEB128 (at->dw_attr);
5822 CHECKSUM_ULEB128 (target_die->die_mark);
5823 }
5824 else
5825 {
5826 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5827
5828 if (decl == NULL)
5829 decl = target_die;
5830 target_die->die_mark = ++(*mark);
5831 CHECKSUM_ULEB128 ('T');
5832 CHECKSUM_ULEB128 (at->dw_attr);
5833 if (decl->die_parent != NULL)
5834 checksum_die_context (decl->die_parent, ctx);
5835 die_checksum_ordered (target_die, ctx, mark);
5836 }
5837 return;
5838 }
5839
5840 CHECKSUM_ULEB128 ('A');
5841 CHECKSUM_ULEB128 (at->dw_attr);
5842
5843 switch (AT_class (at))
5844 {
5845 case dw_val_class_const:
5846 CHECKSUM_ULEB128 (DW_FORM_sdata);
5847 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5848 break;
5849
5850 case dw_val_class_unsigned_const:
5851 CHECKSUM_ULEB128 (DW_FORM_sdata);
5852 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5853 break;
5854
5855 case dw_val_class_const_double:
5856 CHECKSUM_ULEB128 (DW_FORM_block);
5857 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5858 CHECKSUM (at->dw_attr_val.v.val_double);
5859 break;
5860
5861 case dw_val_class_wide_int:
5862 CHECKSUM_ULEB128 (DW_FORM_block);
5863 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5864 CHECKSUM (*at->dw_attr_val.v.val_wide);
5865 break;
5866
5867 case dw_val_class_vec:
5868 CHECKSUM_ULEB128 (DW_FORM_block);
5869 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5870 * at->dw_attr_val.v.val_vec.elt_size);
5871 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5872 (at->dw_attr_val.v.val_vec.length
5873 * at->dw_attr_val.v.val_vec.elt_size));
5874 break;
5875
5876 case dw_val_class_flag:
5877 CHECKSUM_ULEB128 (DW_FORM_flag);
5878 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5879 break;
5880
5881 case dw_val_class_str:
5882 CHECKSUM_ULEB128 (DW_FORM_string);
5883 CHECKSUM_STRING (AT_string (at));
5884 break;
5885
5886 case dw_val_class_addr:
5887 r = AT_addr (at);
5888 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5889 CHECKSUM_ULEB128 (DW_FORM_string);
5890 CHECKSUM_STRING (XSTR (r, 0));
5891 break;
5892
5893 case dw_val_class_offset:
5894 CHECKSUM_ULEB128 (DW_FORM_sdata);
5895 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5896 break;
5897
5898 case dw_val_class_loc:
5899 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5900 loc_checksum_ordered (loc, ctx);
5901 break;
5902
5903 case dw_val_class_fde_ref:
5904 case dw_val_class_lbl_id:
5905 case dw_val_class_lineptr:
5906 case dw_val_class_macptr:
5907 case dw_val_class_high_pc:
5908 break;
5909
5910 case dw_val_class_file:
5911 CHECKSUM_ULEB128 (DW_FORM_string);
5912 CHECKSUM_STRING (AT_file (at)->filename);
5913 break;
5914
5915 case dw_val_class_data8:
5916 CHECKSUM (at->dw_attr_val.v.val_data8);
5917 break;
5918
5919 default:
5920 break;
5921 }
5922 }
5923
5924 struct checksum_attributes
5925 {
5926 dw_attr_ref at_name;
5927 dw_attr_ref at_type;
5928 dw_attr_ref at_friend;
5929 dw_attr_ref at_accessibility;
5930 dw_attr_ref at_address_class;
5931 dw_attr_ref at_allocated;
5932 dw_attr_ref at_artificial;
5933 dw_attr_ref at_associated;
5934 dw_attr_ref at_binary_scale;
5935 dw_attr_ref at_bit_offset;
5936 dw_attr_ref at_bit_size;
5937 dw_attr_ref at_bit_stride;
5938 dw_attr_ref at_byte_size;
5939 dw_attr_ref at_byte_stride;
5940 dw_attr_ref at_const_value;
5941 dw_attr_ref at_containing_type;
5942 dw_attr_ref at_count;
5943 dw_attr_ref at_data_location;
5944 dw_attr_ref at_data_member_location;
5945 dw_attr_ref at_decimal_scale;
5946 dw_attr_ref at_decimal_sign;
5947 dw_attr_ref at_default_value;
5948 dw_attr_ref at_digit_count;
5949 dw_attr_ref at_discr;
5950 dw_attr_ref at_discr_list;
5951 dw_attr_ref at_discr_value;
5952 dw_attr_ref at_encoding;
5953 dw_attr_ref at_endianity;
5954 dw_attr_ref at_explicit;
5955 dw_attr_ref at_is_optional;
5956 dw_attr_ref at_location;
5957 dw_attr_ref at_lower_bound;
5958 dw_attr_ref at_mutable;
5959 dw_attr_ref at_ordering;
5960 dw_attr_ref at_picture_string;
5961 dw_attr_ref at_prototyped;
5962 dw_attr_ref at_small;
5963 dw_attr_ref at_segment;
5964 dw_attr_ref at_string_length;
5965 dw_attr_ref at_threads_scaled;
5966 dw_attr_ref at_upper_bound;
5967 dw_attr_ref at_use_location;
5968 dw_attr_ref at_use_UTF8;
5969 dw_attr_ref at_variable_parameter;
5970 dw_attr_ref at_virtuality;
5971 dw_attr_ref at_visibility;
5972 dw_attr_ref at_vtable_elem_location;
5973 };
5974
5975 /* Collect the attributes that we will want to use for the checksum. */
5976
5977 static void
5978 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5979 {
5980 dw_attr_ref a;
5981 unsigned ix;
5982
5983 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5984 {
5985 switch (a->dw_attr)
5986 {
5987 case DW_AT_name:
5988 attrs->at_name = a;
5989 break;
5990 case DW_AT_type:
5991 attrs->at_type = a;
5992 break;
5993 case DW_AT_friend:
5994 attrs->at_friend = a;
5995 break;
5996 case DW_AT_accessibility:
5997 attrs->at_accessibility = a;
5998 break;
5999 case DW_AT_address_class:
6000 attrs->at_address_class = a;
6001 break;
6002 case DW_AT_allocated:
6003 attrs->at_allocated = a;
6004 break;
6005 case DW_AT_artificial:
6006 attrs->at_artificial = a;
6007 break;
6008 case DW_AT_associated:
6009 attrs->at_associated = a;
6010 break;
6011 case DW_AT_binary_scale:
6012 attrs->at_binary_scale = a;
6013 break;
6014 case DW_AT_bit_offset:
6015 attrs->at_bit_offset = a;
6016 break;
6017 case DW_AT_bit_size:
6018 attrs->at_bit_size = a;
6019 break;
6020 case DW_AT_bit_stride:
6021 attrs->at_bit_stride = a;
6022 break;
6023 case DW_AT_byte_size:
6024 attrs->at_byte_size = a;
6025 break;
6026 case DW_AT_byte_stride:
6027 attrs->at_byte_stride = a;
6028 break;
6029 case DW_AT_const_value:
6030 attrs->at_const_value = a;
6031 break;
6032 case DW_AT_containing_type:
6033 attrs->at_containing_type = a;
6034 break;
6035 case DW_AT_count:
6036 attrs->at_count = a;
6037 break;
6038 case DW_AT_data_location:
6039 attrs->at_data_location = a;
6040 break;
6041 case DW_AT_data_member_location:
6042 attrs->at_data_member_location = a;
6043 break;
6044 case DW_AT_decimal_scale:
6045 attrs->at_decimal_scale = a;
6046 break;
6047 case DW_AT_decimal_sign:
6048 attrs->at_decimal_sign = a;
6049 break;
6050 case DW_AT_default_value:
6051 attrs->at_default_value = a;
6052 break;
6053 case DW_AT_digit_count:
6054 attrs->at_digit_count = a;
6055 break;
6056 case DW_AT_discr:
6057 attrs->at_discr = a;
6058 break;
6059 case DW_AT_discr_list:
6060 attrs->at_discr_list = a;
6061 break;
6062 case DW_AT_discr_value:
6063 attrs->at_discr_value = a;
6064 break;
6065 case DW_AT_encoding:
6066 attrs->at_encoding = a;
6067 break;
6068 case DW_AT_endianity:
6069 attrs->at_endianity = a;
6070 break;
6071 case DW_AT_explicit:
6072 attrs->at_explicit = a;
6073 break;
6074 case DW_AT_is_optional:
6075 attrs->at_is_optional = a;
6076 break;
6077 case DW_AT_location:
6078 attrs->at_location = a;
6079 break;
6080 case DW_AT_lower_bound:
6081 attrs->at_lower_bound = a;
6082 break;
6083 case DW_AT_mutable:
6084 attrs->at_mutable = a;
6085 break;
6086 case DW_AT_ordering:
6087 attrs->at_ordering = a;
6088 break;
6089 case DW_AT_picture_string:
6090 attrs->at_picture_string = a;
6091 break;
6092 case DW_AT_prototyped:
6093 attrs->at_prototyped = a;
6094 break;
6095 case DW_AT_small:
6096 attrs->at_small = a;
6097 break;
6098 case DW_AT_segment:
6099 attrs->at_segment = a;
6100 break;
6101 case DW_AT_string_length:
6102 attrs->at_string_length = a;
6103 break;
6104 case DW_AT_threads_scaled:
6105 attrs->at_threads_scaled = a;
6106 break;
6107 case DW_AT_upper_bound:
6108 attrs->at_upper_bound = a;
6109 break;
6110 case DW_AT_use_location:
6111 attrs->at_use_location = a;
6112 break;
6113 case DW_AT_use_UTF8:
6114 attrs->at_use_UTF8 = a;
6115 break;
6116 case DW_AT_variable_parameter:
6117 attrs->at_variable_parameter = a;
6118 break;
6119 case DW_AT_virtuality:
6120 attrs->at_virtuality = a;
6121 break;
6122 case DW_AT_visibility:
6123 attrs->at_visibility = a;
6124 break;
6125 case DW_AT_vtable_elem_location:
6126 attrs->at_vtable_elem_location = a;
6127 break;
6128 default:
6129 break;
6130 }
6131 }
6132 }
6133
6134 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6135
6136 static void
6137 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6138 {
6139 dw_die_ref c;
6140 dw_die_ref decl;
6141 struct checksum_attributes attrs;
6142
6143 CHECKSUM_ULEB128 ('D');
6144 CHECKSUM_ULEB128 (die->die_tag);
6145
6146 memset (&attrs, 0, sizeof (attrs));
6147
6148 decl = get_AT_ref (die, DW_AT_specification);
6149 if (decl != NULL)
6150 collect_checksum_attributes (&attrs, decl);
6151 collect_checksum_attributes (&attrs, die);
6152
6153 CHECKSUM_ATTR (attrs.at_name);
6154 CHECKSUM_ATTR (attrs.at_accessibility);
6155 CHECKSUM_ATTR (attrs.at_address_class);
6156 CHECKSUM_ATTR (attrs.at_allocated);
6157 CHECKSUM_ATTR (attrs.at_artificial);
6158 CHECKSUM_ATTR (attrs.at_associated);
6159 CHECKSUM_ATTR (attrs.at_binary_scale);
6160 CHECKSUM_ATTR (attrs.at_bit_offset);
6161 CHECKSUM_ATTR (attrs.at_bit_size);
6162 CHECKSUM_ATTR (attrs.at_bit_stride);
6163 CHECKSUM_ATTR (attrs.at_byte_size);
6164 CHECKSUM_ATTR (attrs.at_byte_stride);
6165 CHECKSUM_ATTR (attrs.at_const_value);
6166 CHECKSUM_ATTR (attrs.at_containing_type);
6167 CHECKSUM_ATTR (attrs.at_count);
6168 CHECKSUM_ATTR (attrs.at_data_location);
6169 CHECKSUM_ATTR (attrs.at_data_member_location);
6170 CHECKSUM_ATTR (attrs.at_decimal_scale);
6171 CHECKSUM_ATTR (attrs.at_decimal_sign);
6172 CHECKSUM_ATTR (attrs.at_default_value);
6173 CHECKSUM_ATTR (attrs.at_digit_count);
6174 CHECKSUM_ATTR (attrs.at_discr);
6175 CHECKSUM_ATTR (attrs.at_discr_list);
6176 CHECKSUM_ATTR (attrs.at_discr_value);
6177 CHECKSUM_ATTR (attrs.at_encoding);
6178 CHECKSUM_ATTR (attrs.at_endianity);
6179 CHECKSUM_ATTR (attrs.at_explicit);
6180 CHECKSUM_ATTR (attrs.at_is_optional);
6181 CHECKSUM_ATTR (attrs.at_location);
6182 CHECKSUM_ATTR (attrs.at_lower_bound);
6183 CHECKSUM_ATTR (attrs.at_mutable);
6184 CHECKSUM_ATTR (attrs.at_ordering);
6185 CHECKSUM_ATTR (attrs.at_picture_string);
6186 CHECKSUM_ATTR (attrs.at_prototyped);
6187 CHECKSUM_ATTR (attrs.at_small);
6188 CHECKSUM_ATTR (attrs.at_segment);
6189 CHECKSUM_ATTR (attrs.at_string_length);
6190 CHECKSUM_ATTR (attrs.at_threads_scaled);
6191 CHECKSUM_ATTR (attrs.at_upper_bound);
6192 CHECKSUM_ATTR (attrs.at_use_location);
6193 CHECKSUM_ATTR (attrs.at_use_UTF8);
6194 CHECKSUM_ATTR (attrs.at_variable_parameter);
6195 CHECKSUM_ATTR (attrs.at_virtuality);
6196 CHECKSUM_ATTR (attrs.at_visibility);
6197 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6198 CHECKSUM_ATTR (attrs.at_type);
6199 CHECKSUM_ATTR (attrs.at_friend);
6200
6201 /* Checksum the child DIEs. */
6202 c = die->die_child;
6203 if (c) do {
6204 dw_attr_ref name_attr;
6205
6206 c = c->die_sib;
6207 name_attr = get_AT (c, DW_AT_name);
6208 if (is_template_instantiation (c))
6209 {
6210 /* Ignore instantiations of member type and function templates. */
6211 }
6212 else if (name_attr != NULL
6213 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6214 {
6215 /* Use a shallow checksum for named nested types and member
6216 functions. */
6217 CHECKSUM_ULEB128 ('S');
6218 CHECKSUM_ULEB128 (c->die_tag);
6219 CHECKSUM_STRING (AT_string (name_attr));
6220 }
6221 else
6222 {
6223 /* Use a deep checksum for other children. */
6224 /* Mark this DIE so it gets processed when unmarking. */
6225 if (c->die_mark == 0)
6226 c->die_mark = -1;
6227 die_checksum_ordered (c, ctx, mark);
6228 }
6229 } while (c != die->die_child);
6230
6231 CHECKSUM_ULEB128 (0);
6232 }
6233
6234 /* Add a type name and tag to a hash. */
6235 static void
6236 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6237 {
6238 CHECKSUM_ULEB128 (tag);
6239 CHECKSUM_STRING (name);
6240 }
6241
6242 #undef CHECKSUM
6243 #undef CHECKSUM_STRING
6244 #undef CHECKSUM_ATTR
6245 #undef CHECKSUM_LEB128
6246 #undef CHECKSUM_ULEB128
6247
6248 /* Generate the type signature for DIE. This is computed by generating an
6249 MD5 checksum over the DIE's tag, its relevant attributes, and its
6250 children. Attributes that are references to other DIEs are processed
6251 by recursion, using the MARK field to prevent infinite recursion.
6252 If the DIE is nested inside a namespace or another type, we also
6253 need to include that context in the signature. The lower 64 bits
6254 of the resulting MD5 checksum comprise the signature. */
6255
6256 static void
6257 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6258 {
6259 int mark;
6260 const char *name;
6261 unsigned char checksum[16];
6262 struct md5_ctx ctx;
6263 dw_die_ref decl;
6264 dw_die_ref parent;
6265
6266 name = get_AT_string (die, DW_AT_name);
6267 decl = get_AT_ref (die, DW_AT_specification);
6268 parent = get_die_parent (die);
6269
6270 /* First, compute a signature for just the type name (and its surrounding
6271 context, if any. This is stored in the type unit DIE for link-time
6272 ODR (one-definition rule) checking. */
6273
6274 if (is_cxx () && name != NULL)
6275 {
6276 md5_init_ctx (&ctx);
6277
6278 /* Checksum the names of surrounding namespaces and structures. */
6279 if (parent != NULL)
6280 checksum_die_context (parent, &ctx);
6281
6282 /* Checksum the current DIE. */
6283 die_odr_checksum (die->die_tag, name, &ctx);
6284 md5_finish_ctx (&ctx, checksum);
6285
6286 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6287 }
6288
6289 /* Next, compute the complete type signature. */
6290
6291 md5_init_ctx (&ctx);
6292 mark = 1;
6293 die->die_mark = mark;
6294
6295 /* Checksum the names of surrounding namespaces and structures. */
6296 if (parent != NULL)
6297 checksum_die_context (parent, &ctx);
6298
6299 /* Checksum the DIE and its children. */
6300 die_checksum_ordered (die, &ctx, &mark);
6301 unmark_all_dies (die);
6302 md5_finish_ctx (&ctx, checksum);
6303
6304 /* Store the signature in the type node and link the type DIE and the
6305 type node together. */
6306 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6307 DWARF_TYPE_SIGNATURE_SIZE);
6308 die->comdat_type_p = true;
6309 die->die_id.die_type_node = type_node;
6310 type_node->type_die = die;
6311
6312 /* If the DIE is a specification, link its declaration to the type node
6313 as well. */
6314 if (decl != NULL)
6315 {
6316 decl->comdat_type_p = true;
6317 decl->die_id.die_type_node = type_node;
6318 }
6319 }
6320
6321 /* Do the location expressions look same? */
6322 static inline int
6323 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6324 {
6325 return loc1->dw_loc_opc == loc2->dw_loc_opc
6326 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6327 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6328 }
6329
6330 /* Do the values look the same? */
6331 static int
6332 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6333 {
6334 dw_loc_descr_ref loc1, loc2;
6335 rtx r1, r2;
6336
6337 if (v1->val_class != v2->val_class)
6338 return 0;
6339
6340 switch (v1->val_class)
6341 {
6342 case dw_val_class_const:
6343 return v1->v.val_int == v2->v.val_int;
6344 case dw_val_class_unsigned_const:
6345 return v1->v.val_unsigned == v2->v.val_unsigned;
6346 case dw_val_class_const_double:
6347 return v1->v.val_double.high == v2->v.val_double.high
6348 && v1->v.val_double.low == v2->v.val_double.low;
6349 case dw_val_class_wide_int:
6350 return *v1->v.val_wide == *v2->v.val_wide;
6351 case dw_val_class_vec:
6352 if (v1->v.val_vec.length != v2->v.val_vec.length
6353 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6354 return 0;
6355 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6356 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6357 return 0;
6358 return 1;
6359 case dw_val_class_flag:
6360 return v1->v.val_flag == v2->v.val_flag;
6361 case dw_val_class_str:
6362 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6363
6364 case dw_val_class_addr:
6365 r1 = v1->v.val_addr;
6366 r2 = v2->v.val_addr;
6367 if (GET_CODE (r1) != GET_CODE (r2))
6368 return 0;
6369 return !rtx_equal_p (r1, r2);
6370
6371 case dw_val_class_offset:
6372 return v1->v.val_offset == v2->v.val_offset;
6373
6374 case dw_val_class_loc:
6375 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6376 loc1 && loc2;
6377 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6378 if (!same_loc_p (loc1, loc2, mark))
6379 return 0;
6380 return !loc1 && !loc2;
6381
6382 case dw_val_class_die_ref:
6383 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6384
6385 case dw_val_class_fde_ref:
6386 case dw_val_class_vms_delta:
6387 case dw_val_class_lbl_id:
6388 case dw_val_class_lineptr:
6389 case dw_val_class_macptr:
6390 case dw_val_class_high_pc:
6391 return 1;
6392
6393 case dw_val_class_file:
6394 return v1->v.val_file == v2->v.val_file;
6395
6396 case dw_val_class_data8:
6397 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6398
6399 default:
6400 return 1;
6401 }
6402 }
6403
6404 /* Do the attributes look the same? */
6405
6406 static int
6407 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6408 {
6409 if (at1->dw_attr != at2->dw_attr)
6410 return 0;
6411
6412 /* We don't care that this was compiled with a different compiler
6413 snapshot; if the output is the same, that's what matters. */
6414 if (at1->dw_attr == DW_AT_producer)
6415 return 1;
6416
6417 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6418 }
6419
6420 /* Do the dies look the same? */
6421
6422 static int
6423 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6424 {
6425 dw_die_ref c1, c2;
6426 dw_attr_ref a1;
6427 unsigned ix;
6428
6429 /* To avoid infinite recursion. */
6430 if (die1->die_mark)
6431 return die1->die_mark == die2->die_mark;
6432 die1->die_mark = die2->die_mark = ++(*mark);
6433
6434 if (die1->die_tag != die2->die_tag)
6435 return 0;
6436
6437 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6438 return 0;
6439
6440 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6441 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6442 return 0;
6443
6444 c1 = die1->die_child;
6445 c2 = die2->die_child;
6446 if (! c1)
6447 {
6448 if (c2)
6449 return 0;
6450 }
6451 else
6452 for (;;)
6453 {
6454 if (!same_die_p (c1, c2, mark))
6455 return 0;
6456 c1 = c1->die_sib;
6457 c2 = c2->die_sib;
6458 if (c1 == die1->die_child)
6459 {
6460 if (c2 == die2->die_child)
6461 break;
6462 else
6463 return 0;
6464 }
6465 }
6466
6467 return 1;
6468 }
6469
6470 /* Do the dies look the same? Wrapper around same_die_p. */
6471
6472 static int
6473 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6474 {
6475 int mark = 0;
6476 int ret = same_die_p (die1, die2, &mark);
6477
6478 unmark_all_dies (die1);
6479 unmark_all_dies (die2);
6480
6481 return ret;
6482 }
6483
6484 /* The prefix to attach to symbols on DIEs in the current comdat debug
6485 info section. */
6486 static const char *comdat_symbol_id;
6487
6488 /* The index of the current symbol within the current comdat CU. */
6489 static unsigned int comdat_symbol_number;
6490
6491 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6492 children, and set comdat_symbol_id accordingly. */
6493
6494 static void
6495 compute_section_prefix (dw_die_ref unit_die)
6496 {
6497 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6498 const char *base = die_name ? lbasename (die_name) : "anonymous";
6499 char *name = XALLOCAVEC (char, strlen (base) + 64);
6500 char *p;
6501 int i, mark;
6502 unsigned char checksum[16];
6503 struct md5_ctx ctx;
6504
6505 /* Compute the checksum of the DIE, then append part of it as hex digits to
6506 the name filename of the unit. */
6507
6508 md5_init_ctx (&ctx);
6509 mark = 0;
6510 die_checksum (unit_die, &ctx, &mark);
6511 unmark_all_dies (unit_die);
6512 md5_finish_ctx (&ctx, checksum);
6513
6514 sprintf (name, "%s.", base);
6515 clean_symbol_name (name);
6516
6517 p = name + strlen (name);
6518 for (i = 0; i < 4; i++)
6519 {
6520 sprintf (p, "%.2x", checksum[i]);
6521 p += 2;
6522 }
6523
6524 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6525 comdat_symbol_number = 0;
6526 }
6527
6528 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6529
6530 static int
6531 is_type_die (dw_die_ref die)
6532 {
6533 switch (die->die_tag)
6534 {
6535 case DW_TAG_array_type:
6536 case DW_TAG_class_type:
6537 case DW_TAG_interface_type:
6538 case DW_TAG_enumeration_type:
6539 case DW_TAG_pointer_type:
6540 case DW_TAG_reference_type:
6541 case DW_TAG_rvalue_reference_type:
6542 case DW_TAG_string_type:
6543 case DW_TAG_structure_type:
6544 case DW_TAG_subroutine_type:
6545 case DW_TAG_union_type:
6546 case DW_TAG_ptr_to_member_type:
6547 case DW_TAG_set_type:
6548 case DW_TAG_subrange_type:
6549 case DW_TAG_base_type:
6550 case DW_TAG_const_type:
6551 case DW_TAG_file_type:
6552 case DW_TAG_packed_type:
6553 case DW_TAG_volatile_type:
6554 case DW_TAG_typedef:
6555 return 1;
6556 default:
6557 return 0;
6558 }
6559 }
6560
6561 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6562 Basically, we want to choose the bits that are likely to be shared between
6563 compilations (types) and leave out the bits that are specific to individual
6564 compilations (functions). */
6565
6566 static int
6567 is_comdat_die (dw_die_ref c)
6568 {
6569 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6570 we do for stabs. The advantage is a greater likelihood of sharing between
6571 objects that don't include headers in the same order (and therefore would
6572 put the base types in a different comdat). jason 8/28/00 */
6573
6574 if (c->die_tag == DW_TAG_base_type)
6575 return 0;
6576
6577 if (c->die_tag == DW_TAG_pointer_type
6578 || c->die_tag == DW_TAG_reference_type
6579 || c->die_tag == DW_TAG_rvalue_reference_type
6580 || c->die_tag == DW_TAG_const_type
6581 || c->die_tag == DW_TAG_volatile_type)
6582 {
6583 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6584
6585 return t ? is_comdat_die (t) : 0;
6586 }
6587
6588 return is_type_die (c);
6589 }
6590
6591 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6592 compilation unit. */
6593
6594 static int
6595 is_symbol_die (dw_die_ref c)
6596 {
6597 return (is_type_die (c)
6598 || is_declaration_die (c)
6599 || c->die_tag == DW_TAG_namespace
6600 || c->die_tag == DW_TAG_module);
6601 }
6602
6603 /* Returns true iff C is a compile-unit DIE. */
6604
6605 static inline bool
6606 is_cu_die (dw_die_ref c)
6607 {
6608 return c && c->die_tag == DW_TAG_compile_unit;
6609 }
6610
6611 /* Returns true iff C is a unit DIE of some sort. */
6612
6613 static inline bool
6614 is_unit_die (dw_die_ref c)
6615 {
6616 return c && (c->die_tag == DW_TAG_compile_unit
6617 || c->die_tag == DW_TAG_partial_unit
6618 || c->die_tag == DW_TAG_type_unit);
6619 }
6620
6621 /* Returns true iff C is a namespace DIE. */
6622
6623 static inline bool
6624 is_namespace_die (dw_die_ref c)
6625 {
6626 return c && c->die_tag == DW_TAG_namespace;
6627 }
6628
6629 /* Returns true iff C is a class or structure DIE. */
6630
6631 static inline bool
6632 is_class_die (dw_die_ref c)
6633 {
6634 return c && (c->die_tag == DW_TAG_class_type
6635 || c->die_tag == DW_TAG_structure_type);
6636 }
6637
6638 /* Return non-zero if this DIE is a template parameter. */
6639
6640 static inline bool
6641 is_template_parameter (dw_die_ref die)
6642 {
6643 switch (die->die_tag)
6644 {
6645 case DW_TAG_template_type_param:
6646 case DW_TAG_template_value_param:
6647 case DW_TAG_GNU_template_template_param:
6648 case DW_TAG_GNU_template_parameter_pack:
6649 return true;
6650 default:
6651 return false;
6652 }
6653 }
6654
6655 /* Return non-zero if this DIE represents a template instantiation. */
6656
6657 static inline bool
6658 is_template_instantiation (dw_die_ref die)
6659 {
6660 dw_die_ref c;
6661
6662 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6663 return false;
6664 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6665 return false;
6666 }
6667
6668 static char *
6669 gen_internal_sym (const char *prefix)
6670 {
6671 char buf[256];
6672
6673 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6674 return xstrdup (buf);
6675 }
6676
6677 /* Assign symbols to all worthy DIEs under DIE. */
6678
6679 static void
6680 assign_symbol_names (dw_die_ref die)
6681 {
6682 dw_die_ref c;
6683
6684 if (is_symbol_die (die) && !die->comdat_type_p)
6685 {
6686 if (comdat_symbol_id)
6687 {
6688 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6689
6690 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6691 comdat_symbol_id, comdat_symbol_number++);
6692 die->die_id.die_symbol = xstrdup (p);
6693 }
6694 else
6695 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6696 }
6697
6698 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6699 }
6700
6701 struct cu_hash_table_entry
6702 {
6703 dw_die_ref cu;
6704 unsigned min_comdat_num, max_comdat_num;
6705 struct cu_hash_table_entry *next;
6706 };
6707
6708 /* Helpers to manipulate hash table of CUs. */
6709
6710 struct cu_hash_table_entry_hasher
6711 {
6712 typedef cu_hash_table_entry value_type;
6713 typedef die_struct compare_type;
6714 static inline hashval_t hash (const value_type *);
6715 static inline bool equal (const value_type *, const compare_type *);
6716 static inline void remove (value_type *);
6717 };
6718
6719 inline hashval_t
6720 cu_hash_table_entry_hasher::hash (const value_type *entry)
6721 {
6722 return htab_hash_string (entry->cu->die_id.die_symbol);
6723 }
6724
6725 inline bool
6726 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6727 const compare_type *entry2)
6728 {
6729 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6730 }
6731
6732 inline void
6733 cu_hash_table_entry_hasher::remove (value_type *entry)
6734 {
6735 struct cu_hash_table_entry *next;
6736
6737 while (entry)
6738 {
6739 next = entry->next;
6740 free (entry);
6741 entry = next;
6742 }
6743 }
6744
6745 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6746
6747 /* Check whether we have already seen this CU and set up SYM_NUM
6748 accordingly. */
6749 static int
6750 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6751 {
6752 struct cu_hash_table_entry dummy;
6753 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6754
6755 dummy.max_comdat_num = 0;
6756
6757 slot = htable->find_slot_with_hash (cu,
6758 htab_hash_string (cu->die_id.die_symbol),
6759 INSERT);
6760 entry = *slot;
6761
6762 for (; entry; last = entry, entry = entry->next)
6763 {
6764 if (same_die_p_wrap (cu, entry->cu))
6765 break;
6766 }
6767
6768 if (entry)
6769 {
6770 *sym_num = entry->min_comdat_num;
6771 return 1;
6772 }
6773
6774 entry = XCNEW (struct cu_hash_table_entry);
6775 entry->cu = cu;
6776 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6777 entry->next = *slot;
6778 *slot = entry;
6779
6780 return 0;
6781 }
6782
6783 /* Record SYM_NUM to record of CU in HTABLE. */
6784 static void
6785 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6786 unsigned int sym_num)
6787 {
6788 struct cu_hash_table_entry **slot, *entry;
6789
6790 slot = htable->find_slot_with_hash (cu,
6791 htab_hash_string (cu->die_id.die_symbol),
6792 NO_INSERT);
6793 entry = *slot;
6794
6795 entry->max_comdat_num = sym_num;
6796 }
6797
6798 /* Traverse the DIE (which is always comp_unit_die), and set up
6799 additional compilation units for each of the include files we see
6800 bracketed by BINCL/EINCL. */
6801
6802 static void
6803 break_out_includes (dw_die_ref die)
6804 {
6805 dw_die_ref c;
6806 dw_die_ref unit = NULL;
6807 limbo_die_node *node, **pnode;
6808
6809 c = die->die_child;
6810 if (c) do {
6811 dw_die_ref prev = c;
6812 c = c->die_sib;
6813 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6814 || (unit && is_comdat_die (c)))
6815 {
6816 dw_die_ref next = c->die_sib;
6817
6818 /* This DIE is for a secondary CU; remove it from the main one. */
6819 remove_child_with_prev (c, prev);
6820
6821 if (c->die_tag == DW_TAG_GNU_BINCL)
6822 unit = push_new_compile_unit (unit, c);
6823 else if (c->die_tag == DW_TAG_GNU_EINCL)
6824 unit = pop_compile_unit (unit);
6825 else
6826 add_child_die (unit, c);
6827 c = next;
6828 if (c == die->die_child)
6829 break;
6830 }
6831 } while (c != die->die_child);
6832
6833 #if 0
6834 /* We can only use this in debugging, since the frontend doesn't check
6835 to make sure that we leave every include file we enter. */
6836 gcc_assert (!unit);
6837 #endif
6838
6839 assign_symbol_names (die);
6840 cu_hash_type cu_hash_table (10);
6841 for (node = limbo_die_list, pnode = &limbo_die_list;
6842 node;
6843 node = node->next)
6844 {
6845 int is_dupl;
6846
6847 compute_section_prefix (node->die);
6848 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6849 &comdat_symbol_number);
6850 assign_symbol_names (node->die);
6851 if (is_dupl)
6852 *pnode = node->next;
6853 else
6854 {
6855 pnode = &node->next;
6856 record_comdat_symbol_number (node->die, &cu_hash_table,
6857 comdat_symbol_number);
6858 }
6859 }
6860 }
6861
6862 /* Return non-zero if this DIE is a declaration. */
6863
6864 static int
6865 is_declaration_die (dw_die_ref die)
6866 {
6867 dw_attr_ref a;
6868 unsigned ix;
6869
6870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6871 if (a->dw_attr == DW_AT_declaration)
6872 return 1;
6873
6874 return 0;
6875 }
6876
6877 /* Return non-zero if this DIE is nested inside a subprogram. */
6878
6879 static int
6880 is_nested_in_subprogram (dw_die_ref die)
6881 {
6882 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6883
6884 if (decl == NULL)
6885 decl = die;
6886 return local_scope_p (decl);
6887 }
6888
6889 /* Return non-zero if this DIE contains a defining declaration of a
6890 subprogram. */
6891
6892 static int
6893 contains_subprogram_definition (dw_die_ref die)
6894 {
6895 dw_die_ref c;
6896
6897 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6898 return 1;
6899 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6900 return 0;
6901 }
6902
6903 /* Return non-zero if this is a type DIE that should be moved to a
6904 COMDAT .debug_types section. */
6905
6906 static int
6907 should_move_die_to_comdat (dw_die_ref die)
6908 {
6909 switch (die->die_tag)
6910 {
6911 case DW_TAG_class_type:
6912 case DW_TAG_structure_type:
6913 case DW_TAG_enumeration_type:
6914 case DW_TAG_union_type:
6915 /* Don't move declarations, inlined instances, types nested in a
6916 subprogram, or types that contain subprogram definitions. */
6917 if (is_declaration_die (die)
6918 || get_AT (die, DW_AT_abstract_origin)
6919 || is_nested_in_subprogram (die)
6920 || contains_subprogram_definition (die))
6921 return 0;
6922 return 1;
6923 case DW_TAG_array_type:
6924 case DW_TAG_interface_type:
6925 case DW_TAG_pointer_type:
6926 case DW_TAG_reference_type:
6927 case DW_TAG_rvalue_reference_type:
6928 case DW_TAG_string_type:
6929 case DW_TAG_subroutine_type:
6930 case DW_TAG_ptr_to_member_type:
6931 case DW_TAG_set_type:
6932 case DW_TAG_subrange_type:
6933 case DW_TAG_base_type:
6934 case DW_TAG_const_type:
6935 case DW_TAG_file_type:
6936 case DW_TAG_packed_type:
6937 case DW_TAG_volatile_type:
6938 case DW_TAG_typedef:
6939 default:
6940 return 0;
6941 }
6942 }
6943
6944 /* Make a clone of DIE. */
6945
6946 static dw_die_ref
6947 clone_die (dw_die_ref die)
6948 {
6949 dw_die_ref clone;
6950 dw_attr_ref a;
6951 unsigned ix;
6952
6953 clone = ggc_cleared_alloc<die_node> ();
6954 clone->die_tag = die->die_tag;
6955
6956 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6957 add_dwarf_attr (clone, a);
6958
6959 return clone;
6960 }
6961
6962 /* Make a clone of the tree rooted at DIE. */
6963
6964 static dw_die_ref
6965 clone_tree (dw_die_ref die)
6966 {
6967 dw_die_ref c;
6968 dw_die_ref clone = clone_die (die);
6969
6970 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6971
6972 return clone;
6973 }
6974
6975 /* Make a clone of DIE as a declaration. */
6976
6977 static dw_die_ref
6978 clone_as_declaration (dw_die_ref die)
6979 {
6980 dw_die_ref clone;
6981 dw_die_ref decl;
6982 dw_attr_ref a;
6983 unsigned ix;
6984
6985 /* If the DIE is already a declaration, just clone it. */
6986 if (is_declaration_die (die))
6987 return clone_die (die);
6988
6989 /* If the DIE is a specification, just clone its declaration DIE. */
6990 decl = get_AT_ref (die, DW_AT_specification);
6991 if (decl != NULL)
6992 {
6993 clone = clone_die (decl);
6994 if (die->comdat_type_p)
6995 add_AT_die_ref (clone, DW_AT_signature, die);
6996 return clone;
6997 }
6998
6999 clone = ggc_cleared_alloc<die_node> ();
7000 clone->die_tag = die->die_tag;
7001
7002 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7003 {
7004 /* We don't want to copy over all attributes.
7005 For example we don't want DW_AT_byte_size because otherwise we will no
7006 longer have a declaration and GDB will treat it as a definition. */
7007
7008 switch (a->dw_attr)
7009 {
7010 case DW_AT_abstract_origin:
7011 case DW_AT_artificial:
7012 case DW_AT_containing_type:
7013 case DW_AT_external:
7014 case DW_AT_name:
7015 case DW_AT_type:
7016 case DW_AT_virtuality:
7017 case DW_AT_linkage_name:
7018 case DW_AT_MIPS_linkage_name:
7019 add_dwarf_attr (clone, a);
7020 break;
7021 case DW_AT_byte_size:
7022 default:
7023 break;
7024 }
7025 }
7026
7027 if (die->comdat_type_p)
7028 add_AT_die_ref (clone, DW_AT_signature, die);
7029
7030 add_AT_flag (clone, DW_AT_declaration, 1);
7031 return clone;
7032 }
7033
7034
7035 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7036
7037 struct decl_table_entry
7038 {
7039 dw_die_ref orig;
7040 dw_die_ref copy;
7041 };
7042
7043 /* Helpers to manipulate hash table of copied declarations. */
7044
7045 /* Hashtable helpers. */
7046
7047 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7048 {
7049 typedef decl_table_entry value_type;
7050 typedef die_struct compare_type;
7051 static inline hashval_t hash (const value_type *);
7052 static inline bool equal (const value_type *, const compare_type *);
7053 };
7054
7055 inline hashval_t
7056 decl_table_entry_hasher::hash (const value_type *entry)
7057 {
7058 return htab_hash_pointer (entry->orig);
7059 }
7060
7061 inline bool
7062 decl_table_entry_hasher::equal (const value_type *entry1,
7063 const compare_type *entry2)
7064 {
7065 return entry1->orig == entry2;
7066 }
7067
7068 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7069
7070 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7071 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7072 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7073 to check if the ancestor has already been copied into UNIT. */
7074
7075 static dw_die_ref
7076 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7077 decl_hash_type *decl_table)
7078 {
7079 dw_die_ref parent = die->die_parent;
7080 dw_die_ref new_parent = unit;
7081 dw_die_ref copy;
7082 decl_table_entry **slot = NULL;
7083 struct decl_table_entry *entry = NULL;
7084
7085 if (decl_table)
7086 {
7087 /* Check if the entry has already been copied to UNIT. */
7088 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7089 INSERT);
7090 if (*slot != HTAB_EMPTY_ENTRY)
7091 {
7092 entry = *slot;
7093 return entry->copy;
7094 }
7095
7096 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7097 entry = XCNEW (struct decl_table_entry);
7098 entry->orig = die;
7099 entry->copy = NULL;
7100 *slot = entry;
7101 }
7102
7103 if (parent != NULL)
7104 {
7105 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7106 if (spec != NULL)
7107 parent = spec;
7108 if (!is_unit_die (parent))
7109 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7110 }
7111
7112 copy = clone_as_declaration (die);
7113 add_child_die (new_parent, copy);
7114
7115 if (decl_table)
7116 {
7117 /* Record the pointer to the copy. */
7118 entry->copy = copy;
7119 }
7120
7121 return copy;
7122 }
7123 /* Copy the declaration context to the new type unit DIE. This includes
7124 any surrounding namespace or type declarations. If the DIE has an
7125 AT_specification attribute, it also includes attributes and children
7126 attached to the specification, and returns a pointer to the original
7127 parent of the declaration DIE. Returns NULL otherwise. */
7128
7129 static dw_die_ref
7130 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7131 {
7132 dw_die_ref decl;
7133 dw_die_ref new_decl;
7134 dw_die_ref orig_parent = NULL;
7135
7136 decl = get_AT_ref (die, DW_AT_specification);
7137 if (decl == NULL)
7138 decl = die;
7139 else
7140 {
7141 unsigned ix;
7142 dw_die_ref c;
7143 dw_attr_ref a;
7144
7145 /* The original DIE will be changed to a declaration, and must
7146 be moved to be a child of the original declaration DIE. */
7147 orig_parent = decl->die_parent;
7148
7149 /* Copy the type node pointer from the new DIE to the original
7150 declaration DIE so we can forward references later. */
7151 decl->comdat_type_p = true;
7152 decl->die_id.die_type_node = die->die_id.die_type_node;
7153
7154 remove_AT (die, DW_AT_specification);
7155
7156 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7157 {
7158 if (a->dw_attr != DW_AT_name
7159 && a->dw_attr != DW_AT_declaration
7160 && a->dw_attr != DW_AT_external)
7161 add_dwarf_attr (die, a);
7162 }
7163
7164 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7165 }
7166
7167 if (decl->die_parent != NULL
7168 && !is_unit_die (decl->die_parent))
7169 {
7170 new_decl = copy_ancestor_tree (unit, decl, NULL);
7171 if (new_decl != NULL)
7172 {
7173 remove_AT (new_decl, DW_AT_signature);
7174 add_AT_specification (die, new_decl);
7175 }
7176 }
7177
7178 return orig_parent;
7179 }
7180
7181 /* Generate the skeleton ancestor tree for the given NODE, then clone
7182 the DIE and add the clone into the tree. */
7183
7184 static void
7185 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7186 {
7187 if (node->new_die != NULL)
7188 return;
7189
7190 node->new_die = clone_as_declaration (node->old_die);
7191
7192 if (node->parent != NULL)
7193 {
7194 generate_skeleton_ancestor_tree (node->parent);
7195 add_child_die (node->parent->new_die, node->new_die);
7196 }
7197 }
7198
7199 /* Generate a skeleton tree of DIEs containing any declarations that are
7200 found in the original tree. We traverse the tree looking for declaration
7201 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7202
7203 static void
7204 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7205 {
7206 skeleton_chain_node node;
7207 dw_die_ref c;
7208 dw_die_ref first;
7209 dw_die_ref prev = NULL;
7210 dw_die_ref next = NULL;
7211
7212 node.parent = parent;
7213
7214 first = c = parent->old_die->die_child;
7215 if (c)
7216 next = c->die_sib;
7217 if (c) do {
7218 if (prev == NULL || prev->die_sib == c)
7219 prev = c;
7220 c = next;
7221 next = (c == first ? NULL : c->die_sib);
7222 node.old_die = c;
7223 node.new_die = NULL;
7224 if (is_declaration_die (c))
7225 {
7226 if (is_template_instantiation (c))
7227 {
7228 /* Instantiated templates do not need to be cloned into the
7229 type unit. Just move the DIE and its children back to
7230 the skeleton tree (in the main CU). */
7231 remove_child_with_prev (c, prev);
7232 add_child_die (parent->new_die, c);
7233 c = prev;
7234 }
7235 else
7236 {
7237 /* Clone the existing DIE, move the original to the skeleton
7238 tree (which is in the main CU), and put the clone, with
7239 all the original's children, where the original came from
7240 (which is about to be moved to the type unit). */
7241 dw_die_ref clone = clone_die (c);
7242 move_all_children (c, clone);
7243
7244 /* If the original has a DW_AT_object_pointer attribute,
7245 it would now point to a child DIE just moved to the
7246 cloned tree, so we need to remove that attribute from
7247 the original. */
7248 remove_AT (c, DW_AT_object_pointer);
7249
7250 replace_child (c, clone, prev);
7251 generate_skeleton_ancestor_tree (parent);
7252 add_child_die (parent->new_die, c);
7253 node.new_die = c;
7254 c = clone;
7255 }
7256 }
7257 generate_skeleton_bottom_up (&node);
7258 } while (next != NULL);
7259 }
7260
7261 /* Wrapper function for generate_skeleton_bottom_up. */
7262
7263 static dw_die_ref
7264 generate_skeleton (dw_die_ref die)
7265 {
7266 skeleton_chain_node node;
7267
7268 node.old_die = die;
7269 node.new_die = NULL;
7270 node.parent = NULL;
7271
7272 /* If this type definition is nested inside another type,
7273 and is not an instantiation of a template, always leave
7274 at least a declaration in its place. */
7275 if (die->die_parent != NULL
7276 && is_type_die (die->die_parent)
7277 && !is_template_instantiation (die))
7278 node.new_die = clone_as_declaration (die);
7279
7280 generate_skeleton_bottom_up (&node);
7281 return node.new_die;
7282 }
7283
7284 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7285 declaration. The original DIE is moved to a new compile unit so that
7286 existing references to it follow it to the new location. If any of the
7287 original DIE's descendants is a declaration, we need to replace the
7288 original DIE with a skeleton tree and move the declarations back into the
7289 skeleton tree. */
7290
7291 static dw_die_ref
7292 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7293 dw_die_ref prev)
7294 {
7295 dw_die_ref skeleton, orig_parent;
7296
7297 /* Copy the declaration context to the type unit DIE. If the returned
7298 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7299 that DIE. */
7300 orig_parent = copy_declaration_context (unit, child);
7301
7302 skeleton = generate_skeleton (child);
7303 if (skeleton == NULL)
7304 remove_child_with_prev (child, prev);
7305 else
7306 {
7307 skeleton->comdat_type_p = true;
7308 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7309
7310 /* If the original DIE was a specification, we need to put
7311 the skeleton under the parent DIE of the declaration.
7312 This leaves the original declaration in the tree, but
7313 it will be pruned later since there are no longer any
7314 references to it. */
7315 if (orig_parent != NULL)
7316 {
7317 remove_child_with_prev (child, prev);
7318 add_child_die (orig_parent, skeleton);
7319 }
7320 else
7321 replace_child (child, skeleton, prev);
7322 }
7323
7324 return skeleton;
7325 }
7326
7327 /* Traverse the DIE and set up additional .debug_types sections for each
7328 type worthy of being placed in a COMDAT section. */
7329
7330 static void
7331 break_out_comdat_types (dw_die_ref die)
7332 {
7333 dw_die_ref c;
7334 dw_die_ref first;
7335 dw_die_ref prev = NULL;
7336 dw_die_ref next = NULL;
7337 dw_die_ref unit = NULL;
7338
7339 first = c = die->die_child;
7340 if (c)
7341 next = c->die_sib;
7342 if (c) do {
7343 if (prev == NULL || prev->die_sib == c)
7344 prev = c;
7345 c = next;
7346 next = (c == first ? NULL : c->die_sib);
7347 if (should_move_die_to_comdat (c))
7348 {
7349 dw_die_ref replacement;
7350 comdat_type_node_ref type_node;
7351
7352 /* Break out nested types into their own type units. */
7353 break_out_comdat_types (c);
7354
7355 /* Create a new type unit DIE as the root for the new tree, and
7356 add it to the list of comdat types. */
7357 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7358 add_AT_unsigned (unit, DW_AT_language,
7359 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7360 type_node = ggc_cleared_alloc<comdat_type_node> ();
7361 type_node->root_die = unit;
7362 type_node->next = comdat_type_list;
7363 comdat_type_list = type_node;
7364
7365 /* Generate the type signature. */
7366 generate_type_signature (c, type_node);
7367
7368 /* Copy the declaration context, attributes, and children of the
7369 declaration into the new type unit DIE, then remove this DIE
7370 from the main CU (or replace it with a skeleton if necessary). */
7371 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7372 type_node->skeleton_die = replacement;
7373
7374 /* Add the DIE to the new compunit. */
7375 add_child_die (unit, c);
7376
7377 if (replacement != NULL)
7378 c = replacement;
7379 }
7380 else if (c->die_tag == DW_TAG_namespace
7381 || c->die_tag == DW_TAG_class_type
7382 || c->die_tag == DW_TAG_structure_type
7383 || c->die_tag == DW_TAG_union_type)
7384 {
7385 /* Look for nested types that can be broken out. */
7386 break_out_comdat_types (c);
7387 }
7388 } while (next != NULL);
7389 }
7390
7391 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7392 Enter all the cloned children into the hash table decl_table. */
7393
7394 static dw_die_ref
7395 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7396 {
7397 dw_die_ref c;
7398 dw_die_ref clone;
7399 struct decl_table_entry *entry;
7400 decl_table_entry **slot;
7401
7402 if (die->die_tag == DW_TAG_subprogram)
7403 clone = clone_as_declaration (die);
7404 else
7405 clone = clone_die (die);
7406
7407 slot = decl_table->find_slot_with_hash (die,
7408 htab_hash_pointer (die), INSERT);
7409
7410 /* Assert that DIE isn't in the hash table yet. If it would be there
7411 before, the ancestors would be necessarily there as well, therefore
7412 clone_tree_partial wouldn't be called. */
7413 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7414
7415 entry = XCNEW (struct decl_table_entry);
7416 entry->orig = die;
7417 entry->copy = clone;
7418 *slot = entry;
7419
7420 if (die->die_tag != DW_TAG_subprogram)
7421 FOR_EACH_CHILD (die, c,
7422 add_child_die (clone, clone_tree_partial (c, decl_table)));
7423
7424 return clone;
7425 }
7426
7427 /* Walk the DIE and its children, looking for references to incomplete
7428 or trivial types that are unmarked (i.e., that are not in the current
7429 type_unit). */
7430
7431 static void
7432 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7433 {
7434 dw_die_ref c;
7435 dw_attr_ref a;
7436 unsigned ix;
7437
7438 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7439 {
7440 if (AT_class (a) == dw_val_class_die_ref)
7441 {
7442 dw_die_ref targ = AT_ref (a);
7443 decl_table_entry **slot;
7444 struct decl_table_entry *entry;
7445
7446 if (targ->die_mark != 0 || targ->comdat_type_p)
7447 continue;
7448
7449 slot = decl_table->find_slot_with_hash (targ,
7450 htab_hash_pointer (targ),
7451 INSERT);
7452
7453 if (*slot != HTAB_EMPTY_ENTRY)
7454 {
7455 /* TARG has already been copied, so we just need to
7456 modify the reference to point to the copy. */
7457 entry = *slot;
7458 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7459 }
7460 else
7461 {
7462 dw_die_ref parent = unit;
7463 dw_die_ref copy = clone_die (targ);
7464
7465 /* Record in DECL_TABLE that TARG has been copied.
7466 Need to do this now, before the recursive call,
7467 because DECL_TABLE may be expanded and SLOT
7468 would no longer be a valid pointer. */
7469 entry = XCNEW (struct decl_table_entry);
7470 entry->orig = targ;
7471 entry->copy = copy;
7472 *slot = entry;
7473
7474 /* If TARG is not a declaration DIE, we need to copy its
7475 children. */
7476 if (!is_declaration_die (targ))
7477 {
7478 FOR_EACH_CHILD (
7479 targ, c,
7480 add_child_die (copy,
7481 clone_tree_partial (c, decl_table)));
7482 }
7483
7484 /* Make sure the cloned tree is marked as part of the
7485 type unit. */
7486 mark_dies (copy);
7487
7488 /* If TARG has surrounding context, copy its ancestor tree
7489 into the new type unit. */
7490 if (targ->die_parent != NULL
7491 && !is_unit_die (targ->die_parent))
7492 parent = copy_ancestor_tree (unit, targ->die_parent,
7493 decl_table);
7494
7495 add_child_die (parent, copy);
7496 a->dw_attr_val.v.val_die_ref.die = copy;
7497
7498 /* Make sure the newly-copied DIE is walked. If it was
7499 installed in a previously-added context, it won't
7500 get visited otherwise. */
7501 if (parent != unit)
7502 {
7503 /* Find the highest point of the newly-added tree,
7504 mark each node along the way, and walk from there. */
7505 parent->die_mark = 1;
7506 while (parent->die_parent
7507 && parent->die_parent->die_mark == 0)
7508 {
7509 parent = parent->die_parent;
7510 parent->die_mark = 1;
7511 }
7512 copy_decls_walk (unit, parent, decl_table);
7513 }
7514 }
7515 }
7516 }
7517
7518 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7519 }
7520
7521 /* Copy declarations for "unworthy" types into the new comdat section.
7522 Incomplete types, modified types, and certain other types aren't broken
7523 out into comdat sections of their own, so they don't have a signature,
7524 and we need to copy the declaration into the same section so that we
7525 don't have an external reference. */
7526
7527 static void
7528 copy_decls_for_unworthy_types (dw_die_ref unit)
7529 {
7530 mark_dies (unit);
7531 decl_hash_type decl_table (10);
7532 copy_decls_walk (unit, unit, &decl_table);
7533 unmark_dies (unit);
7534 }
7535
7536 /* Traverse the DIE and add a sibling attribute if it may have the
7537 effect of speeding up access to siblings. To save some space,
7538 avoid generating sibling attributes for DIE's without children. */
7539
7540 static void
7541 add_sibling_attributes (dw_die_ref die)
7542 {
7543 dw_die_ref c;
7544
7545 if (! die->die_child)
7546 return;
7547
7548 if (die->die_parent && die != die->die_parent->die_child)
7549 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7550
7551 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7552 }
7553
7554 /* Output all location lists for the DIE and its children. */
7555
7556 static void
7557 output_location_lists (dw_die_ref die)
7558 {
7559 dw_die_ref c;
7560 dw_attr_ref a;
7561 unsigned ix;
7562
7563 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7564 if (AT_class (a) == dw_val_class_loc_list)
7565 output_loc_list (AT_loc_list (a));
7566
7567 FOR_EACH_CHILD (die, c, output_location_lists (c));
7568 }
7569
7570 /* We want to limit the number of external references, because they are
7571 larger than local references: a relocation takes multiple words, and
7572 even a sig8 reference is always eight bytes, whereas a local reference
7573 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7574 So if we encounter multiple external references to the same type DIE, we
7575 make a local typedef stub for it and redirect all references there.
7576
7577 This is the element of the hash table for keeping track of these
7578 references. */
7579
7580 struct external_ref
7581 {
7582 dw_die_ref type;
7583 dw_die_ref stub;
7584 unsigned n_refs;
7585 };
7586
7587 /* Hashtable helpers. */
7588
7589 struct external_ref_hasher : typed_free_remove <external_ref>
7590 {
7591 typedef external_ref value_type;
7592 typedef external_ref compare_type;
7593 static inline hashval_t hash (const value_type *);
7594 static inline bool equal (const value_type *, const compare_type *);
7595 };
7596
7597 inline hashval_t
7598 external_ref_hasher::hash (const value_type *r)
7599 {
7600 dw_die_ref die = r->type;
7601 hashval_t h = 0;
7602
7603 /* We can't use the address of the DIE for hashing, because
7604 that will make the order of the stub DIEs non-deterministic. */
7605 if (! die->comdat_type_p)
7606 /* We have a symbol; use it to compute a hash. */
7607 h = htab_hash_string (die->die_id.die_symbol);
7608 else
7609 {
7610 /* We have a type signature; use a subset of the bits as the hash.
7611 The 8-byte signature is at least as large as hashval_t. */
7612 comdat_type_node_ref type_node = die->die_id.die_type_node;
7613 memcpy (&h, type_node->signature, sizeof (h));
7614 }
7615 return h;
7616 }
7617
7618 inline bool
7619 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7620 {
7621 return r1->type == r2->type;
7622 }
7623
7624 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7625
7626 /* Return a pointer to the external_ref for references to DIE. */
7627
7628 static struct external_ref *
7629 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7630 {
7631 struct external_ref ref, *ref_p;
7632 external_ref **slot;
7633
7634 ref.type = die;
7635 slot = map->find_slot (&ref, INSERT);
7636 if (*slot != HTAB_EMPTY_ENTRY)
7637 return *slot;
7638
7639 ref_p = XCNEW (struct external_ref);
7640 ref_p->type = die;
7641 *slot = ref_p;
7642 return ref_p;
7643 }
7644
7645 /* Subroutine of optimize_external_refs, below.
7646
7647 If we see a type skeleton, record it as our stub. If we see external
7648 references, remember how many we've seen. */
7649
7650 static void
7651 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7652 {
7653 dw_die_ref c;
7654 dw_attr_ref a;
7655 unsigned ix;
7656 struct external_ref *ref_p;
7657
7658 if (is_type_die (die)
7659 && (c = get_AT_ref (die, DW_AT_signature)))
7660 {
7661 /* This is a local skeleton; use it for local references. */
7662 ref_p = lookup_external_ref (map, c);
7663 ref_p->stub = die;
7664 }
7665
7666 /* Scan the DIE references, and remember any that refer to DIEs from
7667 other CUs (i.e. those which are not marked). */
7668 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7669 if (AT_class (a) == dw_val_class_die_ref
7670 && (c = AT_ref (a))->die_mark == 0
7671 && is_type_die (c))
7672 {
7673 ref_p = lookup_external_ref (map, c);
7674 ref_p->n_refs++;
7675 }
7676
7677 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7678 }
7679
7680 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7681 points to an external_ref, DATA is the CU we're processing. If we don't
7682 already have a local stub, and we have multiple refs, build a stub. */
7683
7684 int
7685 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7686 {
7687 struct external_ref *ref_p = *slot;
7688
7689 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7690 {
7691 /* We have multiple references to this type, so build a small stub.
7692 Both of these forms are a bit dodgy from the perspective of the
7693 DWARF standard, since technically they should have names. */
7694 dw_die_ref cu = data;
7695 dw_die_ref type = ref_p->type;
7696 dw_die_ref stub = NULL;
7697
7698 if (type->comdat_type_p)
7699 {
7700 /* If we refer to this type via sig8, use AT_signature. */
7701 stub = new_die (type->die_tag, cu, NULL_TREE);
7702 add_AT_die_ref (stub, DW_AT_signature, type);
7703 }
7704 else
7705 {
7706 /* Otherwise, use a typedef with no name. */
7707 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7708 add_AT_die_ref (stub, DW_AT_type, type);
7709 }
7710
7711 stub->die_mark++;
7712 ref_p->stub = stub;
7713 }
7714 return 1;
7715 }
7716
7717 /* DIE is a unit; look through all the DIE references to see if there are
7718 any external references to types, and if so, create local stubs for
7719 them which will be applied in build_abbrev_table. This is useful because
7720 references to local DIEs are smaller. */
7721
7722 static external_ref_hash_type *
7723 optimize_external_refs (dw_die_ref die)
7724 {
7725 external_ref_hash_type *map = new external_ref_hash_type (10);
7726 optimize_external_refs_1 (die, map);
7727 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7728 return map;
7729 }
7730
7731 /* The format of each DIE (and its attribute value pairs) is encoded in an
7732 abbreviation table. This routine builds the abbreviation table and assigns
7733 a unique abbreviation id for each abbreviation entry. The children of each
7734 die are visited recursively. */
7735
7736 static void
7737 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7738 {
7739 unsigned long abbrev_id;
7740 unsigned int n_alloc;
7741 dw_die_ref c;
7742 dw_attr_ref a;
7743 unsigned ix;
7744
7745 /* Scan the DIE references, and replace any that refer to
7746 DIEs from other CUs (i.e. those which are not marked) with
7747 the local stubs we built in optimize_external_refs. */
7748 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7749 if (AT_class (a) == dw_val_class_die_ref
7750 && (c = AT_ref (a))->die_mark == 0)
7751 {
7752 struct external_ref *ref_p;
7753 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7754
7755 ref_p = lookup_external_ref (extern_map, c);
7756 if (ref_p->stub && ref_p->stub != die)
7757 change_AT_die_ref (a, ref_p->stub);
7758 else
7759 /* We aren't changing this reference, so mark it external. */
7760 set_AT_ref_external (a, 1);
7761 }
7762
7763 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7764 {
7765 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7766 dw_attr_ref die_a, abbrev_a;
7767 unsigned ix;
7768 bool ok = true;
7769
7770 if (abbrev->die_tag != die->die_tag)
7771 continue;
7772 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7773 continue;
7774
7775 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7776 continue;
7777
7778 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7779 {
7780 abbrev_a = &(*abbrev->die_attr)[ix];
7781 if ((abbrev_a->dw_attr != die_a->dw_attr)
7782 || (value_format (abbrev_a) != value_format (die_a)))
7783 {
7784 ok = false;
7785 break;
7786 }
7787 }
7788 if (ok)
7789 break;
7790 }
7791
7792 if (abbrev_id >= abbrev_die_table_in_use)
7793 {
7794 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7795 {
7796 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7797 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7798 n_alloc);
7799
7800 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7801 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7802 abbrev_die_table_allocated = n_alloc;
7803 }
7804
7805 ++abbrev_die_table_in_use;
7806 abbrev_die_table[abbrev_id] = die;
7807 }
7808
7809 die->die_abbrev = abbrev_id;
7810 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7811 }
7812 \f
7813 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7814
7815 static int
7816 constant_size (unsigned HOST_WIDE_INT value)
7817 {
7818 int log;
7819
7820 if (value == 0)
7821 log = 0;
7822 else
7823 log = floor_log2 (value);
7824
7825 log = log / 8;
7826 log = 1 << (floor_log2 (log) + 1);
7827
7828 return log;
7829 }
7830
7831 /* Return the size of a DIE as it is represented in the
7832 .debug_info section. */
7833
7834 static unsigned long
7835 size_of_die (dw_die_ref die)
7836 {
7837 unsigned long size = 0;
7838 dw_attr_ref a;
7839 unsigned ix;
7840 enum dwarf_form form;
7841
7842 size += size_of_uleb128 (die->die_abbrev);
7843 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7844 {
7845 switch (AT_class (a))
7846 {
7847 case dw_val_class_addr:
7848 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7849 {
7850 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7851 size += size_of_uleb128 (AT_index (a));
7852 }
7853 else
7854 size += DWARF2_ADDR_SIZE;
7855 break;
7856 case dw_val_class_offset:
7857 size += DWARF_OFFSET_SIZE;
7858 break;
7859 case dw_val_class_loc:
7860 {
7861 unsigned long lsize = size_of_locs (AT_loc (a));
7862
7863 /* Block length. */
7864 if (dwarf_version >= 4)
7865 size += size_of_uleb128 (lsize);
7866 else
7867 size += constant_size (lsize);
7868 size += lsize;
7869 }
7870 break;
7871 case dw_val_class_loc_list:
7872 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7873 {
7874 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7875 size += size_of_uleb128 (AT_index (a));
7876 }
7877 else
7878 size += DWARF_OFFSET_SIZE;
7879 break;
7880 case dw_val_class_range_list:
7881 size += DWARF_OFFSET_SIZE;
7882 break;
7883 case dw_val_class_const:
7884 size += size_of_sleb128 (AT_int (a));
7885 break;
7886 case dw_val_class_unsigned_const:
7887 {
7888 int csize = constant_size (AT_unsigned (a));
7889 if (dwarf_version == 3
7890 && a->dw_attr == DW_AT_data_member_location
7891 && csize >= 4)
7892 size += size_of_uleb128 (AT_unsigned (a));
7893 else
7894 size += csize;
7895 }
7896 break;
7897 case dw_val_class_const_double:
7898 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7899 if (HOST_BITS_PER_WIDE_INT >= 64)
7900 size++; /* block */
7901 break;
7902 case dw_val_class_wide_int:
7903 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7904 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7905 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7906 > 64)
7907 size++; /* block */
7908 break;
7909 case dw_val_class_vec:
7910 size += constant_size (a->dw_attr_val.v.val_vec.length
7911 * a->dw_attr_val.v.val_vec.elt_size)
7912 + a->dw_attr_val.v.val_vec.length
7913 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7914 break;
7915 case dw_val_class_flag:
7916 if (dwarf_version >= 4)
7917 /* Currently all add_AT_flag calls pass in 1 as last argument,
7918 so DW_FORM_flag_present can be used. If that ever changes,
7919 we'll need to use DW_FORM_flag and have some optimization
7920 in build_abbrev_table that will change those to
7921 DW_FORM_flag_present if it is set to 1 in all DIEs using
7922 the same abbrev entry. */
7923 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7924 else
7925 size += 1;
7926 break;
7927 case dw_val_class_die_ref:
7928 if (AT_ref_external (a))
7929 {
7930 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7931 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7932 is sized by target address length, whereas in DWARF3
7933 it's always sized as an offset. */
7934 if (use_debug_types)
7935 size += DWARF_TYPE_SIGNATURE_SIZE;
7936 else if (dwarf_version == 2)
7937 size += DWARF2_ADDR_SIZE;
7938 else
7939 size += DWARF_OFFSET_SIZE;
7940 }
7941 else
7942 size += DWARF_OFFSET_SIZE;
7943 break;
7944 case dw_val_class_fde_ref:
7945 size += DWARF_OFFSET_SIZE;
7946 break;
7947 case dw_val_class_lbl_id:
7948 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7949 {
7950 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7951 size += size_of_uleb128 (AT_index (a));
7952 }
7953 else
7954 size += DWARF2_ADDR_SIZE;
7955 break;
7956 case dw_val_class_lineptr:
7957 case dw_val_class_macptr:
7958 size += DWARF_OFFSET_SIZE;
7959 break;
7960 case dw_val_class_str:
7961 form = AT_string_form (a);
7962 if (form == DW_FORM_strp)
7963 size += DWARF_OFFSET_SIZE;
7964 else if (form == DW_FORM_GNU_str_index)
7965 size += size_of_uleb128 (AT_index (a));
7966 else
7967 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7968 break;
7969 case dw_val_class_file:
7970 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7971 break;
7972 case dw_val_class_data8:
7973 size += 8;
7974 break;
7975 case dw_val_class_vms_delta:
7976 size += DWARF_OFFSET_SIZE;
7977 break;
7978 case dw_val_class_high_pc:
7979 size += DWARF2_ADDR_SIZE;
7980 break;
7981 default:
7982 gcc_unreachable ();
7983 }
7984 }
7985
7986 return size;
7987 }
7988
7989 /* Size the debugging information associated with a given DIE. Visits the
7990 DIE's children recursively. Updates the global variable next_die_offset, on
7991 each time through. Uses the current value of next_die_offset to update the
7992 die_offset field in each DIE. */
7993
7994 static void
7995 calc_die_sizes (dw_die_ref die)
7996 {
7997 dw_die_ref c;
7998
7999 gcc_assert (die->die_offset == 0
8000 || (unsigned long int) die->die_offset == next_die_offset);
8001 die->die_offset = next_die_offset;
8002 next_die_offset += size_of_die (die);
8003
8004 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8005
8006 if (die->die_child != NULL)
8007 /* Count the null byte used to terminate sibling lists. */
8008 next_die_offset += 1;
8009 }
8010
8011 /* Size just the base type children at the start of the CU.
8012 This is needed because build_abbrev needs to size locs
8013 and sizing of type based stack ops needs to know die_offset
8014 values for the base types. */
8015
8016 static void
8017 calc_base_type_die_sizes (void)
8018 {
8019 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8020 unsigned int i;
8021 dw_die_ref base_type;
8022 #if ENABLE_ASSERT_CHECKING
8023 dw_die_ref prev = comp_unit_die ()->die_child;
8024 #endif
8025
8026 die_offset += size_of_die (comp_unit_die ());
8027 for (i = 0; base_types.iterate (i, &base_type); i++)
8028 {
8029 #if ENABLE_ASSERT_CHECKING
8030 gcc_assert (base_type->die_offset == 0
8031 && prev->die_sib == base_type
8032 && base_type->die_child == NULL
8033 && base_type->die_abbrev);
8034 prev = base_type;
8035 #endif
8036 base_type->die_offset = die_offset;
8037 die_offset += size_of_die (base_type);
8038 }
8039 }
8040
8041 /* Set the marks for a die and its children. We do this so
8042 that we know whether or not a reference needs to use FORM_ref_addr; only
8043 DIEs in the same CU will be marked. We used to clear out the offset
8044 and use that as the flag, but ran into ordering problems. */
8045
8046 static void
8047 mark_dies (dw_die_ref die)
8048 {
8049 dw_die_ref c;
8050
8051 gcc_assert (!die->die_mark);
8052
8053 die->die_mark = 1;
8054 FOR_EACH_CHILD (die, c, mark_dies (c));
8055 }
8056
8057 /* Clear the marks for a die and its children. */
8058
8059 static void
8060 unmark_dies (dw_die_ref die)
8061 {
8062 dw_die_ref c;
8063
8064 if (! use_debug_types)
8065 gcc_assert (die->die_mark);
8066
8067 die->die_mark = 0;
8068 FOR_EACH_CHILD (die, c, unmark_dies (c));
8069 }
8070
8071 /* Clear the marks for a die, its children and referred dies. */
8072
8073 static void
8074 unmark_all_dies (dw_die_ref die)
8075 {
8076 dw_die_ref c;
8077 dw_attr_ref a;
8078 unsigned ix;
8079
8080 if (!die->die_mark)
8081 return;
8082 die->die_mark = 0;
8083
8084 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8085
8086 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8087 if (AT_class (a) == dw_val_class_die_ref)
8088 unmark_all_dies (AT_ref (a));
8089 }
8090
8091 /* Calculate if the entry should appear in the final output file. It may be
8092 from a pruned a type. */
8093
8094 static bool
8095 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8096 {
8097 /* By limiting gnu pubnames to definitions only, gold can generate a
8098 gdb index without entries for declarations, which don't include
8099 enough information to be useful. */
8100 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8101 return false;
8102
8103 if (table == pubname_table)
8104 {
8105 /* Enumerator names are part of the pubname table, but the
8106 parent DW_TAG_enumeration_type die may have been pruned.
8107 Don't output them if that is the case. */
8108 if (p->die->die_tag == DW_TAG_enumerator &&
8109 (p->die->die_parent == NULL
8110 || !p->die->die_parent->die_perennial_p))
8111 return false;
8112
8113 /* Everything else in the pubname table is included. */
8114 return true;
8115 }
8116
8117 /* The pubtypes table shouldn't include types that have been
8118 pruned. */
8119 return (p->die->die_offset != 0
8120 || !flag_eliminate_unused_debug_types);
8121 }
8122
8123 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8124 generated for the compilation unit. */
8125
8126 static unsigned long
8127 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8128 {
8129 unsigned long size;
8130 unsigned i;
8131 pubname_ref p;
8132 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8133
8134 size = DWARF_PUBNAMES_HEADER_SIZE;
8135 FOR_EACH_VEC_ELT (*names, i, p)
8136 if (include_pubname_in_output (names, p))
8137 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8138
8139 size += DWARF_OFFSET_SIZE;
8140 return size;
8141 }
8142
8143 /* Return the size of the information in the .debug_aranges section. */
8144
8145 static unsigned long
8146 size_of_aranges (void)
8147 {
8148 unsigned long size;
8149
8150 size = DWARF_ARANGES_HEADER_SIZE;
8151
8152 /* Count the address/length pair for this compilation unit. */
8153 if (text_section_used)
8154 size += 2 * DWARF2_ADDR_SIZE;
8155 if (cold_text_section_used)
8156 size += 2 * DWARF2_ADDR_SIZE;
8157 if (have_multiple_function_sections)
8158 {
8159 unsigned fde_idx;
8160 dw_fde_ref fde;
8161
8162 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8163 {
8164 if (DECL_IGNORED_P (fde->decl))
8165 continue;
8166 if (!fde->in_std_section)
8167 size += 2 * DWARF2_ADDR_SIZE;
8168 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8169 size += 2 * DWARF2_ADDR_SIZE;
8170 }
8171 }
8172
8173 /* Count the two zero words used to terminated the address range table. */
8174 size += 2 * DWARF2_ADDR_SIZE;
8175 return size;
8176 }
8177 \f
8178 /* Select the encoding of an attribute value. */
8179
8180 static enum dwarf_form
8181 value_format (dw_attr_ref a)
8182 {
8183 switch (AT_class (a))
8184 {
8185 case dw_val_class_addr:
8186 /* Only very few attributes allow DW_FORM_addr. */
8187 switch (a->dw_attr)
8188 {
8189 case DW_AT_low_pc:
8190 case DW_AT_high_pc:
8191 case DW_AT_entry_pc:
8192 case DW_AT_trampoline:
8193 return (AT_index (a) == NOT_INDEXED
8194 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8195 default:
8196 break;
8197 }
8198 switch (DWARF2_ADDR_SIZE)
8199 {
8200 case 1:
8201 return DW_FORM_data1;
8202 case 2:
8203 return DW_FORM_data2;
8204 case 4:
8205 return DW_FORM_data4;
8206 case 8:
8207 return DW_FORM_data8;
8208 default:
8209 gcc_unreachable ();
8210 }
8211 case dw_val_class_range_list:
8212 case dw_val_class_loc_list:
8213 if (dwarf_version >= 4)
8214 return DW_FORM_sec_offset;
8215 /* FALLTHRU */
8216 case dw_val_class_vms_delta:
8217 case dw_val_class_offset:
8218 switch (DWARF_OFFSET_SIZE)
8219 {
8220 case 4:
8221 return DW_FORM_data4;
8222 case 8:
8223 return DW_FORM_data8;
8224 default:
8225 gcc_unreachable ();
8226 }
8227 case dw_val_class_loc:
8228 if (dwarf_version >= 4)
8229 return DW_FORM_exprloc;
8230 switch (constant_size (size_of_locs (AT_loc (a))))
8231 {
8232 case 1:
8233 return DW_FORM_block1;
8234 case 2:
8235 return DW_FORM_block2;
8236 case 4:
8237 return DW_FORM_block4;
8238 default:
8239 gcc_unreachable ();
8240 }
8241 case dw_val_class_const:
8242 return DW_FORM_sdata;
8243 case dw_val_class_unsigned_const:
8244 switch (constant_size (AT_unsigned (a)))
8245 {
8246 case 1:
8247 return DW_FORM_data1;
8248 case 2:
8249 return DW_FORM_data2;
8250 case 4:
8251 /* In DWARF3 DW_AT_data_member_location with
8252 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8253 constant, so we need to use DW_FORM_udata if we need
8254 a large constant. */
8255 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8256 return DW_FORM_udata;
8257 return DW_FORM_data4;
8258 case 8:
8259 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8260 return DW_FORM_udata;
8261 return DW_FORM_data8;
8262 default:
8263 gcc_unreachable ();
8264 }
8265 case dw_val_class_const_double:
8266 switch (HOST_BITS_PER_WIDE_INT)
8267 {
8268 case 8:
8269 return DW_FORM_data2;
8270 case 16:
8271 return DW_FORM_data4;
8272 case 32:
8273 return DW_FORM_data8;
8274 case 64:
8275 default:
8276 return DW_FORM_block1;
8277 }
8278 case dw_val_class_wide_int:
8279 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8280 {
8281 case 8:
8282 return DW_FORM_data1;
8283 case 16:
8284 return DW_FORM_data2;
8285 case 32:
8286 return DW_FORM_data4;
8287 case 64:
8288 return DW_FORM_data8;
8289 default:
8290 return DW_FORM_block1;
8291 }
8292 case dw_val_class_vec:
8293 switch (constant_size (a->dw_attr_val.v.val_vec.length
8294 * a->dw_attr_val.v.val_vec.elt_size))
8295 {
8296 case 1:
8297 return DW_FORM_block1;
8298 case 2:
8299 return DW_FORM_block2;
8300 case 4:
8301 return DW_FORM_block4;
8302 default:
8303 gcc_unreachable ();
8304 }
8305 case dw_val_class_flag:
8306 if (dwarf_version >= 4)
8307 {
8308 /* Currently all add_AT_flag calls pass in 1 as last argument,
8309 so DW_FORM_flag_present can be used. If that ever changes,
8310 we'll need to use DW_FORM_flag and have some optimization
8311 in build_abbrev_table that will change those to
8312 DW_FORM_flag_present if it is set to 1 in all DIEs using
8313 the same abbrev entry. */
8314 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8315 return DW_FORM_flag_present;
8316 }
8317 return DW_FORM_flag;
8318 case dw_val_class_die_ref:
8319 if (AT_ref_external (a))
8320 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8321 else
8322 return DW_FORM_ref;
8323 case dw_val_class_fde_ref:
8324 return DW_FORM_data;
8325 case dw_val_class_lbl_id:
8326 return (AT_index (a) == NOT_INDEXED
8327 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8328 case dw_val_class_lineptr:
8329 case dw_val_class_macptr:
8330 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8331 case dw_val_class_str:
8332 return AT_string_form (a);
8333 case dw_val_class_file:
8334 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8335 {
8336 case 1:
8337 return DW_FORM_data1;
8338 case 2:
8339 return DW_FORM_data2;
8340 case 4:
8341 return DW_FORM_data4;
8342 default:
8343 gcc_unreachable ();
8344 }
8345
8346 case dw_val_class_data8:
8347 return DW_FORM_data8;
8348
8349 case dw_val_class_high_pc:
8350 switch (DWARF2_ADDR_SIZE)
8351 {
8352 case 1:
8353 return DW_FORM_data1;
8354 case 2:
8355 return DW_FORM_data2;
8356 case 4:
8357 return DW_FORM_data4;
8358 case 8:
8359 return DW_FORM_data8;
8360 default:
8361 gcc_unreachable ();
8362 }
8363
8364 default:
8365 gcc_unreachable ();
8366 }
8367 }
8368
8369 /* Output the encoding of an attribute value. */
8370
8371 static void
8372 output_value_format (dw_attr_ref a)
8373 {
8374 enum dwarf_form form = value_format (a);
8375
8376 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8377 }
8378
8379 /* Given a die and id, produce the appropriate abbreviations. */
8380
8381 static void
8382 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8383 {
8384 unsigned ix;
8385 dw_attr_ref a_attr;
8386
8387 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8388 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8389 dwarf_tag_name (abbrev->die_tag));
8390
8391 if (abbrev->die_child != NULL)
8392 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8393 else
8394 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8395
8396 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8397 {
8398 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8399 dwarf_attr_name (a_attr->dw_attr));
8400 output_value_format (a_attr);
8401 }
8402
8403 dw2_asm_output_data (1, 0, NULL);
8404 dw2_asm_output_data (1, 0, NULL);
8405 }
8406
8407
8408 /* Output the .debug_abbrev section which defines the DIE abbreviation
8409 table. */
8410
8411 static void
8412 output_abbrev_section (void)
8413 {
8414 unsigned long abbrev_id;
8415
8416 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8417 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8418
8419 /* Terminate the table. */
8420 dw2_asm_output_data (1, 0, NULL);
8421 }
8422
8423 /* Output a symbol we can use to refer to this DIE from another CU. */
8424
8425 static inline void
8426 output_die_symbol (dw_die_ref die)
8427 {
8428 const char *sym = die->die_id.die_symbol;
8429
8430 gcc_assert (!die->comdat_type_p);
8431
8432 if (sym == 0)
8433 return;
8434
8435 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8436 /* We make these global, not weak; if the target doesn't support
8437 .linkonce, it doesn't support combining the sections, so debugging
8438 will break. */
8439 targetm.asm_out.globalize_label (asm_out_file, sym);
8440
8441 ASM_OUTPUT_LABEL (asm_out_file, sym);
8442 }
8443
8444 /* Return a new location list, given the begin and end range, and the
8445 expression. */
8446
8447 static inline dw_loc_list_ref
8448 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8449 const char *section)
8450 {
8451 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8452
8453 retlist->begin = begin;
8454 retlist->begin_entry = NULL;
8455 retlist->end = end;
8456 retlist->expr = expr;
8457 retlist->section = section;
8458
8459 return retlist;
8460 }
8461
8462 /* Generate a new internal symbol for this location list node, if it
8463 hasn't got one yet. */
8464
8465 static inline void
8466 gen_llsym (dw_loc_list_ref list)
8467 {
8468 gcc_assert (!list->ll_symbol);
8469 list->ll_symbol = gen_internal_sym ("LLST");
8470 }
8471
8472 /* Output the location list given to us. */
8473
8474 static void
8475 output_loc_list (dw_loc_list_ref list_head)
8476 {
8477 dw_loc_list_ref curr = list_head;
8478
8479 if (list_head->emitted)
8480 return;
8481 list_head->emitted = true;
8482
8483 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8484
8485 /* Walk the location list, and output each range + expression. */
8486 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8487 {
8488 unsigned long size;
8489 /* Don't output an entry that starts and ends at the same address. */
8490 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8491 continue;
8492 size = size_of_locs (curr->expr);
8493 /* If the expression is too large, drop it on the floor. We could
8494 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8495 in the expression, but >= 64KB expressions for a single value
8496 in a single range are unlikely very useful. */
8497 if (size > 0xffff)
8498 continue;
8499 if (dwarf_split_debug_info)
8500 {
8501 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8502 "Location list start/length entry (%s)",
8503 list_head->ll_symbol);
8504 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8505 "Location list range start index (%s)",
8506 curr->begin);
8507 /* The length field is 4 bytes. If we ever need to support
8508 an 8-byte length, we can add a new DW_LLE code or fall back
8509 to DW_LLE_GNU_start_end_entry. */
8510 dw2_asm_output_delta (4, curr->end, curr->begin,
8511 "Location list range length (%s)",
8512 list_head->ll_symbol);
8513 }
8514 else if (!have_multiple_function_sections)
8515 {
8516 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8517 "Location list begin address (%s)",
8518 list_head->ll_symbol);
8519 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8520 "Location list end address (%s)",
8521 list_head->ll_symbol);
8522 }
8523 else
8524 {
8525 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8526 "Location list begin address (%s)",
8527 list_head->ll_symbol);
8528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8529 "Location list end address (%s)",
8530 list_head->ll_symbol);
8531 }
8532
8533 /* Output the block length for this list of location operations. */
8534 gcc_assert (size <= 0xffff);
8535 dw2_asm_output_data (2, size, "%s", "Location expression size");
8536
8537 output_loc_sequence (curr->expr, -1);
8538 }
8539
8540 if (dwarf_split_debug_info)
8541 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8542 "Location list terminator (%s)",
8543 list_head->ll_symbol);
8544 else
8545 {
8546 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8547 "Location list terminator begin (%s)",
8548 list_head->ll_symbol);
8549 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8550 "Location list terminator end (%s)",
8551 list_head->ll_symbol);
8552 }
8553 }
8554
8555 /* Output a range_list offset into the debug_range section. Emit a
8556 relocated reference if val_entry is NULL, otherwise, emit an
8557 indirect reference. */
8558
8559 static void
8560 output_range_list_offset (dw_attr_ref a)
8561 {
8562 const char *name = dwarf_attr_name (a->dw_attr);
8563
8564 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8565 {
8566 char *p = strchr (ranges_section_label, '\0');
8567 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8568 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8569 debug_ranges_section, "%s", name);
8570 *p = '\0';
8571 }
8572 else
8573 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8574 "%s (offset from %s)", name, ranges_section_label);
8575 }
8576
8577 /* Output the offset into the debug_loc section. */
8578
8579 static void
8580 output_loc_list_offset (dw_attr_ref a)
8581 {
8582 char *sym = AT_loc_list (a)->ll_symbol;
8583
8584 gcc_assert (sym);
8585 if (dwarf_split_debug_info)
8586 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8587 "%s", dwarf_attr_name (a->dw_attr));
8588 else
8589 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8590 "%s", dwarf_attr_name (a->dw_attr));
8591 }
8592
8593 /* Output an attribute's index or value appropriately. */
8594
8595 static void
8596 output_attr_index_or_value (dw_attr_ref a)
8597 {
8598 const char *name = dwarf_attr_name (a->dw_attr);
8599
8600 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8601 {
8602 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8603 return;
8604 }
8605 switch (AT_class (a))
8606 {
8607 case dw_val_class_addr:
8608 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8609 break;
8610 case dw_val_class_high_pc:
8611 case dw_val_class_lbl_id:
8612 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8613 break;
8614 case dw_val_class_loc_list:
8615 output_loc_list_offset (a);
8616 break;
8617 default:
8618 gcc_unreachable ();
8619 }
8620 }
8621
8622 /* Output a type signature. */
8623
8624 static inline void
8625 output_signature (const char *sig, const char *name)
8626 {
8627 int i;
8628
8629 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8630 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8631 }
8632
8633 /* Output the DIE and its attributes. Called recursively to generate
8634 the definitions of each child DIE. */
8635
8636 static void
8637 output_die (dw_die_ref die)
8638 {
8639 dw_attr_ref a;
8640 dw_die_ref c;
8641 unsigned long size;
8642 unsigned ix;
8643
8644 /* If someone in another CU might refer to us, set up a symbol for
8645 them to point to. */
8646 if (! die->comdat_type_p && die->die_id.die_symbol)
8647 output_die_symbol (die);
8648
8649 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8650 (unsigned long)die->die_offset,
8651 dwarf_tag_name (die->die_tag));
8652
8653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8654 {
8655 const char *name = dwarf_attr_name (a->dw_attr);
8656
8657 switch (AT_class (a))
8658 {
8659 case dw_val_class_addr:
8660 output_attr_index_or_value (a);
8661 break;
8662
8663 case dw_val_class_offset:
8664 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8665 "%s", name);
8666 break;
8667
8668 case dw_val_class_range_list:
8669 output_range_list_offset (a);
8670 break;
8671
8672 case dw_val_class_loc:
8673 size = size_of_locs (AT_loc (a));
8674
8675 /* Output the block length for this list of location operations. */
8676 if (dwarf_version >= 4)
8677 dw2_asm_output_data_uleb128 (size, "%s", name);
8678 else
8679 dw2_asm_output_data (constant_size (size), size, "%s", name);
8680
8681 output_loc_sequence (AT_loc (a), -1);
8682 break;
8683
8684 case dw_val_class_const:
8685 /* ??? It would be slightly more efficient to use a scheme like is
8686 used for unsigned constants below, but gdb 4.x does not sign
8687 extend. Gdb 5.x does sign extend. */
8688 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8689 break;
8690
8691 case dw_val_class_unsigned_const:
8692 {
8693 int csize = constant_size (AT_unsigned (a));
8694 if (dwarf_version == 3
8695 && a->dw_attr == DW_AT_data_member_location
8696 && csize >= 4)
8697 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8698 else
8699 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8700 }
8701 break;
8702
8703 case dw_val_class_const_double:
8704 {
8705 unsigned HOST_WIDE_INT first, second;
8706
8707 if (HOST_BITS_PER_WIDE_INT >= 64)
8708 dw2_asm_output_data (1,
8709 HOST_BITS_PER_DOUBLE_INT
8710 / HOST_BITS_PER_CHAR,
8711 NULL);
8712
8713 if (WORDS_BIG_ENDIAN)
8714 {
8715 first = a->dw_attr_val.v.val_double.high;
8716 second = a->dw_attr_val.v.val_double.low;
8717 }
8718 else
8719 {
8720 first = a->dw_attr_val.v.val_double.low;
8721 second = a->dw_attr_val.v.val_double.high;
8722 }
8723
8724 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8725 first, "%s", name);
8726 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8727 second, NULL);
8728 }
8729 break;
8730
8731 case dw_val_class_wide_int:
8732 {
8733 int i;
8734 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8735 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8736 if (len * HOST_BITS_PER_WIDE_INT > 64)
8737 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8738 NULL);
8739
8740 if (WORDS_BIG_ENDIAN)
8741 for (i = len - 1; i >= 0; --i)
8742 {
8743 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8744 name);
8745 name = NULL;
8746 }
8747 else
8748 for (i = 0; i < len; ++i)
8749 {
8750 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8751 name);
8752 name = NULL;
8753 }
8754 }
8755 break;
8756
8757 case dw_val_class_vec:
8758 {
8759 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8760 unsigned int len = a->dw_attr_val.v.val_vec.length;
8761 unsigned int i;
8762 unsigned char *p;
8763
8764 dw2_asm_output_data (constant_size (len * elt_size),
8765 len * elt_size, "%s", name);
8766 if (elt_size > sizeof (HOST_WIDE_INT))
8767 {
8768 elt_size /= 2;
8769 len *= 2;
8770 }
8771 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8772 i < len;
8773 i++, p += elt_size)
8774 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8775 "fp or vector constant word %u", i);
8776 break;
8777 }
8778
8779 case dw_val_class_flag:
8780 if (dwarf_version >= 4)
8781 {
8782 /* Currently all add_AT_flag calls pass in 1 as last argument,
8783 so DW_FORM_flag_present can be used. If that ever changes,
8784 we'll need to use DW_FORM_flag and have some optimization
8785 in build_abbrev_table that will change those to
8786 DW_FORM_flag_present if it is set to 1 in all DIEs using
8787 the same abbrev entry. */
8788 gcc_assert (AT_flag (a) == 1);
8789 if (flag_debug_asm)
8790 fprintf (asm_out_file, "\t\t\t%s %s\n",
8791 ASM_COMMENT_START, name);
8792 break;
8793 }
8794 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8795 break;
8796
8797 case dw_val_class_loc_list:
8798 output_attr_index_or_value (a);
8799 break;
8800
8801 case dw_val_class_die_ref:
8802 if (AT_ref_external (a))
8803 {
8804 if (AT_ref (a)->comdat_type_p)
8805 {
8806 comdat_type_node_ref type_node =
8807 AT_ref (a)->die_id.die_type_node;
8808
8809 gcc_assert (type_node);
8810 output_signature (type_node->signature, name);
8811 }
8812 else
8813 {
8814 const char *sym = AT_ref (a)->die_id.die_symbol;
8815 int size;
8816
8817 gcc_assert (sym);
8818 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8819 length, whereas in DWARF3 it's always sized as an
8820 offset. */
8821 if (dwarf_version == 2)
8822 size = DWARF2_ADDR_SIZE;
8823 else
8824 size = DWARF_OFFSET_SIZE;
8825 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8826 name);
8827 }
8828 }
8829 else
8830 {
8831 gcc_assert (AT_ref (a)->die_offset);
8832 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8833 "%s", name);
8834 }
8835 break;
8836
8837 case dw_val_class_fde_ref:
8838 {
8839 char l1[20];
8840
8841 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8842 a->dw_attr_val.v.val_fde_index * 2);
8843 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8844 "%s", name);
8845 }
8846 break;
8847
8848 case dw_val_class_vms_delta:
8849 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8850 AT_vms_delta2 (a), AT_vms_delta1 (a),
8851 "%s", name);
8852 break;
8853
8854 case dw_val_class_lbl_id:
8855 output_attr_index_or_value (a);
8856 break;
8857
8858 case dw_val_class_lineptr:
8859 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8860 debug_line_section, "%s", name);
8861 break;
8862
8863 case dw_val_class_macptr:
8864 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8865 debug_macinfo_section, "%s", name);
8866 break;
8867
8868 case dw_val_class_str:
8869 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8870 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8871 a->dw_attr_val.v.val_str->label,
8872 debug_str_section,
8873 "%s: \"%s\"", name, AT_string (a));
8874 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8875 dw2_asm_output_data_uleb128 (AT_index (a),
8876 "%s: \"%s\"", name, AT_string (a));
8877 else
8878 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8879 break;
8880
8881 case dw_val_class_file:
8882 {
8883 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8884
8885 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8886 a->dw_attr_val.v.val_file->filename);
8887 break;
8888 }
8889
8890 case dw_val_class_data8:
8891 {
8892 int i;
8893
8894 for (i = 0; i < 8; i++)
8895 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8896 i == 0 ? "%s" : NULL, name);
8897 break;
8898 }
8899
8900 case dw_val_class_high_pc:
8901 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8902 get_AT_low_pc (die), "DW_AT_high_pc");
8903 break;
8904
8905 default:
8906 gcc_unreachable ();
8907 }
8908 }
8909
8910 FOR_EACH_CHILD (die, c, output_die (c));
8911
8912 /* Add null byte to terminate sibling list. */
8913 if (die->die_child != NULL)
8914 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8915 (unsigned long) die->die_offset);
8916 }
8917
8918 /* Output the compilation unit that appears at the beginning of the
8919 .debug_info section, and precedes the DIE descriptions. */
8920
8921 static void
8922 output_compilation_unit_header (void)
8923 {
8924 int ver = dwarf_version;
8925
8926 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8927 dw2_asm_output_data (4, 0xffffffff,
8928 "Initial length escape value indicating 64-bit DWARF extension");
8929 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8930 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8931 "Length of Compilation Unit Info");
8932 dw2_asm_output_data (2, ver, "DWARF version number");
8933 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8934 debug_abbrev_section,
8935 "Offset Into Abbrev. Section");
8936 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8937 }
8938
8939 /* Output the compilation unit DIE and its children. */
8940
8941 static void
8942 output_comp_unit (dw_die_ref die, int output_if_empty)
8943 {
8944 const char *secname, *oldsym;
8945 char *tmp;
8946
8947 /* Unless we are outputting main CU, we may throw away empty ones. */
8948 if (!output_if_empty && die->die_child == NULL)
8949 return;
8950
8951 /* Even if there are no children of this DIE, we must output the information
8952 about the compilation unit. Otherwise, on an empty translation unit, we
8953 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8954 will then complain when examining the file. First mark all the DIEs in
8955 this CU so we know which get local refs. */
8956 mark_dies (die);
8957
8958 external_ref_hash_type *extern_map = optimize_external_refs (die);
8959
8960 build_abbrev_table (die, extern_map);
8961
8962 delete extern_map;
8963
8964 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8965 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8966 calc_die_sizes (die);
8967
8968 oldsym = die->die_id.die_symbol;
8969 if (oldsym)
8970 {
8971 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8972
8973 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8974 secname = tmp;
8975 die->die_id.die_symbol = NULL;
8976 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8977 }
8978 else
8979 {
8980 switch_to_section (debug_info_section);
8981 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8982 info_section_emitted = true;
8983 }
8984
8985 /* Output debugging information. */
8986 output_compilation_unit_header ();
8987 output_die (die);
8988
8989 /* Leave the marks on the main CU, so we can check them in
8990 output_pubnames. */
8991 if (oldsym)
8992 {
8993 unmark_dies (die);
8994 die->die_id.die_symbol = oldsym;
8995 }
8996 }
8997
8998 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8999 and .debug_pubtypes. This is configured per-target, but can be
9000 overridden by the -gpubnames or -gno-pubnames options. */
9001
9002 static inline bool
9003 want_pubnames (void)
9004 {
9005 if (debug_info_level <= DINFO_LEVEL_TERSE)
9006 return false;
9007 if (debug_generate_pub_sections != -1)
9008 return debug_generate_pub_sections;
9009 return targetm.want_debug_pub_sections;
9010 }
9011
9012 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9013
9014 static void
9015 add_AT_pubnames (dw_die_ref die)
9016 {
9017 if (want_pubnames ())
9018 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9019 }
9020
9021 /* Add a string attribute value to a skeleton DIE. */
9022
9023 static inline void
9024 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9025 const char *str)
9026 {
9027 dw_attr_node attr;
9028 struct indirect_string_node *node;
9029
9030 if (! skeleton_debug_str_hash)
9031 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9032 debug_str_eq, NULL);
9033
9034 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9035 find_string_form (node);
9036 if (node->form == DW_FORM_GNU_str_index)
9037 node->form = DW_FORM_strp;
9038
9039 attr.dw_attr = attr_kind;
9040 attr.dw_attr_val.val_class = dw_val_class_str;
9041 attr.dw_attr_val.val_entry = NULL;
9042 attr.dw_attr_val.v.val_str = node;
9043 add_dwarf_attr (die, &attr);
9044 }
9045
9046 /* Helper function to generate top-level dies for skeleton debug_info and
9047 debug_types. */
9048
9049 static void
9050 add_top_level_skeleton_die_attrs (dw_die_ref die)
9051 {
9052 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9053 const char *comp_dir = comp_dir_string ();
9054
9055 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9056 if (comp_dir != NULL)
9057 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9058 add_AT_pubnames (die);
9059 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9060 }
9061
9062 /* Return the single type-unit die for skeleton type units. */
9063
9064 static dw_die_ref
9065 get_skeleton_type_unit (void)
9066 {
9067 /* For dwarf_split_debug_sections with use_type info, all type units in the
9068 skeleton sections have identical dies (but different headers). This
9069 single die will be output many times. */
9070
9071 static dw_die_ref skeleton_type_unit = NULL;
9072
9073 if (skeleton_type_unit == NULL)
9074 {
9075 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
9076 add_top_level_skeleton_die_attrs (skeleton_type_unit);
9077 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
9078 }
9079 return skeleton_type_unit;
9080 }
9081
9082 /* Output skeleton debug sections that point to the dwo file. */
9083
9084 static void
9085 output_skeleton_debug_sections (dw_die_ref comp_unit)
9086 {
9087 /* These attributes will be found in the full debug_info section. */
9088 remove_AT (comp_unit, DW_AT_producer);
9089 remove_AT (comp_unit, DW_AT_language);
9090
9091 switch_to_section (debug_skeleton_info_section);
9092 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9093
9094 /* Produce the skeleton compilation-unit header. This one differs enough from
9095 a normal CU header that it's better not to call output_compilation_unit
9096 header. */
9097 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9098 dw2_asm_output_data (4, 0xffffffff,
9099 "Initial length escape value indicating 64-bit DWARF extension");
9100
9101 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9102 DWARF_COMPILE_UNIT_HEADER_SIZE
9103 - DWARF_INITIAL_LENGTH_SIZE
9104 + size_of_die (comp_unit),
9105 "Length of Compilation Unit Info");
9106 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9107 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9108 debug_abbrev_section,
9109 "Offset Into Abbrev. Section");
9110 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9111
9112 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9113 output_die (comp_unit);
9114
9115 /* Build the skeleton debug_abbrev section. */
9116 switch_to_section (debug_skeleton_abbrev_section);
9117 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9118
9119 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9120 if (use_debug_types)
9121 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
9122
9123 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9124 }
9125
9126 /* Output a comdat type unit DIE and its children. */
9127
9128 static void
9129 output_comdat_type_unit (comdat_type_node *node)
9130 {
9131 const char *secname;
9132 char *tmp;
9133 int i;
9134 #if defined (OBJECT_FORMAT_ELF)
9135 tree comdat_key;
9136 #endif
9137
9138 /* First mark all the DIEs in this CU so we know which get local refs. */
9139 mark_dies (node->root_die);
9140
9141 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9142
9143 build_abbrev_table (node->root_die, extern_map);
9144
9145 delete extern_map;
9146 extern_map = NULL;
9147
9148 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9149 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9150 calc_die_sizes (node->root_die);
9151
9152 #if defined (OBJECT_FORMAT_ELF)
9153 if (!dwarf_split_debug_info)
9154 secname = ".debug_types";
9155 else
9156 secname = ".debug_types.dwo";
9157
9158 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9159 sprintf (tmp, "wt.");
9160 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9161 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9162 comdat_key = get_identifier (tmp);
9163 targetm.asm_out.named_section (secname,
9164 SECTION_DEBUG | SECTION_LINKONCE,
9165 comdat_key);
9166 #else
9167 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9168 sprintf (tmp, ".gnu.linkonce.wt.");
9169 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9170 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9171 secname = tmp;
9172 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9173 #endif
9174
9175 /* Output debugging information. */
9176 output_compilation_unit_header ();
9177 output_signature (node->signature, "Type Signature");
9178 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9179 "Offset to Type DIE");
9180 output_die (node->root_die);
9181
9182 unmark_dies (node->root_die);
9183
9184 #if defined (OBJECT_FORMAT_ELF)
9185 if (dwarf_split_debug_info)
9186 {
9187 /* Produce the skeleton type-unit header. */
9188 const char *secname = ".debug_types";
9189
9190 targetm.asm_out.named_section (secname,
9191 SECTION_DEBUG | SECTION_LINKONCE,
9192 comdat_key);
9193 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9194 dw2_asm_output_data (4, 0xffffffff,
9195 "Initial length escape value indicating 64-bit DWARF extension");
9196
9197 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9198 DWARF_COMPILE_UNIT_HEADER_SIZE
9199 - DWARF_INITIAL_LENGTH_SIZE
9200 + size_of_die (get_skeleton_type_unit ())
9201 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9202 "Length of Type Unit Info");
9203 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9204 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9205 debug_skeleton_abbrev_section_label,
9206 debug_abbrev_section,
9207 "Offset Into Abbrev. Section");
9208 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9209 output_signature (node->signature, "Type Signature");
9210 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9211
9212 output_die (get_skeleton_type_unit ());
9213 }
9214 #endif
9215 }
9216
9217 /* Return the DWARF2/3 pubname associated with a decl. */
9218
9219 static const char *
9220 dwarf2_name (tree decl, int scope)
9221 {
9222 if (DECL_NAMELESS (decl))
9223 return NULL;
9224 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9225 }
9226
9227 /* Add a new entry to .debug_pubnames if appropriate. */
9228
9229 static void
9230 add_pubname_string (const char *str, dw_die_ref die)
9231 {
9232 pubname_entry e;
9233
9234 e.die = die;
9235 e.name = xstrdup (str);
9236 vec_safe_push (pubname_table, e);
9237 }
9238
9239 static void
9240 add_pubname (tree decl, dw_die_ref die)
9241 {
9242 if (!want_pubnames ())
9243 return;
9244
9245 /* Don't add items to the table when we expect that the consumer will have
9246 just read the enclosing die. For example, if the consumer is looking at a
9247 class_member, it will either be inside the class already, or will have just
9248 looked up the class to find the member. Either way, searching the class is
9249 faster than searching the index. */
9250 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9251 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9252 {
9253 const char *name = dwarf2_name (decl, 1);
9254
9255 if (name)
9256 add_pubname_string (name, die);
9257 }
9258 }
9259
9260 /* Add an enumerator to the pubnames section. */
9261
9262 static void
9263 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9264 {
9265 pubname_entry e;
9266
9267 gcc_assert (scope_name);
9268 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9269 e.die = die;
9270 vec_safe_push (pubname_table, e);
9271 }
9272
9273 /* Add a new entry to .debug_pubtypes if appropriate. */
9274
9275 static void
9276 add_pubtype (tree decl, dw_die_ref die)
9277 {
9278 pubname_entry e;
9279
9280 if (!want_pubnames ())
9281 return;
9282
9283 if ((TREE_PUBLIC (decl)
9284 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9285 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9286 {
9287 tree scope = NULL;
9288 const char *scope_name = "";
9289 const char *sep = is_cxx () ? "::" : ".";
9290 const char *name;
9291
9292 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9293 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9294 {
9295 scope_name = lang_hooks.dwarf_name (scope, 1);
9296 if (scope_name != NULL && scope_name[0] != '\0')
9297 scope_name = concat (scope_name, sep, NULL);
9298 else
9299 scope_name = "";
9300 }
9301
9302 if (TYPE_P (decl))
9303 name = type_tag (decl);
9304 else
9305 name = lang_hooks.dwarf_name (decl, 1);
9306
9307 /* If we don't have a name for the type, there's no point in adding
9308 it to the table. */
9309 if (name != NULL && name[0] != '\0')
9310 {
9311 e.die = die;
9312 e.name = concat (scope_name, name, NULL);
9313 vec_safe_push (pubtype_table, e);
9314 }
9315
9316 /* Although it might be more consistent to add the pubinfo for the
9317 enumerators as their dies are created, they should only be added if the
9318 enum type meets the criteria above. So rather than re-check the parent
9319 enum type whenever an enumerator die is created, just output them all
9320 here. This isn't protected by the name conditional because anonymous
9321 enums don't have names. */
9322 if (die->die_tag == DW_TAG_enumeration_type)
9323 {
9324 dw_die_ref c;
9325
9326 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9327 }
9328 }
9329 }
9330
9331 /* Output a single entry in the pubnames table. */
9332
9333 static void
9334 output_pubname (dw_offset die_offset, pubname_entry *entry)
9335 {
9336 dw_die_ref die = entry->die;
9337 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9338
9339 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9340
9341 if (debug_generate_pub_sections == 2)
9342 {
9343 /* This logic follows gdb's method for determining the value of the flag
9344 byte. */
9345 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9346 switch (die->die_tag)
9347 {
9348 case DW_TAG_typedef:
9349 case DW_TAG_base_type:
9350 case DW_TAG_subrange_type:
9351 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9352 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9353 break;
9354 case DW_TAG_enumerator:
9355 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9356 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9357 if (!is_cxx () && !is_java ())
9358 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9359 break;
9360 case DW_TAG_subprogram:
9361 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9362 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9363 if (!is_ada ())
9364 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9365 break;
9366 case DW_TAG_constant:
9367 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9368 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9369 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9370 break;
9371 case DW_TAG_variable:
9372 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9373 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9374 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9375 break;
9376 case DW_TAG_namespace:
9377 case DW_TAG_imported_declaration:
9378 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9379 break;
9380 case DW_TAG_class_type:
9381 case DW_TAG_interface_type:
9382 case DW_TAG_structure_type:
9383 case DW_TAG_union_type:
9384 case DW_TAG_enumeration_type:
9385 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9386 if (!is_cxx () && !is_java ())
9387 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9388 break;
9389 default:
9390 /* An unusual tag. Leave the flag-byte empty. */
9391 break;
9392 }
9393 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9394 "GDB-index flags");
9395 }
9396
9397 dw2_asm_output_nstring (entry->name, -1, "external name");
9398 }
9399
9400
9401 /* Output the public names table used to speed up access to externally
9402 visible names; or the public types table used to find type definitions. */
9403
9404 static void
9405 output_pubnames (vec<pubname_entry, va_gc> *names)
9406 {
9407 unsigned i;
9408 unsigned long pubnames_length = size_of_pubnames (names);
9409 pubname_ref pub;
9410
9411 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9412 dw2_asm_output_data (4, 0xffffffff,
9413 "Initial length escape value indicating 64-bit DWARF extension");
9414 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9415
9416 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9417 dw2_asm_output_data (2, 2, "DWARF Version");
9418
9419 if (dwarf_split_debug_info)
9420 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9421 debug_skeleton_info_section,
9422 "Offset of Compilation Unit Info");
9423 else
9424 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9425 debug_info_section,
9426 "Offset of Compilation Unit Info");
9427 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9428 "Compilation Unit Length");
9429
9430 FOR_EACH_VEC_ELT (*names, i, pub)
9431 {
9432 if (include_pubname_in_output (names, pub))
9433 {
9434 dw_offset die_offset = pub->die->die_offset;
9435
9436 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9437 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9438 gcc_assert (pub->die->die_mark);
9439
9440 /* If we're putting types in their own .debug_types sections,
9441 the .debug_pubtypes table will still point to the compile
9442 unit (not the type unit), so we want to use the offset of
9443 the skeleton DIE (if there is one). */
9444 if (pub->die->comdat_type_p && names == pubtype_table)
9445 {
9446 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9447
9448 if (type_node != NULL)
9449 die_offset = (type_node->skeleton_die != NULL
9450 ? type_node->skeleton_die->die_offset
9451 : comp_unit_die ()->die_offset);
9452 }
9453
9454 output_pubname (die_offset, pub);
9455 }
9456 }
9457
9458 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9459 }
9460
9461 /* Output public names and types tables if necessary. */
9462
9463 static void
9464 output_pubtables (void)
9465 {
9466 if (!want_pubnames () || !info_section_emitted)
9467 return;
9468
9469 switch_to_section (debug_pubnames_section);
9470 output_pubnames (pubname_table);
9471 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9472 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9473 simply won't look for the section. */
9474 switch_to_section (debug_pubtypes_section);
9475 output_pubnames (pubtype_table);
9476 }
9477
9478
9479 /* Output the information that goes into the .debug_aranges table.
9480 Namely, define the beginning and ending address range of the
9481 text section generated for this compilation unit. */
9482
9483 static void
9484 output_aranges (unsigned long aranges_length)
9485 {
9486 unsigned i;
9487
9488 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9489 dw2_asm_output_data (4, 0xffffffff,
9490 "Initial length escape value indicating 64-bit DWARF extension");
9491 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9492 "Length of Address Ranges Info");
9493 /* Version number for aranges is still 2, even in DWARF3. */
9494 dw2_asm_output_data (2, 2, "DWARF Version");
9495 if (dwarf_split_debug_info)
9496 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9497 debug_skeleton_info_section,
9498 "Offset of Compilation Unit Info");
9499 else
9500 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9501 debug_info_section,
9502 "Offset of Compilation Unit Info");
9503 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9504 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9505
9506 /* We need to align to twice the pointer size here. */
9507 if (DWARF_ARANGES_PAD_SIZE)
9508 {
9509 /* Pad using a 2 byte words so that padding is correct for any
9510 pointer size. */
9511 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9512 2 * DWARF2_ADDR_SIZE);
9513 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9514 dw2_asm_output_data (2, 0, NULL);
9515 }
9516
9517 /* It is necessary not to output these entries if the sections were
9518 not used; if the sections were not used, the length will be 0 and
9519 the address may end up as 0 if the section is discarded by ld
9520 --gc-sections, leaving an invalid (0, 0) entry that can be
9521 confused with the terminator. */
9522 if (text_section_used)
9523 {
9524 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9525 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9526 text_section_label, "Length");
9527 }
9528 if (cold_text_section_used)
9529 {
9530 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9531 "Address");
9532 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9533 cold_text_section_label, "Length");
9534 }
9535
9536 if (have_multiple_function_sections)
9537 {
9538 unsigned fde_idx;
9539 dw_fde_ref fde;
9540
9541 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9542 {
9543 if (DECL_IGNORED_P (fde->decl))
9544 continue;
9545 if (!fde->in_std_section)
9546 {
9547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9548 "Address");
9549 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9550 fde->dw_fde_begin, "Length");
9551 }
9552 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9553 {
9554 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9555 "Address");
9556 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9557 fde->dw_fde_second_begin, "Length");
9558 }
9559 }
9560 }
9561
9562 /* Output the terminator words. */
9563 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9564 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9565 }
9566
9567 /* Add a new entry to .debug_ranges. Return the offset at which it
9568 was placed. */
9569
9570 static unsigned int
9571 add_ranges_num (int num)
9572 {
9573 unsigned int in_use = ranges_table_in_use;
9574
9575 if (in_use == ranges_table_allocated)
9576 {
9577 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9578 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9579 ranges_table_allocated);
9580 memset (ranges_table + ranges_table_in_use, 0,
9581 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9582 }
9583
9584 ranges_table[in_use].num = num;
9585 ranges_table_in_use = in_use + 1;
9586
9587 return in_use * 2 * DWARF2_ADDR_SIZE;
9588 }
9589
9590 /* Add a new entry to .debug_ranges corresponding to a block, or a
9591 range terminator if BLOCK is NULL. */
9592
9593 static unsigned int
9594 add_ranges (const_tree block)
9595 {
9596 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9597 }
9598
9599 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9600 When using dwarf_split_debug_info, address attributes in dies destined
9601 for the final executable should be direct references--setting the
9602 parameter force_direct ensures this behavior. */
9603
9604 static void
9605 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9606 bool *added, bool force_direct)
9607 {
9608 unsigned int in_use = ranges_by_label_in_use;
9609 unsigned int offset;
9610
9611 if (in_use == ranges_by_label_allocated)
9612 {
9613 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9614 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9615 ranges_by_label,
9616 ranges_by_label_allocated);
9617 memset (ranges_by_label + ranges_by_label_in_use, 0,
9618 RANGES_TABLE_INCREMENT
9619 * sizeof (struct dw_ranges_by_label_struct));
9620 }
9621
9622 ranges_by_label[in_use].begin = begin;
9623 ranges_by_label[in_use].end = end;
9624 ranges_by_label_in_use = in_use + 1;
9625
9626 offset = add_ranges_num (-(int)in_use - 1);
9627 if (!*added)
9628 {
9629 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9630 *added = true;
9631 }
9632 }
9633
9634 static void
9635 output_ranges (void)
9636 {
9637 unsigned i;
9638 static const char *const start_fmt = "Offset %#x";
9639 const char *fmt = start_fmt;
9640
9641 for (i = 0; i < ranges_table_in_use; i++)
9642 {
9643 int block_num = ranges_table[i].num;
9644
9645 if (block_num > 0)
9646 {
9647 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9648 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9649
9650 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9651 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9652
9653 /* If all code is in the text section, then the compilation
9654 unit base address defaults to DW_AT_low_pc, which is the
9655 base of the text section. */
9656 if (!have_multiple_function_sections)
9657 {
9658 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9659 text_section_label,
9660 fmt, i * 2 * DWARF2_ADDR_SIZE);
9661 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9662 text_section_label, NULL);
9663 }
9664
9665 /* Otherwise, the compilation unit base address is zero,
9666 which allows us to use absolute addresses, and not worry
9667 about whether the target supports cross-section
9668 arithmetic. */
9669 else
9670 {
9671 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9672 fmt, i * 2 * DWARF2_ADDR_SIZE);
9673 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9674 }
9675
9676 fmt = NULL;
9677 }
9678
9679 /* Negative block_num stands for an index into ranges_by_label. */
9680 else if (block_num < 0)
9681 {
9682 int lab_idx = - block_num - 1;
9683
9684 if (!have_multiple_function_sections)
9685 {
9686 gcc_unreachable ();
9687 #if 0
9688 /* If we ever use add_ranges_by_labels () for a single
9689 function section, all we have to do is to take out
9690 the #if 0 above. */
9691 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9692 ranges_by_label[lab_idx].begin,
9693 text_section_label,
9694 fmt, i * 2 * DWARF2_ADDR_SIZE);
9695 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9696 ranges_by_label[lab_idx].end,
9697 text_section_label, NULL);
9698 #endif
9699 }
9700 else
9701 {
9702 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9703 ranges_by_label[lab_idx].begin,
9704 fmt, i * 2 * DWARF2_ADDR_SIZE);
9705 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9706 ranges_by_label[lab_idx].end,
9707 NULL);
9708 }
9709 }
9710 else
9711 {
9712 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9713 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9714 fmt = start_fmt;
9715 }
9716 }
9717 }
9718
9719 /* Data structure containing information about input files. */
9720 struct file_info
9721 {
9722 const char *path; /* Complete file name. */
9723 const char *fname; /* File name part. */
9724 int length; /* Length of entire string. */
9725 struct dwarf_file_data * file_idx; /* Index in input file table. */
9726 int dir_idx; /* Index in directory table. */
9727 };
9728
9729 /* Data structure containing information about directories with source
9730 files. */
9731 struct dir_info
9732 {
9733 const char *path; /* Path including directory name. */
9734 int length; /* Path length. */
9735 int prefix; /* Index of directory entry which is a prefix. */
9736 int count; /* Number of files in this directory. */
9737 int dir_idx; /* Index of directory used as base. */
9738 };
9739
9740 /* Callback function for file_info comparison. We sort by looking at
9741 the directories in the path. */
9742
9743 static int
9744 file_info_cmp (const void *p1, const void *p2)
9745 {
9746 const struct file_info *const s1 = (const struct file_info *) p1;
9747 const struct file_info *const s2 = (const struct file_info *) p2;
9748 const unsigned char *cp1;
9749 const unsigned char *cp2;
9750
9751 /* Take care of file names without directories. We need to make sure that
9752 we return consistent values to qsort since some will get confused if
9753 we return the same value when identical operands are passed in opposite
9754 orders. So if neither has a directory, return 0 and otherwise return
9755 1 or -1 depending on which one has the directory. */
9756 if ((s1->path == s1->fname || s2->path == s2->fname))
9757 return (s2->path == s2->fname) - (s1->path == s1->fname);
9758
9759 cp1 = (const unsigned char *) s1->path;
9760 cp2 = (const unsigned char *) s2->path;
9761
9762 while (1)
9763 {
9764 ++cp1;
9765 ++cp2;
9766 /* Reached the end of the first path? If so, handle like above. */
9767 if ((cp1 == (const unsigned char *) s1->fname)
9768 || (cp2 == (const unsigned char *) s2->fname))
9769 return ((cp2 == (const unsigned char *) s2->fname)
9770 - (cp1 == (const unsigned char *) s1->fname));
9771
9772 /* Character of current path component the same? */
9773 else if (*cp1 != *cp2)
9774 return *cp1 - *cp2;
9775 }
9776 }
9777
9778 struct file_name_acquire_data
9779 {
9780 struct file_info *files;
9781 int used_files;
9782 int max_files;
9783 };
9784
9785 /* Traversal function for the hash table. */
9786
9787 static int
9788 file_name_acquire (void ** slot, void *data)
9789 {
9790 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9791 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9792 struct file_info *fi;
9793 const char *f;
9794
9795 gcc_assert (fnad->max_files >= d->emitted_number);
9796
9797 if (! d->emitted_number)
9798 return 1;
9799
9800 gcc_assert (fnad->max_files != fnad->used_files);
9801
9802 fi = fnad->files + fnad->used_files++;
9803
9804 /* Skip all leading "./". */
9805 f = d->filename;
9806 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9807 f += 2;
9808
9809 /* Create a new array entry. */
9810 fi->path = f;
9811 fi->length = strlen (f);
9812 fi->file_idx = d;
9813
9814 /* Search for the file name part. */
9815 f = strrchr (f, DIR_SEPARATOR);
9816 #if defined (DIR_SEPARATOR_2)
9817 {
9818 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9819
9820 if (g != NULL)
9821 {
9822 if (f == NULL || f < g)
9823 f = g;
9824 }
9825 }
9826 #endif
9827
9828 fi->fname = f == NULL ? fi->path : f + 1;
9829 return 1;
9830 }
9831
9832 /* Output the directory table and the file name table. We try to minimize
9833 the total amount of memory needed. A heuristic is used to avoid large
9834 slowdowns with many input files. */
9835
9836 static void
9837 output_file_names (void)
9838 {
9839 struct file_name_acquire_data fnad;
9840 int numfiles;
9841 struct file_info *files;
9842 struct dir_info *dirs;
9843 int *saved;
9844 int *savehere;
9845 int *backmap;
9846 int ndirs;
9847 int idx_offset;
9848 int i;
9849
9850 if (!last_emitted_file)
9851 {
9852 dw2_asm_output_data (1, 0, "End directory table");
9853 dw2_asm_output_data (1, 0, "End file name table");
9854 return;
9855 }
9856
9857 numfiles = last_emitted_file->emitted_number;
9858
9859 /* Allocate the various arrays we need. */
9860 files = XALLOCAVEC (struct file_info, numfiles);
9861 dirs = XALLOCAVEC (struct dir_info, numfiles);
9862
9863 fnad.files = files;
9864 fnad.used_files = 0;
9865 fnad.max_files = numfiles;
9866 htab_traverse (file_table, file_name_acquire, &fnad);
9867 gcc_assert (fnad.used_files == fnad.max_files);
9868
9869 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9870
9871 /* Find all the different directories used. */
9872 dirs[0].path = files[0].path;
9873 dirs[0].length = files[0].fname - files[0].path;
9874 dirs[0].prefix = -1;
9875 dirs[0].count = 1;
9876 dirs[0].dir_idx = 0;
9877 files[0].dir_idx = 0;
9878 ndirs = 1;
9879
9880 for (i = 1; i < numfiles; i++)
9881 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9882 && memcmp (dirs[ndirs - 1].path, files[i].path,
9883 dirs[ndirs - 1].length) == 0)
9884 {
9885 /* Same directory as last entry. */
9886 files[i].dir_idx = ndirs - 1;
9887 ++dirs[ndirs - 1].count;
9888 }
9889 else
9890 {
9891 int j;
9892
9893 /* This is a new directory. */
9894 dirs[ndirs].path = files[i].path;
9895 dirs[ndirs].length = files[i].fname - files[i].path;
9896 dirs[ndirs].count = 1;
9897 dirs[ndirs].dir_idx = ndirs;
9898 files[i].dir_idx = ndirs;
9899
9900 /* Search for a prefix. */
9901 dirs[ndirs].prefix = -1;
9902 for (j = 0; j < ndirs; j++)
9903 if (dirs[j].length < dirs[ndirs].length
9904 && dirs[j].length > 1
9905 && (dirs[ndirs].prefix == -1
9906 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9907 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9908 dirs[ndirs].prefix = j;
9909
9910 ++ndirs;
9911 }
9912
9913 /* Now to the actual work. We have to find a subset of the directories which
9914 allow expressing the file name using references to the directory table
9915 with the least amount of characters. We do not do an exhaustive search
9916 where we would have to check out every combination of every single
9917 possible prefix. Instead we use a heuristic which provides nearly optimal
9918 results in most cases and never is much off. */
9919 saved = XALLOCAVEC (int, ndirs);
9920 savehere = XALLOCAVEC (int, ndirs);
9921
9922 memset (saved, '\0', ndirs * sizeof (saved[0]));
9923 for (i = 0; i < ndirs; i++)
9924 {
9925 int j;
9926 int total;
9927
9928 /* We can always save some space for the current directory. But this
9929 does not mean it will be enough to justify adding the directory. */
9930 savehere[i] = dirs[i].length;
9931 total = (savehere[i] - saved[i]) * dirs[i].count;
9932
9933 for (j = i + 1; j < ndirs; j++)
9934 {
9935 savehere[j] = 0;
9936 if (saved[j] < dirs[i].length)
9937 {
9938 /* Determine whether the dirs[i] path is a prefix of the
9939 dirs[j] path. */
9940 int k;
9941
9942 k = dirs[j].prefix;
9943 while (k != -1 && k != (int) i)
9944 k = dirs[k].prefix;
9945
9946 if (k == (int) i)
9947 {
9948 /* Yes it is. We can possibly save some memory by
9949 writing the filenames in dirs[j] relative to
9950 dirs[i]. */
9951 savehere[j] = dirs[i].length;
9952 total += (savehere[j] - saved[j]) * dirs[j].count;
9953 }
9954 }
9955 }
9956
9957 /* Check whether we can save enough to justify adding the dirs[i]
9958 directory. */
9959 if (total > dirs[i].length + 1)
9960 {
9961 /* It's worthwhile adding. */
9962 for (j = i; j < ndirs; j++)
9963 if (savehere[j] > 0)
9964 {
9965 /* Remember how much we saved for this directory so far. */
9966 saved[j] = savehere[j];
9967
9968 /* Remember the prefix directory. */
9969 dirs[j].dir_idx = i;
9970 }
9971 }
9972 }
9973
9974 /* Emit the directory name table. */
9975 idx_offset = dirs[0].length > 0 ? 1 : 0;
9976 for (i = 1 - idx_offset; i < ndirs; i++)
9977 dw2_asm_output_nstring (dirs[i].path,
9978 dirs[i].length
9979 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9980 "Directory Entry: %#x", i + idx_offset);
9981
9982 dw2_asm_output_data (1, 0, "End directory table");
9983
9984 /* We have to emit them in the order of emitted_number since that's
9985 used in the debug info generation. To do this efficiently we
9986 generate a back-mapping of the indices first. */
9987 backmap = XALLOCAVEC (int, numfiles);
9988 for (i = 0; i < numfiles; i++)
9989 backmap[files[i].file_idx->emitted_number - 1] = i;
9990
9991 /* Now write all the file names. */
9992 for (i = 0; i < numfiles; i++)
9993 {
9994 int file_idx = backmap[i];
9995 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9996
9997 #ifdef VMS_DEBUGGING_INFO
9998 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9999
10000 /* Setting these fields can lead to debugger miscomparisons,
10001 but VMS Debug requires them to be set correctly. */
10002
10003 int ver;
10004 long long cdt;
10005 long siz;
10006 int maxfilelen = strlen (files[file_idx].path)
10007 + dirs[dir_idx].length
10008 + MAX_VMS_VERSION_LEN + 1;
10009 char *filebuf = XALLOCAVEC (char, maxfilelen);
10010
10011 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10012 snprintf (filebuf, maxfilelen, "%s;%d",
10013 files[file_idx].path + dirs[dir_idx].length, ver);
10014
10015 dw2_asm_output_nstring
10016 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10017
10018 /* Include directory index. */
10019 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10020
10021 /* Modification time. */
10022 dw2_asm_output_data_uleb128
10023 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10024 ? cdt : 0,
10025 NULL);
10026
10027 /* File length in bytes. */
10028 dw2_asm_output_data_uleb128
10029 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10030 ? siz : 0,
10031 NULL);
10032 #else
10033 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10034 "File Entry: %#x", (unsigned) i + 1);
10035
10036 /* Include directory index. */
10037 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10038
10039 /* Modification time. */
10040 dw2_asm_output_data_uleb128 (0, NULL);
10041
10042 /* File length in bytes. */
10043 dw2_asm_output_data_uleb128 (0, NULL);
10044 #endif /* VMS_DEBUGGING_INFO */
10045 }
10046
10047 dw2_asm_output_data (1, 0, "End file name table");
10048 }
10049
10050
10051 /* Output one line number table into the .debug_line section. */
10052
10053 static void
10054 output_one_line_info_table (dw_line_info_table *table)
10055 {
10056 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10057 unsigned int current_line = 1;
10058 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10059 dw_line_info_entry *ent;
10060 size_t i;
10061
10062 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10063 {
10064 switch (ent->opcode)
10065 {
10066 case LI_set_address:
10067 /* ??? Unfortunately, we have little choice here currently, and
10068 must always use the most general form. GCC does not know the
10069 address delta itself, so we can't use DW_LNS_advance_pc. Many
10070 ports do have length attributes which will give an upper bound
10071 on the address range. We could perhaps use length attributes
10072 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10073 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10074
10075 /* This can handle any delta. This takes
10076 4+DWARF2_ADDR_SIZE bytes. */
10077 dw2_asm_output_data (1, 0, "set address %s", line_label);
10078 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10079 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10080 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10081 break;
10082
10083 case LI_set_line:
10084 if (ent->val == current_line)
10085 {
10086 /* We still need to start a new row, so output a copy insn. */
10087 dw2_asm_output_data (1, DW_LNS_copy,
10088 "copy line %u", current_line);
10089 }
10090 else
10091 {
10092 int line_offset = ent->val - current_line;
10093 int line_delta = line_offset - DWARF_LINE_BASE;
10094
10095 current_line = ent->val;
10096 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10097 {
10098 /* This can handle deltas from -10 to 234, using the current
10099 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10100 This takes 1 byte. */
10101 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10102 "line %u", current_line);
10103 }
10104 else
10105 {
10106 /* This can handle any delta. This takes at least 4 bytes,
10107 depending on the value being encoded. */
10108 dw2_asm_output_data (1, DW_LNS_advance_line,
10109 "advance to line %u", current_line);
10110 dw2_asm_output_data_sleb128 (line_offset, NULL);
10111 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10112 }
10113 }
10114 break;
10115
10116 case LI_set_file:
10117 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10118 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10119 break;
10120
10121 case LI_set_column:
10122 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10123 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10124 break;
10125
10126 case LI_negate_stmt:
10127 current_is_stmt = !current_is_stmt;
10128 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10129 "is_stmt %d", current_is_stmt);
10130 break;
10131
10132 case LI_set_prologue_end:
10133 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10134 "set prologue end");
10135 break;
10136
10137 case LI_set_epilogue_begin:
10138 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10139 "set epilogue begin");
10140 break;
10141
10142 case LI_set_discriminator:
10143 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10144 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10145 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10146 dw2_asm_output_data_uleb128 (ent->val, NULL);
10147 break;
10148 }
10149 }
10150
10151 /* Emit debug info for the address of the end of the table. */
10152 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10153 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10154 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10155 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10156
10157 dw2_asm_output_data (1, 0, "end sequence");
10158 dw2_asm_output_data_uleb128 (1, NULL);
10159 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10160 }
10161
10162 /* Output the source line number correspondence information. This
10163 information goes into the .debug_line section. */
10164
10165 static void
10166 output_line_info (bool prologue_only)
10167 {
10168 char l1[20], l2[20], p1[20], p2[20];
10169 int ver = dwarf_version;
10170 bool saw_one = false;
10171 int opc;
10172
10173 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10174 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10175 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10176 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10177
10178 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10179 dw2_asm_output_data (4, 0xffffffff,
10180 "Initial length escape value indicating 64-bit DWARF extension");
10181 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10182 "Length of Source Line Info");
10183 ASM_OUTPUT_LABEL (asm_out_file, l1);
10184
10185 dw2_asm_output_data (2, ver, "DWARF Version");
10186 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10187 ASM_OUTPUT_LABEL (asm_out_file, p1);
10188
10189 /* Define the architecture-dependent minimum instruction length (in bytes).
10190 In this implementation of DWARF, this field is used for information
10191 purposes only. Since GCC generates assembly language, we have no
10192 a priori knowledge of how many instruction bytes are generated for each
10193 source line, and therefore can use only the DW_LNE_set_address and
10194 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10195 this as '1', which is "correct enough" for all architectures,
10196 and don't let the target override. */
10197 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10198
10199 if (ver >= 4)
10200 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10201 "Maximum Operations Per Instruction");
10202 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10203 "Default is_stmt_start flag");
10204 dw2_asm_output_data (1, DWARF_LINE_BASE,
10205 "Line Base Value (Special Opcodes)");
10206 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10207 "Line Range Value (Special Opcodes)");
10208 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10209 "Special Opcode Base");
10210
10211 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10212 {
10213 int n_op_args;
10214 switch (opc)
10215 {
10216 case DW_LNS_advance_pc:
10217 case DW_LNS_advance_line:
10218 case DW_LNS_set_file:
10219 case DW_LNS_set_column:
10220 case DW_LNS_fixed_advance_pc:
10221 case DW_LNS_set_isa:
10222 n_op_args = 1;
10223 break;
10224 default:
10225 n_op_args = 0;
10226 break;
10227 }
10228
10229 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10230 opc, n_op_args);
10231 }
10232
10233 /* Write out the information about the files we use. */
10234 output_file_names ();
10235 ASM_OUTPUT_LABEL (asm_out_file, p2);
10236 if (prologue_only)
10237 {
10238 /* Output the marker for the end of the line number info. */
10239 ASM_OUTPUT_LABEL (asm_out_file, l2);
10240 return;
10241 }
10242
10243 if (separate_line_info)
10244 {
10245 dw_line_info_table *table;
10246 size_t i;
10247
10248 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10249 if (table->in_use)
10250 {
10251 output_one_line_info_table (table);
10252 saw_one = true;
10253 }
10254 }
10255 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10256 {
10257 output_one_line_info_table (cold_text_section_line_info);
10258 saw_one = true;
10259 }
10260
10261 /* ??? Some Darwin linkers crash on a .debug_line section with no
10262 sequences. Further, merely a DW_LNE_end_sequence entry is not
10263 sufficient -- the address column must also be initialized.
10264 Make sure to output at least one set_address/end_sequence pair,
10265 choosing .text since that section is always present. */
10266 if (text_section_line_info->in_use || !saw_one)
10267 output_one_line_info_table (text_section_line_info);
10268
10269 /* Output the marker for the end of the line number info. */
10270 ASM_OUTPUT_LABEL (asm_out_file, l2);
10271 }
10272 \f
10273 /* Given a pointer to a tree node for some base type, return a pointer to
10274 a DIE that describes the given type.
10275
10276 This routine must only be called for GCC type nodes that correspond to
10277 Dwarf base (fundamental) types. */
10278
10279 static dw_die_ref
10280 base_type_die (tree type)
10281 {
10282 dw_die_ref base_type_result;
10283 enum dwarf_type encoding;
10284
10285 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10286 return 0;
10287
10288 /* If this is a subtype that should not be emitted as a subrange type,
10289 use the base type. See subrange_type_for_debug_p. */
10290 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10291 type = TREE_TYPE (type);
10292
10293 switch (TREE_CODE (type))
10294 {
10295 case INTEGER_TYPE:
10296 if ((dwarf_version >= 4 || !dwarf_strict)
10297 && TYPE_NAME (type)
10298 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10299 && DECL_IS_BUILTIN (TYPE_NAME (type))
10300 && DECL_NAME (TYPE_NAME (type)))
10301 {
10302 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10303 if (strcmp (name, "char16_t") == 0
10304 || strcmp (name, "char32_t") == 0)
10305 {
10306 encoding = DW_ATE_UTF;
10307 break;
10308 }
10309 }
10310 if (TYPE_STRING_FLAG (type))
10311 {
10312 if (TYPE_UNSIGNED (type))
10313 encoding = DW_ATE_unsigned_char;
10314 else
10315 encoding = DW_ATE_signed_char;
10316 }
10317 else if (TYPE_UNSIGNED (type))
10318 encoding = DW_ATE_unsigned;
10319 else
10320 encoding = DW_ATE_signed;
10321 break;
10322
10323 case REAL_TYPE:
10324 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10325 {
10326 if (dwarf_version >= 3 || !dwarf_strict)
10327 encoding = DW_ATE_decimal_float;
10328 else
10329 encoding = DW_ATE_lo_user;
10330 }
10331 else
10332 encoding = DW_ATE_float;
10333 break;
10334
10335 case FIXED_POINT_TYPE:
10336 if (!(dwarf_version >= 3 || !dwarf_strict))
10337 encoding = DW_ATE_lo_user;
10338 else if (TYPE_UNSIGNED (type))
10339 encoding = DW_ATE_unsigned_fixed;
10340 else
10341 encoding = DW_ATE_signed_fixed;
10342 break;
10343
10344 /* Dwarf2 doesn't know anything about complex ints, so use
10345 a user defined type for it. */
10346 case COMPLEX_TYPE:
10347 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10348 encoding = DW_ATE_complex_float;
10349 else
10350 encoding = DW_ATE_lo_user;
10351 break;
10352
10353 case BOOLEAN_TYPE:
10354 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10355 encoding = DW_ATE_boolean;
10356 break;
10357
10358 default:
10359 /* No other TREE_CODEs are Dwarf fundamental types. */
10360 gcc_unreachable ();
10361 }
10362
10363 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10364
10365 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10366 int_size_in_bytes (type));
10367 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10368 add_pubtype (type, base_type_result);
10369
10370 return base_type_result;
10371 }
10372
10373 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10374 named 'auto' in its type: return true for it, false otherwise. */
10375
10376 static inline bool
10377 is_cxx_auto (tree type)
10378 {
10379 if (is_cxx ())
10380 {
10381 tree name = TYPE_IDENTIFIER (type);
10382 if (name == get_identifier ("auto")
10383 || name == get_identifier ("decltype(auto)"))
10384 return true;
10385 }
10386 return false;
10387 }
10388
10389 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10390 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10391
10392 static inline int
10393 is_base_type (tree type)
10394 {
10395 switch (TREE_CODE (type))
10396 {
10397 case ERROR_MARK:
10398 case VOID_TYPE:
10399 case INTEGER_TYPE:
10400 case REAL_TYPE:
10401 case FIXED_POINT_TYPE:
10402 case COMPLEX_TYPE:
10403 case BOOLEAN_TYPE:
10404 return 1;
10405
10406 case ARRAY_TYPE:
10407 case RECORD_TYPE:
10408 case UNION_TYPE:
10409 case QUAL_UNION_TYPE:
10410 case ENUMERAL_TYPE:
10411 case FUNCTION_TYPE:
10412 case METHOD_TYPE:
10413 case POINTER_TYPE:
10414 case REFERENCE_TYPE:
10415 case NULLPTR_TYPE:
10416 case OFFSET_TYPE:
10417 case LANG_TYPE:
10418 case VECTOR_TYPE:
10419 return 0;
10420
10421 default:
10422 if (is_cxx_auto (type))
10423 return 0;
10424 gcc_unreachable ();
10425 }
10426
10427 return 0;
10428 }
10429
10430 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10431 node, return the size in bits for the type if it is a constant, or else
10432 return the alignment for the type if the type's size is not constant, or
10433 else return BITS_PER_WORD if the type actually turns out to be an
10434 ERROR_MARK node. */
10435
10436 static inline unsigned HOST_WIDE_INT
10437 simple_type_size_in_bits (const_tree type)
10438 {
10439 if (TREE_CODE (type) == ERROR_MARK)
10440 return BITS_PER_WORD;
10441 else if (TYPE_SIZE (type) == NULL_TREE)
10442 return 0;
10443 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10444 return tree_to_uhwi (TYPE_SIZE (type));
10445 else
10446 return TYPE_ALIGN (type);
10447 }
10448
10449 /* Similarly, but return an offset_int instead of UHWI. */
10450
10451 static inline offset_int
10452 offset_int_type_size_in_bits (const_tree type)
10453 {
10454 if (TREE_CODE (type) == ERROR_MARK)
10455 return BITS_PER_WORD;
10456 else if (TYPE_SIZE (type) == NULL_TREE)
10457 return 0;
10458 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10459 return wi::to_offset (TYPE_SIZE (type));
10460 else
10461 return TYPE_ALIGN (type);
10462 }
10463
10464 /* Given a pointer to a tree node for a subrange type, return a pointer
10465 to a DIE that describes the given type. */
10466
10467 static dw_die_ref
10468 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10469 {
10470 dw_die_ref subrange_die;
10471 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10472
10473 if (context_die == NULL)
10474 context_die = comp_unit_die ();
10475
10476 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10477
10478 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10479 {
10480 /* The size of the subrange type and its base type do not match,
10481 so we need to generate a size attribute for the subrange type. */
10482 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10483 }
10484
10485 if (low)
10486 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10487 if (high)
10488 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10489
10490 return subrange_die;
10491 }
10492
10493 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10494 entry that chains various modifiers in front of the given type. */
10495
10496 static dw_die_ref
10497 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10498 dw_die_ref context_die)
10499 {
10500 enum tree_code code = TREE_CODE (type);
10501 dw_die_ref mod_type_die;
10502 dw_die_ref sub_die = NULL;
10503 tree item_type = NULL;
10504 tree qualified_type;
10505 tree name, low, high;
10506 dw_die_ref mod_scope;
10507
10508 if (code == ERROR_MARK)
10509 return NULL;
10510
10511 /* See if we already have the appropriately qualified variant of
10512 this type. */
10513 qualified_type
10514 = get_qualified_type (type,
10515 ((is_const_type ? TYPE_QUAL_CONST : 0)
10516 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10517
10518 if (qualified_type == sizetype
10519 && TYPE_NAME (qualified_type)
10520 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10521 {
10522 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10523
10524 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10525 && TYPE_PRECISION (t)
10526 == TYPE_PRECISION (qualified_type)
10527 && TYPE_UNSIGNED (t)
10528 == TYPE_UNSIGNED (qualified_type));
10529 qualified_type = t;
10530 }
10531
10532 /* If we do, then we can just use its DIE, if it exists. */
10533 if (qualified_type)
10534 {
10535 mod_type_die = lookup_type_die (qualified_type);
10536 if (mod_type_die)
10537 return mod_type_die;
10538 }
10539
10540 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10541
10542 /* Handle C typedef types. */
10543 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10544 && !DECL_ARTIFICIAL (name))
10545 {
10546 tree dtype = TREE_TYPE (name);
10547
10548 if (qualified_type == dtype)
10549 {
10550 /* For a named type, use the typedef. */
10551 gen_type_die (qualified_type, context_die);
10552 return lookup_type_die (qualified_type);
10553 }
10554 else if (is_const_type < TYPE_READONLY (dtype)
10555 || is_volatile_type < TYPE_VOLATILE (dtype)
10556 || (is_const_type <= TYPE_READONLY (dtype)
10557 && is_volatile_type <= TYPE_VOLATILE (dtype)
10558 && DECL_ORIGINAL_TYPE (name) != type))
10559 /* cv-unqualified version of named type. Just use the unnamed
10560 type to which it refers. */
10561 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10562 is_const_type, is_volatile_type,
10563 context_die);
10564 /* Else cv-qualified version of named type; fall through. */
10565 }
10566
10567 mod_scope = scope_die_for (type, context_die);
10568
10569 if (is_const_type
10570 /* If both is_const_type and is_volatile_type, prefer the path
10571 which leads to a qualified type. */
10572 && (!is_volatile_type
10573 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10574 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10575 {
10576 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10577 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10578 }
10579 else if (is_volatile_type)
10580 {
10581 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10582 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10583 }
10584 else if (code == POINTER_TYPE)
10585 {
10586 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10587 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10588 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10589 item_type = TREE_TYPE (type);
10590 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10591 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10592 TYPE_ADDR_SPACE (item_type));
10593 }
10594 else if (code == REFERENCE_TYPE)
10595 {
10596 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10597 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10598 type);
10599 else
10600 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10601 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10602 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10603 item_type = TREE_TYPE (type);
10604 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10605 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10606 TYPE_ADDR_SPACE (item_type));
10607 }
10608 else if (code == INTEGER_TYPE
10609 && TREE_TYPE (type) != NULL_TREE
10610 && subrange_type_for_debug_p (type, &low, &high))
10611 {
10612 mod_type_die = subrange_type_die (type, low, high, context_die);
10613 item_type = TREE_TYPE (type);
10614 }
10615 else if (is_base_type (type))
10616 mod_type_die = base_type_die (type);
10617 else
10618 {
10619 gen_type_die (type, context_die);
10620
10621 /* We have to get the type_main_variant here (and pass that to the
10622 `lookup_type_die' routine) because the ..._TYPE node we have
10623 might simply be a *copy* of some original type node (where the
10624 copy was created to help us keep track of typedef names) and
10625 that copy might have a different TYPE_UID from the original
10626 ..._TYPE node. */
10627 if (TREE_CODE (type) != VECTOR_TYPE)
10628 return lookup_type_die (type_main_variant (type));
10629 else
10630 /* Vectors have the debugging information in the type,
10631 not the main variant. */
10632 return lookup_type_die (type);
10633 }
10634
10635 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10636 don't output a DW_TAG_typedef, since there isn't one in the
10637 user's program; just attach a DW_AT_name to the type.
10638 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10639 if the base type already has the same name. */
10640 if (name
10641 && ((TREE_CODE (name) != TYPE_DECL
10642 && (qualified_type == TYPE_MAIN_VARIANT (type)
10643 || (!is_const_type && !is_volatile_type)))
10644 || (TREE_CODE (name) == TYPE_DECL
10645 && TREE_TYPE (name) == qualified_type
10646 && DECL_NAME (name))))
10647 {
10648 if (TREE_CODE (name) == TYPE_DECL)
10649 /* Could just call add_name_and_src_coords_attributes here,
10650 but since this is a builtin type it doesn't have any
10651 useful source coordinates anyway. */
10652 name = DECL_NAME (name);
10653 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10654 }
10655 /* This probably indicates a bug. */
10656 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10657 {
10658 name = TYPE_IDENTIFIER (type);
10659 add_name_attribute (mod_type_die,
10660 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10661 }
10662
10663 if (qualified_type)
10664 equate_type_number_to_die (qualified_type, mod_type_die);
10665
10666 if (item_type)
10667 /* We must do this after the equate_type_number_to_die call, in case
10668 this is a recursive type. This ensures that the modified_type_die
10669 recursion will terminate even if the type is recursive. Recursive
10670 types are possible in Ada. */
10671 sub_die = modified_type_die (item_type,
10672 TYPE_READONLY (item_type),
10673 TYPE_VOLATILE (item_type),
10674 context_die);
10675
10676 if (sub_die != NULL)
10677 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10678
10679 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10680 if (TYPE_ARTIFICIAL (type))
10681 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10682
10683 return mod_type_die;
10684 }
10685
10686 /* Generate DIEs for the generic parameters of T.
10687 T must be either a generic type or a generic function.
10688 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10689
10690 static void
10691 gen_generic_params_dies (tree t)
10692 {
10693 tree parms, args;
10694 int parms_num, i;
10695 dw_die_ref die = NULL;
10696 int non_default;
10697
10698 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10699 return;
10700
10701 if (TYPE_P (t))
10702 die = lookup_type_die (t);
10703 else if (DECL_P (t))
10704 die = lookup_decl_die (t);
10705
10706 gcc_assert (die);
10707
10708 parms = lang_hooks.get_innermost_generic_parms (t);
10709 if (!parms)
10710 /* T has no generic parameter. It means T is neither a generic type
10711 or function. End of story. */
10712 return;
10713
10714 parms_num = TREE_VEC_LENGTH (parms);
10715 args = lang_hooks.get_innermost_generic_args (t);
10716 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10717 non_default = int_cst_value (TREE_CHAIN (args));
10718 else
10719 non_default = TREE_VEC_LENGTH (args);
10720 for (i = 0; i < parms_num; i++)
10721 {
10722 tree parm, arg, arg_pack_elems;
10723 dw_die_ref parm_die;
10724
10725 parm = TREE_VEC_ELT (parms, i);
10726 arg = TREE_VEC_ELT (args, i);
10727 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10728 gcc_assert (parm && TREE_VALUE (parm) && arg);
10729
10730 if (parm && TREE_VALUE (parm) && arg)
10731 {
10732 /* If PARM represents a template parameter pack,
10733 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10734 by DW_TAG_template_*_parameter DIEs for the argument
10735 pack elements of ARG. Note that ARG would then be
10736 an argument pack. */
10737 if (arg_pack_elems)
10738 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10739 arg_pack_elems,
10740 die);
10741 else
10742 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10743 true /* emit name */, die);
10744 if (i >= non_default)
10745 add_AT_flag (parm_die, DW_AT_default_value, 1);
10746 }
10747 }
10748 }
10749
10750 /* Create and return a DIE for PARM which should be
10751 the representation of a generic type parameter.
10752 For instance, in the C++ front end, PARM would be a template parameter.
10753 ARG is the argument to PARM.
10754 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10755 name of the PARM.
10756 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10757 as a child node. */
10758
10759 static dw_die_ref
10760 generic_parameter_die (tree parm, tree arg,
10761 bool emit_name_p,
10762 dw_die_ref parent_die)
10763 {
10764 dw_die_ref tmpl_die = NULL;
10765 const char *name = NULL;
10766
10767 if (!parm || !DECL_NAME (parm) || !arg)
10768 return NULL;
10769
10770 /* We support non-type generic parameters and arguments,
10771 type generic parameters and arguments, as well as
10772 generic generic parameters (a.k.a. template template parameters in C++)
10773 and arguments. */
10774 if (TREE_CODE (parm) == PARM_DECL)
10775 /* PARM is a nontype generic parameter */
10776 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10777 else if (TREE_CODE (parm) == TYPE_DECL)
10778 /* PARM is a type generic parameter. */
10779 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10780 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10781 /* PARM is a generic generic parameter.
10782 Its DIE is a GNU extension. It shall have a
10783 DW_AT_name attribute to represent the name of the template template
10784 parameter, and a DW_AT_GNU_template_name attribute to represent the
10785 name of the template template argument. */
10786 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10787 parent_die, parm);
10788 else
10789 gcc_unreachable ();
10790
10791 if (tmpl_die)
10792 {
10793 tree tmpl_type;
10794
10795 /* If PARM is a generic parameter pack, it means we are
10796 emitting debug info for a template argument pack element.
10797 In other terms, ARG is a template argument pack element.
10798 In that case, we don't emit any DW_AT_name attribute for
10799 the die. */
10800 if (emit_name_p)
10801 {
10802 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10803 gcc_assert (name);
10804 add_AT_string (tmpl_die, DW_AT_name, name);
10805 }
10806
10807 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10808 {
10809 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10810 TMPL_DIE should have a child DW_AT_type attribute that is set
10811 to the type of the argument to PARM, which is ARG.
10812 If PARM is a type generic parameter, TMPL_DIE should have a
10813 child DW_AT_type that is set to ARG. */
10814 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10815 add_type_attribute (tmpl_die, tmpl_type, 0,
10816 TREE_THIS_VOLATILE (tmpl_type),
10817 parent_die);
10818 }
10819 else
10820 {
10821 /* So TMPL_DIE is a DIE representing a
10822 a generic generic template parameter, a.k.a template template
10823 parameter in C++ and arg is a template. */
10824
10825 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10826 to the name of the argument. */
10827 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10828 if (name)
10829 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10830 }
10831
10832 if (TREE_CODE (parm) == PARM_DECL)
10833 /* So PARM is a non-type generic parameter.
10834 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10835 attribute of TMPL_DIE which value represents the value
10836 of ARG.
10837 We must be careful here:
10838 The value of ARG might reference some function decls.
10839 We might currently be emitting debug info for a generic
10840 type and types are emitted before function decls, we don't
10841 know if the function decls referenced by ARG will actually be
10842 emitted after cgraph computations.
10843 So must defer the generation of the DW_AT_const_value to
10844 after cgraph is ready. */
10845 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10846 }
10847
10848 return tmpl_die;
10849 }
10850
10851 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10852 PARM_PACK must be a template parameter pack. The returned DIE
10853 will be child DIE of PARENT_DIE. */
10854
10855 static dw_die_ref
10856 template_parameter_pack_die (tree parm_pack,
10857 tree parm_pack_args,
10858 dw_die_ref parent_die)
10859 {
10860 dw_die_ref die;
10861 int j;
10862
10863 gcc_assert (parent_die && parm_pack);
10864
10865 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10866 add_name_and_src_coords_attributes (die, parm_pack);
10867 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10868 generic_parameter_die (parm_pack,
10869 TREE_VEC_ELT (parm_pack_args, j),
10870 false /* Don't emit DW_AT_name */,
10871 die);
10872 return die;
10873 }
10874
10875 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10876 an enumerated type. */
10877
10878 static inline int
10879 type_is_enum (const_tree type)
10880 {
10881 return TREE_CODE (type) == ENUMERAL_TYPE;
10882 }
10883
10884 /* Return the DBX register number described by a given RTL node. */
10885
10886 static unsigned int
10887 dbx_reg_number (const_rtx rtl)
10888 {
10889 unsigned regno = REGNO (rtl);
10890
10891 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10892
10893 #ifdef LEAF_REG_REMAP
10894 if (crtl->uses_only_leaf_regs)
10895 {
10896 int leaf_reg = LEAF_REG_REMAP (regno);
10897 if (leaf_reg != -1)
10898 regno = (unsigned) leaf_reg;
10899 }
10900 #endif
10901
10902 regno = DBX_REGISTER_NUMBER (regno);
10903 gcc_assert (regno != INVALID_REGNUM);
10904 return regno;
10905 }
10906
10907 /* Optionally add a DW_OP_piece term to a location description expression.
10908 DW_OP_piece is only added if the location description expression already
10909 doesn't end with DW_OP_piece. */
10910
10911 static void
10912 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10913 {
10914 dw_loc_descr_ref loc;
10915
10916 if (*list_head != NULL)
10917 {
10918 /* Find the end of the chain. */
10919 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10920 ;
10921
10922 if (loc->dw_loc_opc != DW_OP_piece)
10923 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10924 }
10925 }
10926
10927 /* Return a location descriptor that designates a machine register or
10928 zero if there is none. */
10929
10930 static dw_loc_descr_ref
10931 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10932 {
10933 rtx regs;
10934
10935 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10936 return 0;
10937
10938 /* We only use "frame base" when we're sure we're talking about the
10939 post-prologue local stack frame. We do this by *not* running
10940 register elimination until this point, and recognizing the special
10941 argument pointer and soft frame pointer rtx's.
10942 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10943 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10944 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10945 {
10946 dw_loc_descr_ref result = NULL;
10947
10948 if (dwarf_version >= 4 || !dwarf_strict)
10949 {
10950 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10951 initialized);
10952 if (result)
10953 add_loc_descr (&result,
10954 new_loc_descr (DW_OP_stack_value, 0, 0));
10955 }
10956 return result;
10957 }
10958
10959 regs = targetm.dwarf_register_span (rtl);
10960
10961 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10962 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10963 else
10964 {
10965 unsigned int dbx_regnum = dbx_reg_number (rtl);
10966 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10967 return 0;
10968 return one_reg_loc_descriptor (dbx_regnum, initialized);
10969 }
10970 }
10971
10972 /* Return a location descriptor that designates a machine register for
10973 a given hard register number. */
10974
10975 static dw_loc_descr_ref
10976 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10977 {
10978 dw_loc_descr_ref reg_loc_descr;
10979
10980 if (regno <= 31)
10981 reg_loc_descr
10982 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10983 else
10984 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10985
10986 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10987 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10988
10989 return reg_loc_descr;
10990 }
10991
10992 /* Given an RTL of a register, return a location descriptor that
10993 designates a value that spans more than one register. */
10994
10995 static dw_loc_descr_ref
10996 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10997 enum var_init_status initialized)
10998 {
10999 int size, i;
11000 dw_loc_descr_ref loc_result = NULL;
11001
11002 /* Simple, contiguous registers. */
11003 if (regs == NULL_RTX)
11004 {
11005 unsigned reg = REGNO (rtl);
11006 int nregs;
11007
11008 #ifdef LEAF_REG_REMAP
11009 if (crtl->uses_only_leaf_regs)
11010 {
11011 int leaf_reg = LEAF_REG_REMAP (reg);
11012 if (leaf_reg != -1)
11013 reg = (unsigned) leaf_reg;
11014 }
11015 #endif
11016
11017 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11018 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11019
11020 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11021
11022 loc_result = NULL;
11023 while (nregs--)
11024 {
11025 dw_loc_descr_ref t;
11026
11027 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11028 VAR_INIT_STATUS_INITIALIZED);
11029 add_loc_descr (&loc_result, t);
11030 add_loc_descr_op_piece (&loc_result, size);
11031 ++reg;
11032 }
11033 return loc_result;
11034 }
11035
11036 /* Now onto stupid register sets in non contiguous locations. */
11037
11038 gcc_assert (GET_CODE (regs) == PARALLEL);
11039
11040 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11041 loc_result = NULL;
11042
11043 for (i = 0; i < XVECLEN (regs, 0); ++i)
11044 {
11045 dw_loc_descr_ref t;
11046
11047 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11048 VAR_INIT_STATUS_INITIALIZED);
11049 add_loc_descr (&loc_result, t);
11050 add_loc_descr_op_piece (&loc_result, size);
11051 }
11052
11053 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11054 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11055 return loc_result;
11056 }
11057
11058 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11059
11060 /* Return a location descriptor that designates a constant i,
11061 as a compound operation from constant (i >> shift), constant shift
11062 and DW_OP_shl. */
11063
11064 static dw_loc_descr_ref
11065 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11066 {
11067 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11068 add_loc_descr (&ret, int_loc_descriptor (shift));
11069 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11070 return ret;
11071 }
11072
11073 /* Return a location descriptor that designates a constant. */
11074
11075 static dw_loc_descr_ref
11076 int_loc_descriptor (HOST_WIDE_INT i)
11077 {
11078 enum dwarf_location_atom op;
11079
11080 /* Pick the smallest representation of a constant, rather than just
11081 defaulting to the LEB encoding. */
11082 if (i >= 0)
11083 {
11084 int clz = clz_hwi (i);
11085 int ctz = ctz_hwi (i);
11086 if (i <= 31)
11087 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11088 else if (i <= 0xff)
11089 op = DW_OP_const1u;
11090 else if (i <= 0xffff)
11091 op = DW_OP_const2u;
11092 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11093 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11094 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11095 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11096 while DW_OP_const4u is 5 bytes. */
11097 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11098 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11099 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11100 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11101 while DW_OP_const4u is 5 bytes. */
11102 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11103 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11104 op = DW_OP_const4u;
11105 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11106 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11107 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11108 while DW_OP_constu of constant >= 0x100000000 takes at least
11109 6 bytes. */
11110 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11111 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11112 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11113 >= HOST_BITS_PER_WIDE_INT)
11114 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11115 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11116 while DW_OP_constu takes in this case at least 6 bytes. */
11117 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11118 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11119 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11120 && size_of_uleb128 (i) > 6)
11121 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11122 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11123 else
11124 op = DW_OP_constu;
11125 }
11126 else
11127 {
11128 if (i >= -0x80)
11129 op = DW_OP_const1s;
11130 else if (i >= -0x8000)
11131 op = DW_OP_const2s;
11132 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11133 {
11134 if (size_of_int_loc_descriptor (i) < 5)
11135 {
11136 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11137 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11138 return ret;
11139 }
11140 op = DW_OP_const4s;
11141 }
11142 else
11143 {
11144 if (size_of_int_loc_descriptor (i)
11145 < (unsigned long) 1 + size_of_sleb128 (i))
11146 {
11147 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11148 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11149 return ret;
11150 }
11151 op = DW_OP_consts;
11152 }
11153 }
11154
11155 return new_loc_descr (op, i, 0);
11156 }
11157
11158 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11159 without actually allocating it. */
11160
11161 static unsigned long
11162 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11163 {
11164 return size_of_int_loc_descriptor (i >> shift)
11165 + size_of_int_loc_descriptor (shift)
11166 + 1;
11167 }
11168
11169 /* Return size_of_locs (int_loc_descriptor (i)) without
11170 actually allocating it. */
11171
11172 static unsigned long
11173 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11174 {
11175 unsigned long s;
11176
11177 if (i >= 0)
11178 {
11179 int clz, ctz;
11180 if (i <= 31)
11181 return 1;
11182 else if (i <= 0xff)
11183 return 2;
11184 else if (i <= 0xffff)
11185 return 3;
11186 clz = clz_hwi (i);
11187 ctz = ctz_hwi (i);
11188 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11189 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11190 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11191 - clz - 5);
11192 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11193 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11194 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11195 - clz - 8);
11196 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11197 return 5;
11198 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11199 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11200 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11201 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11202 - clz - 8);
11203 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11204 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11205 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11206 - clz - 16);
11207 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11208 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11209 && s > 6)
11210 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11211 - clz - 32);
11212 else
11213 return 1 + s;
11214 }
11215 else
11216 {
11217 if (i >= -0x80)
11218 return 2;
11219 else if (i >= -0x8000)
11220 return 3;
11221 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11222 {
11223 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11224 {
11225 s = size_of_int_loc_descriptor (-i) + 1;
11226 if (s < 5)
11227 return s;
11228 }
11229 return 5;
11230 }
11231 else
11232 {
11233 unsigned long r = 1 + size_of_sleb128 (i);
11234 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11235 {
11236 s = size_of_int_loc_descriptor (-i) + 1;
11237 if (s < r)
11238 return s;
11239 }
11240 return r;
11241 }
11242 }
11243 }
11244
11245 /* Return loc description representing "address" of integer value.
11246 This can appear only as toplevel expression. */
11247
11248 static dw_loc_descr_ref
11249 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11250 {
11251 int litsize;
11252 dw_loc_descr_ref loc_result = NULL;
11253
11254 if (!(dwarf_version >= 4 || !dwarf_strict))
11255 return NULL;
11256
11257 litsize = size_of_int_loc_descriptor (i);
11258 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11259 is more compact. For DW_OP_stack_value we need:
11260 litsize + 1 (DW_OP_stack_value)
11261 and for DW_OP_implicit_value:
11262 1 (DW_OP_implicit_value) + 1 (length) + size. */
11263 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11264 {
11265 loc_result = int_loc_descriptor (i);
11266 add_loc_descr (&loc_result,
11267 new_loc_descr (DW_OP_stack_value, 0, 0));
11268 return loc_result;
11269 }
11270
11271 loc_result = new_loc_descr (DW_OP_implicit_value,
11272 size, 0);
11273 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11274 loc_result->dw_loc_oprnd2.v.val_int = i;
11275 return loc_result;
11276 }
11277
11278 /* Return a location descriptor that designates a base+offset location. */
11279
11280 static dw_loc_descr_ref
11281 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11282 enum var_init_status initialized)
11283 {
11284 unsigned int regno;
11285 dw_loc_descr_ref result;
11286 dw_fde_ref fde = cfun->fde;
11287
11288 /* We only use "frame base" when we're sure we're talking about the
11289 post-prologue local stack frame. We do this by *not* running
11290 register elimination until this point, and recognizing the special
11291 argument pointer and soft frame pointer rtx's. */
11292 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11293 {
11294 rtx elim = (ira_use_lra_p
11295 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11296 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11297
11298 if (elim != reg)
11299 {
11300 if (GET_CODE (elim) == PLUS)
11301 {
11302 offset += INTVAL (XEXP (elim, 1));
11303 elim = XEXP (elim, 0);
11304 }
11305 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11306 && (elim == hard_frame_pointer_rtx
11307 || elim == stack_pointer_rtx))
11308 || elim == (frame_pointer_needed
11309 ? hard_frame_pointer_rtx
11310 : stack_pointer_rtx));
11311
11312 /* If drap register is used to align stack, use frame
11313 pointer + offset to access stack variables. If stack
11314 is aligned without drap, use stack pointer + offset to
11315 access stack variables. */
11316 if (crtl->stack_realign_tried
11317 && reg == frame_pointer_rtx)
11318 {
11319 int base_reg
11320 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11321 ? HARD_FRAME_POINTER_REGNUM
11322 : REGNO (elim));
11323 return new_reg_loc_descr (base_reg, offset);
11324 }
11325
11326 gcc_assert (frame_pointer_fb_offset_valid);
11327 offset += frame_pointer_fb_offset;
11328 return new_loc_descr (DW_OP_fbreg, offset, 0);
11329 }
11330 }
11331
11332 regno = REGNO (reg);
11333 #ifdef LEAF_REG_REMAP
11334 if (crtl->uses_only_leaf_regs)
11335 {
11336 int leaf_reg = LEAF_REG_REMAP (regno);
11337 if (leaf_reg != -1)
11338 regno = (unsigned) leaf_reg;
11339 }
11340 #endif
11341 regno = DWARF_FRAME_REGNUM (regno);
11342
11343 if (!optimize && fde
11344 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11345 {
11346 /* Use cfa+offset to represent the location of arguments passed
11347 on the stack when drap is used to align stack.
11348 Only do this when not optimizing, for optimized code var-tracking
11349 is supposed to track where the arguments live and the register
11350 used as vdrap or drap in some spot might be used for something
11351 else in other part of the routine. */
11352 return new_loc_descr (DW_OP_fbreg, offset, 0);
11353 }
11354
11355 if (regno <= 31)
11356 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11357 offset, 0);
11358 else
11359 result = new_loc_descr (DW_OP_bregx, regno, offset);
11360
11361 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11362 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11363
11364 return result;
11365 }
11366
11367 /* Return true if this RTL expression describes a base+offset calculation. */
11368
11369 static inline int
11370 is_based_loc (const_rtx rtl)
11371 {
11372 return (GET_CODE (rtl) == PLUS
11373 && ((REG_P (XEXP (rtl, 0))
11374 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11375 && CONST_INT_P (XEXP (rtl, 1)))));
11376 }
11377
11378 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11379 failed. */
11380
11381 static dw_loc_descr_ref
11382 tls_mem_loc_descriptor (rtx mem)
11383 {
11384 tree base;
11385 dw_loc_descr_ref loc_result;
11386
11387 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11388 return NULL;
11389
11390 base = get_base_address (MEM_EXPR (mem));
11391 if (base == NULL
11392 || TREE_CODE (base) != VAR_DECL
11393 || !DECL_THREAD_LOCAL_P (base))
11394 return NULL;
11395
11396 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11397 if (loc_result == NULL)
11398 return NULL;
11399
11400 if (MEM_OFFSET (mem))
11401 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11402
11403 return loc_result;
11404 }
11405
11406 /* Output debug info about reason why we failed to expand expression as dwarf
11407 expression. */
11408
11409 static void
11410 expansion_failed (tree expr, rtx rtl, char const *reason)
11411 {
11412 if (dump_file && (dump_flags & TDF_DETAILS))
11413 {
11414 fprintf (dump_file, "Failed to expand as dwarf: ");
11415 if (expr)
11416 print_generic_expr (dump_file, expr, dump_flags);
11417 if (rtl)
11418 {
11419 fprintf (dump_file, "\n");
11420 print_rtl (dump_file, rtl);
11421 }
11422 fprintf (dump_file, "\nReason: %s\n", reason);
11423 }
11424 }
11425
11426 /* Helper function for const_ok_for_output, called either directly
11427 or via for_each_rtx. */
11428
11429 static int
11430 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11431 {
11432 rtx rtl = *rtlp;
11433
11434 if (GET_CODE (rtl) == UNSPEC)
11435 {
11436 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11437 we can't express it in the debug info. */
11438 #ifdef ENABLE_CHECKING
11439 /* Don't complain about TLS UNSPECs, those are just too hard to
11440 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11441 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11442 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11443 if (XVECLEN (rtl, 0) == 0
11444 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11445 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11446 inform (current_function_decl
11447 ? DECL_SOURCE_LOCATION (current_function_decl)
11448 : UNKNOWN_LOCATION,
11449 #if NUM_UNSPEC_VALUES > 0
11450 "non-delegitimized UNSPEC %s (%d) found in variable location",
11451 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11452 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11453 XINT (rtl, 1));
11454 #else
11455 "non-delegitimized UNSPEC %d found in variable location",
11456 XINT (rtl, 1));
11457 #endif
11458 #endif
11459 expansion_failed (NULL_TREE, rtl,
11460 "UNSPEC hasn't been delegitimized.\n");
11461 return 1;
11462 }
11463
11464 if (targetm.const_not_ok_for_debug_p (rtl))
11465 {
11466 expansion_failed (NULL_TREE, rtl,
11467 "Expression rejected for debug by the backend.\n");
11468 return 1;
11469 }
11470
11471 /* FIXME: Refer to PR60655. It is possible for simplification
11472 of rtl expressions in var tracking to produce such expressions.
11473 We should really identify / validate expressions
11474 enclosed in CONST that can be handled by assemblers on various
11475 targets and only handle legitimate cases here. */
11476 if (GET_CODE (rtl) != SYMBOL_REF)
11477 {
11478 if (GET_CODE (rtl) == NOT)
11479 return 1;
11480
11481 return 0;
11482 }
11483
11484 if (CONSTANT_POOL_ADDRESS_P (rtl))
11485 {
11486 bool marked;
11487 get_pool_constant_mark (rtl, &marked);
11488 /* If all references to this pool constant were optimized away,
11489 it was not output and thus we can't represent it. */
11490 if (!marked)
11491 {
11492 expansion_failed (NULL_TREE, rtl,
11493 "Constant was removed from constant pool.\n");
11494 return 1;
11495 }
11496 }
11497
11498 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11499 return 1;
11500
11501 /* Avoid references to external symbols in debug info, on several targets
11502 the linker might even refuse to link when linking a shared library,
11503 and in many other cases the relocations for .debug_info/.debug_loc are
11504 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11505 to be defined within the same shared library or executable are fine. */
11506 if (SYMBOL_REF_EXTERNAL_P (rtl))
11507 {
11508 tree decl = SYMBOL_REF_DECL (rtl);
11509
11510 if (decl == NULL || !targetm.binds_local_p (decl))
11511 {
11512 expansion_failed (NULL_TREE, rtl,
11513 "Symbol not defined in current TU.\n");
11514 return 1;
11515 }
11516 }
11517
11518 return 0;
11519 }
11520
11521 /* Return true if constant RTL can be emitted in DW_OP_addr or
11522 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11523 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11524
11525 static bool
11526 const_ok_for_output (rtx rtl)
11527 {
11528 if (GET_CODE (rtl) == SYMBOL_REF)
11529 return const_ok_for_output_1 (&rtl, NULL) == 0;
11530
11531 if (GET_CODE (rtl) == CONST)
11532 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11533
11534 return true;
11535 }
11536
11537 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11538 if possible, NULL otherwise. */
11539
11540 static dw_die_ref
11541 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11542 {
11543 dw_die_ref type_die;
11544 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11545
11546 if (type == NULL)
11547 return NULL;
11548 switch (TREE_CODE (type))
11549 {
11550 case INTEGER_TYPE:
11551 case REAL_TYPE:
11552 break;
11553 default:
11554 return NULL;
11555 }
11556 type_die = lookup_type_die (type);
11557 if (!type_die)
11558 type_die = modified_type_die (type, false, false, comp_unit_die ());
11559 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11560 return NULL;
11561 return type_die;
11562 }
11563
11564 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11565 type matching MODE, or, if MODE is narrower than or as wide as
11566 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11567 possible. */
11568
11569 static dw_loc_descr_ref
11570 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11571 {
11572 enum machine_mode outer_mode = mode;
11573 dw_die_ref type_die;
11574 dw_loc_descr_ref cvt;
11575
11576 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11577 {
11578 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11579 return op;
11580 }
11581 type_die = base_type_for_mode (outer_mode, 1);
11582 if (type_die == NULL)
11583 return NULL;
11584 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11585 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11586 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11587 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11588 add_loc_descr (&op, cvt);
11589 return op;
11590 }
11591
11592 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11593
11594 static dw_loc_descr_ref
11595 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11596 dw_loc_descr_ref op1)
11597 {
11598 dw_loc_descr_ref ret = op0;
11599 add_loc_descr (&ret, op1);
11600 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11601 if (STORE_FLAG_VALUE != 1)
11602 {
11603 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11604 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11605 }
11606 return ret;
11607 }
11608
11609 /* Return location descriptor for signed comparison OP RTL. */
11610
11611 static dw_loc_descr_ref
11612 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11613 enum machine_mode mem_mode)
11614 {
11615 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11616 dw_loc_descr_ref op0, op1;
11617 int shift;
11618
11619 if (op_mode == VOIDmode)
11620 op_mode = GET_MODE (XEXP (rtl, 1));
11621 if (op_mode == VOIDmode)
11622 return NULL;
11623
11624 if (dwarf_strict
11625 && (GET_MODE_CLASS (op_mode) != MODE_INT
11626 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11627 return NULL;
11628
11629 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11630 VAR_INIT_STATUS_INITIALIZED);
11631 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11632 VAR_INIT_STATUS_INITIALIZED);
11633
11634 if (op0 == NULL || op1 == NULL)
11635 return NULL;
11636
11637 if (GET_MODE_CLASS (op_mode) != MODE_INT
11638 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11639 return compare_loc_descriptor (op, op0, op1);
11640
11641 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11642 {
11643 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11644 dw_loc_descr_ref cvt;
11645
11646 if (type_die == NULL)
11647 return NULL;
11648 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11649 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11650 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11651 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11652 add_loc_descr (&op0, cvt);
11653 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11654 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11655 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11656 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11657 add_loc_descr (&op1, cvt);
11658 return compare_loc_descriptor (op, op0, op1);
11659 }
11660
11661 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11662 /* For eq/ne, if the operands are known to be zero-extended,
11663 there is no need to do the fancy shifting up. */
11664 if (op == DW_OP_eq || op == DW_OP_ne)
11665 {
11666 dw_loc_descr_ref last0, last1;
11667 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11668 ;
11669 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11670 ;
11671 /* deref_size zero extends, and for constants we can check
11672 whether they are zero extended or not. */
11673 if (((last0->dw_loc_opc == DW_OP_deref_size
11674 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11675 || (CONST_INT_P (XEXP (rtl, 0))
11676 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11677 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11678 && ((last1->dw_loc_opc == DW_OP_deref_size
11679 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11680 || (CONST_INT_P (XEXP (rtl, 1))
11681 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11682 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11683 return compare_loc_descriptor (op, op0, op1);
11684
11685 /* EQ/NE comparison against constant in narrower type than
11686 DWARF2_ADDR_SIZE can be performed either as
11687 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11688 DW_OP_{eq,ne}
11689 or
11690 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11691 DW_OP_{eq,ne}. Pick whatever is shorter. */
11692 if (CONST_INT_P (XEXP (rtl, 1))
11693 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11694 && (size_of_int_loc_descriptor (shift) + 1
11695 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11696 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11697 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11698 & GET_MODE_MASK (op_mode))))
11699 {
11700 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11701 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11702 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11703 & GET_MODE_MASK (op_mode));
11704 return compare_loc_descriptor (op, op0, op1);
11705 }
11706 }
11707 add_loc_descr (&op0, int_loc_descriptor (shift));
11708 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11709 if (CONST_INT_P (XEXP (rtl, 1)))
11710 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11711 else
11712 {
11713 add_loc_descr (&op1, int_loc_descriptor (shift));
11714 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11715 }
11716 return compare_loc_descriptor (op, op0, op1);
11717 }
11718
11719 /* Return location descriptor for unsigned comparison OP RTL. */
11720
11721 static dw_loc_descr_ref
11722 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11723 enum machine_mode mem_mode)
11724 {
11725 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11726 dw_loc_descr_ref op0, op1;
11727
11728 if (op_mode == VOIDmode)
11729 op_mode = GET_MODE (XEXP (rtl, 1));
11730 if (op_mode == VOIDmode)
11731 return NULL;
11732 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11733 return NULL;
11734
11735 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11736 return NULL;
11737
11738 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11739 VAR_INIT_STATUS_INITIALIZED);
11740 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11741 VAR_INIT_STATUS_INITIALIZED);
11742
11743 if (op0 == NULL || op1 == NULL)
11744 return NULL;
11745
11746 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11747 {
11748 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11749 dw_loc_descr_ref last0, last1;
11750 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11751 ;
11752 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11753 ;
11754 if (CONST_INT_P (XEXP (rtl, 0)))
11755 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11756 /* deref_size zero extends, so no need to mask it again. */
11757 else if (last0->dw_loc_opc != DW_OP_deref_size
11758 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11759 {
11760 add_loc_descr (&op0, int_loc_descriptor (mask));
11761 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11762 }
11763 if (CONST_INT_P (XEXP (rtl, 1)))
11764 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11765 /* deref_size zero extends, so no need to mask it again. */
11766 else if (last1->dw_loc_opc != DW_OP_deref_size
11767 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11768 {
11769 add_loc_descr (&op1, int_loc_descriptor (mask));
11770 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11771 }
11772 }
11773 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11774 {
11775 HOST_WIDE_INT bias = 1;
11776 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11777 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11778 if (CONST_INT_P (XEXP (rtl, 1)))
11779 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11780 + INTVAL (XEXP (rtl, 1)));
11781 else
11782 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11783 bias, 0));
11784 }
11785 return compare_loc_descriptor (op, op0, op1);
11786 }
11787
11788 /* Return location descriptor for {U,S}{MIN,MAX}. */
11789
11790 static dw_loc_descr_ref
11791 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11792 enum machine_mode mem_mode)
11793 {
11794 enum dwarf_location_atom op;
11795 dw_loc_descr_ref op0, op1, ret;
11796 dw_loc_descr_ref bra_node, drop_node;
11797
11798 if (dwarf_strict
11799 && (GET_MODE_CLASS (mode) != MODE_INT
11800 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11801 return NULL;
11802
11803 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11804 VAR_INIT_STATUS_INITIALIZED);
11805 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11806 VAR_INIT_STATUS_INITIALIZED);
11807
11808 if (op0 == NULL || op1 == NULL)
11809 return NULL;
11810
11811 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11812 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11813 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11814 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11815 {
11816 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11817 {
11818 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11819 add_loc_descr (&op0, int_loc_descriptor (mask));
11820 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11821 add_loc_descr (&op1, int_loc_descriptor (mask));
11822 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11823 }
11824 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11825 {
11826 HOST_WIDE_INT bias = 1;
11827 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11828 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11829 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11830 }
11831 }
11832 else if (GET_MODE_CLASS (mode) == MODE_INT
11833 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11834 {
11835 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11836 add_loc_descr (&op0, int_loc_descriptor (shift));
11837 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11838 add_loc_descr (&op1, int_loc_descriptor (shift));
11839 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11840 }
11841 else if (GET_MODE_CLASS (mode) == MODE_INT
11842 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11843 {
11844 dw_die_ref type_die = base_type_for_mode (mode, 0);
11845 dw_loc_descr_ref cvt;
11846 if (type_die == NULL)
11847 return NULL;
11848 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11849 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11850 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11851 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11852 add_loc_descr (&op0, cvt);
11853 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11854 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11855 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11856 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11857 add_loc_descr (&op1, cvt);
11858 }
11859
11860 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11861 op = DW_OP_lt;
11862 else
11863 op = DW_OP_gt;
11864 ret = op0;
11865 add_loc_descr (&ret, op1);
11866 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11867 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11868 add_loc_descr (&ret, bra_node);
11869 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11870 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11871 add_loc_descr (&ret, drop_node);
11872 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11873 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11874 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11875 && GET_MODE_CLASS (mode) == MODE_INT
11876 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11877 ret = convert_descriptor_to_mode (mode, ret);
11878 return ret;
11879 }
11880
11881 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11882 but after converting arguments to type_die, afterwards
11883 convert back to unsigned. */
11884
11885 static dw_loc_descr_ref
11886 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11887 enum machine_mode mode, enum machine_mode mem_mode)
11888 {
11889 dw_loc_descr_ref cvt, op0, op1;
11890
11891 if (type_die == NULL)
11892 return NULL;
11893 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11894 VAR_INIT_STATUS_INITIALIZED);
11895 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11896 VAR_INIT_STATUS_INITIALIZED);
11897 if (op0 == NULL || op1 == NULL)
11898 return NULL;
11899 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11900 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11901 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11902 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11903 add_loc_descr (&op0, cvt);
11904 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11905 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11906 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11907 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11908 add_loc_descr (&op1, cvt);
11909 add_loc_descr (&op0, op1);
11910 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11911 return convert_descriptor_to_mode (mode, op0);
11912 }
11913
11914 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11915 const0 is DW_OP_lit0 or corresponding typed constant,
11916 const1 is DW_OP_lit1 or corresponding typed constant
11917 and constMSB is constant with just the MSB bit set
11918 for the mode):
11919 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11920 L1: const0 DW_OP_swap
11921 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11922 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11923 L3: DW_OP_drop
11924 L4: DW_OP_nop
11925
11926 CTZ is similar:
11927 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11928 L1: const0 DW_OP_swap
11929 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11930 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11931 L3: DW_OP_drop
11932 L4: DW_OP_nop
11933
11934 FFS is similar:
11935 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11936 L1: const1 DW_OP_swap
11937 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11938 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11939 L3: DW_OP_drop
11940 L4: DW_OP_nop */
11941
11942 static dw_loc_descr_ref
11943 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11944 enum machine_mode mem_mode)
11945 {
11946 dw_loc_descr_ref op0, ret, tmp;
11947 HOST_WIDE_INT valv;
11948 dw_loc_descr_ref l1jump, l1label;
11949 dw_loc_descr_ref l2jump, l2label;
11950 dw_loc_descr_ref l3jump, l3label;
11951 dw_loc_descr_ref l4jump, l4label;
11952 rtx msb;
11953
11954 if (GET_MODE_CLASS (mode) != MODE_INT
11955 || GET_MODE (XEXP (rtl, 0)) != mode)
11956 return NULL;
11957
11958 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11959 VAR_INIT_STATUS_INITIALIZED);
11960 if (op0 == NULL)
11961 return NULL;
11962 ret = op0;
11963 if (GET_CODE (rtl) == CLZ)
11964 {
11965 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11966 valv = GET_MODE_BITSIZE (mode);
11967 }
11968 else if (GET_CODE (rtl) == FFS)
11969 valv = 0;
11970 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11971 valv = GET_MODE_BITSIZE (mode);
11972 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11973 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11974 add_loc_descr (&ret, l1jump);
11975 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11976 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11977 VAR_INIT_STATUS_INITIALIZED);
11978 if (tmp == NULL)
11979 return NULL;
11980 add_loc_descr (&ret, tmp);
11981 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11982 add_loc_descr (&ret, l4jump);
11983 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11984 ? const1_rtx : const0_rtx,
11985 mode, mem_mode,
11986 VAR_INIT_STATUS_INITIALIZED);
11987 if (l1label == NULL)
11988 return NULL;
11989 add_loc_descr (&ret, l1label);
11990 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11991 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11992 add_loc_descr (&ret, l2label);
11993 if (GET_CODE (rtl) != CLZ)
11994 msb = const1_rtx;
11995 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11996 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11997 << (GET_MODE_BITSIZE (mode) - 1));
11998 else
11999 msb = immed_wide_int_const
12000 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12001 GET_MODE_PRECISION (mode)), mode);
12002 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12003 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12004 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12005 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12006 else
12007 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12008 VAR_INIT_STATUS_INITIALIZED);
12009 if (tmp == NULL)
12010 return NULL;
12011 add_loc_descr (&ret, tmp);
12012 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12013 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12014 add_loc_descr (&ret, l3jump);
12015 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12016 VAR_INIT_STATUS_INITIALIZED);
12017 if (tmp == NULL)
12018 return NULL;
12019 add_loc_descr (&ret, tmp);
12020 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12021 ? DW_OP_shl : DW_OP_shr, 0, 0));
12022 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12023 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12024 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12025 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12026 add_loc_descr (&ret, l2jump);
12027 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12028 add_loc_descr (&ret, l3label);
12029 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12030 add_loc_descr (&ret, l4label);
12031 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12032 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12033 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12034 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12035 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12036 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12037 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12038 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12039 return ret;
12040 }
12041
12042 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12043 const1 is DW_OP_lit1 or corresponding typed constant):
12044 const0 DW_OP_swap
12045 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12046 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12047 L2: DW_OP_drop
12048
12049 PARITY is similar:
12050 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12051 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12052 L2: DW_OP_drop */
12053
12054 static dw_loc_descr_ref
12055 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12056 enum machine_mode mem_mode)
12057 {
12058 dw_loc_descr_ref op0, ret, tmp;
12059 dw_loc_descr_ref l1jump, l1label;
12060 dw_loc_descr_ref l2jump, l2label;
12061
12062 if (GET_MODE_CLASS (mode) != MODE_INT
12063 || GET_MODE (XEXP (rtl, 0)) != mode)
12064 return NULL;
12065
12066 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12067 VAR_INIT_STATUS_INITIALIZED);
12068 if (op0 == NULL)
12069 return NULL;
12070 ret = op0;
12071 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12072 VAR_INIT_STATUS_INITIALIZED);
12073 if (tmp == NULL)
12074 return NULL;
12075 add_loc_descr (&ret, tmp);
12076 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12077 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12078 add_loc_descr (&ret, l1label);
12079 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12080 add_loc_descr (&ret, l2jump);
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12083 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12084 VAR_INIT_STATUS_INITIALIZED);
12085 if (tmp == NULL)
12086 return NULL;
12087 add_loc_descr (&ret, tmp);
12088 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12089 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12090 ? DW_OP_plus : DW_OP_xor, 0, 0));
12091 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12092 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12093 VAR_INIT_STATUS_INITIALIZED);
12094 add_loc_descr (&ret, tmp);
12095 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12096 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12097 add_loc_descr (&ret, l1jump);
12098 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12099 add_loc_descr (&ret, l2label);
12100 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12101 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12102 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12103 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12104 return ret;
12105 }
12106
12107 /* BSWAP (constS is initial shift count, either 56 or 24):
12108 constS const0
12109 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12110 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12111 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12112 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12113 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12114
12115 static dw_loc_descr_ref
12116 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12117 enum machine_mode mem_mode)
12118 {
12119 dw_loc_descr_ref op0, ret, tmp;
12120 dw_loc_descr_ref l1jump, l1label;
12121 dw_loc_descr_ref l2jump, l2label;
12122
12123 if (GET_MODE_CLASS (mode) != MODE_INT
12124 || BITS_PER_UNIT != 8
12125 || (GET_MODE_BITSIZE (mode) != 32
12126 && GET_MODE_BITSIZE (mode) != 64))
12127 return NULL;
12128
12129 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12130 VAR_INIT_STATUS_INITIALIZED);
12131 if (op0 == NULL)
12132 return NULL;
12133
12134 ret = op0;
12135 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12136 mode, mem_mode,
12137 VAR_INIT_STATUS_INITIALIZED);
12138 if (tmp == NULL)
12139 return NULL;
12140 add_loc_descr (&ret, tmp);
12141 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12142 VAR_INIT_STATUS_INITIALIZED);
12143 if (tmp == NULL)
12144 return NULL;
12145 add_loc_descr (&ret, tmp);
12146 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12147 add_loc_descr (&ret, l1label);
12148 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12149 mode, mem_mode,
12150 VAR_INIT_STATUS_INITIALIZED);
12151 add_loc_descr (&ret, tmp);
12152 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12153 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12154 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12155 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12156 VAR_INIT_STATUS_INITIALIZED);
12157 if (tmp == NULL)
12158 return NULL;
12159 add_loc_descr (&ret, tmp);
12160 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12161 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12162 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12163 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12164 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12165 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12166 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12167 VAR_INIT_STATUS_INITIALIZED);
12168 add_loc_descr (&ret, tmp);
12169 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12170 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12171 add_loc_descr (&ret, l2jump);
12172 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12173 VAR_INIT_STATUS_INITIALIZED);
12174 add_loc_descr (&ret, tmp);
12175 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12176 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12177 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12178 add_loc_descr (&ret, l1jump);
12179 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12180 add_loc_descr (&ret, l2label);
12181 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12182 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12183 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12184 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12185 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12186 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12187 return ret;
12188 }
12189
12190 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12191 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12192 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12193 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12194
12195 ROTATERT is similar:
12196 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12197 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12198 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12199
12200 static dw_loc_descr_ref
12201 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12202 enum machine_mode mem_mode)
12203 {
12204 rtx rtlop1 = XEXP (rtl, 1);
12205 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12206 int i;
12207
12208 if (GET_MODE_CLASS (mode) != MODE_INT)
12209 return NULL;
12210
12211 if (GET_MODE (rtlop1) != VOIDmode
12212 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12213 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12214 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12215 VAR_INIT_STATUS_INITIALIZED);
12216 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12217 VAR_INIT_STATUS_INITIALIZED);
12218 if (op0 == NULL || op1 == NULL)
12219 return NULL;
12220 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12221 for (i = 0; i < 2; i++)
12222 {
12223 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12224 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12225 mode, mem_mode,
12226 VAR_INIT_STATUS_INITIALIZED);
12227 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12228 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12229 ? DW_OP_const4u
12230 : HOST_BITS_PER_WIDE_INT == 64
12231 ? DW_OP_const8u : DW_OP_constu,
12232 GET_MODE_MASK (mode), 0);
12233 else
12234 mask[i] = NULL;
12235 if (mask[i] == NULL)
12236 return NULL;
12237 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12238 }
12239 ret = op0;
12240 add_loc_descr (&ret, op1);
12241 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12242 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12243 if (GET_CODE (rtl) == ROTATERT)
12244 {
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12246 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12247 GET_MODE_BITSIZE (mode), 0));
12248 }
12249 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12250 if (mask[0] != NULL)
12251 add_loc_descr (&ret, mask[0]);
12252 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12253 if (mask[1] != NULL)
12254 {
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12256 add_loc_descr (&ret, mask[1]);
12257 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12258 }
12259 if (GET_CODE (rtl) == ROTATE)
12260 {
12261 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12262 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12263 GET_MODE_BITSIZE (mode), 0));
12264 }
12265 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12266 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12267 return ret;
12268 }
12269
12270 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12271 for DEBUG_PARAMETER_REF RTL. */
12272
12273 static dw_loc_descr_ref
12274 parameter_ref_descriptor (rtx rtl)
12275 {
12276 dw_loc_descr_ref ret;
12277 dw_die_ref ref;
12278
12279 if (dwarf_strict)
12280 return NULL;
12281 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12282 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12283 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12284 if (ref)
12285 {
12286 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12287 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12288 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12289 }
12290 else
12291 {
12292 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12293 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12294 }
12295 return ret;
12296 }
12297
12298 /* The following routine converts the RTL for a variable or parameter
12299 (resident in memory) into an equivalent Dwarf representation of a
12300 mechanism for getting the address of that same variable onto the top of a
12301 hypothetical "address evaluation" stack.
12302
12303 When creating memory location descriptors, we are effectively transforming
12304 the RTL for a memory-resident object into its Dwarf postfix expression
12305 equivalent. This routine recursively descends an RTL tree, turning
12306 it into Dwarf postfix code as it goes.
12307
12308 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12309
12310 MEM_MODE is the mode of the memory reference, needed to handle some
12311 autoincrement addressing modes.
12312
12313 Return 0 if we can't represent the location. */
12314
12315 dw_loc_descr_ref
12316 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12317 enum machine_mode mem_mode,
12318 enum var_init_status initialized)
12319 {
12320 dw_loc_descr_ref mem_loc_result = NULL;
12321 enum dwarf_location_atom op;
12322 dw_loc_descr_ref op0, op1;
12323 rtx inner = NULL_RTX;
12324
12325 if (mode == VOIDmode)
12326 mode = GET_MODE (rtl);
12327
12328 /* Note that for a dynamically sized array, the location we will generate a
12329 description of here will be the lowest numbered location which is
12330 actually within the array. That's *not* necessarily the same as the
12331 zeroth element of the array. */
12332
12333 rtl = targetm.delegitimize_address (rtl);
12334
12335 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12336 return NULL;
12337
12338 switch (GET_CODE (rtl))
12339 {
12340 case POST_INC:
12341 case POST_DEC:
12342 case POST_MODIFY:
12343 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12344
12345 case SUBREG:
12346 /* The case of a subreg may arise when we have a local (register)
12347 variable or a formal (register) parameter which doesn't quite fill
12348 up an entire register. For now, just assume that it is
12349 legitimate to make the Dwarf info refer to the whole register which
12350 contains the given subreg. */
12351 if (!subreg_lowpart_p (rtl))
12352 break;
12353 inner = SUBREG_REG (rtl);
12354 case TRUNCATE:
12355 if (inner == NULL_RTX)
12356 inner = XEXP (rtl, 0);
12357 if (GET_MODE_CLASS (mode) == MODE_INT
12358 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12359 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12360 #ifdef POINTERS_EXTEND_UNSIGNED
12361 || (mode == Pmode && mem_mode != VOIDmode)
12362 #endif
12363 )
12364 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12365 {
12366 mem_loc_result = mem_loc_descriptor (inner,
12367 GET_MODE (inner),
12368 mem_mode, initialized);
12369 break;
12370 }
12371 if (dwarf_strict)
12372 break;
12373 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12374 break;
12375 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12376 && (GET_MODE_CLASS (mode) != MODE_INT
12377 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12378 break;
12379 else
12380 {
12381 dw_die_ref type_die;
12382 dw_loc_descr_ref cvt;
12383
12384 mem_loc_result = mem_loc_descriptor (inner,
12385 GET_MODE (inner),
12386 mem_mode, initialized);
12387 if (mem_loc_result == NULL)
12388 break;
12389 type_die = base_type_for_mode (mode,
12390 GET_MODE_CLASS (mode) == MODE_INT);
12391 if (type_die == NULL)
12392 {
12393 mem_loc_result = NULL;
12394 break;
12395 }
12396 if (GET_MODE_SIZE (mode)
12397 != GET_MODE_SIZE (GET_MODE (inner)))
12398 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12399 else
12400 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12401 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12402 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12403 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12404 add_loc_descr (&mem_loc_result, cvt);
12405 }
12406 break;
12407
12408 case REG:
12409 if (GET_MODE_CLASS (mode) != MODE_INT
12410 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12411 && rtl != arg_pointer_rtx
12412 && rtl != frame_pointer_rtx
12413 #ifdef POINTERS_EXTEND_UNSIGNED
12414 && (mode != Pmode || mem_mode == VOIDmode)
12415 #endif
12416 ))
12417 {
12418 dw_die_ref type_die;
12419 unsigned int dbx_regnum;
12420
12421 if (dwarf_strict)
12422 break;
12423 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12424 break;
12425 type_die = base_type_for_mode (mode,
12426 GET_MODE_CLASS (mode) == MODE_INT);
12427 if (type_die == NULL)
12428 break;
12429
12430 dbx_regnum = dbx_reg_number (rtl);
12431 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12432 break;
12433 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12434 dbx_regnum, 0);
12435 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12436 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12437 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12438 break;
12439 }
12440 /* Whenever a register number forms a part of the description of the
12441 method for calculating the (dynamic) address of a memory resident
12442 object, DWARF rules require the register number be referred to as
12443 a "base register". This distinction is not based in any way upon
12444 what category of register the hardware believes the given register
12445 belongs to. This is strictly DWARF terminology we're dealing with
12446 here. Note that in cases where the location of a memory-resident
12447 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12448 OP_CONST (0)) the actual DWARF location descriptor that we generate
12449 may just be OP_BASEREG (basereg). This may look deceptively like
12450 the object in question was allocated to a register (rather than in
12451 memory) so DWARF consumers need to be aware of the subtle
12452 distinction between OP_REG and OP_BASEREG. */
12453 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12454 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12455 else if (stack_realign_drap
12456 && crtl->drap_reg
12457 && crtl->args.internal_arg_pointer == rtl
12458 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12459 {
12460 /* If RTL is internal_arg_pointer, which has been optimized
12461 out, use DRAP instead. */
12462 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12463 VAR_INIT_STATUS_INITIALIZED);
12464 }
12465 break;
12466
12467 case SIGN_EXTEND:
12468 case ZERO_EXTEND:
12469 if (GET_MODE_CLASS (mode) != MODE_INT)
12470 break;
12471 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12472 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12473 if (op0 == 0)
12474 break;
12475 else if (GET_CODE (rtl) == ZERO_EXTEND
12476 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12477 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12478 < HOST_BITS_PER_WIDE_INT
12479 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12480 to expand zero extend as two shifts instead of
12481 masking. */
12482 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12483 {
12484 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12485 mem_loc_result = op0;
12486 add_loc_descr (&mem_loc_result,
12487 int_loc_descriptor (GET_MODE_MASK (imode)));
12488 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12489 }
12490 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12491 {
12492 int shift = DWARF2_ADDR_SIZE
12493 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12494 shift *= BITS_PER_UNIT;
12495 if (GET_CODE (rtl) == SIGN_EXTEND)
12496 op = DW_OP_shra;
12497 else
12498 op = DW_OP_shr;
12499 mem_loc_result = op0;
12500 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12501 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12502 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12503 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12504 }
12505 else if (!dwarf_strict)
12506 {
12507 dw_die_ref type_die1, type_die2;
12508 dw_loc_descr_ref cvt;
12509
12510 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12511 GET_CODE (rtl) == ZERO_EXTEND);
12512 if (type_die1 == NULL)
12513 break;
12514 type_die2 = base_type_for_mode (mode, 1);
12515 if (type_die2 == NULL)
12516 break;
12517 mem_loc_result = op0;
12518 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12519 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12520 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12521 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12522 add_loc_descr (&mem_loc_result, cvt);
12523 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12524 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12525 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12526 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12527 add_loc_descr (&mem_loc_result, cvt);
12528 }
12529 break;
12530
12531 case MEM:
12532 {
12533 rtx new_rtl = avoid_constant_pool_reference (rtl);
12534 if (new_rtl != rtl)
12535 {
12536 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12537 initialized);
12538 if (mem_loc_result != NULL)
12539 return mem_loc_result;
12540 }
12541 }
12542 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12543 get_address_mode (rtl), mode,
12544 VAR_INIT_STATUS_INITIALIZED);
12545 if (mem_loc_result == NULL)
12546 mem_loc_result = tls_mem_loc_descriptor (rtl);
12547 if (mem_loc_result != NULL)
12548 {
12549 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12550 || GET_MODE_CLASS (mode) != MODE_INT)
12551 {
12552 dw_die_ref type_die;
12553 dw_loc_descr_ref deref;
12554
12555 if (dwarf_strict)
12556 return NULL;
12557 type_die
12558 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12559 if (type_die == NULL)
12560 return NULL;
12561 deref = new_loc_descr (DW_OP_GNU_deref_type,
12562 GET_MODE_SIZE (mode), 0);
12563 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12564 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12565 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12566 add_loc_descr (&mem_loc_result, deref);
12567 }
12568 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12569 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12570 else
12571 add_loc_descr (&mem_loc_result,
12572 new_loc_descr (DW_OP_deref_size,
12573 GET_MODE_SIZE (mode), 0));
12574 }
12575 break;
12576
12577 case LO_SUM:
12578 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12579
12580 case LABEL_REF:
12581 /* Some ports can transform a symbol ref into a label ref, because
12582 the symbol ref is too far away and has to be dumped into a constant
12583 pool. */
12584 case CONST:
12585 case SYMBOL_REF:
12586 if (GET_MODE_CLASS (mode) != MODE_INT
12587 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12588 #ifdef POINTERS_EXTEND_UNSIGNED
12589 && (mode != Pmode || mem_mode == VOIDmode)
12590 #endif
12591 ))
12592 break;
12593 if (GET_CODE (rtl) == SYMBOL_REF
12594 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12595 {
12596 dw_loc_descr_ref temp;
12597
12598 /* If this is not defined, we have no way to emit the data. */
12599 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12600 break;
12601
12602 temp = new_addr_loc_descr (rtl, dtprel_true);
12603
12604 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12605 add_loc_descr (&mem_loc_result, temp);
12606
12607 break;
12608 }
12609
12610 if (!const_ok_for_output (rtl))
12611 break;
12612
12613 symref:
12614 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12615 vec_safe_push (used_rtx_array, rtl);
12616 break;
12617
12618 case CONCAT:
12619 case CONCATN:
12620 case VAR_LOCATION:
12621 case DEBUG_IMPLICIT_PTR:
12622 expansion_failed (NULL_TREE, rtl,
12623 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12624 return 0;
12625
12626 case ENTRY_VALUE:
12627 if (dwarf_strict)
12628 return NULL;
12629 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12630 {
12631 if (GET_MODE_CLASS (mode) != MODE_INT
12632 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12633 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12634 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12635 else
12636 {
12637 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12638 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12639 return NULL;
12640 op0 = one_reg_loc_descriptor (dbx_regnum,
12641 VAR_INIT_STATUS_INITIALIZED);
12642 }
12643 }
12644 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12645 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12646 {
12647 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12648 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12649 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12650 return NULL;
12651 }
12652 else
12653 gcc_unreachable ();
12654 if (op0 == NULL)
12655 return NULL;
12656 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12657 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12658 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12659 break;
12660
12661 case DEBUG_PARAMETER_REF:
12662 mem_loc_result = parameter_ref_descriptor (rtl);
12663 break;
12664
12665 case PRE_MODIFY:
12666 /* Extract the PLUS expression nested inside and fall into
12667 PLUS code below. */
12668 rtl = XEXP (rtl, 1);
12669 goto plus;
12670
12671 case PRE_INC:
12672 case PRE_DEC:
12673 /* Turn these into a PLUS expression and fall into the PLUS code
12674 below. */
12675 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12676 gen_int_mode (GET_CODE (rtl) == PRE_INC
12677 ? GET_MODE_UNIT_SIZE (mem_mode)
12678 : -GET_MODE_UNIT_SIZE (mem_mode),
12679 mode));
12680
12681 /* ... fall through ... */
12682
12683 case PLUS:
12684 plus:
12685 if (is_based_loc (rtl)
12686 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12687 || XEXP (rtl, 0) == arg_pointer_rtx
12688 || XEXP (rtl, 0) == frame_pointer_rtx)
12689 && GET_MODE_CLASS (mode) == MODE_INT)
12690 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12691 INTVAL (XEXP (rtl, 1)),
12692 VAR_INIT_STATUS_INITIALIZED);
12693 else
12694 {
12695 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12696 VAR_INIT_STATUS_INITIALIZED);
12697 if (mem_loc_result == 0)
12698 break;
12699
12700 if (CONST_INT_P (XEXP (rtl, 1))
12701 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12702 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12703 else
12704 {
12705 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12706 VAR_INIT_STATUS_INITIALIZED);
12707 if (op1 == 0)
12708 break;
12709 add_loc_descr (&mem_loc_result, op1);
12710 add_loc_descr (&mem_loc_result,
12711 new_loc_descr (DW_OP_plus, 0, 0));
12712 }
12713 }
12714 break;
12715
12716 /* If a pseudo-reg is optimized away, it is possible for it to
12717 be replaced with a MEM containing a multiply or shift. */
12718 case MINUS:
12719 op = DW_OP_minus;
12720 goto do_binop;
12721
12722 case MULT:
12723 op = DW_OP_mul;
12724 goto do_binop;
12725
12726 case DIV:
12727 if (!dwarf_strict
12728 && GET_MODE_CLASS (mode) == MODE_INT
12729 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12730 {
12731 mem_loc_result = typed_binop (DW_OP_div, rtl,
12732 base_type_for_mode (mode, 0),
12733 mode, mem_mode);
12734 break;
12735 }
12736 op = DW_OP_div;
12737 goto do_binop;
12738
12739 case UMOD:
12740 op = DW_OP_mod;
12741 goto do_binop;
12742
12743 case ASHIFT:
12744 op = DW_OP_shl;
12745 goto do_shift;
12746
12747 case ASHIFTRT:
12748 op = DW_OP_shra;
12749 goto do_shift;
12750
12751 case LSHIFTRT:
12752 op = DW_OP_shr;
12753 goto do_shift;
12754
12755 do_shift:
12756 if (GET_MODE_CLASS (mode) != MODE_INT)
12757 break;
12758 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12759 VAR_INIT_STATUS_INITIALIZED);
12760 {
12761 rtx rtlop1 = XEXP (rtl, 1);
12762 if (GET_MODE (rtlop1) != VOIDmode
12763 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12764 < GET_MODE_BITSIZE (mode))
12765 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12766 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12767 VAR_INIT_STATUS_INITIALIZED);
12768 }
12769
12770 if (op0 == 0 || op1 == 0)
12771 break;
12772
12773 mem_loc_result = op0;
12774 add_loc_descr (&mem_loc_result, op1);
12775 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12776 break;
12777
12778 case AND:
12779 op = DW_OP_and;
12780 goto do_binop;
12781
12782 case IOR:
12783 op = DW_OP_or;
12784 goto do_binop;
12785
12786 case XOR:
12787 op = DW_OP_xor;
12788 goto do_binop;
12789
12790 do_binop:
12791 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12792 VAR_INIT_STATUS_INITIALIZED);
12793 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12794 VAR_INIT_STATUS_INITIALIZED);
12795
12796 if (op0 == 0 || op1 == 0)
12797 break;
12798
12799 mem_loc_result = op0;
12800 add_loc_descr (&mem_loc_result, op1);
12801 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12802 break;
12803
12804 case MOD:
12805 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12806 {
12807 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12808 base_type_for_mode (mode, 0),
12809 mode, mem_mode);
12810 break;
12811 }
12812
12813 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12814 VAR_INIT_STATUS_INITIALIZED);
12815 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12816 VAR_INIT_STATUS_INITIALIZED);
12817
12818 if (op0 == 0 || op1 == 0)
12819 break;
12820
12821 mem_loc_result = op0;
12822 add_loc_descr (&mem_loc_result, op1);
12823 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12824 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12825 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12826 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12827 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12828 break;
12829
12830 case UDIV:
12831 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12832 {
12833 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12834 {
12835 op = DW_OP_div;
12836 goto do_binop;
12837 }
12838 mem_loc_result = typed_binop (DW_OP_div, rtl,
12839 base_type_for_mode (mode, 1),
12840 mode, mem_mode);
12841 }
12842 break;
12843
12844 case NOT:
12845 op = DW_OP_not;
12846 goto do_unop;
12847
12848 case ABS:
12849 op = DW_OP_abs;
12850 goto do_unop;
12851
12852 case NEG:
12853 op = DW_OP_neg;
12854 goto do_unop;
12855
12856 do_unop:
12857 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12858 VAR_INIT_STATUS_INITIALIZED);
12859
12860 if (op0 == 0)
12861 break;
12862
12863 mem_loc_result = op0;
12864 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12865 break;
12866
12867 case CONST_INT:
12868 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12869 #ifdef POINTERS_EXTEND_UNSIGNED
12870 || (mode == Pmode
12871 && mem_mode != VOIDmode
12872 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12873 #endif
12874 )
12875 {
12876 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12877 break;
12878 }
12879 if (!dwarf_strict
12880 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12881 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12882 {
12883 dw_die_ref type_die = base_type_for_mode (mode, 1);
12884 enum machine_mode amode;
12885 if (type_die == NULL)
12886 return NULL;
12887 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12888 MODE_INT, 0);
12889 if (INTVAL (rtl) >= 0
12890 && amode != BLKmode
12891 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12892 /* const DW_OP_GNU_convert <XXX> vs.
12893 DW_OP_GNU_const_type <XXX, 1, const>. */
12894 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12895 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12896 {
12897 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12898 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12899 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12900 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12901 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12902 add_loc_descr (&mem_loc_result, op0);
12903 return mem_loc_result;
12904 }
12905 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12906 INTVAL (rtl));
12907 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12908 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12909 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12910 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12911 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12912 else
12913 {
12914 mem_loc_result->dw_loc_oprnd2.val_class
12915 = dw_val_class_const_double;
12916 mem_loc_result->dw_loc_oprnd2.v.val_double
12917 = double_int::from_shwi (INTVAL (rtl));
12918 }
12919 }
12920 break;
12921
12922 case CONST_DOUBLE:
12923 if (!dwarf_strict)
12924 {
12925 dw_die_ref type_die;
12926
12927 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12928 CONST_DOUBLE rtx could represent either a large integer
12929 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12930 the value is always a floating point constant.
12931
12932 When it is an integer, a CONST_DOUBLE is used whenever
12933 the constant requires 2 HWIs to be adequately represented.
12934 We output CONST_DOUBLEs as blocks. */
12935 if (mode == VOIDmode
12936 || (GET_MODE (rtl) == VOIDmode
12937 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12938 break;
12939 type_die = base_type_for_mode (mode,
12940 GET_MODE_CLASS (mode) == MODE_INT);
12941 if (type_die == NULL)
12942 return NULL;
12943 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12944 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12945 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12946 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12947 #if TARGET_SUPPORTS_WIDE_INT == 0
12948 if (!SCALAR_FLOAT_MODE_P (mode))
12949 {
12950 mem_loc_result->dw_loc_oprnd2.val_class
12951 = dw_val_class_const_double;
12952 mem_loc_result->dw_loc_oprnd2.v.val_double
12953 = rtx_to_double_int (rtl);
12954 }
12955 else
12956 #endif
12957 {
12958 unsigned int length = GET_MODE_SIZE (mode);
12959 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12960
12961 insert_float (rtl, array);
12962 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12963 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12964 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12965 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12966 }
12967 }
12968 break;
12969
12970 case CONST_WIDE_INT:
12971 if (!dwarf_strict)
12972 {
12973 dw_die_ref type_die;
12974
12975 type_die = base_type_for_mode (mode,
12976 GET_MODE_CLASS (mode) == MODE_INT);
12977 if (type_die == NULL)
12978 return NULL;
12979 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12980 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12981 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12982 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12983 mem_loc_result->dw_loc_oprnd2.val_class
12984 = dw_val_class_wide_int;
12985 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
12986 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12987 }
12988 break;
12989
12990 case EQ:
12991 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12992 break;
12993
12994 case GE:
12995 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12996 break;
12997
12998 case GT:
12999 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13000 break;
13001
13002 case LE:
13003 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13004 break;
13005
13006 case LT:
13007 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13008 break;
13009
13010 case NE:
13011 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13012 break;
13013
13014 case GEU:
13015 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13016 break;
13017
13018 case GTU:
13019 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13020 break;
13021
13022 case LEU:
13023 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13024 break;
13025
13026 case LTU:
13027 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13028 break;
13029
13030 case UMIN:
13031 case UMAX:
13032 if (GET_MODE_CLASS (mode) != MODE_INT)
13033 break;
13034 /* FALLTHRU */
13035 case SMIN:
13036 case SMAX:
13037 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13038 break;
13039
13040 case ZERO_EXTRACT:
13041 case SIGN_EXTRACT:
13042 if (CONST_INT_P (XEXP (rtl, 1))
13043 && CONST_INT_P (XEXP (rtl, 2))
13044 && ((unsigned) INTVAL (XEXP (rtl, 1))
13045 + (unsigned) INTVAL (XEXP (rtl, 2))
13046 <= GET_MODE_BITSIZE (mode))
13047 && GET_MODE_CLASS (mode) == MODE_INT
13048 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13049 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13050 {
13051 int shift, size;
13052 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13053 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13054 if (op0 == 0)
13055 break;
13056 if (GET_CODE (rtl) == SIGN_EXTRACT)
13057 op = DW_OP_shra;
13058 else
13059 op = DW_OP_shr;
13060 mem_loc_result = op0;
13061 size = INTVAL (XEXP (rtl, 1));
13062 shift = INTVAL (XEXP (rtl, 2));
13063 if (BITS_BIG_ENDIAN)
13064 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13065 - shift - size;
13066 if (shift + size != (int) DWARF2_ADDR_SIZE)
13067 {
13068 add_loc_descr (&mem_loc_result,
13069 int_loc_descriptor (DWARF2_ADDR_SIZE
13070 - shift - size));
13071 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13072 }
13073 if (size != (int) DWARF2_ADDR_SIZE)
13074 {
13075 add_loc_descr (&mem_loc_result,
13076 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13077 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13078 }
13079 }
13080 break;
13081
13082 case IF_THEN_ELSE:
13083 {
13084 dw_loc_descr_ref op2, bra_node, drop_node;
13085 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13086 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13087 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13088 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13089 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13090 VAR_INIT_STATUS_INITIALIZED);
13091 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13092 VAR_INIT_STATUS_INITIALIZED);
13093 if (op0 == NULL || op1 == NULL || op2 == NULL)
13094 break;
13095
13096 mem_loc_result = op1;
13097 add_loc_descr (&mem_loc_result, op2);
13098 add_loc_descr (&mem_loc_result, op0);
13099 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13100 add_loc_descr (&mem_loc_result, bra_node);
13101 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13102 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13103 add_loc_descr (&mem_loc_result, drop_node);
13104 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13105 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13106 }
13107 break;
13108
13109 case FLOAT_EXTEND:
13110 case FLOAT_TRUNCATE:
13111 case FLOAT:
13112 case UNSIGNED_FLOAT:
13113 case FIX:
13114 case UNSIGNED_FIX:
13115 if (!dwarf_strict)
13116 {
13117 dw_die_ref type_die;
13118 dw_loc_descr_ref cvt;
13119
13120 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13121 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13122 if (op0 == NULL)
13123 break;
13124 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13125 && (GET_CODE (rtl) == FLOAT
13126 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13127 <= DWARF2_ADDR_SIZE))
13128 {
13129 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13130 GET_CODE (rtl) == UNSIGNED_FLOAT);
13131 if (type_die == NULL)
13132 break;
13133 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13134 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13135 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13136 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13137 add_loc_descr (&op0, cvt);
13138 }
13139 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13140 if (type_die == NULL)
13141 break;
13142 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13143 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13144 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13145 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13146 add_loc_descr (&op0, cvt);
13147 if (GET_MODE_CLASS (mode) == MODE_INT
13148 && (GET_CODE (rtl) == FIX
13149 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13150 {
13151 op0 = convert_descriptor_to_mode (mode, op0);
13152 if (op0 == NULL)
13153 break;
13154 }
13155 mem_loc_result = op0;
13156 }
13157 break;
13158
13159 case CLZ:
13160 case CTZ:
13161 case FFS:
13162 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13163 break;
13164
13165 case POPCOUNT:
13166 case PARITY:
13167 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13168 break;
13169
13170 case BSWAP:
13171 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13172 break;
13173
13174 case ROTATE:
13175 case ROTATERT:
13176 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13177 break;
13178
13179 case COMPARE:
13180 /* In theory, we could implement the above. */
13181 /* DWARF cannot represent the unsigned compare operations
13182 natively. */
13183 case SS_MULT:
13184 case US_MULT:
13185 case SS_DIV:
13186 case US_DIV:
13187 case SS_PLUS:
13188 case US_PLUS:
13189 case SS_MINUS:
13190 case US_MINUS:
13191 case SS_NEG:
13192 case US_NEG:
13193 case SS_ABS:
13194 case SS_ASHIFT:
13195 case US_ASHIFT:
13196 case SS_TRUNCATE:
13197 case US_TRUNCATE:
13198 case UNORDERED:
13199 case ORDERED:
13200 case UNEQ:
13201 case UNGE:
13202 case UNGT:
13203 case UNLE:
13204 case UNLT:
13205 case LTGT:
13206 case FRACT_CONVERT:
13207 case UNSIGNED_FRACT_CONVERT:
13208 case SAT_FRACT:
13209 case UNSIGNED_SAT_FRACT:
13210 case SQRT:
13211 case ASM_OPERANDS:
13212 case VEC_MERGE:
13213 case VEC_SELECT:
13214 case VEC_CONCAT:
13215 case VEC_DUPLICATE:
13216 case UNSPEC:
13217 case HIGH:
13218 case FMA:
13219 case STRICT_LOW_PART:
13220 case CONST_VECTOR:
13221 case CONST_FIXED:
13222 case CLRSB:
13223 case CLOBBER:
13224 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13225 can't express it in the debug info. This can happen e.g. with some
13226 TLS UNSPECs. */
13227 break;
13228
13229 case CONST_STRING:
13230 resolve_one_addr (&rtl, NULL);
13231 goto symref;
13232
13233 default:
13234 #ifdef ENABLE_CHECKING
13235 print_rtl (stderr, rtl);
13236 gcc_unreachable ();
13237 #else
13238 break;
13239 #endif
13240 }
13241
13242 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13243 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13244
13245 return mem_loc_result;
13246 }
13247
13248 /* Return a descriptor that describes the concatenation of two locations.
13249 This is typically a complex variable. */
13250
13251 static dw_loc_descr_ref
13252 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13253 {
13254 dw_loc_descr_ref cc_loc_result = NULL;
13255 dw_loc_descr_ref x0_ref
13256 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13257 dw_loc_descr_ref x1_ref
13258 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13259
13260 if (x0_ref == 0 || x1_ref == 0)
13261 return 0;
13262
13263 cc_loc_result = x0_ref;
13264 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13265
13266 add_loc_descr (&cc_loc_result, x1_ref);
13267 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13268
13269 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13270 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13271
13272 return cc_loc_result;
13273 }
13274
13275 /* Return a descriptor that describes the concatenation of N
13276 locations. */
13277
13278 static dw_loc_descr_ref
13279 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13280 {
13281 unsigned int i;
13282 dw_loc_descr_ref cc_loc_result = NULL;
13283 unsigned int n = XVECLEN (concatn, 0);
13284
13285 for (i = 0; i < n; ++i)
13286 {
13287 dw_loc_descr_ref ref;
13288 rtx x = XVECEXP (concatn, 0, i);
13289
13290 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13291 if (ref == NULL)
13292 return NULL;
13293
13294 add_loc_descr (&cc_loc_result, ref);
13295 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13296 }
13297
13298 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13299 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13300
13301 return cc_loc_result;
13302 }
13303
13304 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13305 for DEBUG_IMPLICIT_PTR RTL. */
13306
13307 static dw_loc_descr_ref
13308 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13309 {
13310 dw_loc_descr_ref ret;
13311 dw_die_ref ref;
13312
13313 if (dwarf_strict)
13314 return NULL;
13315 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13316 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13317 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13318 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13319 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13320 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13321 if (ref)
13322 {
13323 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13324 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13325 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13326 }
13327 else
13328 {
13329 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13330 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13331 }
13332 return ret;
13333 }
13334
13335 /* Output a proper Dwarf location descriptor for a variable or parameter
13336 which is either allocated in a register or in a memory location. For a
13337 register, we just generate an OP_REG and the register number. For a
13338 memory location we provide a Dwarf postfix expression describing how to
13339 generate the (dynamic) address of the object onto the address stack.
13340
13341 MODE is mode of the decl if this loc_descriptor is going to be used in
13342 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13343 allowed, VOIDmode otherwise.
13344
13345 If we don't know how to describe it, return 0. */
13346
13347 static dw_loc_descr_ref
13348 loc_descriptor (rtx rtl, enum machine_mode mode,
13349 enum var_init_status initialized)
13350 {
13351 dw_loc_descr_ref loc_result = NULL;
13352
13353 switch (GET_CODE (rtl))
13354 {
13355 case SUBREG:
13356 /* The case of a subreg may arise when we have a local (register)
13357 variable or a formal (register) parameter which doesn't quite fill
13358 up an entire register. For now, just assume that it is
13359 legitimate to make the Dwarf info refer to the whole register which
13360 contains the given subreg. */
13361 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13362 loc_result = loc_descriptor (SUBREG_REG (rtl),
13363 GET_MODE (SUBREG_REG (rtl)), initialized);
13364 else
13365 goto do_default;
13366 break;
13367
13368 case REG:
13369 loc_result = reg_loc_descriptor (rtl, initialized);
13370 break;
13371
13372 case MEM:
13373 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13374 GET_MODE (rtl), initialized);
13375 if (loc_result == NULL)
13376 loc_result = tls_mem_loc_descriptor (rtl);
13377 if (loc_result == NULL)
13378 {
13379 rtx new_rtl = avoid_constant_pool_reference (rtl);
13380 if (new_rtl != rtl)
13381 loc_result = loc_descriptor (new_rtl, mode, initialized);
13382 }
13383 break;
13384
13385 case CONCAT:
13386 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13387 initialized);
13388 break;
13389
13390 case CONCATN:
13391 loc_result = concatn_loc_descriptor (rtl, initialized);
13392 break;
13393
13394 case VAR_LOCATION:
13395 /* Single part. */
13396 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13397 {
13398 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13399 if (GET_CODE (loc) == EXPR_LIST)
13400 loc = XEXP (loc, 0);
13401 loc_result = loc_descriptor (loc, mode, initialized);
13402 break;
13403 }
13404
13405 rtl = XEXP (rtl, 1);
13406 /* FALLTHRU */
13407
13408 case PARALLEL:
13409 {
13410 rtvec par_elems = XVEC (rtl, 0);
13411 int num_elem = GET_NUM_ELEM (par_elems);
13412 enum machine_mode mode;
13413 int i;
13414
13415 /* Create the first one, so we have something to add to. */
13416 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13417 VOIDmode, initialized);
13418 if (loc_result == NULL)
13419 return NULL;
13420 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13421 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13422 for (i = 1; i < num_elem; i++)
13423 {
13424 dw_loc_descr_ref temp;
13425
13426 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13427 VOIDmode, initialized);
13428 if (temp == NULL)
13429 return NULL;
13430 add_loc_descr (&loc_result, temp);
13431 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13432 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13433 }
13434 }
13435 break;
13436
13437 case CONST_INT:
13438 if (mode != VOIDmode && mode != BLKmode)
13439 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13440 INTVAL (rtl));
13441 break;
13442
13443 case CONST_DOUBLE:
13444 if (mode == VOIDmode)
13445 mode = GET_MODE (rtl);
13446
13447 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13448 {
13449 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13450
13451 /* Note that a CONST_DOUBLE rtx could represent either an integer
13452 or a floating-point constant. A CONST_DOUBLE is used whenever
13453 the constant requires more than one word in order to be
13454 adequately represented. We output CONST_DOUBLEs as blocks. */
13455 loc_result = new_loc_descr (DW_OP_implicit_value,
13456 GET_MODE_SIZE (mode), 0);
13457 #if TARGET_SUPPORTS_WIDE_INT == 0
13458 if (!SCALAR_FLOAT_MODE_P (mode))
13459 {
13460 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13461 loc_result->dw_loc_oprnd2.v.val_double
13462 = rtx_to_double_int (rtl);
13463 }
13464 else
13465 #endif
13466 {
13467 unsigned int length = GET_MODE_SIZE (mode);
13468 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13469
13470 insert_float (rtl, array);
13471 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13472 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13473 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13474 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13475 }
13476 }
13477 break;
13478
13479 case CONST_WIDE_INT:
13480 if (mode == VOIDmode)
13481 mode = GET_MODE (rtl);
13482
13483 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13484 {
13485 loc_result = new_loc_descr (DW_OP_implicit_value,
13486 GET_MODE_SIZE (mode), 0);
13487 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13488 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13489 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13490 }
13491 break;
13492
13493 case CONST_VECTOR:
13494 if (mode == VOIDmode)
13495 mode = GET_MODE (rtl);
13496
13497 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13498 {
13499 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13500 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13501 unsigned char *array
13502 = ggc_vec_alloc<unsigned char> (length * elt_size);
13503 unsigned int i;
13504 unsigned char *p;
13505 enum machine_mode imode = GET_MODE_INNER (mode);
13506
13507 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13508 switch (GET_MODE_CLASS (mode))
13509 {
13510 case MODE_VECTOR_INT:
13511 for (i = 0, p = array; i < length; i++, p += elt_size)
13512 {
13513 rtx elt = CONST_VECTOR_ELT (rtl, i);
13514 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13515 }
13516 break;
13517
13518 case MODE_VECTOR_FLOAT:
13519 for (i = 0, p = array; i < length; i++, p += elt_size)
13520 {
13521 rtx elt = CONST_VECTOR_ELT (rtl, i);
13522 insert_float (elt, p);
13523 }
13524 break;
13525
13526 default:
13527 gcc_unreachable ();
13528 }
13529
13530 loc_result = new_loc_descr (DW_OP_implicit_value,
13531 length * elt_size, 0);
13532 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13533 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13534 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13535 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13536 }
13537 break;
13538
13539 case CONST:
13540 if (mode == VOIDmode
13541 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13542 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13543 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13544 {
13545 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13546 break;
13547 }
13548 /* FALLTHROUGH */
13549 case SYMBOL_REF:
13550 if (!const_ok_for_output (rtl))
13551 break;
13552 case LABEL_REF:
13553 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13554 && (dwarf_version >= 4 || !dwarf_strict))
13555 {
13556 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13557 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13558 vec_safe_push (used_rtx_array, rtl);
13559 }
13560 break;
13561
13562 case DEBUG_IMPLICIT_PTR:
13563 loc_result = implicit_ptr_descriptor (rtl, 0);
13564 break;
13565
13566 case PLUS:
13567 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13568 && CONST_INT_P (XEXP (rtl, 1)))
13569 {
13570 loc_result
13571 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13572 break;
13573 }
13574 /* FALLTHRU */
13575 do_default:
13576 default:
13577 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13578 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13579 && dwarf_version >= 4)
13580 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13581 {
13582 /* Value expression. */
13583 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13584 if (loc_result)
13585 add_loc_descr (&loc_result,
13586 new_loc_descr (DW_OP_stack_value, 0, 0));
13587 }
13588 break;
13589 }
13590
13591 return loc_result;
13592 }
13593
13594 /* We need to figure out what section we should use as the base for the
13595 address ranges where a given location is valid.
13596 1. If this particular DECL has a section associated with it, use that.
13597 2. If this function has a section associated with it, use that.
13598 3. Otherwise, use the text section.
13599 XXX: If you split a variable across multiple sections, we won't notice. */
13600
13601 static const char *
13602 secname_for_decl (const_tree decl)
13603 {
13604 const char *secname;
13605
13606 if (VAR_OR_FUNCTION_DECL_P (decl)
13607 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13608 && DECL_SECTION_NAME (decl))
13609 secname = DECL_SECTION_NAME (decl);
13610 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13611 secname = DECL_SECTION_NAME (current_function_decl);
13612 else if (cfun && in_cold_section_p)
13613 secname = crtl->subsections.cold_section_label;
13614 else
13615 secname = text_section_label;
13616
13617 return secname;
13618 }
13619
13620 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13621
13622 static bool
13623 decl_by_reference_p (tree decl)
13624 {
13625 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13626 || TREE_CODE (decl) == VAR_DECL)
13627 && DECL_BY_REFERENCE (decl));
13628 }
13629
13630 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13631 for VARLOC. */
13632
13633 static dw_loc_descr_ref
13634 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13635 enum var_init_status initialized)
13636 {
13637 int have_address = 0;
13638 dw_loc_descr_ref descr;
13639 enum machine_mode mode;
13640
13641 if (want_address != 2)
13642 {
13643 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13644 /* Single part. */
13645 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13646 {
13647 varloc = PAT_VAR_LOCATION_LOC (varloc);
13648 if (GET_CODE (varloc) == EXPR_LIST)
13649 varloc = XEXP (varloc, 0);
13650 mode = GET_MODE (varloc);
13651 if (MEM_P (varloc))
13652 {
13653 rtx addr = XEXP (varloc, 0);
13654 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13655 mode, initialized);
13656 if (descr)
13657 have_address = 1;
13658 else
13659 {
13660 rtx x = avoid_constant_pool_reference (varloc);
13661 if (x != varloc)
13662 descr = mem_loc_descriptor (x, mode, VOIDmode,
13663 initialized);
13664 }
13665 }
13666 else
13667 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13668 }
13669 else
13670 return 0;
13671 }
13672 else
13673 {
13674 if (GET_CODE (varloc) == VAR_LOCATION)
13675 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13676 else
13677 mode = DECL_MODE (loc);
13678 descr = loc_descriptor (varloc, mode, initialized);
13679 have_address = 1;
13680 }
13681
13682 if (!descr)
13683 return 0;
13684
13685 if (want_address == 2 && !have_address
13686 && (dwarf_version >= 4 || !dwarf_strict))
13687 {
13688 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13689 {
13690 expansion_failed (loc, NULL_RTX,
13691 "DWARF address size mismatch");
13692 return 0;
13693 }
13694 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13695 have_address = 1;
13696 }
13697 /* Show if we can't fill the request for an address. */
13698 if (want_address && !have_address)
13699 {
13700 expansion_failed (loc, NULL_RTX,
13701 "Want address and only have value");
13702 return 0;
13703 }
13704
13705 /* If we've got an address and don't want one, dereference. */
13706 if (!want_address && have_address)
13707 {
13708 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13709 enum dwarf_location_atom op;
13710
13711 if (size > DWARF2_ADDR_SIZE || size == -1)
13712 {
13713 expansion_failed (loc, NULL_RTX,
13714 "DWARF address size mismatch");
13715 return 0;
13716 }
13717 else if (size == DWARF2_ADDR_SIZE)
13718 op = DW_OP_deref;
13719 else
13720 op = DW_OP_deref_size;
13721
13722 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13723 }
13724
13725 return descr;
13726 }
13727
13728 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13729 if it is not possible. */
13730
13731 static dw_loc_descr_ref
13732 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13733 {
13734 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13735 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13736 else if (dwarf_version >= 3 || !dwarf_strict)
13737 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13738 else
13739 return NULL;
13740 }
13741
13742 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13743 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13744
13745 static dw_loc_descr_ref
13746 dw_sra_loc_expr (tree decl, rtx loc)
13747 {
13748 rtx p;
13749 unsigned int padsize = 0;
13750 dw_loc_descr_ref descr, *descr_tail;
13751 unsigned HOST_WIDE_INT decl_size;
13752 rtx varloc;
13753 enum var_init_status initialized;
13754
13755 if (DECL_SIZE (decl) == NULL
13756 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13757 return NULL;
13758
13759 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13760 descr = NULL;
13761 descr_tail = &descr;
13762
13763 for (p = loc; p; p = XEXP (p, 1))
13764 {
13765 unsigned int bitsize = decl_piece_bitsize (p);
13766 rtx loc_note = *decl_piece_varloc_ptr (p);
13767 dw_loc_descr_ref cur_descr;
13768 dw_loc_descr_ref *tail, last = NULL;
13769 unsigned int opsize = 0;
13770
13771 if (loc_note == NULL_RTX
13772 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13773 {
13774 padsize += bitsize;
13775 continue;
13776 }
13777 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13778 varloc = NOTE_VAR_LOCATION (loc_note);
13779 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13780 if (cur_descr == NULL)
13781 {
13782 padsize += bitsize;
13783 continue;
13784 }
13785
13786 /* Check that cur_descr either doesn't use
13787 DW_OP_*piece operations, or their sum is equal
13788 to bitsize. Otherwise we can't embed it. */
13789 for (tail = &cur_descr; *tail != NULL;
13790 tail = &(*tail)->dw_loc_next)
13791 if ((*tail)->dw_loc_opc == DW_OP_piece)
13792 {
13793 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13794 * BITS_PER_UNIT;
13795 last = *tail;
13796 }
13797 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13798 {
13799 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13800 last = *tail;
13801 }
13802
13803 if (last != NULL && opsize != bitsize)
13804 {
13805 padsize += bitsize;
13806 /* Discard the current piece of the descriptor and release any
13807 addr_table entries it uses. */
13808 remove_loc_list_addr_table_entries (cur_descr);
13809 continue;
13810 }
13811
13812 /* If there is a hole, add DW_OP_*piece after empty DWARF
13813 expression, which means that those bits are optimized out. */
13814 if (padsize)
13815 {
13816 if (padsize > decl_size)
13817 {
13818 remove_loc_list_addr_table_entries (cur_descr);
13819 goto discard_descr;
13820 }
13821 decl_size -= padsize;
13822 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13823 if (*descr_tail == NULL)
13824 {
13825 remove_loc_list_addr_table_entries (cur_descr);
13826 goto discard_descr;
13827 }
13828 descr_tail = &(*descr_tail)->dw_loc_next;
13829 padsize = 0;
13830 }
13831 *descr_tail = cur_descr;
13832 descr_tail = tail;
13833 if (bitsize > decl_size)
13834 goto discard_descr;
13835 decl_size -= bitsize;
13836 if (last == NULL)
13837 {
13838 HOST_WIDE_INT offset = 0;
13839 if (GET_CODE (varloc) == VAR_LOCATION
13840 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13841 {
13842 varloc = PAT_VAR_LOCATION_LOC (varloc);
13843 if (GET_CODE (varloc) == EXPR_LIST)
13844 varloc = XEXP (varloc, 0);
13845 }
13846 do
13847 {
13848 if (GET_CODE (varloc) == CONST
13849 || GET_CODE (varloc) == SIGN_EXTEND
13850 || GET_CODE (varloc) == ZERO_EXTEND)
13851 varloc = XEXP (varloc, 0);
13852 else if (GET_CODE (varloc) == SUBREG)
13853 varloc = SUBREG_REG (varloc);
13854 else
13855 break;
13856 }
13857 while (1);
13858 /* DW_OP_bit_size offset should be zero for register
13859 or implicit location descriptions and empty location
13860 descriptions, but for memory addresses needs big endian
13861 adjustment. */
13862 if (MEM_P (varloc))
13863 {
13864 unsigned HOST_WIDE_INT memsize
13865 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13866 if (memsize != bitsize)
13867 {
13868 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13869 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13870 goto discard_descr;
13871 if (memsize < bitsize)
13872 goto discard_descr;
13873 if (BITS_BIG_ENDIAN)
13874 offset = memsize - bitsize;
13875 }
13876 }
13877
13878 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13879 if (*descr_tail == NULL)
13880 goto discard_descr;
13881 descr_tail = &(*descr_tail)->dw_loc_next;
13882 }
13883 }
13884
13885 /* If there were any non-empty expressions, add padding till the end of
13886 the decl. */
13887 if (descr != NULL && decl_size != 0)
13888 {
13889 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13890 if (*descr_tail == NULL)
13891 goto discard_descr;
13892 }
13893 return descr;
13894
13895 discard_descr:
13896 /* Discard the descriptor and release any addr_table entries it uses. */
13897 remove_loc_list_addr_table_entries (descr);
13898 return NULL;
13899 }
13900
13901 /* Return the dwarf representation of the location list LOC_LIST of
13902 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13903 function. */
13904
13905 static dw_loc_list_ref
13906 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13907 {
13908 const char *endname, *secname;
13909 rtx varloc;
13910 enum var_init_status initialized;
13911 struct var_loc_node *node;
13912 dw_loc_descr_ref descr;
13913 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13914 dw_loc_list_ref list = NULL;
13915 dw_loc_list_ref *listp = &list;
13916
13917 /* Now that we know what section we are using for a base,
13918 actually construct the list of locations.
13919 The first location information is what is passed to the
13920 function that creates the location list, and the remaining
13921 locations just get added on to that list.
13922 Note that we only know the start address for a location
13923 (IE location changes), so to build the range, we use
13924 the range [current location start, next location start].
13925 This means we have to special case the last node, and generate
13926 a range of [last location start, end of function label]. */
13927
13928 secname = secname_for_decl (decl);
13929
13930 for (node = loc_list->first; node; node = node->next)
13931 if (GET_CODE (node->loc) == EXPR_LIST
13932 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13933 {
13934 if (GET_CODE (node->loc) == EXPR_LIST)
13935 {
13936 /* This requires DW_OP_{,bit_}piece, which is not usable
13937 inside DWARF expressions. */
13938 if (want_address != 2)
13939 continue;
13940 descr = dw_sra_loc_expr (decl, node->loc);
13941 if (descr == NULL)
13942 continue;
13943 }
13944 else
13945 {
13946 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13947 varloc = NOTE_VAR_LOCATION (node->loc);
13948 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13949 }
13950 if (descr)
13951 {
13952 bool range_across_switch = false;
13953 /* If section switch happens in between node->label
13954 and node->next->label (or end of function) and
13955 we can't emit it as a single entry list,
13956 emit two ranges, first one ending at the end
13957 of first partition and second one starting at the
13958 beginning of second partition. */
13959 if (node == loc_list->last_before_switch
13960 && (node != loc_list->first || loc_list->first->next)
13961 && current_function_decl)
13962 {
13963 endname = cfun->fde->dw_fde_end;
13964 range_across_switch = true;
13965 }
13966 /* The variable has a location between NODE->LABEL and
13967 NODE->NEXT->LABEL. */
13968 else if (node->next)
13969 endname = node->next->label;
13970 /* If the variable has a location at the last label
13971 it keeps its location until the end of function. */
13972 else if (!current_function_decl)
13973 endname = text_end_label;
13974 else
13975 {
13976 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13977 current_function_funcdef_no);
13978 endname = ggc_strdup (label_id);
13979 }
13980
13981 *listp = new_loc_list (descr, node->label, endname, secname);
13982 if (TREE_CODE (decl) == PARM_DECL
13983 && node == loc_list->first
13984 && NOTE_P (node->loc)
13985 && strcmp (node->label, endname) == 0)
13986 (*listp)->force = true;
13987 listp = &(*listp)->dw_loc_next;
13988
13989 if (range_across_switch)
13990 {
13991 if (GET_CODE (node->loc) == EXPR_LIST)
13992 descr = dw_sra_loc_expr (decl, node->loc);
13993 else
13994 {
13995 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13996 varloc = NOTE_VAR_LOCATION (node->loc);
13997 descr = dw_loc_list_1 (decl, varloc, want_address,
13998 initialized);
13999 }
14000 gcc_assert (descr);
14001 /* The variable has a location between NODE->LABEL and
14002 NODE->NEXT->LABEL. */
14003 if (node->next)
14004 endname = node->next->label;
14005 else
14006 endname = cfun->fde->dw_fde_second_end;
14007 *listp = new_loc_list (descr,
14008 cfun->fde->dw_fde_second_begin,
14009 endname, secname);
14010 listp = &(*listp)->dw_loc_next;
14011 }
14012 }
14013 }
14014
14015 /* Try to avoid the overhead of a location list emitting a location
14016 expression instead, but only if we didn't have more than one
14017 location entry in the first place. If some entries were not
14018 representable, we don't want to pretend a single entry that was
14019 applies to the entire scope in which the variable is
14020 available. */
14021 if (list && loc_list->first->next)
14022 gen_llsym (list);
14023
14024 return list;
14025 }
14026
14027 /* Return if the loc_list has only single element and thus can be represented
14028 as location description. */
14029
14030 static bool
14031 single_element_loc_list_p (dw_loc_list_ref list)
14032 {
14033 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14034 return !list->ll_symbol;
14035 }
14036
14037 /* To each location in list LIST add loc descr REF. */
14038
14039 static void
14040 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14041 {
14042 dw_loc_descr_ref copy;
14043 add_loc_descr (&list->expr, ref);
14044 list = list->dw_loc_next;
14045 while (list)
14046 {
14047 copy = ggc_alloc<dw_loc_descr_node> ();
14048 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14049 add_loc_descr (&list->expr, copy);
14050 while (copy->dw_loc_next)
14051 {
14052 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14053 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14054 copy->dw_loc_next = new_copy;
14055 copy = new_copy;
14056 }
14057 list = list->dw_loc_next;
14058 }
14059 }
14060
14061 /* Given two lists RET and LIST
14062 produce location list that is result of adding expression in LIST
14063 to expression in RET on each position in program.
14064 Might be destructive on both RET and LIST.
14065
14066 TODO: We handle only simple cases of RET or LIST having at most one
14067 element. General case would inolve sorting the lists in program order
14068 and merging them that will need some additional work.
14069 Adding that will improve quality of debug info especially for SRA-ed
14070 structures. */
14071
14072 static void
14073 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14074 {
14075 if (!list)
14076 return;
14077 if (!*ret)
14078 {
14079 *ret = list;
14080 return;
14081 }
14082 if (!list->dw_loc_next)
14083 {
14084 add_loc_descr_to_each (*ret, list->expr);
14085 return;
14086 }
14087 if (!(*ret)->dw_loc_next)
14088 {
14089 add_loc_descr_to_each (list, (*ret)->expr);
14090 *ret = list;
14091 return;
14092 }
14093 expansion_failed (NULL_TREE, NULL_RTX,
14094 "Don't know how to merge two non-trivial"
14095 " location lists.\n");
14096 *ret = NULL;
14097 return;
14098 }
14099
14100 /* LOC is constant expression. Try a luck, look it up in constant
14101 pool and return its loc_descr of its address. */
14102
14103 static dw_loc_descr_ref
14104 cst_pool_loc_descr (tree loc)
14105 {
14106 /* Get an RTL for this, if something has been emitted. */
14107 rtx rtl = lookup_constant_def (loc);
14108
14109 if (!rtl || !MEM_P (rtl))
14110 {
14111 gcc_assert (!rtl);
14112 return 0;
14113 }
14114 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14115
14116 /* TODO: We might get more coverage if we was actually delaying expansion
14117 of all expressions till end of compilation when constant pools are fully
14118 populated. */
14119 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14120 {
14121 expansion_failed (loc, NULL_RTX,
14122 "CST value in contant pool but not marked.");
14123 return 0;
14124 }
14125 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14126 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14127 }
14128
14129 /* Return dw_loc_list representing address of addr_expr LOC
14130 by looking for inner INDIRECT_REF expression and turning
14131 it into simple arithmetics. */
14132
14133 static dw_loc_list_ref
14134 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14135 {
14136 tree obj, offset;
14137 HOST_WIDE_INT bitsize, bitpos, bytepos;
14138 enum machine_mode mode;
14139 int unsignedp, volatilep = 0;
14140 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14141
14142 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14143 &bitsize, &bitpos, &offset, &mode,
14144 &unsignedp, &volatilep, false);
14145 STRIP_NOPS (obj);
14146 if (bitpos % BITS_PER_UNIT)
14147 {
14148 expansion_failed (loc, NULL_RTX, "bitfield access");
14149 return 0;
14150 }
14151 if (!INDIRECT_REF_P (obj))
14152 {
14153 expansion_failed (obj,
14154 NULL_RTX, "no indirect ref in inner refrence");
14155 return 0;
14156 }
14157 if (!offset && !bitpos)
14158 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14159 else if (toplev
14160 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14161 && (dwarf_version >= 4 || !dwarf_strict))
14162 {
14163 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14164 if (!list_ret)
14165 return 0;
14166 if (offset)
14167 {
14168 /* Variable offset. */
14169 list_ret1 = loc_list_from_tree (offset, 0);
14170 if (list_ret1 == 0)
14171 return 0;
14172 add_loc_list (&list_ret, list_ret1);
14173 if (!list_ret)
14174 return 0;
14175 add_loc_descr_to_each (list_ret,
14176 new_loc_descr (DW_OP_plus, 0, 0));
14177 }
14178 bytepos = bitpos / BITS_PER_UNIT;
14179 if (bytepos > 0)
14180 add_loc_descr_to_each (list_ret,
14181 new_loc_descr (DW_OP_plus_uconst,
14182 bytepos, 0));
14183 else if (bytepos < 0)
14184 loc_list_plus_const (list_ret, bytepos);
14185 add_loc_descr_to_each (list_ret,
14186 new_loc_descr (DW_OP_stack_value, 0, 0));
14187 }
14188 return list_ret;
14189 }
14190
14191
14192 /* Generate Dwarf location list representing LOC.
14193 If WANT_ADDRESS is false, expression computing LOC will be computed
14194 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14195 if WANT_ADDRESS is 2, expression computing address useable in location
14196 will be returned (i.e. DW_OP_reg can be used
14197 to refer to register values). */
14198
14199 static dw_loc_list_ref
14200 loc_list_from_tree (tree loc, int want_address)
14201 {
14202 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14203 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14204 int have_address = 0;
14205 enum dwarf_location_atom op;
14206
14207 /* ??? Most of the time we do not take proper care for sign/zero
14208 extending the values properly. Hopefully this won't be a real
14209 problem... */
14210
14211 switch (TREE_CODE (loc))
14212 {
14213 case ERROR_MARK:
14214 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14215 return 0;
14216
14217 case PLACEHOLDER_EXPR:
14218 /* This case involves extracting fields from an object to determine the
14219 position of other fields. We don't try to encode this here. The
14220 only user of this is Ada, which encodes the needed information using
14221 the names of types. */
14222 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14223 return 0;
14224
14225 case CALL_EXPR:
14226 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14227 /* There are no opcodes for these operations. */
14228 return 0;
14229
14230 case PREINCREMENT_EXPR:
14231 case PREDECREMENT_EXPR:
14232 case POSTINCREMENT_EXPR:
14233 case POSTDECREMENT_EXPR:
14234 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14235 /* There are no opcodes for these operations. */
14236 return 0;
14237
14238 case ADDR_EXPR:
14239 /* If we already want an address, see if there is INDIRECT_REF inside
14240 e.g. for &this->field. */
14241 if (want_address)
14242 {
14243 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14244 (loc, want_address == 2);
14245 if (list_ret)
14246 have_address = 1;
14247 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14248 && (ret = cst_pool_loc_descr (loc)))
14249 have_address = 1;
14250 }
14251 /* Otherwise, process the argument and look for the address. */
14252 if (!list_ret && !ret)
14253 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14254 else
14255 {
14256 if (want_address)
14257 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14258 return NULL;
14259 }
14260 break;
14261
14262 case VAR_DECL:
14263 if (DECL_THREAD_LOCAL_P (loc))
14264 {
14265 rtx rtl;
14266 enum dwarf_location_atom tls_op;
14267 enum dtprel_bool dtprel = dtprel_false;
14268
14269 if (targetm.have_tls)
14270 {
14271 /* If this is not defined, we have no way to emit the
14272 data. */
14273 if (!targetm.asm_out.output_dwarf_dtprel)
14274 return 0;
14275
14276 /* The way DW_OP_GNU_push_tls_address is specified, we
14277 can only look up addresses of objects in the current
14278 module. We used DW_OP_addr as first op, but that's
14279 wrong, because DW_OP_addr is relocated by the debug
14280 info consumer, while DW_OP_GNU_push_tls_address
14281 operand shouldn't be. */
14282 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14283 return 0;
14284 dtprel = dtprel_true;
14285 tls_op = DW_OP_GNU_push_tls_address;
14286 }
14287 else
14288 {
14289 if (!targetm.emutls.debug_form_tls_address
14290 || !(dwarf_version >= 3 || !dwarf_strict))
14291 return 0;
14292 /* We stuffed the control variable into the DECL_VALUE_EXPR
14293 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14294 no longer appear in gimple code. We used the control
14295 variable in specific so that we could pick it up here. */
14296 loc = DECL_VALUE_EXPR (loc);
14297 tls_op = DW_OP_form_tls_address;
14298 }
14299
14300 rtl = rtl_for_decl_location (loc);
14301 if (rtl == NULL_RTX)
14302 return 0;
14303
14304 if (!MEM_P (rtl))
14305 return 0;
14306 rtl = XEXP (rtl, 0);
14307 if (! CONSTANT_P (rtl))
14308 return 0;
14309
14310 ret = new_addr_loc_descr (rtl, dtprel);
14311 ret1 = new_loc_descr (tls_op, 0, 0);
14312 add_loc_descr (&ret, ret1);
14313
14314 have_address = 1;
14315 break;
14316 }
14317 /* FALLTHRU */
14318
14319 case PARM_DECL:
14320 case RESULT_DECL:
14321 if (DECL_HAS_VALUE_EXPR_P (loc))
14322 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14323 want_address);
14324 /* FALLTHRU */
14325
14326 case FUNCTION_DECL:
14327 {
14328 rtx rtl;
14329 var_loc_list *loc_list = lookup_decl_loc (loc);
14330
14331 if (loc_list && loc_list->first)
14332 {
14333 list_ret = dw_loc_list (loc_list, loc, want_address);
14334 have_address = want_address != 0;
14335 break;
14336 }
14337 rtl = rtl_for_decl_location (loc);
14338 if (rtl == NULL_RTX)
14339 {
14340 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14341 return 0;
14342 }
14343 else if (CONST_INT_P (rtl))
14344 {
14345 HOST_WIDE_INT val = INTVAL (rtl);
14346 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14347 val &= GET_MODE_MASK (DECL_MODE (loc));
14348 ret = int_loc_descriptor (val);
14349 }
14350 else if (GET_CODE (rtl) == CONST_STRING)
14351 {
14352 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14353 return 0;
14354 }
14355 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14356 ret = new_addr_loc_descr (rtl, dtprel_false);
14357 else
14358 {
14359 enum machine_mode mode, mem_mode;
14360
14361 /* Certain constructs can only be represented at top-level. */
14362 if (want_address == 2)
14363 {
14364 ret = loc_descriptor (rtl, VOIDmode,
14365 VAR_INIT_STATUS_INITIALIZED);
14366 have_address = 1;
14367 }
14368 else
14369 {
14370 mode = GET_MODE (rtl);
14371 mem_mode = VOIDmode;
14372 if (MEM_P (rtl))
14373 {
14374 mem_mode = mode;
14375 mode = get_address_mode (rtl);
14376 rtl = XEXP (rtl, 0);
14377 have_address = 1;
14378 }
14379 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14380 VAR_INIT_STATUS_INITIALIZED);
14381 }
14382 if (!ret)
14383 expansion_failed (loc, rtl,
14384 "failed to produce loc descriptor for rtl");
14385 }
14386 }
14387 break;
14388
14389 case MEM_REF:
14390 /* ??? FIXME. */
14391 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14392 return 0;
14393 /* Fallthru. */
14394 case INDIRECT_REF:
14395 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14396 have_address = 1;
14397 break;
14398
14399 case COMPOUND_EXPR:
14400 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14401
14402 CASE_CONVERT:
14403 case VIEW_CONVERT_EXPR:
14404 case SAVE_EXPR:
14405 case MODIFY_EXPR:
14406 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14407
14408 case COMPONENT_REF:
14409 case BIT_FIELD_REF:
14410 case ARRAY_REF:
14411 case ARRAY_RANGE_REF:
14412 case REALPART_EXPR:
14413 case IMAGPART_EXPR:
14414 {
14415 tree obj, offset;
14416 HOST_WIDE_INT bitsize, bitpos, bytepos;
14417 enum machine_mode mode;
14418 int unsignedp, volatilep = 0;
14419
14420 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14421 &unsignedp, &volatilep, false);
14422
14423 gcc_assert (obj != loc);
14424
14425 list_ret = loc_list_from_tree (obj,
14426 want_address == 2
14427 && !bitpos && !offset ? 2 : 1);
14428 /* TODO: We can extract value of the small expression via shifting even
14429 for nonzero bitpos. */
14430 if (list_ret == 0)
14431 return 0;
14432 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14433 {
14434 expansion_failed (loc, NULL_RTX,
14435 "bitfield access");
14436 return 0;
14437 }
14438
14439 if (offset != NULL_TREE)
14440 {
14441 /* Variable offset. */
14442 list_ret1 = loc_list_from_tree (offset, 0);
14443 if (list_ret1 == 0)
14444 return 0;
14445 add_loc_list (&list_ret, list_ret1);
14446 if (!list_ret)
14447 return 0;
14448 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14449 }
14450
14451 bytepos = bitpos / BITS_PER_UNIT;
14452 if (bytepos > 0)
14453 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14454 else if (bytepos < 0)
14455 loc_list_plus_const (list_ret, bytepos);
14456
14457 have_address = 1;
14458 break;
14459 }
14460
14461 case INTEGER_CST:
14462 if ((want_address || !tree_fits_shwi_p (loc))
14463 && (ret = cst_pool_loc_descr (loc)))
14464 have_address = 1;
14465 else if (want_address == 2
14466 && tree_fits_shwi_p (loc)
14467 && (ret = address_of_int_loc_descriptor
14468 (int_size_in_bytes (TREE_TYPE (loc)),
14469 tree_to_shwi (loc))))
14470 have_address = 1;
14471 else if (tree_fits_shwi_p (loc))
14472 ret = int_loc_descriptor (tree_to_shwi (loc));
14473 else
14474 {
14475 expansion_failed (loc, NULL_RTX,
14476 "Integer operand is not host integer");
14477 return 0;
14478 }
14479 break;
14480
14481 case CONSTRUCTOR:
14482 case REAL_CST:
14483 case STRING_CST:
14484 case COMPLEX_CST:
14485 if ((ret = cst_pool_loc_descr (loc)))
14486 have_address = 1;
14487 else
14488 /* We can construct small constants here using int_loc_descriptor. */
14489 expansion_failed (loc, NULL_RTX,
14490 "constructor or constant not in constant pool");
14491 break;
14492
14493 case TRUTH_AND_EXPR:
14494 case TRUTH_ANDIF_EXPR:
14495 case BIT_AND_EXPR:
14496 op = DW_OP_and;
14497 goto do_binop;
14498
14499 case TRUTH_XOR_EXPR:
14500 case BIT_XOR_EXPR:
14501 op = DW_OP_xor;
14502 goto do_binop;
14503
14504 case TRUTH_OR_EXPR:
14505 case TRUTH_ORIF_EXPR:
14506 case BIT_IOR_EXPR:
14507 op = DW_OP_or;
14508 goto do_binop;
14509
14510 case FLOOR_DIV_EXPR:
14511 case CEIL_DIV_EXPR:
14512 case ROUND_DIV_EXPR:
14513 case TRUNC_DIV_EXPR:
14514 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14515 return 0;
14516 op = DW_OP_div;
14517 goto do_binop;
14518
14519 case MINUS_EXPR:
14520 op = DW_OP_minus;
14521 goto do_binop;
14522
14523 case FLOOR_MOD_EXPR:
14524 case CEIL_MOD_EXPR:
14525 case ROUND_MOD_EXPR:
14526 case TRUNC_MOD_EXPR:
14527 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14528 {
14529 op = DW_OP_mod;
14530 goto do_binop;
14531 }
14532 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14533 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14534 if (list_ret == 0 || list_ret1 == 0)
14535 return 0;
14536
14537 add_loc_list (&list_ret, list_ret1);
14538 if (list_ret == 0)
14539 return 0;
14540 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14541 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14542 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14543 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14544 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14545 break;
14546
14547 case MULT_EXPR:
14548 op = DW_OP_mul;
14549 goto do_binop;
14550
14551 case LSHIFT_EXPR:
14552 op = DW_OP_shl;
14553 goto do_binop;
14554
14555 case RSHIFT_EXPR:
14556 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14557 goto do_binop;
14558
14559 case POINTER_PLUS_EXPR:
14560 case PLUS_EXPR:
14561 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14562 {
14563 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14564 if (list_ret == 0)
14565 return 0;
14566
14567 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14568 break;
14569 }
14570
14571 op = DW_OP_plus;
14572 goto do_binop;
14573
14574 case LE_EXPR:
14575 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14576 return 0;
14577
14578 op = DW_OP_le;
14579 goto do_binop;
14580
14581 case GE_EXPR:
14582 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14583 return 0;
14584
14585 op = DW_OP_ge;
14586 goto do_binop;
14587
14588 case LT_EXPR:
14589 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14590 return 0;
14591
14592 op = DW_OP_lt;
14593 goto do_binop;
14594
14595 case GT_EXPR:
14596 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14597 return 0;
14598
14599 op = DW_OP_gt;
14600 goto do_binop;
14601
14602 case EQ_EXPR:
14603 op = DW_OP_eq;
14604 goto do_binop;
14605
14606 case NE_EXPR:
14607 op = DW_OP_ne;
14608 goto do_binop;
14609
14610 do_binop:
14611 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14612 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14613 if (list_ret == 0 || list_ret1 == 0)
14614 return 0;
14615
14616 add_loc_list (&list_ret, list_ret1);
14617 if (list_ret == 0)
14618 return 0;
14619 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14620 break;
14621
14622 case TRUTH_NOT_EXPR:
14623 case BIT_NOT_EXPR:
14624 op = DW_OP_not;
14625 goto do_unop;
14626
14627 case ABS_EXPR:
14628 op = DW_OP_abs;
14629 goto do_unop;
14630
14631 case NEGATE_EXPR:
14632 op = DW_OP_neg;
14633 goto do_unop;
14634
14635 do_unop:
14636 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14637 if (list_ret == 0)
14638 return 0;
14639
14640 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14641 break;
14642
14643 case MIN_EXPR:
14644 case MAX_EXPR:
14645 {
14646 const enum tree_code code =
14647 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14648
14649 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14650 build2 (code, integer_type_node,
14651 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14652 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14653 }
14654
14655 /* ... fall through ... */
14656
14657 case COND_EXPR:
14658 {
14659 dw_loc_descr_ref lhs
14660 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14661 dw_loc_list_ref rhs
14662 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14663 dw_loc_descr_ref bra_node, jump_node, tmp;
14664
14665 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14666 if (list_ret == 0 || lhs == 0 || rhs == 0)
14667 return 0;
14668
14669 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14670 add_loc_descr_to_each (list_ret, bra_node);
14671
14672 add_loc_list (&list_ret, rhs);
14673 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14674 add_loc_descr_to_each (list_ret, jump_node);
14675
14676 add_loc_descr_to_each (list_ret, lhs);
14677 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14678 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14679
14680 /* ??? Need a node to point the skip at. Use a nop. */
14681 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14682 add_loc_descr_to_each (list_ret, tmp);
14683 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14684 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14685 }
14686 break;
14687
14688 case FIX_TRUNC_EXPR:
14689 return 0;
14690
14691 default:
14692 /* Leave front-end specific codes as simply unknown. This comes
14693 up, for instance, with the C STMT_EXPR. */
14694 if ((unsigned int) TREE_CODE (loc)
14695 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14696 {
14697 expansion_failed (loc, NULL_RTX,
14698 "language specific tree node");
14699 return 0;
14700 }
14701
14702 #ifdef ENABLE_CHECKING
14703 /* Otherwise this is a generic code; we should just lists all of
14704 these explicitly. We forgot one. */
14705 gcc_unreachable ();
14706 #else
14707 /* In a release build, we want to degrade gracefully: better to
14708 generate incomplete debugging information than to crash. */
14709 return NULL;
14710 #endif
14711 }
14712
14713 if (!ret && !list_ret)
14714 return 0;
14715
14716 if (want_address == 2 && !have_address
14717 && (dwarf_version >= 4 || !dwarf_strict))
14718 {
14719 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14720 {
14721 expansion_failed (loc, NULL_RTX,
14722 "DWARF address size mismatch");
14723 return 0;
14724 }
14725 if (ret)
14726 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14727 else
14728 add_loc_descr_to_each (list_ret,
14729 new_loc_descr (DW_OP_stack_value, 0, 0));
14730 have_address = 1;
14731 }
14732 /* Show if we can't fill the request for an address. */
14733 if (want_address && !have_address)
14734 {
14735 expansion_failed (loc, NULL_RTX,
14736 "Want address and only have value");
14737 return 0;
14738 }
14739
14740 gcc_assert (!ret || !list_ret);
14741
14742 /* If we've got an address and don't want one, dereference. */
14743 if (!want_address && have_address)
14744 {
14745 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14746
14747 if (size > DWARF2_ADDR_SIZE || size == -1)
14748 {
14749 expansion_failed (loc, NULL_RTX,
14750 "DWARF address size mismatch");
14751 return 0;
14752 }
14753 else if (size == DWARF2_ADDR_SIZE)
14754 op = DW_OP_deref;
14755 else
14756 op = DW_OP_deref_size;
14757
14758 if (ret)
14759 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14760 else
14761 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14762 }
14763 if (ret)
14764 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14765
14766 return list_ret;
14767 }
14768
14769 /* Same as above but return only single location expression. */
14770 static dw_loc_descr_ref
14771 loc_descriptor_from_tree (tree loc, int want_address)
14772 {
14773 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14774 if (!ret)
14775 return NULL;
14776 if (ret->dw_loc_next)
14777 {
14778 expansion_failed (loc, NULL_RTX,
14779 "Location list where only loc descriptor needed");
14780 return NULL;
14781 }
14782 return ret->expr;
14783 }
14784
14785 /* Given a value, round it up to the lowest multiple of `boundary'
14786 which is not less than the value itself. */
14787
14788 static inline HOST_WIDE_INT
14789 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14790 {
14791 return (((value + boundary - 1) / boundary) * boundary);
14792 }
14793
14794 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14795 pointer to the declared type for the relevant field variable, or return
14796 `integer_type_node' if the given node turns out to be an
14797 ERROR_MARK node. */
14798
14799 static inline tree
14800 field_type (const_tree decl)
14801 {
14802 tree type;
14803
14804 if (TREE_CODE (decl) == ERROR_MARK)
14805 return integer_type_node;
14806
14807 type = DECL_BIT_FIELD_TYPE (decl);
14808 if (type == NULL_TREE)
14809 type = TREE_TYPE (decl);
14810
14811 return type;
14812 }
14813
14814 /* Given a pointer to a tree node, return the alignment in bits for
14815 it, or else return BITS_PER_WORD if the node actually turns out to
14816 be an ERROR_MARK node. */
14817
14818 static inline unsigned
14819 simple_type_align_in_bits (const_tree type)
14820 {
14821 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14822 }
14823
14824 static inline unsigned
14825 simple_decl_align_in_bits (const_tree decl)
14826 {
14827 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14828 }
14829
14830 /* Return the result of rounding T up to ALIGN. */
14831
14832 static inline offset_int
14833 round_up_to_align (const offset_int &t, unsigned int align)
14834 {
14835 return wi::udiv_trunc (t + align - 1, align) * align;
14836 }
14837
14838 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14839 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14840 or return 0 if we are unable to determine what that offset is, either
14841 because the argument turns out to be a pointer to an ERROR_MARK node, or
14842 because the offset is actually variable. (We can't handle the latter case
14843 just yet). */
14844
14845 static HOST_WIDE_INT
14846 field_byte_offset (const_tree decl)
14847 {
14848 offset_int object_offset_in_bits;
14849 offset_int object_offset_in_bytes;
14850 offset_int bitpos_int;
14851
14852 if (TREE_CODE (decl) == ERROR_MARK)
14853 return 0;
14854
14855 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14856
14857 /* We cannot yet cope with fields whose positions are variable, so
14858 for now, when we see such things, we simply return 0. Someday, we may
14859 be able to handle such cases, but it will be damn difficult. */
14860 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14861 return 0;
14862
14863 bitpos_int = wi::to_offset (bit_position (decl));
14864
14865 #ifdef PCC_BITFIELD_TYPE_MATTERS
14866 if (PCC_BITFIELD_TYPE_MATTERS)
14867 {
14868 tree type;
14869 tree field_size_tree;
14870 offset_int deepest_bitpos;
14871 offset_int field_size_in_bits;
14872 unsigned int type_align_in_bits;
14873 unsigned int decl_align_in_bits;
14874 offset_int type_size_in_bits;
14875
14876 type = field_type (decl);
14877 type_size_in_bits = offset_int_type_size_in_bits (type);
14878 type_align_in_bits = simple_type_align_in_bits (type);
14879
14880 field_size_tree = DECL_SIZE (decl);
14881
14882 /* The size could be unspecified if there was an error, or for
14883 a flexible array member. */
14884 if (!field_size_tree)
14885 field_size_tree = bitsize_zero_node;
14886
14887 /* If the size of the field is not constant, use the type size. */
14888 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14889 field_size_in_bits = wi::to_offset (field_size_tree);
14890 else
14891 field_size_in_bits = type_size_in_bits;
14892
14893 decl_align_in_bits = simple_decl_align_in_bits (decl);
14894
14895 /* The GCC front-end doesn't make any attempt to keep track of the
14896 starting bit offset (relative to the start of the containing
14897 structure type) of the hypothetical "containing object" for a
14898 bit-field. Thus, when computing the byte offset value for the
14899 start of the "containing object" of a bit-field, we must deduce
14900 this information on our own. This can be rather tricky to do in
14901 some cases. For example, handling the following structure type
14902 definition when compiling for an i386/i486 target (which only
14903 aligns long long's to 32-bit boundaries) can be very tricky:
14904
14905 struct S { int field1; long long field2:31; };
14906
14907 Fortunately, there is a simple rule-of-thumb which can be used
14908 in such cases. When compiling for an i386/i486, GCC will
14909 allocate 8 bytes for the structure shown above. It decides to
14910 do this based upon one simple rule for bit-field allocation.
14911 GCC allocates each "containing object" for each bit-field at
14912 the first (i.e. lowest addressed) legitimate alignment boundary
14913 (based upon the required minimum alignment for the declared
14914 type of the field) which it can possibly use, subject to the
14915 condition that there is still enough available space remaining
14916 in the containing object (when allocated at the selected point)
14917 to fully accommodate all of the bits of the bit-field itself.
14918
14919 This simple rule makes it obvious why GCC allocates 8 bytes for
14920 each object of the structure type shown above. When looking
14921 for a place to allocate the "containing object" for `field2',
14922 the compiler simply tries to allocate a 64-bit "containing
14923 object" at each successive 32-bit boundary (starting at zero)
14924 until it finds a place to allocate that 64- bit field such that
14925 at least 31 contiguous (and previously unallocated) bits remain
14926 within that selected 64 bit field. (As it turns out, for the
14927 example above, the compiler finds it is OK to allocate the
14928 "containing object" 64-bit field at bit-offset zero within the
14929 structure type.)
14930
14931 Here we attempt to work backwards from the limited set of facts
14932 we're given, and we try to deduce from those facts, where GCC
14933 must have believed that the containing object started (within
14934 the structure type). The value we deduce is then used (by the
14935 callers of this routine) to generate DW_AT_location and
14936 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14937 the case of DW_AT_location, regular fields as well). */
14938
14939 /* Figure out the bit-distance from the start of the structure to
14940 the "deepest" bit of the bit-field. */
14941 deepest_bitpos = bitpos_int + field_size_in_bits;
14942
14943 /* This is the tricky part. Use some fancy footwork to deduce
14944 where the lowest addressed bit of the containing object must
14945 be. */
14946 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14947
14948 /* Round up to type_align by default. This works best for
14949 bitfields. */
14950 object_offset_in_bits
14951 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14952
14953 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14954 {
14955 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14956
14957 /* Round up to decl_align instead. */
14958 object_offset_in_bits
14959 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14960 }
14961 }
14962 else
14963 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14964 object_offset_in_bits = bitpos_int;
14965
14966 object_offset_in_bytes
14967 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14968 return object_offset_in_bytes.to_shwi ();
14969 }
14970 \f
14971 /* The following routines define various Dwarf attributes and any data
14972 associated with them. */
14973
14974 /* Add a location description attribute value to a DIE.
14975
14976 This emits location attributes suitable for whole variables and
14977 whole parameters. Note that the location attributes for struct fields are
14978 generated by the routine `data_member_location_attribute' below. */
14979
14980 static inline void
14981 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14982 dw_loc_list_ref descr)
14983 {
14984 if (descr == 0)
14985 return;
14986 if (single_element_loc_list_p (descr))
14987 add_AT_loc (die, attr_kind, descr->expr);
14988 else
14989 add_AT_loc_list (die, attr_kind, descr);
14990 }
14991
14992 /* Add DW_AT_accessibility attribute to DIE if needed. */
14993
14994 static void
14995 add_accessibility_attribute (dw_die_ref die, tree decl)
14996 {
14997 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14998 children, otherwise the default is DW_ACCESS_public. In DWARF2
14999 the default has always been DW_ACCESS_public. */
15000 if (TREE_PROTECTED (decl))
15001 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15002 else if (TREE_PRIVATE (decl))
15003 {
15004 if (dwarf_version == 2
15005 || die->die_parent == NULL
15006 || die->die_parent->die_tag != DW_TAG_class_type)
15007 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15008 }
15009 else if (dwarf_version > 2
15010 && die->die_parent
15011 && die->die_parent->die_tag == DW_TAG_class_type)
15012 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15013 }
15014
15015 /* Attach the specialized form of location attribute used for data members of
15016 struct and union types. In the special case of a FIELD_DECL node which
15017 represents a bit-field, the "offset" part of this special location
15018 descriptor must indicate the distance in bytes from the lowest-addressed
15019 byte of the containing struct or union type to the lowest-addressed byte of
15020 the "containing object" for the bit-field. (See the `field_byte_offset'
15021 function above).
15022
15023 For any given bit-field, the "containing object" is a hypothetical object
15024 (of some integral or enum type) within which the given bit-field lives. The
15025 type of this hypothetical "containing object" is always the same as the
15026 declared type of the individual bit-field itself (for GCC anyway... the
15027 DWARF spec doesn't actually mandate this). Note that it is the size (in
15028 bytes) of the hypothetical "containing object" which will be given in the
15029 DW_AT_byte_size attribute for this bit-field. (See the
15030 `byte_size_attribute' function below.) It is also used when calculating the
15031 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15032 function below.) */
15033
15034 static void
15035 add_data_member_location_attribute (dw_die_ref die, tree decl)
15036 {
15037 HOST_WIDE_INT offset;
15038 dw_loc_descr_ref loc_descr = 0;
15039
15040 if (TREE_CODE (decl) == TREE_BINFO)
15041 {
15042 /* We're working on the TAG_inheritance for a base class. */
15043 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15044 {
15045 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15046 aren't at a fixed offset from all (sub)objects of the same
15047 type. We need to extract the appropriate offset from our
15048 vtable. The following dwarf expression means
15049
15050 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15051
15052 This is specific to the V3 ABI, of course. */
15053
15054 dw_loc_descr_ref tmp;
15055
15056 /* Make a copy of the object address. */
15057 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15058 add_loc_descr (&loc_descr, tmp);
15059
15060 /* Extract the vtable address. */
15061 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15062 add_loc_descr (&loc_descr, tmp);
15063
15064 /* Calculate the address of the offset. */
15065 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15066 gcc_assert (offset < 0);
15067
15068 tmp = int_loc_descriptor (-offset);
15069 add_loc_descr (&loc_descr, tmp);
15070 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15071 add_loc_descr (&loc_descr, tmp);
15072
15073 /* Extract the offset. */
15074 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15075 add_loc_descr (&loc_descr, tmp);
15076
15077 /* Add it to the object address. */
15078 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15079 add_loc_descr (&loc_descr, tmp);
15080 }
15081 else
15082 offset = tree_to_shwi (BINFO_OFFSET (decl));
15083 }
15084 else
15085 offset = field_byte_offset (decl);
15086
15087 if (! loc_descr)
15088 {
15089 if (dwarf_version > 2)
15090 {
15091 /* Don't need to output a location expression, just the constant. */
15092 if (offset < 0)
15093 add_AT_int (die, DW_AT_data_member_location, offset);
15094 else
15095 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15096 return;
15097 }
15098 else
15099 {
15100 enum dwarf_location_atom op;
15101
15102 /* The DWARF2 standard says that we should assume that the structure
15103 address is already on the stack, so we can specify a structure
15104 field address by using DW_OP_plus_uconst. */
15105 op = DW_OP_plus_uconst;
15106 loc_descr = new_loc_descr (op, offset, 0);
15107 }
15108 }
15109
15110 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15111 }
15112
15113 /* Writes integer values to dw_vec_const array. */
15114
15115 static void
15116 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15117 {
15118 while (size != 0)
15119 {
15120 *dest++ = val & 0xff;
15121 val >>= 8;
15122 --size;
15123 }
15124 }
15125
15126 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15127
15128 static HOST_WIDE_INT
15129 extract_int (const unsigned char *src, unsigned int size)
15130 {
15131 HOST_WIDE_INT val = 0;
15132
15133 src += size;
15134 while (size != 0)
15135 {
15136 val <<= 8;
15137 val |= *--src & 0xff;
15138 --size;
15139 }
15140 return val;
15141 }
15142
15143 /* Writes wide_int values to dw_vec_const array. */
15144
15145 static void
15146 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15147 {
15148 int i;
15149
15150 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15151 {
15152 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15153 return;
15154 }
15155
15156 /* We'd have to extend this code to support odd sizes. */
15157 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15158
15159 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15160
15161 if (WORDS_BIG_ENDIAN)
15162 for (i = n - 1; i >= 0; i--)
15163 {
15164 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15165 dest += sizeof (HOST_WIDE_INT);
15166 }
15167 else
15168 for (i = 0; i < n; i++)
15169 {
15170 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15171 dest += sizeof (HOST_WIDE_INT);
15172 }
15173 }
15174
15175 /* Writes floating point values to dw_vec_const array. */
15176
15177 static void
15178 insert_float (const_rtx rtl, unsigned char *array)
15179 {
15180 REAL_VALUE_TYPE rv;
15181 long val[4];
15182 int i;
15183
15184 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15185 real_to_target (val, &rv, GET_MODE (rtl));
15186
15187 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15188 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15189 {
15190 insert_int (val[i], 4, array);
15191 array += 4;
15192 }
15193 }
15194
15195 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15196 does not have a "location" either in memory or in a register. These
15197 things can arise in GNU C when a constant is passed as an actual parameter
15198 to an inlined function. They can also arise in C++ where declared
15199 constants do not necessarily get memory "homes". */
15200
15201 static bool
15202 add_const_value_attribute (dw_die_ref die, rtx rtl)
15203 {
15204 switch (GET_CODE (rtl))
15205 {
15206 case CONST_INT:
15207 {
15208 HOST_WIDE_INT val = INTVAL (rtl);
15209
15210 if (val < 0)
15211 add_AT_int (die, DW_AT_const_value, val);
15212 else
15213 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15214 }
15215 return true;
15216
15217 case CONST_WIDE_INT:
15218 add_AT_wide (die, DW_AT_const_value,
15219 std::make_pair (rtl, GET_MODE (rtl)));
15220 return true;
15221
15222 case CONST_DOUBLE:
15223 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15224 floating-point constant. A CONST_DOUBLE is used whenever the
15225 constant requires more than one word in order to be adequately
15226 represented. */
15227 {
15228 enum machine_mode mode = GET_MODE (rtl);
15229
15230 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15231 add_AT_double (die, DW_AT_const_value,
15232 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15233 else
15234 {
15235 unsigned int length = GET_MODE_SIZE (mode);
15236 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15237
15238 insert_float (rtl, array);
15239 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15240 }
15241 }
15242 return true;
15243
15244 case CONST_VECTOR:
15245 {
15246 enum machine_mode mode = GET_MODE (rtl);
15247 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15248 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15249 unsigned char *array
15250 = ggc_vec_alloc<unsigned char> (length * elt_size);
15251 unsigned int i;
15252 unsigned char *p;
15253 enum machine_mode imode = GET_MODE_INNER (mode);
15254
15255 switch (GET_MODE_CLASS (mode))
15256 {
15257 case MODE_VECTOR_INT:
15258 for (i = 0, p = array; i < length; i++, p += elt_size)
15259 {
15260 rtx elt = CONST_VECTOR_ELT (rtl, i);
15261 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15262 }
15263 break;
15264
15265 case MODE_VECTOR_FLOAT:
15266 for (i = 0, p = array; i < length; i++, p += elt_size)
15267 {
15268 rtx elt = CONST_VECTOR_ELT (rtl, i);
15269 insert_float (elt, p);
15270 }
15271 break;
15272
15273 default:
15274 gcc_unreachable ();
15275 }
15276
15277 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15278 }
15279 return true;
15280
15281 case CONST_STRING:
15282 if (dwarf_version >= 4 || !dwarf_strict)
15283 {
15284 dw_loc_descr_ref loc_result;
15285 resolve_one_addr (&rtl, NULL);
15286 rtl_addr:
15287 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15288 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15289 add_AT_loc (die, DW_AT_location, loc_result);
15290 vec_safe_push (used_rtx_array, rtl);
15291 return true;
15292 }
15293 return false;
15294
15295 case CONST:
15296 if (CONSTANT_P (XEXP (rtl, 0)))
15297 return add_const_value_attribute (die, XEXP (rtl, 0));
15298 /* FALLTHROUGH */
15299 case SYMBOL_REF:
15300 if (!const_ok_for_output (rtl))
15301 return false;
15302 case LABEL_REF:
15303 if (dwarf_version >= 4 || !dwarf_strict)
15304 goto rtl_addr;
15305 return false;
15306
15307 case PLUS:
15308 /* In cases where an inlined instance of an inline function is passed
15309 the address of an `auto' variable (which is local to the caller) we
15310 can get a situation where the DECL_RTL of the artificial local
15311 variable (for the inlining) which acts as a stand-in for the
15312 corresponding formal parameter (of the inline function) will look
15313 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15314 exactly a compile-time constant expression, but it isn't the address
15315 of the (artificial) local variable either. Rather, it represents the
15316 *value* which the artificial local variable always has during its
15317 lifetime. We currently have no way to represent such quasi-constant
15318 values in Dwarf, so for now we just punt and generate nothing. */
15319 return false;
15320
15321 case HIGH:
15322 case CONST_FIXED:
15323 return false;
15324
15325 case MEM:
15326 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15327 && MEM_READONLY_P (rtl)
15328 && GET_MODE (rtl) == BLKmode)
15329 {
15330 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15331 return true;
15332 }
15333 return false;
15334
15335 default:
15336 /* No other kinds of rtx should be possible here. */
15337 gcc_unreachable ();
15338 }
15339 return false;
15340 }
15341
15342 /* Determine whether the evaluation of EXPR references any variables
15343 or functions which aren't otherwise used (and therefore may not be
15344 output). */
15345 static tree
15346 reference_to_unused (tree * tp, int * walk_subtrees,
15347 void * data ATTRIBUTE_UNUSED)
15348 {
15349 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15350 *walk_subtrees = 0;
15351
15352 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15353 && ! TREE_ASM_WRITTEN (*tp))
15354 return *tp;
15355 /* ??? The C++ FE emits debug information for using decls, so
15356 putting gcc_unreachable here falls over. See PR31899. For now
15357 be conservative. */
15358 else if (!cgraph_global_info_ready
15359 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15360 return *tp;
15361 else if (TREE_CODE (*tp) == VAR_DECL)
15362 {
15363 varpool_node *node = varpool_get_node (*tp);
15364 if (!node || !node->definition)
15365 return *tp;
15366 }
15367 else if (TREE_CODE (*tp) == FUNCTION_DECL
15368 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15369 {
15370 /* The call graph machinery must have finished analyzing,
15371 optimizing and gimplifying the CU by now.
15372 So if *TP has no call graph node associated
15373 to it, it means *TP will not be emitted. */
15374 if (!cgraph_get_node (*tp))
15375 return *tp;
15376 }
15377 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15378 return *tp;
15379
15380 return NULL_TREE;
15381 }
15382
15383 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15384 for use in a later add_const_value_attribute call. */
15385
15386 static rtx
15387 rtl_for_decl_init (tree init, tree type)
15388 {
15389 rtx rtl = NULL_RTX;
15390
15391 STRIP_NOPS (init);
15392
15393 /* If a variable is initialized with a string constant without embedded
15394 zeros, build CONST_STRING. */
15395 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15396 {
15397 tree enttype = TREE_TYPE (type);
15398 tree domain = TYPE_DOMAIN (type);
15399 enum machine_mode mode = TYPE_MODE (enttype);
15400
15401 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15402 && domain
15403 && integer_zerop (TYPE_MIN_VALUE (domain))
15404 && compare_tree_int (TYPE_MAX_VALUE (domain),
15405 TREE_STRING_LENGTH (init) - 1) == 0
15406 && ((size_t) TREE_STRING_LENGTH (init)
15407 == strlen (TREE_STRING_POINTER (init)) + 1))
15408 {
15409 rtl = gen_rtx_CONST_STRING (VOIDmode,
15410 ggc_strdup (TREE_STRING_POINTER (init)));
15411 rtl = gen_rtx_MEM (BLKmode, rtl);
15412 MEM_READONLY_P (rtl) = 1;
15413 }
15414 }
15415 /* Other aggregates, and complex values, could be represented using
15416 CONCAT: FIXME! */
15417 else if (AGGREGATE_TYPE_P (type)
15418 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15419 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15420 || TREE_CODE (type) == COMPLEX_TYPE)
15421 ;
15422 /* Vectors only work if their mode is supported by the target.
15423 FIXME: generic vectors ought to work too. */
15424 else if (TREE_CODE (type) == VECTOR_TYPE
15425 && !VECTOR_MODE_P (TYPE_MODE (type)))
15426 ;
15427 /* If the initializer is something that we know will expand into an
15428 immediate RTL constant, expand it now. We must be careful not to
15429 reference variables which won't be output. */
15430 else if (initializer_constant_valid_p (init, type)
15431 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15432 {
15433 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15434 possible. */
15435 if (TREE_CODE (type) == VECTOR_TYPE)
15436 switch (TREE_CODE (init))
15437 {
15438 case VECTOR_CST:
15439 break;
15440 case CONSTRUCTOR:
15441 if (TREE_CONSTANT (init))
15442 {
15443 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15444 bool constant_p = true;
15445 tree value;
15446 unsigned HOST_WIDE_INT ix;
15447
15448 /* Even when ctor is constant, it might contain non-*_CST
15449 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15450 belong into VECTOR_CST nodes. */
15451 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15452 if (!CONSTANT_CLASS_P (value))
15453 {
15454 constant_p = false;
15455 break;
15456 }
15457
15458 if (constant_p)
15459 {
15460 init = build_vector_from_ctor (type, elts);
15461 break;
15462 }
15463 }
15464 /* FALLTHRU */
15465
15466 default:
15467 return NULL;
15468 }
15469
15470 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15471
15472 /* If expand_expr returns a MEM, it wasn't immediate. */
15473 gcc_assert (!rtl || !MEM_P (rtl));
15474 }
15475
15476 return rtl;
15477 }
15478
15479 /* Generate RTL for the variable DECL to represent its location. */
15480
15481 static rtx
15482 rtl_for_decl_location (tree decl)
15483 {
15484 rtx rtl;
15485
15486 /* Here we have to decide where we are going to say the parameter "lives"
15487 (as far as the debugger is concerned). We only have a couple of
15488 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15489
15490 DECL_RTL normally indicates where the parameter lives during most of the
15491 activation of the function. If optimization is enabled however, this
15492 could be either NULL or else a pseudo-reg. Both of those cases indicate
15493 that the parameter doesn't really live anywhere (as far as the code
15494 generation parts of GCC are concerned) during most of the function's
15495 activation. That will happen (for example) if the parameter is never
15496 referenced within the function.
15497
15498 We could just generate a location descriptor here for all non-NULL
15499 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15500 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15501 where DECL_RTL is NULL or is a pseudo-reg.
15502
15503 Note however that we can only get away with using DECL_INCOMING_RTL as
15504 a backup substitute for DECL_RTL in certain limited cases. In cases
15505 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15506 we can be sure that the parameter was passed using the same type as it is
15507 declared to have within the function, and that its DECL_INCOMING_RTL
15508 points us to a place where a value of that type is passed.
15509
15510 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15511 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15512 because in these cases DECL_INCOMING_RTL points us to a value of some
15513 type which is *different* from the type of the parameter itself. Thus,
15514 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15515 such cases, the debugger would end up (for example) trying to fetch a
15516 `float' from a place which actually contains the first part of a
15517 `double'. That would lead to really incorrect and confusing
15518 output at debug-time.
15519
15520 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15521 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15522 are a couple of exceptions however. On little-endian machines we can
15523 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15524 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15525 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15526 when (on a little-endian machine) a non-prototyped function has a
15527 parameter declared to be of type `short' or `char'. In such cases,
15528 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15529 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15530 passed `int' value. If the debugger then uses that address to fetch
15531 a `short' or a `char' (on a little-endian machine) the result will be
15532 the correct data, so we allow for such exceptional cases below.
15533
15534 Note that our goal here is to describe the place where the given formal
15535 parameter lives during most of the function's activation (i.e. between the
15536 end of the prologue and the start of the epilogue). We'll do that as best
15537 as we can. Note however that if the given formal parameter is modified
15538 sometime during the execution of the function, then a stack backtrace (at
15539 debug-time) will show the function as having been called with the *new*
15540 value rather than the value which was originally passed in. This happens
15541 rarely enough that it is not a major problem, but it *is* a problem, and
15542 I'd like to fix it.
15543
15544 A future version of dwarf2out.c may generate two additional attributes for
15545 any given DW_TAG_formal_parameter DIE which will describe the "passed
15546 type" and the "passed location" for the given formal parameter in addition
15547 to the attributes we now generate to indicate the "declared type" and the
15548 "active location" for each parameter. This additional set of attributes
15549 could be used by debuggers for stack backtraces. Separately, note that
15550 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15551 This happens (for example) for inlined-instances of inline function formal
15552 parameters which are never referenced. This really shouldn't be
15553 happening. All PARM_DECL nodes should get valid non-NULL
15554 DECL_INCOMING_RTL values. FIXME. */
15555
15556 /* Use DECL_RTL as the "location" unless we find something better. */
15557 rtl = DECL_RTL_IF_SET (decl);
15558
15559 /* When generating abstract instances, ignore everything except
15560 constants, symbols living in memory, and symbols living in
15561 fixed registers. */
15562 if (! reload_completed)
15563 {
15564 if (rtl
15565 && (CONSTANT_P (rtl)
15566 || (MEM_P (rtl)
15567 && CONSTANT_P (XEXP (rtl, 0)))
15568 || (REG_P (rtl)
15569 && TREE_CODE (decl) == VAR_DECL
15570 && TREE_STATIC (decl))))
15571 {
15572 rtl = targetm.delegitimize_address (rtl);
15573 return rtl;
15574 }
15575 rtl = NULL_RTX;
15576 }
15577 else if (TREE_CODE (decl) == PARM_DECL)
15578 {
15579 if (rtl == NULL_RTX
15580 || is_pseudo_reg (rtl)
15581 || (MEM_P (rtl)
15582 && is_pseudo_reg (XEXP (rtl, 0))
15583 && DECL_INCOMING_RTL (decl)
15584 && MEM_P (DECL_INCOMING_RTL (decl))
15585 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15586 {
15587 tree declared_type = TREE_TYPE (decl);
15588 tree passed_type = DECL_ARG_TYPE (decl);
15589 enum machine_mode dmode = TYPE_MODE (declared_type);
15590 enum machine_mode pmode = TYPE_MODE (passed_type);
15591
15592 /* This decl represents a formal parameter which was optimized out.
15593 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15594 all cases where (rtl == NULL_RTX) just below. */
15595 if (dmode == pmode)
15596 rtl = DECL_INCOMING_RTL (decl);
15597 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15598 && SCALAR_INT_MODE_P (dmode)
15599 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15600 && DECL_INCOMING_RTL (decl))
15601 {
15602 rtx inc = DECL_INCOMING_RTL (decl);
15603 if (REG_P (inc))
15604 rtl = inc;
15605 else if (MEM_P (inc))
15606 {
15607 if (BYTES_BIG_ENDIAN)
15608 rtl = adjust_address_nv (inc, dmode,
15609 GET_MODE_SIZE (pmode)
15610 - GET_MODE_SIZE (dmode));
15611 else
15612 rtl = inc;
15613 }
15614 }
15615 }
15616
15617 /* If the parm was passed in registers, but lives on the stack, then
15618 make a big endian correction if the mode of the type of the
15619 parameter is not the same as the mode of the rtl. */
15620 /* ??? This is the same series of checks that are made in dbxout.c before
15621 we reach the big endian correction code there. It isn't clear if all
15622 of these checks are necessary here, but keeping them all is the safe
15623 thing to do. */
15624 else if (MEM_P (rtl)
15625 && XEXP (rtl, 0) != const0_rtx
15626 && ! CONSTANT_P (XEXP (rtl, 0))
15627 /* Not passed in memory. */
15628 && !MEM_P (DECL_INCOMING_RTL (decl))
15629 /* Not passed by invisible reference. */
15630 && (!REG_P (XEXP (rtl, 0))
15631 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15632 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15633 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15634 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15635 #endif
15636 )
15637 /* Big endian correction check. */
15638 && BYTES_BIG_ENDIAN
15639 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15640 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15641 < UNITS_PER_WORD))
15642 {
15643 enum machine_mode addr_mode = get_address_mode (rtl);
15644 int offset = (UNITS_PER_WORD
15645 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15646
15647 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15648 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15649 }
15650 }
15651 else if (TREE_CODE (decl) == VAR_DECL
15652 && rtl
15653 && MEM_P (rtl)
15654 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15655 && BYTES_BIG_ENDIAN)
15656 {
15657 enum machine_mode addr_mode = get_address_mode (rtl);
15658 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15659 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15660
15661 /* If a variable is declared "register" yet is smaller than
15662 a register, then if we store the variable to memory, it
15663 looks like we're storing a register-sized value, when in
15664 fact we are not. We need to adjust the offset of the
15665 storage location to reflect the actual value's bytes,
15666 else gdb will not be able to display it. */
15667 if (rsize > dsize)
15668 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15669 plus_constant (addr_mode, XEXP (rtl, 0),
15670 rsize - dsize));
15671 }
15672
15673 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15674 and will have been substituted directly into all expressions that use it.
15675 C does not have such a concept, but C++ and other languages do. */
15676 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15677 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15678
15679 if (rtl)
15680 rtl = targetm.delegitimize_address (rtl);
15681
15682 /* If we don't look past the constant pool, we risk emitting a
15683 reference to a constant pool entry that isn't referenced from
15684 code, and thus is not emitted. */
15685 if (rtl)
15686 rtl = avoid_constant_pool_reference (rtl);
15687
15688 /* Try harder to get a rtl. If this symbol ends up not being emitted
15689 in the current CU, resolve_addr will remove the expression referencing
15690 it. */
15691 if (rtl == NULL_RTX
15692 && TREE_CODE (decl) == VAR_DECL
15693 && !DECL_EXTERNAL (decl)
15694 && TREE_STATIC (decl)
15695 && DECL_NAME (decl)
15696 && !DECL_HARD_REGISTER (decl)
15697 && DECL_MODE (decl) != VOIDmode)
15698 {
15699 rtl = make_decl_rtl_for_debug (decl);
15700 if (!MEM_P (rtl)
15701 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15702 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15703 rtl = NULL_RTX;
15704 }
15705
15706 return rtl;
15707 }
15708
15709 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15710 returned. If so, the decl for the COMMON block is returned, and the
15711 value is the offset into the common block for the symbol. */
15712
15713 static tree
15714 fortran_common (tree decl, HOST_WIDE_INT *value)
15715 {
15716 tree val_expr, cvar;
15717 enum machine_mode mode;
15718 HOST_WIDE_INT bitsize, bitpos;
15719 tree offset;
15720 int unsignedp, volatilep = 0;
15721
15722 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15723 it does not have a value (the offset into the common area), or if it
15724 is thread local (as opposed to global) then it isn't common, and shouldn't
15725 be handled as such. */
15726 if (TREE_CODE (decl) != VAR_DECL
15727 || !TREE_STATIC (decl)
15728 || !DECL_HAS_VALUE_EXPR_P (decl)
15729 || !is_fortran ())
15730 return NULL_TREE;
15731
15732 val_expr = DECL_VALUE_EXPR (decl);
15733 if (TREE_CODE (val_expr) != COMPONENT_REF)
15734 return NULL_TREE;
15735
15736 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15737 &mode, &unsignedp, &volatilep, true);
15738
15739 if (cvar == NULL_TREE
15740 || TREE_CODE (cvar) != VAR_DECL
15741 || DECL_ARTIFICIAL (cvar)
15742 || !TREE_PUBLIC (cvar))
15743 return NULL_TREE;
15744
15745 *value = 0;
15746 if (offset != NULL)
15747 {
15748 if (!tree_fits_shwi_p (offset))
15749 return NULL_TREE;
15750 *value = tree_to_shwi (offset);
15751 }
15752 if (bitpos != 0)
15753 *value += bitpos / BITS_PER_UNIT;
15754
15755 return cvar;
15756 }
15757
15758 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15759 data attribute for a variable or a parameter. We generate the
15760 DW_AT_const_value attribute only in those cases where the given variable
15761 or parameter does not have a true "location" either in memory or in a
15762 register. This can happen (for example) when a constant is passed as an
15763 actual argument in a call to an inline function. (It's possible that
15764 these things can crop up in other ways also.) Note that one type of
15765 constant value which can be passed into an inlined function is a constant
15766 pointer. This can happen for example if an actual argument in an inlined
15767 function call evaluates to a compile-time constant address.
15768
15769 CACHE_P is true if it is worth caching the location list for DECL,
15770 so that future calls can reuse it rather than regenerate it from scratch.
15771 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15772 since we will need to refer to them each time the function is inlined. */
15773
15774 static bool
15775 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15776 enum dwarf_attribute attr)
15777 {
15778 rtx rtl;
15779 dw_loc_list_ref list;
15780 var_loc_list *loc_list;
15781 cached_dw_loc_list *cache;
15782 void **slot;
15783
15784 if (TREE_CODE (decl) == ERROR_MARK)
15785 return false;
15786
15787 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15788 || TREE_CODE (decl) == RESULT_DECL);
15789
15790 /* Try to get some constant RTL for this decl, and use that as the value of
15791 the location. */
15792
15793 rtl = rtl_for_decl_location (decl);
15794 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15795 && add_const_value_attribute (die, rtl))
15796 return true;
15797
15798 /* See if we have single element location list that is equivalent to
15799 a constant value. That way we are better to use add_const_value_attribute
15800 rather than expanding constant value equivalent. */
15801 loc_list = lookup_decl_loc (decl);
15802 if (loc_list
15803 && loc_list->first
15804 && loc_list->first->next == NULL
15805 && NOTE_P (loc_list->first->loc)
15806 && NOTE_VAR_LOCATION (loc_list->first->loc)
15807 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15808 {
15809 struct var_loc_node *node;
15810
15811 node = loc_list->first;
15812 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15813 if (GET_CODE (rtl) == EXPR_LIST)
15814 rtl = XEXP (rtl, 0);
15815 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15816 && add_const_value_attribute (die, rtl))
15817 return true;
15818 }
15819 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15820 list several times. See if we've already cached the contents. */
15821 list = NULL;
15822 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15823 cache_p = false;
15824 if (cache_p)
15825 {
15826 cache = (cached_dw_loc_list *)
15827 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15828 if (cache)
15829 list = cache->loc_list;
15830 }
15831 if (list == NULL)
15832 {
15833 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15834 /* It is usually worth caching this result if the decl is from
15835 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15836 if (cache_p && list && list->dw_loc_next)
15837 {
15838 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15839 DECL_UID (decl), INSERT);
15840 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15841 cache->decl_id = DECL_UID (decl);
15842 cache->loc_list = list;
15843 *slot = cache;
15844 }
15845 }
15846 if (list)
15847 {
15848 add_AT_location_description (die, attr, list);
15849 return true;
15850 }
15851 /* None of that worked, so it must not really have a location;
15852 try adding a constant value attribute from the DECL_INITIAL. */
15853 return tree_add_const_value_attribute_for_decl (die, decl);
15854 }
15855
15856 /* Add VARIABLE and DIE into deferred locations list. */
15857
15858 static void
15859 defer_location (tree variable, dw_die_ref die)
15860 {
15861 deferred_locations entry;
15862 entry.variable = variable;
15863 entry.die = die;
15864 vec_safe_push (deferred_locations_list, entry);
15865 }
15866
15867 /* Helper function for tree_add_const_value_attribute. Natively encode
15868 initializer INIT into an array. Return true if successful. */
15869
15870 static bool
15871 native_encode_initializer (tree init, unsigned char *array, int size)
15872 {
15873 tree type;
15874
15875 if (init == NULL_TREE)
15876 return false;
15877
15878 STRIP_NOPS (init);
15879 switch (TREE_CODE (init))
15880 {
15881 case STRING_CST:
15882 type = TREE_TYPE (init);
15883 if (TREE_CODE (type) == ARRAY_TYPE)
15884 {
15885 tree enttype = TREE_TYPE (type);
15886 enum machine_mode mode = TYPE_MODE (enttype);
15887
15888 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15889 return false;
15890 if (int_size_in_bytes (type) != size)
15891 return false;
15892 if (size > TREE_STRING_LENGTH (init))
15893 {
15894 memcpy (array, TREE_STRING_POINTER (init),
15895 TREE_STRING_LENGTH (init));
15896 memset (array + TREE_STRING_LENGTH (init),
15897 '\0', size - TREE_STRING_LENGTH (init));
15898 }
15899 else
15900 memcpy (array, TREE_STRING_POINTER (init), size);
15901 return true;
15902 }
15903 return false;
15904 case CONSTRUCTOR:
15905 type = TREE_TYPE (init);
15906 if (int_size_in_bytes (type) != size)
15907 return false;
15908 if (TREE_CODE (type) == ARRAY_TYPE)
15909 {
15910 HOST_WIDE_INT min_index;
15911 unsigned HOST_WIDE_INT cnt;
15912 int curpos = 0, fieldsize;
15913 constructor_elt *ce;
15914
15915 if (TYPE_DOMAIN (type) == NULL_TREE
15916 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15917 return false;
15918
15919 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15920 if (fieldsize <= 0)
15921 return false;
15922
15923 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15924 memset (array, '\0', size);
15925 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15926 {
15927 tree val = ce->value;
15928 tree index = ce->index;
15929 int pos = curpos;
15930 if (index && TREE_CODE (index) == RANGE_EXPR)
15931 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15932 * fieldsize;
15933 else if (index)
15934 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15935
15936 if (val)
15937 {
15938 STRIP_NOPS (val);
15939 if (!native_encode_initializer (val, array + pos, fieldsize))
15940 return false;
15941 }
15942 curpos = pos + fieldsize;
15943 if (index && TREE_CODE (index) == RANGE_EXPR)
15944 {
15945 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15946 - tree_to_shwi (TREE_OPERAND (index, 0));
15947 while (count-- > 0)
15948 {
15949 if (val)
15950 memcpy (array + curpos, array + pos, fieldsize);
15951 curpos += fieldsize;
15952 }
15953 }
15954 gcc_assert (curpos <= size);
15955 }
15956 return true;
15957 }
15958 else if (TREE_CODE (type) == RECORD_TYPE
15959 || TREE_CODE (type) == UNION_TYPE)
15960 {
15961 tree field = NULL_TREE;
15962 unsigned HOST_WIDE_INT cnt;
15963 constructor_elt *ce;
15964
15965 if (int_size_in_bytes (type) != size)
15966 return false;
15967
15968 if (TREE_CODE (type) == RECORD_TYPE)
15969 field = TYPE_FIELDS (type);
15970
15971 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15972 {
15973 tree val = ce->value;
15974 int pos, fieldsize;
15975
15976 if (ce->index != 0)
15977 field = ce->index;
15978
15979 if (val)
15980 STRIP_NOPS (val);
15981
15982 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15983 return false;
15984
15985 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15986 && TYPE_DOMAIN (TREE_TYPE (field))
15987 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15988 return false;
15989 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15990 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15991 return false;
15992 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15993 pos = int_byte_position (field);
15994 gcc_assert (pos + fieldsize <= size);
15995 if (val
15996 && !native_encode_initializer (val, array + pos, fieldsize))
15997 return false;
15998 }
15999 return true;
16000 }
16001 return false;
16002 case VIEW_CONVERT_EXPR:
16003 case NON_LVALUE_EXPR:
16004 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16005 default:
16006 return native_encode_expr (init, array, size) == size;
16007 }
16008 }
16009
16010 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16011 attribute is the const value T. */
16012
16013 static bool
16014 tree_add_const_value_attribute (dw_die_ref die, tree t)
16015 {
16016 tree init;
16017 tree type = TREE_TYPE (t);
16018 rtx rtl;
16019
16020 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16021 return false;
16022
16023 init = t;
16024 gcc_assert (!DECL_P (init));
16025
16026 rtl = rtl_for_decl_init (init, type);
16027 if (rtl)
16028 return add_const_value_attribute (die, rtl);
16029 /* If the host and target are sane, try harder. */
16030 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16031 && initializer_constant_valid_p (init, type))
16032 {
16033 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16034 if (size > 0 && (int) size == size)
16035 {
16036 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16037
16038 if (native_encode_initializer (init, array, size))
16039 {
16040 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16041 return true;
16042 }
16043 ggc_free (array);
16044 }
16045 }
16046 return false;
16047 }
16048
16049 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16050 attribute is the const value of T, where T is an integral constant
16051 variable with static storage duration
16052 (so it can't be a PARM_DECL or a RESULT_DECL). */
16053
16054 static bool
16055 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16056 {
16057
16058 if (!decl
16059 || (TREE_CODE (decl) != VAR_DECL
16060 && TREE_CODE (decl) != CONST_DECL)
16061 || (TREE_CODE (decl) == VAR_DECL
16062 && !TREE_STATIC (decl)))
16063 return false;
16064
16065 if (TREE_READONLY (decl)
16066 && ! TREE_THIS_VOLATILE (decl)
16067 && DECL_INITIAL (decl))
16068 /* OK */;
16069 else
16070 return false;
16071
16072 /* Don't add DW_AT_const_value if abstract origin already has one. */
16073 if (get_AT (var_die, DW_AT_const_value))
16074 return false;
16075
16076 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16077 }
16078
16079 /* Convert the CFI instructions for the current function into a
16080 location list. This is used for DW_AT_frame_base when we targeting
16081 a dwarf2 consumer that does not support the dwarf3
16082 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16083 expressions. */
16084
16085 static dw_loc_list_ref
16086 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16087 {
16088 int ix;
16089 dw_fde_ref fde;
16090 dw_loc_list_ref list, *list_tail;
16091 dw_cfi_ref cfi;
16092 dw_cfa_location last_cfa, next_cfa;
16093 const char *start_label, *last_label, *section;
16094 dw_cfa_location remember;
16095
16096 fde = cfun->fde;
16097 gcc_assert (fde != NULL);
16098
16099 section = secname_for_decl (current_function_decl);
16100 list_tail = &list;
16101 list = NULL;
16102
16103 memset (&next_cfa, 0, sizeof (next_cfa));
16104 next_cfa.reg = INVALID_REGNUM;
16105 remember = next_cfa;
16106
16107 start_label = fde->dw_fde_begin;
16108
16109 /* ??? Bald assumption that the CIE opcode list does not contain
16110 advance opcodes. */
16111 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16112 lookup_cfa_1 (cfi, &next_cfa, &remember);
16113
16114 last_cfa = next_cfa;
16115 last_label = start_label;
16116
16117 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16118 {
16119 /* If the first partition contained no CFI adjustments, the
16120 CIE opcodes apply to the whole first partition. */
16121 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16122 fde->dw_fde_begin, fde->dw_fde_end, section);
16123 list_tail =&(*list_tail)->dw_loc_next;
16124 start_label = last_label = fde->dw_fde_second_begin;
16125 }
16126
16127 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16128 {
16129 switch (cfi->dw_cfi_opc)
16130 {
16131 case DW_CFA_set_loc:
16132 case DW_CFA_advance_loc1:
16133 case DW_CFA_advance_loc2:
16134 case DW_CFA_advance_loc4:
16135 if (!cfa_equal_p (&last_cfa, &next_cfa))
16136 {
16137 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16138 start_label, last_label, section);
16139
16140 list_tail = &(*list_tail)->dw_loc_next;
16141 last_cfa = next_cfa;
16142 start_label = last_label;
16143 }
16144 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16145 break;
16146
16147 case DW_CFA_advance_loc:
16148 /* The encoding is complex enough that we should never emit this. */
16149 gcc_unreachable ();
16150
16151 default:
16152 lookup_cfa_1 (cfi, &next_cfa, &remember);
16153 break;
16154 }
16155 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16156 {
16157 if (!cfa_equal_p (&last_cfa, &next_cfa))
16158 {
16159 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16160 start_label, last_label, section);
16161
16162 list_tail = &(*list_tail)->dw_loc_next;
16163 last_cfa = next_cfa;
16164 start_label = last_label;
16165 }
16166 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16167 start_label, fde->dw_fde_end, section);
16168 list_tail = &(*list_tail)->dw_loc_next;
16169 start_label = last_label = fde->dw_fde_second_begin;
16170 }
16171 }
16172
16173 if (!cfa_equal_p (&last_cfa, &next_cfa))
16174 {
16175 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16176 start_label, last_label, section);
16177 list_tail = &(*list_tail)->dw_loc_next;
16178 start_label = last_label;
16179 }
16180
16181 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16182 start_label,
16183 fde->dw_fde_second_begin
16184 ? fde->dw_fde_second_end : fde->dw_fde_end,
16185 section);
16186
16187 if (list && list->dw_loc_next)
16188 gen_llsym (list);
16189
16190 return list;
16191 }
16192
16193 /* Compute a displacement from the "steady-state frame pointer" to the
16194 frame base (often the same as the CFA), and store it in
16195 frame_pointer_fb_offset. OFFSET is added to the displacement
16196 before the latter is negated. */
16197
16198 static void
16199 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16200 {
16201 rtx reg, elim;
16202
16203 #ifdef FRAME_POINTER_CFA_OFFSET
16204 reg = frame_pointer_rtx;
16205 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16206 #else
16207 reg = arg_pointer_rtx;
16208 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16209 #endif
16210
16211 elim = (ira_use_lra_p
16212 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16213 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16214 if (GET_CODE (elim) == PLUS)
16215 {
16216 offset += INTVAL (XEXP (elim, 1));
16217 elim = XEXP (elim, 0);
16218 }
16219
16220 frame_pointer_fb_offset = -offset;
16221
16222 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16223 in which to eliminate. This is because it's stack pointer isn't
16224 directly accessible as a register within the ISA. To work around
16225 this, assume that while we cannot provide a proper value for
16226 frame_pointer_fb_offset, we won't need one either. */
16227 frame_pointer_fb_offset_valid
16228 = ((SUPPORTS_STACK_ALIGNMENT
16229 && (elim == hard_frame_pointer_rtx
16230 || elim == stack_pointer_rtx))
16231 || elim == (frame_pointer_needed
16232 ? hard_frame_pointer_rtx
16233 : stack_pointer_rtx));
16234 }
16235
16236 /* Generate a DW_AT_name attribute given some string value to be included as
16237 the value of the attribute. */
16238
16239 static void
16240 add_name_attribute (dw_die_ref die, const char *name_string)
16241 {
16242 if (name_string != NULL && *name_string != 0)
16243 {
16244 if (demangle_name_func)
16245 name_string = (*demangle_name_func) (name_string);
16246
16247 add_AT_string (die, DW_AT_name, name_string);
16248 }
16249 }
16250
16251 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16252 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16253 of TYPE accordingly.
16254
16255 ??? This is a temporary measure until after we're able to generate
16256 regular DWARF for the complex Ada type system. */
16257
16258 static void
16259 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16260 dw_die_ref context_die)
16261 {
16262 tree dtype;
16263 dw_die_ref dtype_die;
16264
16265 if (!lang_hooks.types.descriptive_type)
16266 return;
16267
16268 dtype = lang_hooks.types.descriptive_type (type);
16269 if (!dtype)
16270 return;
16271
16272 dtype_die = lookup_type_die (dtype);
16273 if (!dtype_die)
16274 {
16275 gen_type_die (dtype, context_die);
16276 dtype_die = lookup_type_die (dtype);
16277 gcc_assert (dtype_die);
16278 }
16279
16280 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16281 }
16282
16283 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16284
16285 static const char *
16286 comp_dir_string (void)
16287 {
16288 const char *wd;
16289 char *wd1;
16290 static const char *cached_wd = NULL;
16291
16292 if (cached_wd != NULL)
16293 return cached_wd;
16294
16295 wd = get_src_pwd ();
16296 if (wd == NULL)
16297 return NULL;
16298
16299 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16300 {
16301 int wdlen;
16302
16303 wdlen = strlen (wd);
16304 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16305 strcpy (wd1, wd);
16306 wd1 [wdlen] = DIR_SEPARATOR;
16307 wd1 [wdlen + 1] = 0;
16308 wd = wd1;
16309 }
16310
16311 cached_wd = remap_debug_filename (wd);
16312 return cached_wd;
16313 }
16314
16315 /* Generate a DW_AT_comp_dir attribute for DIE. */
16316
16317 static void
16318 add_comp_dir_attribute (dw_die_ref die)
16319 {
16320 const char * wd = comp_dir_string ();
16321 if (wd != NULL)
16322 add_AT_string (die, DW_AT_comp_dir, wd);
16323 }
16324
16325 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16326 default. */
16327
16328 static int
16329 lower_bound_default (void)
16330 {
16331 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16332 {
16333 case DW_LANG_C:
16334 case DW_LANG_C89:
16335 case DW_LANG_C99:
16336 case DW_LANG_C_plus_plus:
16337 case DW_LANG_ObjC:
16338 case DW_LANG_ObjC_plus_plus:
16339 case DW_LANG_Java:
16340 return 0;
16341 case DW_LANG_Fortran77:
16342 case DW_LANG_Fortran90:
16343 case DW_LANG_Fortran95:
16344 return 1;
16345 case DW_LANG_UPC:
16346 case DW_LANG_D:
16347 case DW_LANG_Python:
16348 return dwarf_version >= 4 ? 0 : -1;
16349 case DW_LANG_Ada95:
16350 case DW_LANG_Ada83:
16351 case DW_LANG_Cobol74:
16352 case DW_LANG_Cobol85:
16353 case DW_LANG_Pascal83:
16354 case DW_LANG_Modula2:
16355 case DW_LANG_PLI:
16356 return dwarf_version >= 4 ? 1 : -1;
16357 default:
16358 return -1;
16359 }
16360 }
16361
16362 /* Given a tree node describing an array bound (either lower or upper) output
16363 a representation for that bound. */
16364
16365 static void
16366 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16367 {
16368 switch (TREE_CODE (bound))
16369 {
16370 case ERROR_MARK:
16371 return;
16372
16373 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16374 case INTEGER_CST:
16375 {
16376 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16377 int dflt;
16378
16379 /* Use the default if possible. */
16380 if (bound_attr == DW_AT_lower_bound
16381 && tree_fits_shwi_p (bound)
16382 && (dflt = lower_bound_default ()) != -1
16383 && tree_to_shwi (bound) == dflt)
16384 ;
16385
16386 /* If HOST_WIDE_INT is big enough then represent the bound as
16387 a constant value. We need to choose a form based on
16388 whether the type is signed or unsigned. We cannot just
16389 call add_AT_unsigned if the value itself is positive
16390 (add_AT_unsigned might add the unsigned value encoded as
16391 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16392 bounds type and then sign extend any unsigned values found
16393 for signed types. This is needed only for
16394 DW_AT_{lower,upper}_bound, since for most other attributes,
16395 consumers will treat DW_FORM_data[1248] as unsigned values,
16396 regardless of the underlying type. */
16397 else if (prec <= HOST_BITS_PER_WIDE_INT
16398 || tree_fits_uhwi_p (bound))
16399 {
16400 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16401 add_AT_unsigned (subrange_die, bound_attr,
16402 TREE_INT_CST_LOW (bound));
16403 else
16404 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16405 }
16406 else
16407 /* Otherwise represent the bound as an unsigned value with
16408 the precision of its type. The precision and signedness
16409 of the type will be necessary to re-interpret it
16410 unambiguously. */
16411 add_AT_wide (subrange_die, bound_attr, bound);
16412 }
16413 break;
16414
16415 CASE_CONVERT:
16416 case VIEW_CONVERT_EXPR:
16417 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16418 break;
16419
16420 case SAVE_EXPR:
16421 break;
16422
16423 case VAR_DECL:
16424 case PARM_DECL:
16425 case RESULT_DECL:
16426 {
16427 dw_die_ref decl_die = lookup_decl_die (bound);
16428
16429 /* ??? Can this happen, or should the variable have been bound
16430 first? Probably it can, since I imagine that we try to create
16431 the types of parameters in the order in which they exist in
16432 the list, and won't have created a forward reference to a
16433 later parameter. */
16434 if (decl_die != NULL)
16435 {
16436 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16437 break;
16438 }
16439 }
16440 /* FALLTHRU */
16441
16442 default:
16443 {
16444 /* Otherwise try to create a stack operation procedure to
16445 evaluate the value of the array bound. */
16446
16447 dw_die_ref ctx, decl_die;
16448 dw_loc_list_ref list;
16449
16450 list = loc_list_from_tree (bound, 2);
16451 if (list == NULL || single_element_loc_list_p (list))
16452 {
16453 /* If DW_AT_*bound is not a reference nor constant, it is
16454 a DWARF expression rather than location description.
16455 For that loc_list_from_tree (bound, 0) is needed.
16456 If that fails to give a single element list,
16457 fall back to outputting this as a reference anyway. */
16458 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16459 if (list2 && single_element_loc_list_p (list2))
16460 {
16461 add_AT_loc (subrange_die, bound_attr, list2->expr);
16462 break;
16463 }
16464 }
16465 if (list == NULL)
16466 break;
16467
16468 if (current_function_decl == 0)
16469 ctx = comp_unit_die ();
16470 else
16471 ctx = lookup_decl_die (current_function_decl);
16472
16473 decl_die = new_die (DW_TAG_variable, ctx, bound);
16474 add_AT_flag (decl_die, DW_AT_artificial, 1);
16475 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16476 add_AT_location_description (decl_die, DW_AT_location, list);
16477 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16478 break;
16479 }
16480 }
16481 }
16482
16483 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16484 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16485 Note that the block of subscript information for an array type also
16486 includes information about the element type of the given array type. */
16487
16488 static void
16489 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16490 {
16491 unsigned dimension_number;
16492 tree lower, upper;
16493 dw_die_ref subrange_die;
16494
16495 for (dimension_number = 0;
16496 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16497 type = TREE_TYPE (type), dimension_number++)
16498 {
16499 tree domain = TYPE_DOMAIN (type);
16500
16501 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16502 break;
16503
16504 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16505 and (in GNU C only) variable bounds. Handle all three forms
16506 here. */
16507 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16508 if (domain)
16509 {
16510 /* We have an array type with specified bounds. */
16511 lower = TYPE_MIN_VALUE (domain);
16512 upper = TYPE_MAX_VALUE (domain);
16513
16514 /* Define the index type. */
16515 if (TREE_TYPE (domain))
16516 {
16517 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16518 TREE_TYPE field. We can't emit debug info for this
16519 because it is an unnamed integral type. */
16520 if (TREE_CODE (domain) == INTEGER_TYPE
16521 && TYPE_NAME (domain) == NULL_TREE
16522 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16523 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16524 ;
16525 else
16526 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16527 type_die);
16528 }
16529
16530 /* ??? If upper is NULL, the array has unspecified length,
16531 but it does have a lower bound. This happens with Fortran
16532 dimension arr(N:*)
16533 Since the debugger is definitely going to need to know N
16534 to produce useful results, go ahead and output the lower
16535 bound solo, and hope the debugger can cope. */
16536
16537 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16538 if (upper)
16539 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16540 }
16541
16542 /* Otherwise we have an array type with an unspecified length. The
16543 DWARF-2 spec does not say how to handle this; let's just leave out the
16544 bounds. */
16545 }
16546 }
16547
16548 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16549
16550 static void
16551 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16552 {
16553 dw_die_ref decl_die;
16554 HOST_WIDE_INT size;
16555
16556 switch (TREE_CODE (tree_node))
16557 {
16558 case ERROR_MARK:
16559 size = 0;
16560 break;
16561 case ENUMERAL_TYPE:
16562 case RECORD_TYPE:
16563 case UNION_TYPE:
16564 case QUAL_UNION_TYPE:
16565 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16566 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16567 {
16568 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16569 return;
16570 }
16571 size = int_size_in_bytes (tree_node);
16572 break;
16573 case FIELD_DECL:
16574 /* For a data member of a struct or union, the DW_AT_byte_size is
16575 generally given as the number of bytes normally allocated for an
16576 object of the *declared* type of the member itself. This is true
16577 even for bit-fields. */
16578 size = int_size_in_bytes (field_type (tree_node));
16579 break;
16580 default:
16581 gcc_unreachable ();
16582 }
16583
16584 /* Note that `size' might be -1 when we get to this point. If it is, that
16585 indicates that the byte size of the entity in question is variable. We
16586 have no good way of expressing this fact in Dwarf at the present time,
16587 when location description was not used by the caller code instead. */
16588 if (size >= 0)
16589 add_AT_unsigned (die, DW_AT_byte_size, size);
16590 }
16591
16592 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16593 which specifies the distance in bits from the highest order bit of the
16594 "containing object" for the bit-field to the highest order bit of the
16595 bit-field itself.
16596
16597 For any given bit-field, the "containing object" is a hypothetical object
16598 (of some integral or enum type) within which the given bit-field lives. The
16599 type of this hypothetical "containing object" is always the same as the
16600 declared type of the individual bit-field itself. The determination of the
16601 exact location of the "containing object" for a bit-field is rather
16602 complicated. It's handled by the `field_byte_offset' function (above).
16603
16604 Note that it is the size (in bytes) of the hypothetical "containing object"
16605 which will be given in the DW_AT_byte_size attribute for this bit-field.
16606 (See `byte_size_attribute' above). */
16607
16608 static inline void
16609 add_bit_offset_attribute (dw_die_ref die, tree decl)
16610 {
16611 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16612 tree type = DECL_BIT_FIELD_TYPE (decl);
16613 HOST_WIDE_INT bitpos_int;
16614 HOST_WIDE_INT highest_order_object_bit_offset;
16615 HOST_WIDE_INT highest_order_field_bit_offset;
16616 HOST_WIDE_INT bit_offset;
16617
16618 /* Must be a field and a bit field. */
16619 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16620
16621 /* We can't yet handle bit-fields whose offsets are variable, so if we
16622 encounter such things, just return without generating any attribute
16623 whatsoever. Likewise for variable or too large size. */
16624 if (! tree_fits_shwi_p (bit_position (decl))
16625 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16626 return;
16627
16628 bitpos_int = int_bit_position (decl);
16629
16630 /* Note that the bit offset is always the distance (in bits) from the
16631 highest-order bit of the "containing object" to the highest-order bit of
16632 the bit-field itself. Since the "high-order end" of any object or field
16633 is different on big-endian and little-endian machines, the computation
16634 below must take account of these differences. */
16635 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16636 highest_order_field_bit_offset = bitpos_int;
16637
16638 if (! BYTES_BIG_ENDIAN)
16639 {
16640 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16641 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16642 }
16643
16644 bit_offset
16645 = (! BYTES_BIG_ENDIAN
16646 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16647 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16648
16649 if (bit_offset < 0)
16650 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16651 else
16652 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16653 }
16654
16655 /* For a FIELD_DECL node which represents a bit field, output an attribute
16656 which specifies the length in bits of the given field. */
16657
16658 static inline void
16659 add_bit_size_attribute (dw_die_ref die, tree decl)
16660 {
16661 /* Must be a field and a bit field. */
16662 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16663 && DECL_BIT_FIELD_TYPE (decl));
16664
16665 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16666 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16667 }
16668
16669 /* If the compiled language is ANSI C, then add a 'prototyped'
16670 attribute, if arg types are given for the parameters of a function. */
16671
16672 static inline void
16673 add_prototyped_attribute (dw_die_ref die, tree func_type)
16674 {
16675 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16676 && prototype_p (func_type))
16677 add_AT_flag (die, DW_AT_prototyped, 1);
16678 }
16679
16680 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16681 by looking in either the type declaration or object declaration
16682 equate table. */
16683
16684 static inline dw_die_ref
16685 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16686 {
16687 dw_die_ref origin_die = NULL;
16688
16689 if (TREE_CODE (origin) != FUNCTION_DECL)
16690 {
16691 /* We may have gotten separated from the block for the inlined
16692 function, if we're in an exception handler or some such; make
16693 sure that the abstract function has been written out.
16694
16695 Doing this for nested functions is wrong, however; functions are
16696 distinct units, and our context might not even be inline. */
16697 tree fn = origin;
16698
16699 if (TYPE_P (fn))
16700 fn = TYPE_STUB_DECL (fn);
16701
16702 fn = decl_function_context (fn);
16703 if (fn)
16704 dwarf2out_abstract_function (fn);
16705 }
16706
16707 if (DECL_P (origin))
16708 origin_die = lookup_decl_die (origin);
16709 else if (TYPE_P (origin))
16710 origin_die = lookup_type_die (origin);
16711
16712 /* XXX: Functions that are never lowered don't always have correct block
16713 trees (in the case of java, they simply have no block tree, in some other
16714 languages). For these functions, there is nothing we can really do to
16715 output correct debug info for inlined functions in all cases. Rather
16716 than die, we'll just produce deficient debug info now, in that we will
16717 have variables without a proper abstract origin. In the future, when all
16718 functions are lowered, we should re-add a gcc_assert (origin_die)
16719 here. */
16720
16721 if (origin_die)
16722 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16723 return origin_die;
16724 }
16725
16726 /* We do not currently support the pure_virtual attribute. */
16727
16728 static inline void
16729 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16730 {
16731 if (DECL_VINDEX (func_decl))
16732 {
16733 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16734
16735 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16736 add_AT_loc (die, DW_AT_vtable_elem_location,
16737 new_loc_descr (DW_OP_constu,
16738 tree_to_shwi (DECL_VINDEX (func_decl)),
16739 0));
16740
16741 /* GNU extension: Record what type this method came from originally. */
16742 if (debug_info_level > DINFO_LEVEL_TERSE
16743 && DECL_CONTEXT (func_decl))
16744 add_AT_die_ref (die, DW_AT_containing_type,
16745 lookup_type_die (DECL_CONTEXT (func_decl)));
16746 }
16747 }
16748 \f
16749 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16750 given decl. This used to be a vendor extension until after DWARF 4
16751 standardized it. */
16752
16753 static void
16754 add_linkage_attr (dw_die_ref die, tree decl)
16755 {
16756 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16757
16758 /* Mimic what assemble_name_raw does with a leading '*'. */
16759 if (name[0] == '*')
16760 name = &name[1];
16761
16762 if (dwarf_version >= 4)
16763 add_AT_string (die, DW_AT_linkage_name, name);
16764 else
16765 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16766 }
16767
16768 /* Add source coordinate attributes for the given decl. */
16769
16770 static void
16771 add_src_coords_attributes (dw_die_ref die, tree decl)
16772 {
16773 expanded_location s;
16774
16775 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16776 return;
16777 s = expand_location (DECL_SOURCE_LOCATION (decl));
16778 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16779 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16780 }
16781
16782 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16783
16784 static void
16785 add_linkage_name (dw_die_ref die, tree decl)
16786 {
16787 if (debug_info_level > DINFO_LEVEL_NONE
16788 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16789 && TREE_PUBLIC (decl)
16790 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16791 && die->die_tag != DW_TAG_member)
16792 {
16793 /* Defer until we have an assembler name set. */
16794 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16795 {
16796 limbo_die_node *asm_name;
16797
16798 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16799 asm_name->die = die;
16800 asm_name->created_for = decl;
16801 asm_name->next = deferred_asm_name;
16802 deferred_asm_name = asm_name;
16803 }
16804 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16805 add_linkage_attr (die, decl);
16806 }
16807 }
16808
16809 /* Add a DW_AT_name attribute and source coordinate attribute for the
16810 given decl, but only if it actually has a name. */
16811
16812 static void
16813 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16814 {
16815 tree decl_name;
16816
16817 decl_name = DECL_NAME (decl);
16818 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16819 {
16820 const char *name = dwarf2_name (decl, 0);
16821 if (name)
16822 add_name_attribute (die, name);
16823 if (! DECL_ARTIFICIAL (decl))
16824 add_src_coords_attributes (die, decl);
16825
16826 add_linkage_name (die, decl);
16827 }
16828
16829 #ifdef VMS_DEBUGGING_INFO
16830 /* Get the function's name, as described by its RTL. This may be different
16831 from the DECL_NAME name used in the source file. */
16832 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16833 {
16834 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16835 XEXP (DECL_RTL (decl), 0), false);
16836 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16837 }
16838 #endif /* VMS_DEBUGGING_INFO */
16839 }
16840
16841 #ifdef VMS_DEBUGGING_INFO
16842 /* Output the debug main pointer die for VMS */
16843
16844 void
16845 dwarf2out_vms_debug_main_pointer (void)
16846 {
16847 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16848 dw_die_ref die;
16849
16850 /* Allocate the VMS debug main subprogram die. */
16851 die = ggc_cleared_alloc<die_node> ();
16852 die->die_tag = DW_TAG_subprogram;
16853 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16854 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16855 current_function_funcdef_no);
16856 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16857
16858 /* Make it the first child of comp_unit_die (). */
16859 die->die_parent = comp_unit_die ();
16860 if (comp_unit_die ()->die_child)
16861 {
16862 die->die_sib = comp_unit_die ()->die_child->die_sib;
16863 comp_unit_die ()->die_child->die_sib = die;
16864 }
16865 else
16866 {
16867 die->die_sib = die;
16868 comp_unit_die ()->die_child = die;
16869 }
16870 }
16871 #endif /* VMS_DEBUGGING_INFO */
16872
16873 /* Push a new declaration scope. */
16874
16875 static void
16876 push_decl_scope (tree scope)
16877 {
16878 vec_safe_push (decl_scope_table, scope);
16879 }
16880
16881 /* Pop a declaration scope. */
16882
16883 static inline void
16884 pop_decl_scope (void)
16885 {
16886 decl_scope_table->pop ();
16887 }
16888
16889 /* walk_tree helper function for uses_local_type, below. */
16890
16891 static tree
16892 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16893 {
16894 if (!TYPE_P (*tp))
16895 *walk_subtrees = 0;
16896 else
16897 {
16898 tree name = TYPE_NAME (*tp);
16899 if (name && DECL_P (name) && decl_function_context (name))
16900 return *tp;
16901 }
16902 return NULL_TREE;
16903 }
16904
16905 /* If TYPE involves a function-local type (including a local typedef to a
16906 non-local type), returns that type; otherwise returns NULL_TREE. */
16907
16908 static tree
16909 uses_local_type (tree type)
16910 {
16911 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16912 return used;
16913 }
16914
16915 /* Return the DIE for the scope that immediately contains this type.
16916 Non-named types that do not involve a function-local type get global
16917 scope. Named types nested in namespaces or other types get their
16918 containing scope. All other types (i.e. function-local named types) get
16919 the current active scope. */
16920
16921 static dw_die_ref
16922 scope_die_for (tree t, dw_die_ref context_die)
16923 {
16924 dw_die_ref scope_die = NULL;
16925 tree containing_scope;
16926
16927 /* Non-types always go in the current scope. */
16928 gcc_assert (TYPE_P (t));
16929
16930 /* Use the scope of the typedef, rather than the scope of the type
16931 it refers to. */
16932 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16933 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16934 else
16935 containing_scope = TYPE_CONTEXT (t);
16936
16937 /* Use the containing namespace if there is one. */
16938 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16939 {
16940 if (context_die == lookup_decl_die (containing_scope))
16941 /* OK */;
16942 else if (debug_info_level > DINFO_LEVEL_TERSE)
16943 context_die = get_context_die (containing_scope);
16944 else
16945 containing_scope = NULL_TREE;
16946 }
16947
16948 /* Ignore function type "scopes" from the C frontend. They mean that
16949 a tagged type is local to a parmlist of a function declarator, but
16950 that isn't useful to DWARF. */
16951 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16952 containing_scope = NULL_TREE;
16953
16954 if (SCOPE_FILE_SCOPE_P (containing_scope))
16955 {
16956 /* If T uses a local type keep it local as well, to avoid references
16957 to function-local DIEs from outside the function. */
16958 if (current_function_decl && uses_local_type (t))
16959 scope_die = context_die;
16960 else
16961 scope_die = comp_unit_die ();
16962 }
16963 else if (TYPE_P (containing_scope))
16964 {
16965 /* For types, we can just look up the appropriate DIE. */
16966 if (debug_info_level > DINFO_LEVEL_TERSE)
16967 scope_die = get_context_die (containing_scope);
16968 else
16969 {
16970 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16971 if (scope_die == NULL)
16972 scope_die = comp_unit_die ();
16973 }
16974 }
16975 else
16976 scope_die = context_die;
16977
16978 return scope_die;
16979 }
16980
16981 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16982
16983 static inline int
16984 local_scope_p (dw_die_ref context_die)
16985 {
16986 for (; context_die; context_die = context_die->die_parent)
16987 if (context_die->die_tag == DW_TAG_inlined_subroutine
16988 || context_die->die_tag == DW_TAG_subprogram)
16989 return 1;
16990
16991 return 0;
16992 }
16993
16994 /* Returns nonzero if CONTEXT_DIE is a class. */
16995
16996 static inline int
16997 class_scope_p (dw_die_ref context_die)
16998 {
16999 return (context_die
17000 && (context_die->die_tag == DW_TAG_structure_type
17001 || context_die->die_tag == DW_TAG_class_type
17002 || context_die->die_tag == DW_TAG_interface_type
17003 || context_die->die_tag == DW_TAG_union_type));
17004 }
17005
17006 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17007 whether or not to treat a DIE in this context as a declaration. */
17008
17009 static inline int
17010 class_or_namespace_scope_p (dw_die_ref context_die)
17011 {
17012 return (class_scope_p (context_die)
17013 || (context_die && context_die->die_tag == DW_TAG_namespace));
17014 }
17015
17016 /* Many forms of DIEs require a "type description" attribute. This
17017 routine locates the proper "type descriptor" die for the type given
17018 by 'type', and adds a DW_AT_type attribute below the given die. */
17019
17020 static void
17021 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17022 int decl_volatile, dw_die_ref context_die)
17023 {
17024 enum tree_code code = TREE_CODE (type);
17025 dw_die_ref type_die = NULL;
17026
17027 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17028 or fixed-point type, use the inner type. This is because we have no
17029 support for unnamed types in base_type_die. This can happen if this is
17030 an Ada subrange type. Correct solution is emit a subrange type die. */
17031 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17032 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17033 type = TREE_TYPE (type), code = TREE_CODE (type);
17034
17035 if (code == ERROR_MARK
17036 /* Handle a special case. For functions whose return type is void, we
17037 generate *no* type attribute. (Note that no object may have type
17038 `void', so this only applies to function return types). */
17039 || code == VOID_TYPE)
17040 return;
17041
17042 type_die = modified_type_die (type,
17043 decl_const || TYPE_READONLY (type),
17044 decl_volatile || TYPE_VOLATILE (type),
17045 context_die);
17046
17047 if (type_die != NULL)
17048 add_AT_die_ref (object_die, DW_AT_type, type_die);
17049 }
17050
17051 /* Given an object die, add the calling convention attribute for the
17052 function call type. */
17053 static void
17054 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17055 {
17056 enum dwarf_calling_convention value = DW_CC_normal;
17057
17058 value = ((enum dwarf_calling_convention)
17059 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17060
17061 if (is_fortran ()
17062 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17063 {
17064 /* DWARF 2 doesn't provide a way to identify a program's source-level
17065 entry point. DW_AT_calling_convention attributes are only meant
17066 to describe functions' calling conventions. However, lacking a
17067 better way to signal the Fortran main program, we used this for
17068 a long time, following existing custom. Now, DWARF 4 has
17069 DW_AT_main_subprogram, which we add below, but some tools still
17070 rely on the old way, which we thus keep. */
17071 value = DW_CC_program;
17072
17073 if (dwarf_version >= 4 || !dwarf_strict)
17074 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17075 }
17076
17077 /* Only add the attribute if the backend requests it, and
17078 is not DW_CC_normal. */
17079 if (value && (value != DW_CC_normal))
17080 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17081 }
17082
17083 /* Given a tree pointer to a struct, class, union, or enum type node, return
17084 a pointer to the (string) tag name for the given type, or zero if the type
17085 was declared without a tag. */
17086
17087 static const char *
17088 type_tag (const_tree type)
17089 {
17090 const char *name = 0;
17091
17092 if (TYPE_NAME (type) != 0)
17093 {
17094 tree t = 0;
17095
17096 /* Find the IDENTIFIER_NODE for the type name. */
17097 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17098 && !TYPE_NAMELESS (type))
17099 t = TYPE_NAME (type);
17100
17101 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17102 a TYPE_DECL node, regardless of whether or not a `typedef' was
17103 involved. */
17104 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17105 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17106 {
17107 /* We want to be extra verbose. Don't call dwarf_name if
17108 DECL_NAME isn't set. The default hook for decl_printable_name
17109 doesn't like that, and in this context it's correct to return
17110 0, instead of "<anonymous>" or the like. */
17111 if (DECL_NAME (TYPE_NAME (type))
17112 && !DECL_NAMELESS (TYPE_NAME (type)))
17113 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17114 }
17115
17116 /* Now get the name as a string, or invent one. */
17117 if (!name && t != 0)
17118 name = IDENTIFIER_POINTER (t);
17119 }
17120
17121 return (name == 0 || *name == '\0') ? 0 : name;
17122 }
17123
17124 /* Return the type associated with a data member, make a special check
17125 for bit field types. */
17126
17127 static inline tree
17128 member_declared_type (const_tree member)
17129 {
17130 return (DECL_BIT_FIELD_TYPE (member)
17131 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17132 }
17133
17134 /* Get the decl's label, as described by its RTL. This may be different
17135 from the DECL_NAME name used in the source file. */
17136
17137 #if 0
17138 static const char *
17139 decl_start_label (tree decl)
17140 {
17141 rtx x;
17142 const char *fnname;
17143
17144 x = DECL_RTL (decl);
17145 gcc_assert (MEM_P (x));
17146
17147 x = XEXP (x, 0);
17148 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17149
17150 fnname = XSTR (x, 0);
17151 return fnname;
17152 }
17153 #endif
17154 \f
17155 /* These routines generate the internal representation of the DIE's for
17156 the compilation unit. Debugging information is collected by walking
17157 the declaration trees passed in from dwarf2out_decl(). */
17158
17159 static void
17160 gen_array_type_die (tree type, dw_die_ref context_die)
17161 {
17162 dw_die_ref scope_die = scope_die_for (type, context_die);
17163 dw_die_ref array_die;
17164
17165 /* GNU compilers represent multidimensional array types as sequences of one
17166 dimensional array types whose element types are themselves array types.
17167 We sometimes squish that down to a single array_type DIE with multiple
17168 subscripts in the Dwarf debugging info. The draft Dwarf specification
17169 say that we are allowed to do this kind of compression in C, because
17170 there is no difference between an array of arrays and a multidimensional
17171 array. We don't do this for Ada to remain as close as possible to the
17172 actual representation, which is especially important against the language
17173 flexibilty wrt arrays of variable size. */
17174
17175 bool collapse_nested_arrays = !is_ada ();
17176 tree element_type;
17177
17178 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17179 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17180 if (TYPE_STRING_FLAG (type)
17181 && TREE_CODE (type) == ARRAY_TYPE
17182 && is_fortran ()
17183 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17184 {
17185 HOST_WIDE_INT size;
17186
17187 array_die = new_die (DW_TAG_string_type, scope_die, type);
17188 add_name_attribute (array_die, type_tag (type));
17189 equate_type_number_to_die (type, array_die);
17190 size = int_size_in_bytes (type);
17191 if (size >= 0)
17192 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17193 else if (TYPE_DOMAIN (type) != NULL_TREE
17194 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17195 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17196 {
17197 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17198 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17199
17200 size = int_size_in_bytes (TREE_TYPE (szdecl));
17201 if (loc && size > 0)
17202 {
17203 add_AT_location_description (array_die, DW_AT_string_length, loc);
17204 if (size != DWARF2_ADDR_SIZE)
17205 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17206 }
17207 }
17208 return;
17209 }
17210
17211 array_die = new_die (DW_TAG_array_type, scope_die, type);
17212 add_name_attribute (array_die, type_tag (type));
17213 equate_type_number_to_die (type, array_die);
17214
17215 if (TREE_CODE (type) == VECTOR_TYPE)
17216 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17217
17218 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17219 if (is_fortran ()
17220 && TREE_CODE (type) == ARRAY_TYPE
17221 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17222 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17223 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17224
17225 #if 0
17226 /* We default the array ordering. SDB will probably do
17227 the right things even if DW_AT_ordering is not present. It's not even
17228 an issue until we start to get into multidimensional arrays anyway. If
17229 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17230 then we'll have to put the DW_AT_ordering attribute back in. (But if
17231 and when we find out that we need to put these in, we will only do so
17232 for multidimensional arrays. */
17233 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17234 #endif
17235
17236 if (TREE_CODE (type) == VECTOR_TYPE)
17237 {
17238 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17239 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17240 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17241 add_bound_info (subrange_die, DW_AT_upper_bound,
17242 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17243 }
17244 else
17245 add_subscript_info (array_die, type, collapse_nested_arrays);
17246
17247 /* Add representation of the type of the elements of this array type and
17248 emit the corresponding DIE if we haven't done it already. */
17249 element_type = TREE_TYPE (type);
17250 if (collapse_nested_arrays)
17251 while (TREE_CODE (element_type) == ARRAY_TYPE)
17252 {
17253 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17254 break;
17255 element_type = TREE_TYPE (element_type);
17256 }
17257
17258 add_type_attribute (array_die, element_type, 0, 0, context_die);
17259
17260 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17261 if (TYPE_ARTIFICIAL (type))
17262 add_AT_flag (array_die, DW_AT_artificial, 1);
17263
17264 if (get_AT (array_die, DW_AT_name))
17265 add_pubtype (type, array_die);
17266 }
17267
17268 static dw_loc_descr_ref
17269 descr_info_loc (tree val, tree base_decl)
17270 {
17271 HOST_WIDE_INT size;
17272 dw_loc_descr_ref loc, loc2;
17273 enum dwarf_location_atom op;
17274
17275 if (val == base_decl)
17276 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17277
17278 switch (TREE_CODE (val))
17279 {
17280 CASE_CONVERT:
17281 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17282 case VAR_DECL:
17283 return loc_descriptor_from_tree (val, 0);
17284 case INTEGER_CST:
17285 if (tree_fits_shwi_p (val))
17286 return int_loc_descriptor (tree_to_shwi (val));
17287 break;
17288 case INDIRECT_REF:
17289 size = int_size_in_bytes (TREE_TYPE (val));
17290 if (size < 0)
17291 break;
17292 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17293 if (!loc)
17294 break;
17295 if (size == DWARF2_ADDR_SIZE)
17296 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17297 else
17298 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17299 return loc;
17300 case POINTER_PLUS_EXPR:
17301 case PLUS_EXPR:
17302 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17303 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17304 {
17305 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17306 if (!loc)
17307 break;
17308 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17309 }
17310 else
17311 {
17312 op = DW_OP_plus;
17313 do_binop:
17314 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17315 if (!loc)
17316 break;
17317 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17318 if (!loc2)
17319 break;
17320 add_loc_descr (&loc, loc2);
17321 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17322 }
17323 return loc;
17324 case MINUS_EXPR:
17325 op = DW_OP_minus;
17326 goto do_binop;
17327 case MULT_EXPR:
17328 op = DW_OP_mul;
17329 goto do_binop;
17330 case EQ_EXPR:
17331 op = DW_OP_eq;
17332 goto do_binop;
17333 case NE_EXPR:
17334 op = DW_OP_ne;
17335 goto do_binop;
17336 default:
17337 break;
17338 }
17339 return NULL;
17340 }
17341
17342 static void
17343 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17344 tree val, tree base_decl)
17345 {
17346 dw_loc_descr_ref loc;
17347
17348 if (tree_fits_shwi_p (val))
17349 {
17350 add_AT_unsigned (die, attr, tree_to_shwi (val));
17351 return;
17352 }
17353
17354 loc = descr_info_loc (val, base_decl);
17355 if (!loc)
17356 return;
17357
17358 add_AT_loc (die, attr, loc);
17359 }
17360
17361 /* This routine generates DIE for array with hidden descriptor, details
17362 are filled into *info by a langhook. */
17363
17364 static void
17365 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17366 dw_die_ref context_die)
17367 {
17368 dw_die_ref scope_die = scope_die_for (type, context_die);
17369 dw_die_ref array_die;
17370 int dim;
17371
17372 array_die = new_die (DW_TAG_array_type, scope_die, type);
17373 add_name_attribute (array_die, type_tag (type));
17374 equate_type_number_to_die (type, array_die);
17375
17376 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17377 if (is_fortran ()
17378 && info->ndimensions >= 2)
17379 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17380
17381 if (info->data_location)
17382 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17383 info->base_decl);
17384 if (info->associated)
17385 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17386 info->base_decl);
17387 if (info->allocated)
17388 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17389 info->base_decl);
17390
17391 for (dim = 0; dim < info->ndimensions; dim++)
17392 {
17393 dw_die_ref subrange_die
17394 = new_die (DW_TAG_subrange_type, array_die, NULL);
17395
17396 if (info->dimen[dim].lower_bound)
17397 {
17398 /* If it is the default value, omit it. */
17399 int dflt;
17400
17401 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17402 && (dflt = lower_bound_default ()) != -1
17403 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17404 ;
17405 else
17406 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17407 info->dimen[dim].lower_bound,
17408 info->base_decl);
17409 }
17410 if (info->dimen[dim].upper_bound)
17411 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17412 info->dimen[dim].upper_bound,
17413 info->base_decl);
17414 if (info->dimen[dim].stride)
17415 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17416 info->dimen[dim].stride,
17417 info->base_decl);
17418 }
17419
17420 gen_type_die (info->element_type, context_die);
17421 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17422
17423 if (get_AT (array_die, DW_AT_name))
17424 add_pubtype (type, array_die);
17425 }
17426
17427 #if 0
17428 static void
17429 gen_entry_point_die (tree decl, dw_die_ref context_die)
17430 {
17431 tree origin = decl_ultimate_origin (decl);
17432 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17433
17434 if (origin != NULL)
17435 add_abstract_origin_attribute (decl_die, origin);
17436 else
17437 {
17438 add_name_and_src_coords_attributes (decl_die, decl);
17439 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17440 0, 0, context_die);
17441 }
17442
17443 if (DECL_ABSTRACT (decl))
17444 equate_decl_number_to_die (decl, decl_die);
17445 else
17446 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17447 }
17448 #endif
17449
17450 /* Walk through the list of incomplete types again, trying once more to
17451 emit full debugging info for them. */
17452
17453 static void
17454 retry_incomplete_types (void)
17455 {
17456 int i;
17457
17458 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17459 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17460 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17461 }
17462
17463 /* Determine what tag to use for a record type. */
17464
17465 static enum dwarf_tag
17466 record_type_tag (tree type)
17467 {
17468 if (! lang_hooks.types.classify_record)
17469 return DW_TAG_structure_type;
17470
17471 switch (lang_hooks.types.classify_record (type))
17472 {
17473 case RECORD_IS_STRUCT:
17474 return DW_TAG_structure_type;
17475
17476 case RECORD_IS_CLASS:
17477 return DW_TAG_class_type;
17478
17479 case RECORD_IS_INTERFACE:
17480 if (dwarf_version >= 3 || !dwarf_strict)
17481 return DW_TAG_interface_type;
17482 return DW_TAG_structure_type;
17483
17484 default:
17485 gcc_unreachable ();
17486 }
17487 }
17488
17489 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17490 include all of the information about the enumeration values also. Each
17491 enumerated type name/value is listed as a child of the enumerated type
17492 DIE. */
17493
17494 static dw_die_ref
17495 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17496 {
17497 dw_die_ref type_die = lookup_type_die (type);
17498
17499 if (type_die == NULL)
17500 {
17501 type_die = new_die (DW_TAG_enumeration_type,
17502 scope_die_for (type, context_die), type);
17503 equate_type_number_to_die (type, type_die);
17504 add_name_attribute (type_die, type_tag (type));
17505 if (dwarf_version >= 4 || !dwarf_strict)
17506 {
17507 if (ENUM_IS_SCOPED (type))
17508 add_AT_flag (type_die, DW_AT_enum_class, 1);
17509 if (ENUM_IS_OPAQUE (type))
17510 add_AT_flag (type_die, DW_AT_declaration, 1);
17511 }
17512 }
17513 else if (! TYPE_SIZE (type))
17514 return type_die;
17515 else
17516 remove_AT (type_die, DW_AT_declaration);
17517
17518 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17519 given enum type is incomplete, do not generate the DW_AT_byte_size
17520 attribute or the DW_AT_element_list attribute. */
17521 if (TYPE_SIZE (type))
17522 {
17523 tree link;
17524
17525 TREE_ASM_WRITTEN (type) = 1;
17526 add_byte_size_attribute (type_die, type);
17527 if (dwarf_version >= 3 || !dwarf_strict)
17528 {
17529 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17530 add_type_attribute (type_die, underlying, 0, 0, context_die);
17531 }
17532 if (TYPE_STUB_DECL (type) != NULL_TREE)
17533 {
17534 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17535 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17536 }
17537
17538 /* If the first reference to this type was as the return type of an
17539 inline function, then it may not have a parent. Fix this now. */
17540 if (type_die->die_parent == NULL)
17541 add_child_die (scope_die_for (type, context_die), type_die);
17542
17543 for (link = TYPE_VALUES (type);
17544 link != NULL; link = TREE_CHAIN (link))
17545 {
17546 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17547 tree value = TREE_VALUE (link);
17548
17549 add_name_attribute (enum_die,
17550 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17551
17552 if (TREE_CODE (value) == CONST_DECL)
17553 value = DECL_INITIAL (value);
17554
17555 if (simple_type_size_in_bits (TREE_TYPE (value))
17556 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17557 {
17558 /* For constant forms created by add_AT_unsigned DWARF
17559 consumers (GDB, elfutils, etc.) always zero extend
17560 the value. Only when the actual value is negative
17561 do we need to use add_AT_int to generate a constant
17562 form that can represent negative values. */
17563 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17564 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17565 add_AT_unsigned (enum_die, DW_AT_const_value,
17566 (unsigned HOST_WIDE_INT) val);
17567 else
17568 add_AT_int (enum_die, DW_AT_const_value, val);
17569 }
17570 else
17571 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17572 that here. TODO: This should be re-worked to use correct
17573 signed/unsigned double tags for all cases. */
17574 add_AT_wide (enum_die, DW_AT_const_value, value);
17575 }
17576
17577 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17578 if (TYPE_ARTIFICIAL (type))
17579 add_AT_flag (type_die, DW_AT_artificial, 1);
17580 }
17581 else
17582 add_AT_flag (type_die, DW_AT_declaration, 1);
17583
17584 add_pubtype (type, type_die);
17585
17586 return type_die;
17587 }
17588
17589 /* Generate a DIE to represent either a real live formal parameter decl or to
17590 represent just the type of some formal parameter position in some function
17591 type.
17592
17593 Note that this routine is a bit unusual because its argument may be a
17594 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17595 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17596 node. If it's the former then this function is being called to output a
17597 DIE to represent a formal parameter object (or some inlining thereof). If
17598 it's the latter, then this function is only being called to output a
17599 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17600 argument type of some subprogram type.
17601 If EMIT_NAME_P is true, name and source coordinate attributes
17602 are emitted. */
17603
17604 static dw_die_ref
17605 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17606 dw_die_ref context_die)
17607 {
17608 tree node_or_origin = node ? node : origin;
17609 tree ultimate_origin;
17610 dw_die_ref parm_die
17611 = new_die (DW_TAG_formal_parameter, context_die, node);
17612
17613 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17614 {
17615 case tcc_declaration:
17616 ultimate_origin = decl_ultimate_origin (node_or_origin);
17617 if (node || ultimate_origin)
17618 origin = ultimate_origin;
17619 if (origin != NULL)
17620 add_abstract_origin_attribute (parm_die, origin);
17621 else if (emit_name_p)
17622 add_name_and_src_coords_attributes (parm_die, node);
17623 if (origin == NULL
17624 || (! DECL_ABSTRACT (node_or_origin)
17625 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17626 decl_function_context
17627 (node_or_origin))))
17628 {
17629 tree type = TREE_TYPE (node_or_origin);
17630 if (decl_by_reference_p (node_or_origin))
17631 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17632 context_die);
17633 else
17634 add_type_attribute (parm_die, type,
17635 TREE_READONLY (node_or_origin),
17636 TREE_THIS_VOLATILE (node_or_origin),
17637 context_die);
17638 }
17639 if (origin == NULL && DECL_ARTIFICIAL (node))
17640 add_AT_flag (parm_die, DW_AT_artificial, 1);
17641
17642 if (node && node != origin)
17643 equate_decl_number_to_die (node, parm_die);
17644 if (! DECL_ABSTRACT (node_or_origin))
17645 add_location_or_const_value_attribute (parm_die, node_or_origin,
17646 node == NULL, DW_AT_location);
17647
17648 break;
17649
17650 case tcc_type:
17651 /* We were called with some kind of a ..._TYPE node. */
17652 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17653 break;
17654
17655 default:
17656 gcc_unreachable ();
17657 }
17658
17659 return parm_die;
17660 }
17661
17662 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17663 children DW_TAG_formal_parameter DIEs representing the arguments of the
17664 parameter pack.
17665
17666 PARM_PACK must be a function parameter pack.
17667 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17668 must point to the subsequent arguments of the function PACK_ARG belongs to.
17669 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17670 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17671 following the last one for which a DIE was generated. */
17672
17673 static dw_die_ref
17674 gen_formal_parameter_pack_die (tree parm_pack,
17675 tree pack_arg,
17676 dw_die_ref subr_die,
17677 tree *next_arg)
17678 {
17679 tree arg;
17680 dw_die_ref parm_pack_die;
17681
17682 gcc_assert (parm_pack
17683 && lang_hooks.function_parameter_pack_p (parm_pack)
17684 && subr_die);
17685
17686 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17687 add_src_coords_attributes (parm_pack_die, parm_pack);
17688
17689 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17690 {
17691 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17692 parm_pack))
17693 break;
17694 gen_formal_parameter_die (arg, NULL,
17695 false /* Don't emit name attribute. */,
17696 parm_pack_die);
17697 }
17698 if (next_arg)
17699 *next_arg = arg;
17700 return parm_pack_die;
17701 }
17702
17703 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17704 at the end of an (ANSI prototyped) formal parameters list. */
17705
17706 static void
17707 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17708 {
17709 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17710 }
17711
17712 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17713 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17714 parameters as specified in some function type specification (except for
17715 those which appear as part of a function *definition*). */
17716
17717 static void
17718 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17719 {
17720 tree link;
17721 tree formal_type = NULL;
17722 tree first_parm_type;
17723 tree arg;
17724
17725 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17726 {
17727 arg = DECL_ARGUMENTS (function_or_method_type);
17728 function_or_method_type = TREE_TYPE (function_or_method_type);
17729 }
17730 else
17731 arg = NULL_TREE;
17732
17733 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17734
17735 /* Make our first pass over the list of formal parameter types and output a
17736 DW_TAG_formal_parameter DIE for each one. */
17737 for (link = first_parm_type; link; )
17738 {
17739 dw_die_ref parm_die;
17740
17741 formal_type = TREE_VALUE (link);
17742 if (formal_type == void_type_node)
17743 break;
17744
17745 /* Output a (nameless) DIE to represent the formal parameter itself. */
17746 parm_die = gen_formal_parameter_die (formal_type, NULL,
17747 true /* Emit name attribute. */,
17748 context_die);
17749 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17750 && link == first_parm_type)
17751 {
17752 add_AT_flag (parm_die, DW_AT_artificial, 1);
17753 if (dwarf_version >= 3 || !dwarf_strict)
17754 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17755 }
17756 else if (arg && DECL_ARTIFICIAL (arg))
17757 add_AT_flag (parm_die, DW_AT_artificial, 1);
17758
17759 link = TREE_CHAIN (link);
17760 if (arg)
17761 arg = DECL_CHAIN (arg);
17762 }
17763
17764 /* If this function type has an ellipsis, add a
17765 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17766 if (formal_type != void_type_node)
17767 gen_unspecified_parameters_die (function_or_method_type, context_die);
17768
17769 /* Make our second (and final) pass over the list of formal parameter types
17770 and output DIEs to represent those types (as necessary). */
17771 for (link = TYPE_ARG_TYPES (function_or_method_type);
17772 link && TREE_VALUE (link);
17773 link = TREE_CHAIN (link))
17774 gen_type_die (TREE_VALUE (link), context_die);
17775 }
17776
17777 /* We want to generate the DIE for TYPE so that we can generate the
17778 die for MEMBER, which has been defined; we will need to refer back
17779 to the member declaration nested within TYPE. If we're trying to
17780 generate minimal debug info for TYPE, processing TYPE won't do the
17781 trick; we need to attach the member declaration by hand. */
17782
17783 static void
17784 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17785 {
17786 gen_type_die (type, context_die);
17787
17788 /* If we're trying to avoid duplicate debug info, we may not have
17789 emitted the member decl for this function. Emit it now. */
17790 if (TYPE_STUB_DECL (type)
17791 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17792 && ! lookup_decl_die (member))
17793 {
17794 dw_die_ref type_die;
17795 gcc_assert (!decl_ultimate_origin (member));
17796
17797 push_decl_scope (type);
17798 type_die = lookup_type_die_strip_naming_typedef (type);
17799 if (TREE_CODE (member) == FUNCTION_DECL)
17800 gen_subprogram_die (member, type_die);
17801 else if (TREE_CODE (member) == FIELD_DECL)
17802 {
17803 /* Ignore the nameless fields that are used to skip bits but handle
17804 C++ anonymous unions and structs. */
17805 if (DECL_NAME (member) != NULL_TREE
17806 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17807 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17808 {
17809 gen_type_die (member_declared_type (member), type_die);
17810 gen_field_die (member, type_die);
17811 }
17812 }
17813 else
17814 gen_variable_die (member, NULL_TREE, type_die);
17815
17816 pop_decl_scope ();
17817 }
17818 }
17819 \f
17820 /* Forward declare these functions, because they are mutually recursive
17821 with their set_block_* pairing functions. */
17822 static void set_decl_origin_self (tree);
17823 static void set_decl_abstract_flags (tree, int);
17824
17825 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17826 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17827 that it points to the node itself, thus indicating that the node is its
17828 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17829 the given node is NULL, recursively descend the decl/block tree which
17830 it is the root of, and for each other ..._DECL or BLOCK node contained
17831 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17832 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17833 values to point to themselves. */
17834
17835 static void
17836 set_block_origin_self (tree stmt)
17837 {
17838 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17839 {
17840 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17841
17842 {
17843 tree local_decl;
17844
17845 for (local_decl = BLOCK_VARS (stmt);
17846 local_decl != NULL_TREE;
17847 local_decl = DECL_CHAIN (local_decl))
17848 if (! DECL_EXTERNAL (local_decl))
17849 set_decl_origin_self (local_decl); /* Potential recursion. */
17850 }
17851
17852 {
17853 tree subblock;
17854
17855 for (subblock = BLOCK_SUBBLOCKS (stmt);
17856 subblock != NULL_TREE;
17857 subblock = BLOCK_CHAIN (subblock))
17858 set_block_origin_self (subblock); /* Recurse. */
17859 }
17860 }
17861 }
17862
17863 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17864 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17865 node to so that it points to the node itself, thus indicating that the
17866 node represents its own (abstract) origin. Additionally, if the
17867 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17868 the decl/block tree of which the given node is the root of, and for
17869 each other ..._DECL or BLOCK node contained therein whose
17870 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17871 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17872 point to themselves. */
17873
17874 static void
17875 set_decl_origin_self (tree decl)
17876 {
17877 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17878 {
17879 DECL_ABSTRACT_ORIGIN (decl) = decl;
17880 if (TREE_CODE (decl) == FUNCTION_DECL)
17881 {
17882 tree arg;
17883
17884 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17885 DECL_ABSTRACT_ORIGIN (arg) = arg;
17886 if (DECL_INITIAL (decl) != NULL_TREE
17887 && DECL_INITIAL (decl) != error_mark_node)
17888 set_block_origin_self (DECL_INITIAL (decl));
17889 }
17890 }
17891 }
17892 \f
17893 /* Given a pointer to some BLOCK node, and a boolean value to set the
17894 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17895 the given block, and for all local decls and all local sub-blocks
17896 (recursively) which are contained therein. */
17897
17898 static void
17899 set_block_abstract_flags (tree stmt, int setting)
17900 {
17901 tree local_decl;
17902 tree subblock;
17903 unsigned int i;
17904
17905 BLOCK_ABSTRACT (stmt) = setting;
17906
17907 for (local_decl = BLOCK_VARS (stmt);
17908 local_decl != NULL_TREE;
17909 local_decl = DECL_CHAIN (local_decl))
17910 if (! DECL_EXTERNAL (local_decl))
17911 set_decl_abstract_flags (local_decl, setting);
17912
17913 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17914 {
17915 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17916 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17917 || TREE_CODE (local_decl) == PARM_DECL)
17918 set_decl_abstract_flags (local_decl, setting);
17919 }
17920
17921 for (subblock = BLOCK_SUBBLOCKS (stmt);
17922 subblock != NULL_TREE;
17923 subblock = BLOCK_CHAIN (subblock))
17924 set_block_abstract_flags (subblock, setting);
17925 }
17926
17927 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17928 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17929 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17930 set the abstract flags for all of the parameters, local vars, local
17931 blocks and sub-blocks (recursively) to the same setting. */
17932
17933 static void
17934 set_decl_abstract_flags (tree decl, int setting)
17935 {
17936 DECL_ABSTRACT (decl) = setting;
17937 if (TREE_CODE (decl) == FUNCTION_DECL)
17938 {
17939 tree arg;
17940
17941 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17942 DECL_ABSTRACT (arg) = setting;
17943 if (DECL_INITIAL (decl) != NULL_TREE
17944 && DECL_INITIAL (decl) != error_mark_node)
17945 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17946 }
17947 }
17948
17949 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17950 may later generate inlined and/or out-of-line instances of. */
17951
17952 static void
17953 dwarf2out_abstract_function (tree decl)
17954 {
17955 dw_die_ref old_die;
17956 tree save_fn;
17957 tree context;
17958 int was_abstract;
17959 htab_t old_decl_loc_table;
17960 htab_t old_cached_dw_loc_list_table;
17961 int old_call_site_count, old_tail_call_site_count;
17962 struct call_arg_loc_node *old_call_arg_locations;
17963
17964 /* Make sure we have the actual abstract inline, not a clone. */
17965 decl = DECL_ORIGIN (decl);
17966
17967 old_die = lookup_decl_die (decl);
17968 if (old_die && get_AT (old_die, DW_AT_inline))
17969 /* We've already generated the abstract instance. */
17970 return;
17971
17972 /* We can be called while recursively when seeing block defining inlined subroutine
17973 DIE. Be sure to not clobber the outer location table nor use it or we would
17974 get locations in abstract instantces. */
17975 old_decl_loc_table = decl_loc_table;
17976 decl_loc_table = NULL;
17977 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17978 cached_dw_loc_list_table = NULL;
17979 old_call_arg_locations = call_arg_locations;
17980 call_arg_locations = NULL;
17981 old_call_site_count = call_site_count;
17982 call_site_count = -1;
17983 old_tail_call_site_count = tail_call_site_count;
17984 tail_call_site_count = -1;
17985
17986 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17987 we don't get confused by DECL_ABSTRACT. */
17988 if (debug_info_level > DINFO_LEVEL_TERSE)
17989 {
17990 context = decl_class_context (decl);
17991 if (context)
17992 gen_type_die_for_member
17993 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17994 }
17995
17996 /* Pretend we've just finished compiling this function. */
17997 save_fn = current_function_decl;
17998 current_function_decl = decl;
17999
18000 was_abstract = DECL_ABSTRACT (decl);
18001 set_decl_abstract_flags (decl, 1);
18002 dwarf2out_decl (decl);
18003 if (! was_abstract)
18004 set_decl_abstract_flags (decl, 0);
18005
18006 current_function_decl = save_fn;
18007 decl_loc_table = old_decl_loc_table;
18008 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18009 call_arg_locations = old_call_arg_locations;
18010 call_site_count = old_call_site_count;
18011 tail_call_site_count = old_tail_call_site_count;
18012 }
18013
18014 /* Helper function of premark_used_types() which gets called through
18015 htab_traverse.
18016
18017 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18018 marked as unused by prune_unused_types. */
18019
18020 static int
18021 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18022 {
18023 tree type;
18024 dw_die_ref die;
18025
18026 type = (tree) *slot;
18027 die = lookup_type_die (type);
18028 if (die != NULL)
18029 die->die_perennial_p = 1;
18030 return 1;
18031 }
18032
18033 /* Helper function of premark_types_used_by_global_vars which gets called
18034 through htab_traverse.
18035
18036 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18037 marked as unused by prune_unused_types. The DIE of the type is marked
18038 only if the global variable using the type will actually be emitted. */
18039
18040 static int
18041 premark_types_used_by_global_vars_helper (void **slot,
18042 void *data ATTRIBUTE_UNUSED)
18043 {
18044 struct types_used_by_vars_entry *entry;
18045 dw_die_ref die;
18046
18047 entry = (struct types_used_by_vars_entry *) *slot;
18048 gcc_assert (entry->type != NULL
18049 && entry->var_decl != NULL);
18050 die = lookup_type_die (entry->type);
18051 if (die)
18052 {
18053 /* Ask cgraph if the global variable really is to be emitted.
18054 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18055 varpool_node *node = varpool_get_node (entry->var_decl);
18056 if (node && node->definition)
18057 {
18058 die->die_perennial_p = 1;
18059 /* Keep the parent DIEs as well. */
18060 while ((die = die->die_parent) && die->die_perennial_p == 0)
18061 die->die_perennial_p = 1;
18062 }
18063 }
18064 return 1;
18065 }
18066
18067 /* Mark all members of used_types_hash as perennial. */
18068
18069 static void
18070 premark_used_types (struct function *fun)
18071 {
18072 if (fun && fun->used_types_hash)
18073 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18074 }
18075
18076 /* Mark all members of types_used_by_vars_entry as perennial. */
18077
18078 static void
18079 premark_types_used_by_global_vars (void)
18080 {
18081 if (types_used_by_vars_hash)
18082 htab_traverse (types_used_by_vars_hash,
18083 premark_types_used_by_global_vars_helper, NULL);
18084 }
18085
18086 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18087 for CA_LOC call arg loc node. */
18088
18089 static dw_die_ref
18090 gen_call_site_die (tree decl, dw_die_ref subr_die,
18091 struct call_arg_loc_node *ca_loc)
18092 {
18093 dw_die_ref stmt_die = NULL, die;
18094 tree block = ca_loc->block;
18095
18096 while (block
18097 && block != DECL_INITIAL (decl)
18098 && TREE_CODE (block) == BLOCK)
18099 {
18100 if (block_map.length () > BLOCK_NUMBER (block))
18101 stmt_die = block_map[BLOCK_NUMBER (block)];
18102 if (stmt_die)
18103 break;
18104 block = BLOCK_SUPERCONTEXT (block);
18105 }
18106 if (stmt_die == NULL)
18107 stmt_die = subr_die;
18108 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18109 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18110 if (ca_loc->tail_call_p)
18111 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18112 if (ca_loc->symbol_ref)
18113 {
18114 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18115 if (tdie)
18116 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18117 else
18118 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18119 }
18120 return die;
18121 }
18122
18123 /* Generate a DIE to represent a declared function (either file-scope or
18124 block-local). */
18125
18126 static void
18127 gen_subprogram_die (tree decl, dw_die_ref context_die)
18128 {
18129 tree origin = decl_ultimate_origin (decl);
18130 dw_die_ref subr_die;
18131 tree outer_scope;
18132 dw_die_ref old_die = lookup_decl_die (decl);
18133 int declaration = (current_function_decl != decl
18134 || class_or_namespace_scope_p (context_die));
18135
18136 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18137
18138 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18139 started to generate the abstract instance of an inline, decided to output
18140 its containing class, and proceeded to emit the declaration of the inline
18141 from the member list for the class. If so, DECLARATION takes priority;
18142 we'll get back to the abstract instance when done with the class. */
18143
18144 /* The class-scope declaration DIE must be the primary DIE. */
18145 if (origin && declaration && class_or_namespace_scope_p (context_die))
18146 {
18147 origin = NULL;
18148 gcc_assert (!old_die);
18149 }
18150
18151 /* Now that the C++ front end lazily declares artificial member fns, we
18152 might need to retrofit the declaration into its class. */
18153 if (!declaration && !origin && !old_die
18154 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18155 && !class_or_namespace_scope_p (context_die)
18156 && debug_info_level > DINFO_LEVEL_TERSE)
18157 old_die = force_decl_die (decl);
18158
18159 if (origin != NULL)
18160 {
18161 gcc_assert (!declaration || local_scope_p (context_die));
18162
18163 /* Fixup die_parent for the abstract instance of a nested
18164 inline function. */
18165 if (old_die && old_die->die_parent == NULL)
18166 add_child_die (context_die, old_die);
18167
18168 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18169 add_abstract_origin_attribute (subr_die, origin);
18170 /* This is where the actual code for a cloned function is.
18171 Let's emit linkage name attribute for it. This helps
18172 debuggers to e.g, set breakpoints into
18173 constructors/destructors when the user asks "break
18174 K::K". */
18175 add_linkage_name (subr_die, decl);
18176 }
18177 else if (old_die)
18178 {
18179 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18180 struct dwarf_file_data * file_index = lookup_filename (s.file);
18181
18182 if (!get_AT_flag (old_die, DW_AT_declaration)
18183 /* We can have a normal definition following an inline one in the
18184 case of redefinition of GNU C extern inlines.
18185 It seems reasonable to use AT_specification in this case. */
18186 && !get_AT (old_die, DW_AT_inline))
18187 {
18188 /* Detect and ignore this case, where we are trying to output
18189 something we have already output. */
18190 return;
18191 }
18192
18193 /* If the definition comes from the same place as the declaration,
18194 maybe use the old DIE. We always want the DIE for this function
18195 that has the *_pc attributes to be under comp_unit_die so the
18196 debugger can find it. We also need to do this for abstract
18197 instances of inlines, since the spec requires the out-of-line copy
18198 to have the same parent. For local class methods, this doesn't
18199 apply; we just use the old DIE. */
18200 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18201 && (DECL_ARTIFICIAL (decl)
18202 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18203 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18204 == (unsigned) s.line))))
18205 {
18206 subr_die = old_die;
18207
18208 /* Clear out the declaration attribute and the formal parameters.
18209 Do not remove all children, because it is possible that this
18210 declaration die was forced using force_decl_die(). In such
18211 cases die that forced declaration die (e.g. TAG_imported_module)
18212 is one of the children that we do not want to remove. */
18213 remove_AT (subr_die, DW_AT_declaration);
18214 remove_AT (subr_die, DW_AT_object_pointer);
18215 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18216 }
18217 else
18218 {
18219 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18220 add_AT_specification (subr_die, old_die);
18221 add_pubname (decl, subr_die);
18222 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18223 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18224 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18225 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18226
18227 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18228 emit the real type on the definition die. */
18229 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18230 {
18231 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18232 if (die == auto_die || die == decltype_auto_die)
18233 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18234 0, 0, context_die);
18235 }
18236 }
18237 }
18238 else
18239 {
18240 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18241
18242 if (TREE_PUBLIC (decl))
18243 add_AT_flag (subr_die, DW_AT_external, 1);
18244
18245 add_name_and_src_coords_attributes (subr_die, decl);
18246 add_pubname (decl, subr_die);
18247 if (debug_info_level > DINFO_LEVEL_TERSE)
18248 {
18249 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18250 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18251 0, 0, context_die);
18252 }
18253
18254 add_pure_or_virtual_attribute (subr_die, decl);
18255 if (DECL_ARTIFICIAL (decl))
18256 add_AT_flag (subr_die, DW_AT_artificial, 1);
18257
18258 add_accessibility_attribute (subr_die, decl);
18259 }
18260
18261 if (declaration)
18262 {
18263 if (!old_die || !get_AT (old_die, DW_AT_inline))
18264 {
18265 add_AT_flag (subr_die, DW_AT_declaration, 1);
18266
18267 /* If this is an explicit function declaration then generate
18268 a DW_AT_explicit attribute. */
18269 if (lang_hooks.decls.function_decl_explicit_p (decl)
18270 && (dwarf_version >= 3 || !dwarf_strict))
18271 add_AT_flag (subr_die, DW_AT_explicit, 1);
18272
18273 /* The first time we see a member function, it is in the context of
18274 the class to which it belongs. We make sure of this by emitting
18275 the class first. The next time is the definition, which is
18276 handled above. The two may come from the same source text.
18277
18278 Note that force_decl_die() forces function declaration die. It is
18279 later reused to represent definition. */
18280 equate_decl_number_to_die (decl, subr_die);
18281 }
18282 }
18283 else if (DECL_ABSTRACT (decl))
18284 {
18285 if (DECL_DECLARED_INLINE_P (decl))
18286 {
18287 if (cgraph_function_possibly_inlined_p (decl))
18288 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18289 else
18290 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18291 }
18292 else
18293 {
18294 if (cgraph_function_possibly_inlined_p (decl))
18295 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18296 else
18297 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18298 }
18299
18300 if (DECL_DECLARED_INLINE_P (decl)
18301 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18302 add_AT_flag (subr_die, DW_AT_artificial, 1);
18303
18304 equate_decl_number_to_die (decl, subr_die);
18305 }
18306 else if (!DECL_EXTERNAL (decl))
18307 {
18308 HOST_WIDE_INT cfa_fb_offset;
18309 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18310
18311 if (!old_die || !get_AT (old_die, DW_AT_inline))
18312 equate_decl_number_to_die (decl, subr_die);
18313
18314 gcc_checking_assert (fun);
18315 if (!flag_reorder_blocks_and_partition)
18316 {
18317 dw_fde_ref fde = fun->fde;
18318 if (fde->dw_fde_begin)
18319 {
18320 /* We have already generated the labels. */
18321 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18322 fde->dw_fde_end, false);
18323 }
18324 else
18325 {
18326 /* Create start/end labels and add the range. */
18327 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18328 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18329 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18330 current_function_funcdef_no);
18331 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18332 current_function_funcdef_no);
18333 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18334 false);
18335 }
18336
18337 #if VMS_DEBUGGING_INFO
18338 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18339 Section 2.3 Prologue and Epilogue Attributes:
18340 When a breakpoint is set on entry to a function, it is generally
18341 desirable for execution to be suspended, not on the very first
18342 instruction of the function, but rather at a point after the
18343 function's frame has been set up, after any language defined local
18344 declaration processing has been completed, and before execution of
18345 the first statement of the function begins. Debuggers generally
18346 cannot properly determine where this point is. Similarly for a
18347 breakpoint set on exit from a function. The prologue and epilogue
18348 attributes allow a compiler to communicate the location(s) to use. */
18349
18350 {
18351 if (fde->dw_fde_vms_end_prologue)
18352 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18353 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18354
18355 if (fde->dw_fde_vms_begin_epilogue)
18356 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18357 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18358 }
18359 #endif
18360
18361 }
18362 else
18363 {
18364 /* Generate pubnames entries for the split function code ranges. */
18365 dw_fde_ref fde = fun->fde;
18366
18367 if (fde->dw_fde_second_begin)
18368 {
18369 if (dwarf_version >= 3 || !dwarf_strict)
18370 {
18371 /* We should use ranges for non-contiguous code section
18372 addresses. Use the actual code range for the initial
18373 section, since the HOT/COLD labels might precede an
18374 alignment offset. */
18375 bool range_list_added = false;
18376 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18377 fde->dw_fde_end, &range_list_added,
18378 false);
18379 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18380 fde->dw_fde_second_end,
18381 &range_list_added, false);
18382 if (range_list_added)
18383 add_ranges (NULL);
18384 }
18385 else
18386 {
18387 /* There is no real support in DW2 for this .. so we make
18388 a work-around. First, emit the pub name for the segment
18389 containing the function label. Then make and emit a
18390 simplified subprogram DIE for the second segment with the
18391 name pre-fixed by __hot/cold_sect_of_. We use the same
18392 linkage name for the second die so that gdb will find both
18393 sections when given "b foo". */
18394 const char *name = NULL;
18395 tree decl_name = DECL_NAME (decl);
18396 dw_die_ref seg_die;
18397
18398 /* Do the 'primary' section. */
18399 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18400 fde->dw_fde_end, false);
18401
18402 /* Build a minimal DIE for the secondary section. */
18403 seg_die = new_die (DW_TAG_subprogram,
18404 subr_die->die_parent, decl);
18405
18406 if (TREE_PUBLIC (decl))
18407 add_AT_flag (seg_die, DW_AT_external, 1);
18408
18409 if (decl_name != NULL
18410 && IDENTIFIER_POINTER (decl_name) != NULL)
18411 {
18412 name = dwarf2_name (decl, 1);
18413 if (! DECL_ARTIFICIAL (decl))
18414 add_src_coords_attributes (seg_die, decl);
18415
18416 add_linkage_name (seg_die, decl);
18417 }
18418 gcc_assert (name != NULL);
18419 add_pure_or_virtual_attribute (seg_die, decl);
18420 if (DECL_ARTIFICIAL (decl))
18421 add_AT_flag (seg_die, DW_AT_artificial, 1);
18422
18423 name = concat ("__second_sect_of_", name, NULL);
18424 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18425 fde->dw_fde_second_end, false);
18426 add_name_attribute (seg_die, name);
18427 if (want_pubnames ())
18428 add_pubname_string (name, seg_die);
18429 }
18430 }
18431 else
18432 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18433 false);
18434 }
18435
18436 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18437
18438 /* We define the "frame base" as the function's CFA. This is more
18439 convenient for several reasons: (1) It's stable across the prologue
18440 and epilogue, which makes it better than just a frame pointer,
18441 (2) With dwarf3, there exists a one-byte encoding that allows us
18442 to reference the .debug_frame data by proxy, but failing that,
18443 (3) We can at least reuse the code inspection and interpretation
18444 code that determines the CFA position at various points in the
18445 function. */
18446 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18447 {
18448 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18449 add_AT_loc (subr_die, DW_AT_frame_base, op);
18450 }
18451 else
18452 {
18453 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18454 if (list->dw_loc_next)
18455 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18456 else
18457 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18458 }
18459
18460 /* Compute a displacement from the "steady-state frame pointer" to
18461 the CFA. The former is what all stack slots and argument slots
18462 will reference in the rtl; the latter is what we've told the
18463 debugger about. We'll need to adjust all frame_base references
18464 by this displacement. */
18465 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18466
18467 if (fun->static_chain_decl)
18468 add_AT_location_description (subr_die, DW_AT_static_link,
18469 loc_list_from_tree (fun->static_chain_decl, 2));
18470 }
18471
18472 /* Generate child dies for template paramaters. */
18473 if (debug_info_level > DINFO_LEVEL_TERSE)
18474 gen_generic_params_dies (decl);
18475
18476 /* Now output descriptions of the arguments for this function. This gets
18477 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18478 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18479 `...' at the end of the formal parameter list. In order to find out if
18480 there was a trailing ellipsis or not, we must instead look at the type
18481 associated with the FUNCTION_DECL. This will be a node of type
18482 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18483 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18484 an ellipsis at the end. */
18485
18486 /* In the case where we are describing a mere function declaration, all we
18487 need to do here (and all we *can* do here) is to describe the *types* of
18488 its formal parameters. */
18489 if (debug_info_level <= DINFO_LEVEL_TERSE)
18490 ;
18491 else if (declaration)
18492 gen_formal_types_die (decl, subr_die);
18493 else
18494 {
18495 /* Generate DIEs to represent all known formal parameters. */
18496 tree parm = DECL_ARGUMENTS (decl);
18497 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18498 tree generic_decl_parm = generic_decl
18499 ? DECL_ARGUMENTS (generic_decl)
18500 : NULL;
18501
18502 /* Now we want to walk the list of parameters of the function and
18503 emit their relevant DIEs.
18504
18505 We consider the case of DECL being an instance of a generic function
18506 as well as it being a normal function.
18507
18508 If DECL is an instance of a generic function we walk the
18509 parameters of the generic function declaration _and_ the parameters of
18510 DECL itself. This is useful because we want to emit specific DIEs for
18511 function parameter packs and those are declared as part of the
18512 generic function declaration. In that particular case,
18513 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18514 That DIE has children DIEs representing the set of arguments
18515 of the pack. Note that the set of pack arguments can be empty.
18516 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18517 children DIE.
18518
18519 Otherwise, we just consider the parameters of DECL. */
18520 while (generic_decl_parm || parm)
18521 {
18522 if (generic_decl_parm
18523 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18524 gen_formal_parameter_pack_die (generic_decl_parm,
18525 parm, subr_die,
18526 &parm);
18527 else if (parm)
18528 {
18529 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18530
18531 if (parm == DECL_ARGUMENTS (decl)
18532 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18533 && parm_die
18534 && (dwarf_version >= 3 || !dwarf_strict))
18535 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18536
18537 parm = DECL_CHAIN (parm);
18538 }
18539
18540 if (generic_decl_parm)
18541 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18542 }
18543
18544 /* Decide whether we need an unspecified_parameters DIE at the end.
18545 There are 2 more cases to do this for: 1) the ansi ... declaration -
18546 this is detectable when the end of the arg list is not a
18547 void_type_node 2) an unprototyped function declaration (not a
18548 definition). This just means that we have no info about the
18549 parameters at all. */
18550 if (prototype_p (TREE_TYPE (decl)))
18551 {
18552 /* This is the prototyped case, check for.... */
18553 if (stdarg_p (TREE_TYPE (decl)))
18554 gen_unspecified_parameters_die (decl, subr_die);
18555 }
18556 else if (DECL_INITIAL (decl) == NULL_TREE)
18557 gen_unspecified_parameters_die (decl, subr_die);
18558 }
18559
18560 /* Output Dwarf info for all of the stuff within the body of the function
18561 (if it has one - it may be just a declaration). */
18562 outer_scope = DECL_INITIAL (decl);
18563
18564 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18565 a function. This BLOCK actually represents the outermost binding contour
18566 for the function, i.e. the contour in which the function's formal
18567 parameters and labels get declared. Curiously, it appears that the front
18568 end doesn't actually put the PARM_DECL nodes for the current function onto
18569 the BLOCK_VARS list for this outer scope, but are strung off of the
18570 DECL_ARGUMENTS list for the function instead.
18571
18572 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18573 the LABEL_DECL nodes for the function however, and we output DWARF info
18574 for those in decls_for_scope. Just within the `outer_scope' there will be
18575 a BLOCK node representing the function's outermost pair of curly braces,
18576 and any blocks used for the base and member initializers of a C++
18577 constructor function. */
18578 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18579 {
18580 int call_site_note_count = 0;
18581 int tail_call_site_note_count = 0;
18582
18583 /* Emit a DW_TAG_variable DIE for a named return value. */
18584 if (DECL_NAME (DECL_RESULT (decl)))
18585 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18586
18587 current_function_has_inlines = 0;
18588 decls_for_scope (outer_scope, subr_die, 0);
18589
18590 if (call_arg_locations && !dwarf_strict)
18591 {
18592 struct call_arg_loc_node *ca_loc;
18593 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18594 {
18595 dw_die_ref die = NULL;
18596 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18597 rtx arg, next_arg;
18598
18599 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18600 arg; arg = next_arg)
18601 {
18602 dw_loc_descr_ref reg, val;
18603 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18604 dw_die_ref cdie, tdie = NULL;
18605
18606 next_arg = XEXP (arg, 1);
18607 if (REG_P (XEXP (XEXP (arg, 0), 0))
18608 && next_arg
18609 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18610 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18611 && REGNO (XEXP (XEXP (arg, 0), 0))
18612 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18613 next_arg = XEXP (next_arg, 1);
18614 if (mode == VOIDmode)
18615 {
18616 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18617 if (mode == VOIDmode)
18618 mode = GET_MODE (XEXP (arg, 0));
18619 }
18620 if (mode == VOIDmode || mode == BLKmode)
18621 continue;
18622 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18623 {
18624 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18625 tloc = XEXP (XEXP (arg, 0), 1);
18626 continue;
18627 }
18628 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18629 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18630 {
18631 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18632 tlocc = XEXP (XEXP (arg, 0), 1);
18633 continue;
18634 }
18635 reg = NULL;
18636 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18637 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18638 VAR_INIT_STATUS_INITIALIZED);
18639 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18640 {
18641 rtx mem = XEXP (XEXP (arg, 0), 0);
18642 reg = mem_loc_descriptor (XEXP (mem, 0),
18643 get_address_mode (mem),
18644 GET_MODE (mem),
18645 VAR_INIT_STATUS_INITIALIZED);
18646 }
18647 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18648 == DEBUG_PARAMETER_REF)
18649 {
18650 tree tdecl
18651 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18652 tdie = lookup_decl_die (tdecl);
18653 if (tdie == NULL)
18654 continue;
18655 }
18656 else
18657 continue;
18658 if (reg == NULL
18659 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18660 != DEBUG_PARAMETER_REF)
18661 continue;
18662 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18663 VOIDmode,
18664 VAR_INIT_STATUS_INITIALIZED);
18665 if (val == NULL)
18666 continue;
18667 if (die == NULL)
18668 die = gen_call_site_die (decl, subr_die, ca_loc);
18669 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18670 NULL_TREE);
18671 if (reg != NULL)
18672 add_AT_loc (cdie, DW_AT_location, reg);
18673 else if (tdie != NULL)
18674 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18675 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18676 if (next_arg != XEXP (arg, 1))
18677 {
18678 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18679 if (mode == VOIDmode)
18680 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18681 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18682 0), 1),
18683 mode, VOIDmode,
18684 VAR_INIT_STATUS_INITIALIZED);
18685 if (val != NULL)
18686 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18687 }
18688 }
18689 if (die == NULL
18690 && (ca_loc->symbol_ref || tloc))
18691 die = gen_call_site_die (decl, subr_die, ca_loc);
18692 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18693 {
18694 dw_loc_descr_ref tval = NULL;
18695
18696 if (tloc != NULL_RTX)
18697 tval = mem_loc_descriptor (tloc,
18698 GET_MODE (tloc) == VOIDmode
18699 ? Pmode : GET_MODE (tloc),
18700 VOIDmode,
18701 VAR_INIT_STATUS_INITIALIZED);
18702 if (tval)
18703 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18704 else if (tlocc != NULL_RTX)
18705 {
18706 tval = mem_loc_descriptor (tlocc,
18707 GET_MODE (tlocc) == VOIDmode
18708 ? Pmode : GET_MODE (tlocc),
18709 VOIDmode,
18710 VAR_INIT_STATUS_INITIALIZED);
18711 if (tval)
18712 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18713 tval);
18714 }
18715 }
18716 if (die != NULL)
18717 {
18718 call_site_note_count++;
18719 if (ca_loc->tail_call_p)
18720 tail_call_site_note_count++;
18721 }
18722 }
18723 }
18724 call_arg_locations = NULL;
18725 call_arg_loc_last = NULL;
18726 if (tail_call_site_count >= 0
18727 && tail_call_site_count == tail_call_site_note_count
18728 && !dwarf_strict)
18729 {
18730 if (call_site_count >= 0
18731 && call_site_count == call_site_note_count)
18732 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18733 else
18734 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18735 }
18736 call_site_count = -1;
18737 tail_call_site_count = -1;
18738 }
18739
18740 if (subr_die != old_die)
18741 /* Add the calling convention attribute if requested. */
18742 add_calling_convention_attribute (subr_die, decl);
18743 }
18744
18745 /* Returns a hash value for X (which really is a die_struct). */
18746
18747 static hashval_t
18748 common_block_die_table_hash (const void *x)
18749 {
18750 const_dw_die_ref d = (const_dw_die_ref) x;
18751 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18752 }
18753
18754 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18755 as decl_id and die_parent of die_struct Y. */
18756
18757 static int
18758 common_block_die_table_eq (const void *x, const void *y)
18759 {
18760 const_dw_die_ref d = (const_dw_die_ref) x;
18761 const_dw_die_ref e = (const_dw_die_ref) y;
18762 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18763 }
18764
18765 /* Generate a DIE to represent a declared data object.
18766 Either DECL or ORIGIN must be non-null. */
18767
18768 static void
18769 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18770 {
18771 HOST_WIDE_INT off = 0;
18772 tree com_decl;
18773 tree decl_or_origin = decl ? decl : origin;
18774 tree ultimate_origin;
18775 dw_die_ref var_die;
18776 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18777 dw_die_ref origin_die;
18778 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18779 || class_or_namespace_scope_p (context_die));
18780 bool specialization_p = false;
18781
18782 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18783 if (decl || ultimate_origin)
18784 origin = ultimate_origin;
18785 com_decl = fortran_common (decl_or_origin, &off);
18786
18787 /* Symbol in common gets emitted as a child of the common block, in the form
18788 of a data member. */
18789 if (com_decl)
18790 {
18791 dw_die_ref com_die;
18792 dw_loc_list_ref loc;
18793 die_node com_die_arg;
18794
18795 var_die = lookup_decl_die (decl_or_origin);
18796 if (var_die)
18797 {
18798 if (get_AT (var_die, DW_AT_location) == NULL)
18799 {
18800 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18801 if (loc)
18802 {
18803 if (off)
18804 {
18805 /* Optimize the common case. */
18806 if (single_element_loc_list_p (loc)
18807 && loc->expr->dw_loc_opc == DW_OP_addr
18808 && loc->expr->dw_loc_next == NULL
18809 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18810 == SYMBOL_REF)
18811 {
18812 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18813 loc->expr->dw_loc_oprnd1.v.val_addr
18814 = plus_constant (GET_MODE (x), x , off);
18815 }
18816 else
18817 loc_list_plus_const (loc, off);
18818 }
18819 add_AT_location_description (var_die, DW_AT_location, loc);
18820 remove_AT (var_die, DW_AT_declaration);
18821 }
18822 }
18823 return;
18824 }
18825
18826 if (common_block_die_table == NULL)
18827 common_block_die_table
18828 = htab_create_ggc (10, common_block_die_table_hash,
18829 common_block_die_table_eq, NULL);
18830
18831 com_die_arg.decl_id = DECL_UID (com_decl);
18832 com_die_arg.die_parent = context_die;
18833 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18834 loc = loc_list_from_tree (com_decl, 2);
18835 if (com_die == NULL)
18836 {
18837 const char *cnam
18838 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18839 void **slot;
18840
18841 com_die = new_die (DW_TAG_common_block, context_die, decl);
18842 add_name_and_src_coords_attributes (com_die, com_decl);
18843 if (loc)
18844 {
18845 add_AT_location_description (com_die, DW_AT_location, loc);
18846 /* Avoid sharing the same loc descriptor between
18847 DW_TAG_common_block and DW_TAG_variable. */
18848 loc = loc_list_from_tree (com_decl, 2);
18849 }
18850 else if (DECL_EXTERNAL (decl))
18851 add_AT_flag (com_die, DW_AT_declaration, 1);
18852 if (want_pubnames ())
18853 add_pubname_string (cnam, com_die); /* ??? needed? */
18854 com_die->decl_id = DECL_UID (com_decl);
18855 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18856 *slot = (void *) com_die;
18857 }
18858 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18859 {
18860 add_AT_location_description (com_die, DW_AT_location, loc);
18861 loc = loc_list_from_tree (com_decl, 2);
18862 remove_AT (com_die, DW_AT_declaration);
18863 }
18864 var_die = new_die (DW_TAG_variable, com_die, decl);
18865 add_name_and_src_coords_attributes (var_die, decl);
18866 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18867 TREE_THIS_VOLATILE (decl), context_die);
18868 add_AT_flag (var_die, DW_AT_external, 1);
18869 if (loc)
18870 {
18871 if (off)
18872 {
18873 /* Optimize the common case. */
18874 if (single_element_loc_list_p (loc)
18875 && loc->expr->dw_loc_opc == DW_OP_addr
18876 && loc->expr->dw_loc_next == NULL
18877 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18878 {
18879 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18880 loc->expr->dw_loc_oprnd1.v.val_addr
18881 = plus_constant (GET_MODE (x), x, off);
18882 }
18883 else
18884 loc_list_plus_const (loc, off);
18885 }
18886 add_AT_location_description (var_die, DW_AT_location, loc);
18887 }
18888 else if (DECL_EXTERNAL (decl))
18889 add_AT_flag (var_die, DW_AT_declaration, 1);
18890 equate_decl_number_to_die (decl, var_die);
18891 return;
18892 }
18893
18894 /* If the compiler emitted a definition for the DECL declaration
18895 and if we already emitted a DIE for it, don't emit a second
18896 DIE for it again. Allow re-declarations of DECLs that are
18897 inside functions, though. */
18898 if (old_die && declaration && !local_scope_p (context_die))
18899 return;
18900
18901 /* For static data members, the declaration in the class is supposed
18902 to have DW_TAG_member tag; the specification should still be
18903 DW_TAG_variable referencing the DW_TAG_member DIE. */
18904 if (declaration && class_scope_p (context_die))
18905 var_die = new_die (DW_TAG_member, context_die, decl);
18906 else
18907 var_die = new_die (DW_TAG_variable, context_die, decl);
18908
18909 origin_die = NULL;
18910 if (origin != NULL)
18911 origin_die = add_abstract_origin_attribute (var_die, origin);
18912
18913 /* Loop unrolling can create multiple blocks that refer to the same
18914 static variable, so we must test for the DW_AT_declaration flag.
18915
18916 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18917 copy decls and set the DECL_ABSTRACT flag on them instead of
18918 sharing them.
18919
18920 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18921
18922 ??? The declare_in_namespace support causes us to get two DIEs for one
18923 variable, both of which are declarations. We want to avoid considering
18924 one to be a specification, so we must test that this DIE is not a
18925 declaration. */
18926 else if (old_die && TREE_STATIC (decl) && ! declaration
18927 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18928 {
18929 /* This is a definition of a C++ class level static. */
18930 add_AT_specification (var_die, old_die);
18931 specialization_p = true;
18932 if (DECL_NAME (decl))
18933 {
18934 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18935 struct dwarf_file_data * file_index = lookup_filename (s.file);
18936
18937 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18938 add_AT_file (var_die, DW_AT_decl_file, file_index);
18939
18940 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18941 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18942
18943 if (old_die->die_tag == DW_TAG_member)
18944 add_linkage_name (var_die, decl);
18945 }
18946 }
18947 else
18948 add_name_and_src_coords_attributes (var_die, decl);
18949
18950 if ((origin == NULL && !specialization_p)
18951 || (origin != NULL
18952 && !DECL_ABSTRACT (decl_or_origin)
18953 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18954 decl_function_context
18955 (decl_or_origin))))
18956 {
18957 tree type = TREE_TYPE (decl_or_origin);
18958
18959 if (decl_by_reference_p (decl_or_origin))
18960 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18961 else
18962 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18963 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18964 }
18965
18966 if (origin == NULL && !specialization_p)
18967 {
18968 if (TREE_PUBLIC (decl))
18969 add_AT_flag (var_die, DW_AT_external, 1);
18970
18971 if (DECL_ARTIFICIAL (decl))
18972 add_AT_flag (var_die, DW_AT_artificial, 1);
18973
18974 add_accessibility_attribute (var_die, decl);
18975 }
18976
18977 if (declaration)
18978 add_AT_flag (var_die, DW_AT_declaration, 1);
18979
18980 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18981 equate_decl_number_to_die (decl, var_die);
18982
18983 if (! declaration
18984 && (! DECL_ABSTRACT (decl_or_origin)
18985 /* Local static vars are shared between all clones/inlines,
18986 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18987 already set. */
18988 || (TREE_CODE (decl_or_origin) == VAR_DECL
18989 && TREE_STATIC (decl_or_origin)
18990 && DECL_RTL_SET_P (decl_or_origin)))
18991 /* When abstract origin already has DW_AT_location attribute, no need
18992 to add it again. */
18993 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18994 {
18995 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18996 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18997 defer_location (decl_or_origin, var_die);
18998 else
18999 add_location_or_const_value_attribute (var_die, decl_or_origin,
19000 decl == NULL, DW_AT_location);
19001 add_pubname (decl_or_origin, var_die);
19002 }
19003 else
19004 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19005 }
19006
19007 /* Generate a DIE to represent a named constant. */
19008
19009 static void
19010 gen_const_die (tree decl, dw_die_ref context_die)
19011 {
19012 dw_die_ref const_die;
19013 tree type = TREE_TYPE (decl);
19014
19015 const_die = new_die (DW_TAG_constant, context_die, decl);
19016 add_name_and_src_coords_attributes (const_die, decl);
19017 add_type_attribute (const_die, type, 1, 0, context_die);
19018 if (TREE_PUBLIC (decl))
19019 add_AT_flag (const_die, DW_AT_external, 1);
19020 if (DECL_ARTIFICIAL (decl))
19021 add_AT_flag (const_die, DW_AT_artificial, 1);
19022 tree_add_const_value_attribute_for_decl (const_die, decl);
19023 }
19024
19025 /* Generate a DIE to represent a label identifier. */
19026
19027 static void
19028 gen_label_die (tree decl, dw_die_ref context_die)
19029 {
19030 tree origin = decl_ultimate_origin (decl);
19031 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19032 rtx insn;
19033 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19034
19035 if (origin != NULL)
19036 add_abstract_origin_attribute (lbl_die, origin);
19037 else
19038 add_name_and_src_coords_attributes (lbl_die, decl);
19039
19040 if (DECL_ABSTRACT (decl))
19041 equate_decl_number_to_die (decl, lbl_die);
19042 else
19043 {
19044 insn = DECL_RTL_IF_SET (decl);
19045
19046 /* Deleted labels are programmer specified labels which have been
19047 eliminated because of various optimizations. We still emit them
19048 here so that it is possible to put breakpoints on them. */
19049 if (insn
19050 && (LABEL_P (insn)
19051 || ((NOTE_P (insn)
19052 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19053 {
19054 /* When optimization is enabled (via -O) some parts of the compiler
19055 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19056 represent source-level labels which were explicitly declared by
19057 the user. This really shouldn't be happening though, so catch
19058 it if it ever does happen. */
19059 gcc_assert (!INSN_DELETED_P (insn));
19060
19061 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19062 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19063 }
19064 else if (insn
19065 && NOTE_P (insn)
19066 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19067 && CODE_LABEL_NUMBER (insn) != -1)
19068 {
19069 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19070 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19071 }
19072 }
19073 }
19074
19075 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19076 attributes to the DIE for a block STMT, to describe where the inlined
19077 function was called from. This is similar to add_src_coords_attributes. */
19078
19079 static inline void
19080 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19081 {
19082 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19083
19084 if (dwarf_version >= 3 || !dwarf_strict)
19085 {
19086 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19087 add_AT_unsigned (die, DW_AT_call_line, s.line);
19088 }
19089 }
19090
19091
19092 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19093 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19094
19095 static inline void
19096 add_high_low_attributes (tree stmt, dw_die_ref die)
19097 {
19098 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19099
19100 if (BLOCK_FRAGMENT_CHAIN (stmt)
19101 && (dwarf_version >= 3 || !dwarf_strict))
19102 {
19103 tree chain, superblock = NULL_TREE;
19104 dw_die_ref pdie;
19105 dw_attr_ref attr = NULL;
19106
19107 if (inlined_function_outer_scope_p (stmt))
19108 {
19109 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19110 BLOCK_NUMBER (stmt));
19111 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19112 }
19113
19114 /* Optimize duplicate .debug_ranges lists or even tails of
19115 lists. If this BLOCK has same ranges as its supercontext,
19116 lookup DW_AT_ranges attribute in the supercontext (and
19117 recursively so), verify that the ranges_table contains the
19118 right values and use it instead of adding a new .debug_range. */
19119 for (chain = stmt, pdie = die;
19120 BLOCK_SAME_RANGE (chain);
19121 chain = BLOCK_SUPERCONTEXT (chain))
19122 {
19123 dw_attr_ref new_attr;
19124
19125 pdie = pdie->die_parent;
19126 if (pdie == NULL)
19127 break;
19128 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19129 break;
19130 new_attr = get_AT (pdie, DW_AT_ranges);
19131 if (new_attr == NULL
19132 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19133 break;
19134 attr = new_attr;
19135 superblock = BLOCK_SUPERCONTEXT (chain);
19136 }
19137 if (attr != NULL
19138 && (ranges_table[attr->dw_attr_val.v.val_offset
19139 / 2 / DWARF2_ADDR_SIZE].num
19140 == BLOCK_NUMBER (superblock))
19141 && BLOCK_FRAGMENT_CHAIN (superblock))
19142 {
19143 unsigned long off = attr->dw_attr_val.v.val_offset
19144 / 2 / DWARF2_ADDR_SIZE;
19145 unsigned long supercnt = 0, thiscnt = 0;
19146 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19147 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19148 {
19149 ++supercnt;
19150 gcc_checking_assert (ranges_table[off + supercnt].num
19151 == BLOCK_NUMBER (chain));
19152 }
19153 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19154 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19155 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19156 ++thiscnt;
19157 gcc_assert (supercnt >= thiscnt);
19158 add_AT_range_list (die, DW_AT_ranges,
19159 ((off + supercnt - thiscnt)
19160 * 2 * DWARF2_ADDR_SIZE),
19161 false);
19162 return;
19163 }
19164
19165 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19166
19167 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19168 do
19169 {
19170 add_ranges (chain);
19171 chain = BLOCK_FRAGMENT_CHAIN (chain);
19172 }
19173 while (chain);
19174 add_ranges (NULL);
19175 }
19176 else
19177 {
19178 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19179 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19180 BLOCK_NUMBER (stmt));
19181 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19182 BLOCK_NUMBER (stmt));
19183 add_AT_low_high_pc (die, label, label_high, false);
19184 }
19185 }
19186
19187 /* Generate a DIE for a lexical block. */
19188
19189 static void
19190 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19191 {
19192 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19193
19194 if (call_arg_locations)
19195 {
19196 if (block_map.length () <= BLOCK_NUMBER (stmt))
19197 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19198 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19199 }
19200
19201 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19202 add_high_low_attributes (stmt, stmt_die);
19203
19204 decls_for_scope (stmt, stmt_die, depth);
19205 }
19206
19207 /* Generate a DIE for an inlined subprogram. */
19208
19209 static void
19210 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19211 {
19212 tree decl;
19213
19214 /* The instance of function that is effectively being inlined shall not
19215 be abstract. */
19216 gcc_assert (! BLOCK_ABSTRACT (stmt));
19217
19218 decl = block_ultimate_origin (stmt);
19219
19220 /* Emit info for the abstract instance first, if we haven't yet. We
19221 must emit this even if the block is abstract, otherwise when we
19222 emit the block below (or elsewhere), we may end up trying to emit
19223 a die whose origin die hasn't been emitted, and crashing. */
19224 dwarf2out_abstract_function (decl);
19225
19226 if (! BLOCK_ABSTRACT (stmt))
19227 {
19228 dw_die_ref subr_die
19229 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19230
19231 if (call_arg_locations)
19232 {
19233 if (block_map.length () <= BLOCK_NUMBER (stmt))
19234 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19235 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19236 }
19237 add_abstract_origin_attribute (subr_die, decl);
19238 if (TREE_ASM_WRITTEN (stmt))
19239 add_high_low_attributes (stmt, subr_die);
19240 add_call_src_coords_attributes (stmt, subr_die);
19241
19242 decls_for_scope (stmt, subr_die, depth);
19243 current_function_has_inlines = 1;
19244 }
19245 }
19246
19247 /* Generate a DIE for a field in a record, or structure. */
19248
19249 static void
19250 gen_field_die (tree decl, dw_die_ref context_die)
19251 {
19252 dw_die_ref decl_die;
19253
19254 if (TREE_TYPE (decl) == error_mark_node)
19255 return;
19256
19257 decl_die = new_die (DW_TAG_member, context_die, decl);
19258 add_name_and_src_coords_attributes (decl_die, decl);
19259 add_type_attribute (decl_die, member_declared_type (decl),
19260 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19261 context_die);
19262
19263 if (DECL_BIT_FIELD_TYPE (decl))
19264 {
19265 add_byte_size_attribute (decl_die, decl);
19266 add_bit_size_attribute (decl_die, decl);
19267 add_bit_offset_attribute (decl_die, decl);
19268 }
19269
19270 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19271 add_data_member_location_attribute (decl_die, decl);
19272
19273 if (DECL_ARTIFICIAL (decl))
19274 add_AT_flag (decl_die, DW_AT_artificial, 1);
19275
19276 add_accessibility_attribute (decl_die, decl);
19277
19278 /* Equate decl number to die, so that we can look up this decl later on. */
19279 equate_decl_number_to_die (decl, decl_die);
19280 }
19281
19282 #if 0
19283 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19284 Use modified_type_die instead.
19285 We keep this code here just in case these types of DIEs may be needed to
19286 represent certain things in other languages (e.g. Pascal) someday. */
19287
19288 static void
19289 gen_pointer_type_die (tree type, dw_die_ref context_die)
19290 {
19291 dw_die_ref ptr_die
19292 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19293
19294 equate_type_number_to_die (type, ptr_die);
19295 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19296 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19297 }
19298
19299 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19300 Use modified_type_die instead.
19301 We keep this code here just in case these types of DIEs may be needed to
19302 represent certain things in other languages (e.g. Pascal) someday. */
19303
19304 static void
19305 gen_reference_type_die (tree type, dw_die_ref context_die)
19306 {
19307 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19308
19309 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19310 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19311 else
19312 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19313
19314 equate_type_number_to_die (type, ref_die);
19315 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19316 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19317 }
19318 #endif
19319
19320 /* Generate a DIE for a pointer to a member type. */
19321
19322 static void
19323 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19324 {
19325 dw_die_ref ptr_die
19326 = new_die (DW_TAG_ptr_to_member_type,
19327 scope_die_for (type, context_die), type);
19328
19329 equate_type_number_to_die (type, ptr_die);
19330 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19331 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19332 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19333 }
19334
19335 typedef const char *dchar_p; /* For DEF_VEC_P. */
19336
19337 static char *producer_string;
19338
19339 /* Return a heap allocated producer string including command line options
19340 if -grecord-gcc-switches. */
19341
19342 static char *
19343 gen_producer_string (void)
19344 {
19345 size_t j;
19346 auto_vec<dchar_p> switches;
19347 const char *language_string = lang_hooks.name;
19348 char *producer, *tail;
19349 const char *p;
19350 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19351 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19352
19353 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19354 switch (save_decoded_options[j].opt_index)
19355 {
19356 case OPT_o:
19357 case OPT_d:
19358 case OPT_dumpbase:
19359 case OPT_dumpdir:
19360 case OPT_auxbase:
19361 case OPT_auxbase_strip:
19362 case OPT_quiet:
19363 case OPT_version:
19364 case OPT_v:
19365 case OPT_w:
19366 case OPT_L:
19367 case OPT_D:
19368 case OPT_I:
19369 case OPT_U:
19370 case OPT_SPECIAL_unknown:
19371 case OPT_SPECIAL_ignore:
19372 case OPT_SPECIAL_program_name:
19373 case OPT_SPECIAL_input_file:
19374 case OPT_grecord_gcc_switches:
19375 case OPT_gno_record_gcc_switches:
19376 case OPT__output_pch_:
19377 case OPT_fdiagnostics_show_location_:
19378 case OPT_fdiagnostics_show_option:
19379 case OPT_fdiagnostics_show_caret:
19380 case OPT_fdiagnostics_color_:
19381 case OPT_fverbose_asm:
19382 case OPT____:
19383 case OPT__sysroot_:
19384 case OPT_nostdinc:
19385 case OPT_nostdinc__:
19386 /* Ignore these. */
19387 continue;
19388 default:
19389 if (cl_options[save_decoded_options[j].opt_index].flags
19390 & CL_NO_DWARF_RECORD)
19391 continue;
19392 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19393 == '-');
19394 switch (save_decoded_options[j].canonical_option[0][1])
19395 {
19396 case 'M':
19397 case 'i':
19398 case 'W':
19399 continue;
19400 case 'f':
19401 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19402 "dump", 4) == 0)
19403 continue;
19404 break;
19405 default:
19406 break;
19407 }
19408 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19409 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19410 break;
19411 }
19412
19413 producer = XNEWVEC (char, plen + 1 + len + 1);
19414 tail = producer;
19415 sprintf (tail, "%s %s", language_string, version_string);
19416 tail += plen;
19417
19418 FOR_EACH_VEC_ELT (switches, j, p)
19419 {
19420 len = strlen (p);
19421 *tail = ' ';
19422 memcpy (tail + 1, p, len);
19423 tail += len + 1;
19424 }
19425
19426 *tail = '\0';
19427 return producer;
19428 }
19429
19430 /* Generate the DIE for the compilation unit. */
19431
19432 static dw_die_ref
19433 gen_compile_unit_die (const char *filename)
19434 {
19435 dw_die_ref die;
19436 const char *language_string = lang_hooks.name;
19437 int language;
19438
19439 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19440
19441 if (filename)
19442 {
19443 add_name_attribute (die, filename);
19444 /* Don't add cwd for <built-in>. */
19445 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19446 add_comp_dir_attribute (die);
19447 }
19448
19449 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19450
19451 /* If our producer is LTO try to figure out a common language to use
19452 from the global list of translation units. */
19453 if (strcmp (language_string, "GNU GIMPLE") == 0)
19454 {
19455 unsigned i;
19456 tree t;
19457 const char *common_lang = NULL;
19458
19459 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19460 {
19461 if (!TRANSLATION_UNIT_LANGUAGE (t))
19462 continue;
19463 if (!common_lang)
19464 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19465 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19466 ;
19467 else if (strncmp (common_lang, "GNU C", 5) == 0
19468 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19469 /* Mixing C and C++ is ok, use C++ in that case. */
19470 common_lang = "GNU C++";
19471 else
19472 {
19473 /* Fall back to C. */
19474 common_lang = NULL;
19475 break;
19476 }
19477 }
19478
19479 if (common_lang)
19480 language_string = common_lang;
19481 }
19482
19483 language = DW_LANG_C89;
19484 if (strcmp (language_string, "GNU C++") == 0)
19485 language = DW_LANG_C_plus_plus;
19486 else if (strcmp (language_string, "GNU F77") == 0)
19487 language = DW_LANG_Fortran77;
19488 else if (strcmp (language_string, "GNU Pascal") == 0)
19489 language = DW_LANG_Pascal83;
19490 else if (dwarf_version >= 3 || !dwarf_strict)
19491 {
19492 if (strcmp (language_string, "GNU Ada") == 0)
19493 language = DW_LANG_Ada95;
19494 else if (strcmp (language_string, "GNU Fortran") == 0)
19495 language = DW_LANG_Fortran95;
19496 else if (strcmp (language_string, "GNU Java") == 0)
19497 language = DW_LANG_Java;
19498 else if (strcmp (language_string, "GNU Objective-C") == 0)
19499 language = DW_LANG_ObjC;
19500 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19501 language = DW_LANG_ObjC_plus_plus;
19502 else if (dwarf_version >= 5 || !dwarf_strict)
19503 {
19504 if (strcmp (language_string, "GNU Go") == 0)
19505 language = DW_LANG_Go;
19506 }
19507 }
19508 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19509 else if (strcmp (language_string, "GNU Fortran") == 0)
19510 language = DW_LANG_Fortran90;
19511
19512 add_AT_unsigned (die, DW_AT_language, language);
19513
19514 switch (language)
19515 {
19516 case DW_LANG_Fortran77:
19517 case DW_LANG_Fortran90:
19518 case DW_LANG_Fortran95:
19519 /* Fortran has case insensitive identifiers and the front-end
19520 lowercases everything. */
19521 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19522 break;
19523 default:
19524 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19525 break;
19526 }
19527 return die;
19528 }
19529
19530 /* Generate the DIE for a base class. */
19531
19532 static void
19533 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19534 {
19535 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19536
19537 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19538 add_data_member_location_attribute (die, binfo);
19539
19540 if (BINFO_VIRTUAL_P (binfo))
19541 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19542
19543 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19544 children, otherwise the default is DW_ACCESS_public. In DWARF2
19545 the default has always been DW_ACCESS_private. */
19546 if (access == access_public_node)
19547 {
19548 if (dwarf_version == 2
19549 || context_die->die_tag == DW_TAG_class_type)
19550 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19551 }
19552 else if (access == access_protected_node)
19553 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19554 else if (dwarf_version > 2
19555 && context_die->die_tag != DW_TAG_class_type)
19556 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19557 }
19558
19559 /* Generate a DIE for a class member. */
19560
19561 static void
19562 gen_member_die (tree type, dw_die_ref context_die)
19563 {
19564 tree member;
19565 tree binfo = TYPE_BINFO (type);
19566 dw_die_ref child;
19567
19568 /* If this is not an incomplete type, output descriptions of each of its
19569 members. Note that as we output the DIEs necessary to represent the
19570 members of this record or union type, we will also be trying to output
19571 DIEs to represent the *types* of those members. However the `type'
19572 function (above) will specifically avoid generating type DIEs for member
19573 types *within* the list of member DIEs for this (containing) type except
19574 for those types (of members) which are explicitly marked as also being
19575 members of this (containing) type themselves. The g++ front- end can
19576 force any given type to be treated as a member of some other (containing)
19577 type by setting the TYPE_CONTEXT of the given (member) type to point to
19578 the TREE node representing the appropriate (containing) type. */
19579
19580 /* First output info about the base classes. */
19581 if (binfo)
19582 {
19583 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19584 int i;
19585 tree base;
19586
19587 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19588 gen_inheritance_die (base,
19589 (accesses ? (*accesses)[i] : access_public_node),
19590 context_die);
19591 }
19592
19593 /* Now output info about the data members and type members. */
19594 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19595 {
19596 /* If we thought we were generating minimal debug info for TYPE
19597 and then changed our minds, some of the member declarations
19598 may have already been defined. Don't define them again, but
19599 do put them in the right order. */
19600
19601 child = lookup_decl_die (member);
19602 if (child)
19603 splice_child_die (context_die, child);
19604 else
19605 gen_decl_die (member, NULL, context_die);
19606 }
19607
19608 /* Now output info about the function members (if any). */
19609 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19610 {
19611 /* Don't include clones in the member list. */
19612 if (DECL_ABSTRACT_ORIGIN (member))
19613 continue;
19614
19615 child = lookup_decl_die (member);
19616 if (child)
19617 splice_child_die (context_die, child);
19618 else
19619 gen_decl_die (member, NULL, context_die);
19620 }
19621 }
19622
19623 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19624 is set, we pretend that the type was never defined, so we only get the
19625 member DIEs needed by later specification DIEs. */
19626
19627 static void
19628 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19629 enum debug_info_usage usage)
19630 {
19631 dw_die_ref type_die = lookup_type_die (type);
19632 dw_die_ref scope_die = 0;
19633 int nested = 0;
19634 int complete = (TYPE_SIZE (type)
19635 && (! TYPE_STUB_DECL (type)
19636 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19637 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19638 complete = complete && should_emit_struct_debug (type, usage);
19639
19640 if (type_die && ! complete)
19641 return;
19642
19643 if (TYPE_CONTEXT (type) != NULL_TREE
19644 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19645 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19646 nested = 1;
19647
19648 scope_die = scope_die_for (type, context_die);
19649
19650 /* Generate child dies for template paramaters. */
19651 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19652 schedule_generic_params_dies_gen (type);
19653
19654 if (! type_die || (nested && is_cu_die (scope_die)))
19655 /* First occurrence of type or toplevel definition of nested class. */
19656 {
19657 dw_die_ref old_die = type_die;
19658
19659 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19660 ? record_type_tag (type) : DW_TAG_union_type,
19661 scope_die, type);
19662 equate_type_number_to_die (type, type_die);
19663 if (old_die)
19664 add_AT_specification (type_die, old_die);
19665 else
19666 add_name_attribute (type_die, type_tag (type));
19667 }
19668 else
19669 remove_AT (type_die, DW_AT_declaration);
19670
19671 /* If this type has been completed, then give it a byte_size attribute and
19672 then give a list of members. */
19673 if (complete && !ns_decl)
19674 {
19675 /* Prevent infinite recursion in cases where the type of some member of
19676 this type is expressed in terms of this type itself. */
19677 TREE_ASM_WRITTEN (type) = 1;
19678 add_byte_size_attribute (type_die, type);
19679 if (TYPE_STUB_DECL (type) != NULL_TREE)
19680 {
19681 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19682 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19683 }
19684
19685 /* If the first reference to this type was as the return type of an
19686 inline function, then it may not have a parent. Fix this now. */
19687 if (type_die->die_parent == NULL)
19688 add_child_die (scope_die, type_die);
19689
19690 push_decl_scope (type);
19691 gen_member_die (type, type_die);
19692 pop_decl_scope ();
19693
19694 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19695 if (TYPE_ARTIFICIAL (type))
19696 add_AT_flag (type_die, DW_AT_artificial, 1);
19697
19698 /* GNU extension: Record what type our vtable lives in. */
19699 if (TYPE_VFIELD (type))
19700 {
19701 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19702
19703 gen_type_die (vtype, context_die);
19704 add_AT_die_ref (type_die, DW_AT_containing_type,
19705 lookup_type_die (vtype));
19706 }
19707 }
19708 else
19709 {
19710 add_AT_flag (type_die, DW_AT_declaration, 1);
19711
19712 /* We don't need to do this for function-local types. */
19713 if (TYPE_STUB_DECL (type)
19714 && ! decl_function_context (TYPE_STUB_DECL (type)))
19715 vec_safe_push (incomplete_types, type);
19716 }
19717
19718 if (get_AT (type_die, DW_AT_name))
19719 add_pubtype (type, type_die);
19720 }
19721
19722 /* Generate a DIE for a subroutine _type_. */
19723
19724 static void
19725 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19726 {
19727 tree return_type = TREE_TYPE (type);
19728 dw_die_ref subr_die
19729 = new_die (DW_TAG_subroutine_type,
19730 scope_die_for (type, context_die), type);
19731
19732 equate_type_number_to_die (type, subr_die);
19733 add_prototyped_attribute (subr_die, type);
19734 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19735 gen_formal_types_die (type, subr_die);
19736
19737 if (get_AT (subr_die, DW_AT_name))
19738 add_pubtype (type, subr_die);
19739 }
19740
19741 /* Generate a DIE for a type definition. */
19742
19743 static void
19744 gen_typedef_die (tree decl, dw_die_ref context_die)
19745 {
19746 dw_die_ref type_die;
19747 tree origin;
19748
19749 if (TREE_ASM_WRITTEN (decl))
19750 return;
19751
19752 TREE_ASM_WRITTEN (decl) = 1;
19753 type_die = new_die (DW_TAG_typedef, context_die, decl);
19754 origin = decl_ultimate_origin (decl);
19755 if (origin != NULL)
19756 add_abstract_origin_attribute (type_die, origin);
19757 else
19758 {
19759 tree type;
19760
19761 add_name_and_src_coords_attributes (type_die, decl);
19762 if (DECL_ORIGINAL_TYPE (decl))
19763 {
19764 type = DECL_ORIGINAL_TYPE (decl);
19765
19766 gcc_assert (type != TREE_TYPE (decl));
19767 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19768 }
19769 else
19770 {
19771 type = TREE_TYPE (decl);
19772
19773 if (is_naming_typedef_decl (TYPE_NAME (type)))
19774 {
19775 /* Here, we are in the case of decl being a typedef naming
19776 an anonymous type, e.g:
19777 typedef struct {...} foo;
19778 In that case TREE_TYPE (decl) is not a typedef variant
19779 type and TYPE_NAME of the anonymous type is set to the
19780 TYPE_DECL of the typedef. This construct is emitted by
19781 the C++ FE.
19782
19783 TYPE is the anonymous struct named by the typedef
19784 DECL. As we need the DW_AT_type attribute of the
19785 DW_TAG_typedef to point to the DIE of TYPE, let's
19786 generate that DIE right away. add_type_attribute
19787 called below will then pick (via lookup_type_die) that
19788 anonymous struct DIE. */
19789 if (!TREE_ASM_WRITTEN (type))
19790 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19791
19792 /* This is a GNU Extension. We are adding a
19793 DW_AT_linkage_name attribute to the DIE of the
19794 anonymous struct TYPE. The value of that attribute
19795 is the name of the typedef decl naming the anonymous
19796 struct. This greatly eases the work of consumers of
19797 this debug info. */
19798 add_linkage_attr (lookup_type_die (type), decl);
19799 }
19800 }
19801
19802 add_type_attribute (type_die, type, TREE_READONLY (decl),
19803 TREE_THIS_VOLATILE (decl), context_die);
19804
19805 if (is_naming_typedef_decl (decl))
19806 /* We want that all subsequent calls to lookup_type_die with
19807 TYPE in argument yield the DW_TAG_typedef we have just
19808 created. */
19809 equate_type_number_to_die (type, type_die);
19810
19811 add_accessibility_attribute (type_die, decl);
19812 }
19813
19814 if (DECL_ABSTRACT (decl))
19815 equate_decl_number_to_die (decl, type_die);
19816
19817 if (get_AT (type_die, DW_AT_name))
19818 add_pubtype (decl, type_die);
19819 }
19820
19821 /* Generate a DIE for a struct, class, enum or union type. */
19822
19823 static void
19824 gen_tagged_type_die (tree type,
19825 dw_die_ref context_die,
19826 enum debug_info_usage usage)
19827 {
19828 int need_pop;
19829
19830 if (type == NULL_TREE
19831 || !is_tagged_type (type))
19832 return;
19833
19834 /* If this is a nested type whose containing class hasn't been written
19835 out yet, writing it out will cover this one, too. This does not apply
19836 to instantiations of member class templates; they need to be added to
19837 the containing class as they are generated. FIXME: This hurts the
19838 idea of combining type decls from multiple TUs, since we can't predict
19839 what set of template instantiations we'll get. */
19840 if (TYPE_CONTEXT (type)
19841 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19842 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19843 {
19844 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19845
19846 if (TREE_ASM_WRITTEN (type))
19847 return;
19848
19849 /* If that failed, attach ourselves to the stub. */
19850 push_decl_scope (TYPE_CONTEXT (type));
19851 context_die = lookup_type_die (TYPE_CONTEXT (type));
19852 need_pop = 1;
19853 }
19854 else if (TYPE_CONTEXT (type) != NULL_TREE
19855 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19856 {
19857 /* If this type is local to a function that hasn't been written
19858 out yet, use a NULL context for now; it will be fixed up in
19859 decls_for_scope. */
19860 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19861 /* A declaration DIE doesn't count; nested types need to go in the
19862 specification. */
19863 if (context_die && is_declaration_die (context_die))
19864 context_die = NULL;
19865 need_pop = 0;
19866 }
19867 else
19868 {
19869 context_die = declare_in_namespace (type, context_die);
19870 need_pop = 0;
19871 }
19872
19873 if (TREE_CODE (type) == ENUMERAL_TYPE)
19874 {
19875 /* This might have been written out by the call to
19876 declare_in_namespace. */
19877 if (!TREE_ASM_WRITTEN (type))
19878 gen_enumeration_type_die (type, context_die);
19879 }
19880 else
19881 gen_struct_or_union_type_die (type, context_die, usage);
19882
19883 if (need_pop)
19884 pop_decl_scope ();
19885
19886 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19887 it up if it is ever completed. gen_*_type_die will set it for us
19888 when appropriate. */
19889 }
19890
19891 /* Generate a type description DIE. */
19892
19893 static void
19894 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19895 enum debug_info_usage usage)
19896 {
19897 struct array_descr_info info;
19898
19899 if (type == NULL_TREE || type == error_mark_node)
19900 return;
19901
19902 if (TYPE_NAME (type) != NULL_TREE
19903 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19904 && is_redundant_typedef (TYPE_NAME (type))
19905 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19906 /* The DECL of this type is a typedef we don't want to emit debug
19907 info for but we want debug info for its underlying typedef.
19908 This can happen for e.g, the injected-class-name of a C++
19909 type. */
19910 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19911
19912 /* If TYPE is a typedef type variant, let's generate debug info
19913 for the parent typedef which TYPE is a type of. */
19914 if (typedef_variant_p (type))
19915 {
19916 if (TREE_ASM_WRITTEN (type))
19917 return;
19918
19919 /* Prevent broken recursion; we can't hand off to the same type. */
19920 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19921
19922 /* Give typedefs the right scope. */
19923 context_die = scope_die_for (type, context_die);
19924
19925 TREE_ASM_WRITTEN (type) = 1;
19926
19927 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19928 return;
19929 }
19930
19931 /* If type is an anonymous tagged type named by a typedef, let's
19932 generate debug info for the typedef. */
19933 if (is_naming_typedef_decl (TYPE_NAME (type)))
19934 {
19935 /* Use the DIE of the containing namespace as the parent DIE of
19936 the type description DIE we want to generate. */
19937 if (DECL_CONTEXT (TYPE_NAME (type))
19938 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19939 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19940
19941 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19942 return;
19943 }
19944
19945 /* If this is an array type with hidden descriptor, handle it first. */
19946 if (!TREE_ASM_WRITTEN (type)
19947 && lang_hooks.types.get_array_descr_info
19948 && lang_hooks.types.get_array_descr_info (type, &info)
19949 && (dwarf_version >= 3 || !dwarf_strict))
19950 {
19951 gen_descr_array_type_die (type, &info, context_die);
19952 TREE_ASM_WRITTEN (type) = 1;
19953 return;
19954 }
19955
19956 /* We are going to output a DIE to represent the unqualified version
19957 of this type (i.e. without any const or volatile qualifiers) so
19958 get the main variant (i.e. the unqualified version) of this type
19959 now. (Vectors are special because the debugging info is in the
19960 cloned type itself). */
19961 if (TREE_CODE (type) != VECTOR_TYPE)
19962 type = type_main_variant (type);
19963
19964 if (TREE_ASM_WRITTEN (type))
19965 return;
19966
19967 switch (TREE_CODE (type))
19968 {
19969 case ERROR_MARK:
19970 break;
19971
19972 case POINTER_TYPE:
19973 case REFERENCE_TYPE:
19974 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19975 ensures that the gen_type_die recursion will terminate even if the
19976 type is recursive. Recursive types are possible in Ada. */
19977 /* ??? We could perhaps do this for all types before the switch
19978 statement. */
19979 TREE_ASM_WRITTEN (type) = 1;
19980
19981 /* For these types, all that is required is that we output a DIE (or a
19982 set of DIEs) to represent the "basis" type. */
19983 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19984 DINFO_USAGE_IND_USE);
19985 break;
19986
19987 case OFFSET_TYPE:
19988 /* This code is used for C++ pointer-to-data-member types.
19989 Output a description of the relevant class type. */
19990 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19991 DINFO_USAGE_IND_USE);
19992
19993 /* Output a description of the type of the object pointed to. */
19994 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19995 DINFO_USAGE_IND_USE);
19996
19997 /* Now output a DIE to represent this pointer-to-data-member type
19998 itself. */
19999 gen_ptr_to_mbr_type_die (type, context_die);
20000 break;
20001
20002 case FUNCTION_TYPE:
20003 /* Force out return type (in case it wasn't forced out already). */
20004 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20005 DINFO_USAGE_DIR_USE);
20006 gen_subroutine_type_die (type, context_die);
20007 break;
20008
20009 case METHOD_TYPE:
20010 /* Force out return type (in case it wasn't forced out already). */
20011 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20012 DINFO_USAGE_DIR_USE);
20013 gen_subroutine_type_die (type, context_die);
20014 break;
20015
20016 case ARRAY_TYPE:
20017 gen_array_type_die (type, context_die);
20018 break;
20019
20020 case VECTOR_TYPE:
20021 gen_array_type_die (type, context_die);
20022 break;
20023
20024 case ENUMERAL_TYPE:
20025 case RECORD_TYPE:
20026 case UNION_TYPE:
20027 case QUAL_UNION_TYPE:
20028 gen_tagged_type_die (type, context_die, usage);
20029 return;
20030
20031 case VOID_TYPE:
20032 case INTEGER_TYPE:
20033 case REAL_TYPE:
20034 case FIXED_POINT_TYPE:
20035 case COMPLEX_TYPE:
20036 case BOOLEAN_TYPE:
20037 /* No DIEs needed for fundamental types. */
20038 break;
20039
20040 case NULLPTR_TYPE:
20041 case LANG_TYPE:
20042 /* Just use DW_TAG_unspecified_type. */
20043 {
20044 dw_die_ref type_die = lookup_type_die (type);
20045 if (type_die == NULL)
20046 {
20047 tree name = TYPE_IDENTIFIER (type);
20048 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20049 type);
20050 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20051 equate_type_number_to_die (type, type_die);
20052 }
20053 }
20054 break;
20055
20056 default:
20057 if (is_cxx_auto (type))
20058 {
20059 tree name = TYPE_IDENTIFIER (type);
20060 dw_die_ref *die = (name == get_identifier ("auto")
20061 ? &auto_die : &decltype_auto_die);
20062 if (!*die)
20063 {
20064 *die = new_die (DW_TAG_unspecified_type,
20065 comp_unit_die (), NULL_TREE);
20066 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20067 }
20068 equate_type_number_to_die (type, *die);
20069 break;
20070 }
20071 gcc_unreachable ();
20072 }
20073
20074 TREE_ASM_WRITTEN (type) = 1;
20075 }
20076
20077 static void
20078 gen_type_die (tree type, dw_die_ref context_die)
20079 {
20080 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20081 }
20082
20083 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20084 things which are local to the given block. */
20085
20086 static void
20087 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20088 {
20089 int must_output_die = 0;
20090 bool inlined_func;
20091
20092 /* Ignore blocks that are NULL. */
20093 if (stmt == NULL_TREE)
20094 return;
20095
20096 inlined_func = inlined_function_outer_scope_p (stmt);
20097
20098 /* If the block is one fragment of a non-contiguous block, do not
20099 process the variables, since they will have been done by the
20100 origin block. Do process subblocks. */
20101 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20102 {
20103 tree sub;
20104
20105 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20106 gen_block_die (sub, context_die, depth + 1);
20107
20108 return;
20109 }
20110
20111 /* Determine if we need to output any Dwarf DIEs at all to represent this
20112 block. */
20113 if (inlined_func)
20114 /* The outer scopes for inlinings *must* always be represented. We
20115 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20116 must_output_die = 1;
20117 else
20118 {
20119 /* Determine if this block directly contains any "significant"
20120 local declarations which we will need to output DIEs for. */
20121 if (debug_info_level > DINFO_LEVEL_TERSE)
20122 /* We are not in terse mode so *any* local declaration counts
20123 as being a "significant" one. */
20124 must_output_die = ((BLOCK_VARS (stmt) != NULL
20125 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20126 && (TREE_USED (stmt)
20127 || TREE_ASM_WRITTEN (stmt)
20128 || BLOCK_ABSTRACT (stmt)));
20129 else if ((TREE_USED (stmt)
20130 || TREE_ASM_WRITTEN (stmt)
20131 || BLOCK_ABSTRACT (stmt))
20132 && !dwarf2out_ignore_block (stmt))
20133 must_output_die = 1;
20134 }
20135
20136 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20137 DIE for any block which contains no significant local declarations at
20138 all. Rather, in such cases we just call `decls_for_scope' so that any
20139 needed Dwarf info for any sub-blocks will get properly generated. Note
20140 that in terse mode, our definition of what constitutes a "significant"
20141 local declaration gets restricted to include only inlined function
20142 instances and local (nested) function definitions. */
20143 if (must_output_die)
20144 {
20145 if (inlined_func)
20146 {
20147 /* If STMT block is abstract, that means we have been called
20148 indirectly from dwarf2out_abstract_function.
20149 That function rightfully marks the descendent blocks (of
20150 the abstract function it is dealing with) as being abstract,
20151 precisely to prevent us from emitting any
20152 DW_TAG_inlined_subroutine DIE as a descendent
20153 of an abstract function instance. So in that case, we should
20154 not call gen_inlined_subroutine_die.
20155
20156 Later though, when cgraph asks dwarf2out to emit info
20157 for the concrete instance of the function decl into which
20158 the concrete instance of STMT got inlined, the later will lead
20159 to the generation of a DW_TAG_inlined_subroutine DIE. */
20160 if (! BLOCK_ABSTRACT (stmt))
20161 gen_inlined_subroutine_die (stmt, context_die, depth);
20162 }
20163 else
20164 gen_lexical_block_die (stmt, context_die, depth);
20165 }
20166 else
20167 decls_for_scope (stmt, context_die, depth);
20168 }
20169
20170 /* Process variable DECL (or variable with origin ORIGIN) within
20171 block STMT and add it to CONTEXT_DIE. */
20172 static void
20173 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20174 {
20175 dw_die_ref die;
20176 tree decl_or_origin = decl ? decl : origin;
20177
20178 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20179 die = lookup_decl_die (decl_or_origin);
20180 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20181 && TYPE_DECL_IS_STUB (decl_or_origin))
20182 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20183 else
20184 die = NULL;
20185
20186 if (die != NULL && die->die_parent == NULL)
20187 add_child_die (context_die, die);
20188 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20189 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20190 stmt, context_die);
20191 else
20192 gen_decl_die (decl, origin, context_die);
20193 }
20194
20195 /* Generate all of the decls declared within a given scope and (recursively)
20196 all of its sub-blocks. */
20197
20198 static void
20199 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20200 {
20201 tree decl;
20202 unsigned int i;
20203 tree subblocks;
20204
20205 /* Ignore NULL blocks. */
20206 if (stmt == NULL_TREE)
20207 return;
20208
20209 /* Output the DIEs to represent all of the data objects and typedefs
20210 declared directly within this block but not within any nested
20211 sub-blocks. Also, nested function and tag DIEs have been
20212 generated with a parent of NULL; fix that up now. We don't
20213 have to do this if we're at -g1. */
20214 if (debug_info_level > DINFO_LEVEL_TERSE)
20215 {
20216 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20217 process_scope_var (stmt, decl, NULL_TREE, context_die);
20218 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20219 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20220 context_die);
20221 }
20222
20223 /* Even if we're at -g1, we need to process the subblocks in order to get
20224 inlined call information. */
20225
20226 /* Output the DIEs to represent all sub-blocks (and the items declared
20227 therein) of this block. */
20228 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20229 subblocks != NULL;
20230 subblocks = BLOCK_CHAIN (subblocks))
20231 gen_block_die (subblocks, context_die, depth + 1);
20232 }
20233
20234 /* Is this a typedef we can avoid emitting? */
20235
20236 static inline int
20237 is_redundant_typedef (const_tree decl)
20238 {
20239 if (TYPE_DECL_IS_STUB (decl))
20240 return 1;
20241
20242 if (DECL_ARTIFICIAL (decl)
20243 && DECL_CONTEXT (decl)
20244 && is_tagged_type (DECL_CONTEXT (decl))
20245 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20246 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20247 /* Also ignore the artificial member typedef for the class name. */
20248 return 1;
20249
20250 return 0;
20251 }
20252
20253 /* Return TRUE if TYPE is a typedef that names a type for linkage
20254 purposes. This kind of typedefs is produced by the C++ FE for
20255 constructs like:
20256
20257 typedef struct {...} foo;
20258
20259 In that case, there is no typedef variant type produced for foo.
20260 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20261 struct type. */
20262
20263 static bool
20264 is_naming_typedef_decl (const_tree decl)
20265 {
20266 if (decl == NULL_TREE
20267 || TREE_CODE (decl) != TYPE_DECL
20268 || !is_tagged_type (TREE_TYPE (decl))
20269 || DECL_IS_BUILTIN (decl)
20270 || is_redundant_typedef (decl)
20271 /* It looks like Ada produces TYPE_DECLs that are very similar
20272 to C++ naming typedefs but that have different
20273 semantics. Let's be specific to c++ for now. */
20274 || !is_cxx ())
20275 return FALSE;
20276
20277 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20278 && TYPE_NAME (TREE_TYPE (decl)) == decl
20279 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20280 != TYPE_NAME (TREE_TYPE (decl))));
20281 }
20282
20283 /* Returns the DIE for a context. */
20284
20285 static inline dw_die_ref
20286 get_context_die (tree context)
20287 {
20288 if (context)
20289 {
20290 /* Find die that represents this context. */
20291 if (TYPE_P (context))
20292 {
20293 context = TYPE_MAIN_VARIANT (context);
20294 return strip_naming_typedef (context, force_type_die (context));
20295 }
20296 else
20297 return force_decl_die (context);
20298 }
20299 return comp_unit_die ();
20300 }
20301
20302 /* Returns the DIE for decl. A DIE will always be returned. */
20303
20304 static dw_die_ref
20305 force_decl_die (tree decl)
20306 {
20307 dw_die_ref decl_die;
20308 unsigned saved_external_flag;
20309 tree save_fn = NULL_TREE;
20310 decl_die = lookup_decl_die (decl);
20311 if (!decl_die)
20312 {
20313 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20314
20315 decl_die = lookup_decl_die (decl);
20316 if (decl_die)
20317 return decl_die;
20318
20319 switch (TREE_CODE (decl))
20320 {
20321 case FUNCTION_DECL:
20322 /* Clear current_function_decl, so that gen_subprogram_die thinks
20323 that this is a declaration. At this point, we just want to force
20324 declaration die. */
20325 save_fn = current_function_decl;
20326 current_function_decl = NULL_TREE;
20327 gen_subprogram_die (decl, context_die);
20328 current_function_decl = save_fn;
20329 break;
20330
20331 case VAR_DECL:
20332 /* Set external flag to force declaration die. Restore it after
20333 gen_decl_die() call. */
20334 saved_external_flag = DECL_EXTERNAL (decl);
20335 DECL_EXTERNAL (decl) = 1;
20336 gen_decl_die (decl, NULL, context_die);
20337 DECL_EXTERNAL (decl) = saved_external_flag;
20338 break;
20339
20340 case NAMESPACE_DECL:
20341 if (dwarf_version >= 3 || !dwarf_strict)
20342 dwarf2out_decl (decl);
20343 else
20344 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20345 decl_die = comp_unit_die ();
20346 break;
20347
20348 case TRANSLATION_UNIT_DECL:
20349 decl_die = comp_unit_die ();
20350 break;
20351
20352 default:
20353 gcc_unreachable ();
20354 }
20355
20356 /* We should be able to find the DIE now. */
20357 if (!decl_die)
20358 decl_die = lookup_decl_die (decl);
20359 gcc_assert (decl_die);
20360 }
20361
20362 return decl_die;
20363 }
20364
20365 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20366 always returned. */
20367
20368 static dw_die_ref
20369 force_type_die (tree type)
20370 {
20371 dw_die_ref type_die;
20372
20373 type_die = lookup_type_die (type);
20374 if (!type_die)
20375 {
20376 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20377
20378 type_die = modified_type_die (type, TYPE_READONLY (type),
20379 TYPE_VOLATILE (type), context_die);
20380 gcc_assert (type_die);
20381 }
20382 return type_die;
20383 }
20384
20385 /* Force out any required namespaces to be able to output DECL,
20386 and return the new context_die for it, if it's changed. */
20387
20388 static dw_die_ref
20389 setup_namespace_context (tree thing, dw_die_ref context_die)
20390 {
20391 tree context = (DECL_P (thing)
20392 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20393 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20394 /* Force out the namespace. */
20395 context_die = force_decl_die (context);
20396
20397 return context_die;
20398 }
20399
20400 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20401 type) within its namespace, if appropriate.
20402
20403 For compatibility with older debuggers, namespace DIEs only contain
20404 declarations; all definitions are emitted at CU scope. */
20405
20406 static dw_die_ref
20407 declare_in_namespace (tree thing, dw_die_ref context_die)
20408 {
20409 dw_die_ref ns_context;
20410
20411 if (debug_info_level <= DINFO_LEVEL_TERSE)
20412 return context_die;
20413
20414 /* If this decl is from an inlined function, then don't try to emit it in its
20415 namespace, as we will get confused. It would have already been emitted
20416 when the abstract instance of the inline function was emitted anyways. */
20417 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20418 return context_die;
20419
20420 ns_context = setup_namespace_context (thing, context_die);
20421
20422 if (ns_context != context_die)
20423 {
20424 if (is_fortran ())
20425 return ns_context;
20426 if (DECL_P (thing))
20427 gen_decl_die (thing, NULL, ns_context);
20428 else
20429 gen_type_die (thing, ns_context);
20430 }
20431 return context_die;
20432 }
20433
20434 /* Generate a DIE for a namespace or namespace alias. */
20435
20436 static void
20437 gen_namespace_die (tree decl, dw_die_ref context_die)
20438 {
20439 dw_die_ref namespace_die;
20440
20441 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20442 they are an alias of. */
20443 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20444 {
20445 /* Output a real namespace or module. */
20446 context_die = setup_namespace_context (decl, comp_unit_die ());
20447 namespace_die = new_die (is_fortran ()
20448 ? DW_TAG_module : DW_TAG_namespace,
20449 context_die, decl);
20450 /* For Fortran modules defined in different CU don't add src coords. */
20451 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20452 {
20453 const char *name = dwarf2_name (decl, 0);
20454 if (name)
20455 add_name_attribute (namespace_die, name);
20456 }
20457 else
20458 add_name_and_src_coords_attributes (namespace_die, decl);
20459 if (DECL_EXTERNAL (decl))
20460 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20461 equate_decl_number_to_die (decl, namespace_die);
20462 }
20463 else
20464 {
20465 /* Output a namespace alias. */
20466
20467 /* Force out the namespace we are an alias of, if necessary. */
20468 dw_die_ref origin_die
20469 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20470
20471 if (DECL_FILE_SCOPE_P (decl)
20472 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20473 context_die = setup_namespace_context (decl, comp_unit_die ());
20474 /* Now create the namespace alias DIE. */
20475 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20476 add_name_and_src_coords_attributes (namespace_die, decl);
20477 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20478 equate_decl_number_to_die (decl, namespace_die);
20479 }
20480 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20481 if (want_pubnames ())
20482 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20483 }
20484
20485 /* Generate Dwarf debug information for a decl described by DECL.
20486 The return value is currently only meaningful for PARM_DECLs,
20487 for all other decls it returns NULL. */
20488
20489 static dw_die_ref
20490 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20491 {
20492 tree decl_or_origin = decl ? decl : origin;
20493 tree class_origin = NULL, ultimate_origin;
20494
20495 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20496 return NULL;
20497
20498 switch (TREE_CODE (decl_or_origin))
20499 {
20500 case ERROR_MARK:
20501 break;
20502
20503 case CONST_DECL:
20504 if (!is_fortran () && !is_ada ())
20505 {
20506 /* The individual enumerators of an enum type get output when we output
20507 the Dwarf representation of the relevant enum type itself. */
20508 break;
20509 }
20510
20511 /* Emit its type. */
20512 gen_type_die (TREE_TYPE (decl), context_die);
20513
20514 /* And its containing namespace. */
20515 context_die = declare_in_namespace (decl, context_die);
20516
20517 gen_const_die (decl, context_die);
20518 break;
20519
20520 case FUNCTION_DECL:
20521 /* Don't output any DIEs to represent mere function declarations,
20522 unless they are class members or explicit block externs. */
20523 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20524 && DECL_FILE_SCOPE_P (decl_or_origin)
20525 && (current_function_decl == NULL_TREE
20526 || DECL_ARTIFICIAL (decl_or_origin)))
20527 break;
20528
20529 #if 0
20530 /* FIXME */
20531 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20532 on local redeclarations of global functions. That seems broken. */
20533 if (current_function_decl != decl)
20534 /* This is only a declaration. */;
20535 #endif
20536
20537 /* If we're emitting a clone, emit info for the abstract instance. */
20538 if (origin || DECL_ORIGIN (decl) != decl)
20539 dwarf2out_abstract_function (origin
20540 ? DECL_ORIGIN (origin)
20541 : DECL_ABSTRACT_ORIGIN (decl));
20542
20543 /* If we're emitting an out-of-line copy of an inline function,
20544 emit info for the abstract instance and set up to refer to it. */
20545 else if (cgraph_function_possibly_inlined_p (decl)
20546 && ! DECL_ABSTRACT (decl)
20547 && ! class_or_namespace_scope_p (context_die)
20548 /* dwarf2out_abstract_function won't emit a die if this is just
20549 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20550 that case, because that works only if we have a die. */
20551 && DECL_INITIAL (decl) != NULL_TREE)
20552 {
20553 dwarf2out_abstract_function (decl);
20554 set_decl_origin_self (decl);
20555 }
20556
20557 /* Otherwise we're emitting the primary DIE for this decl. */
20558 else if (debug_info_level > DINFO_LEVEL_TERSE)
20559 {
20560 /* Before we describe the FUNCTION_DECL itself, make sure that we
20561 have its containing type. */
20562 if (!origin)
20563 origin = decl_class_context (decl);
20564 if (origin != NULL_TREE)
20565 gen_type_die (origin, context_die);
20566
20567 /* And its return type. */
20568 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20569
20570 /* And its virtual context. */
20571 if (DECL_VINDEX (decl) != NULL_TREE)
20572 gen_type_die (DECL_CONTEXT (decl), context_die);
20573
20574 /* Make sure we have a member DIE for decl. */
20575 if (origin != NULL_TREE)
20576 gen_type_die_for_member (origin, decl, context_die);
20577
20578 /* And its containing namespace. */
20579 context_die = declare_in_namespace (decl, context_die);
20580 }
20581
20582 /* Now output a DIE to represent the function itself. */
20583 if (decl)
20584 gen_subprogram_die (decl, context_die);
20585 break;
20586
20587 case TYPE_DECL:
20588 /* If we are in terse mode, don't generate any DIEs to represent any
20589 actual typedefs. */
20590 if (debug_info_level <= DINFO_LEVEL_TERSE)
20591 break;
20592
20593 /* In the special case of a TYPE_DECL node representing the declaration
20594 of some type tag, if the given TYPE_DECL is marked as having been
20595 instantiated from some other (original) TYPE_DECL node (e.g. one which
20596 was generated within the original definition of an inline function) we
20597 used to generate a special (abbreviated) DW_TAG_structure_type,
20598 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20599 should be actually referencing those DIEs, as variable DIEs with that
20600 type would be emitted already in the abstract origin, so it was always
20601 removed during unused type prunning. Don't add anything in this
20602 case. */
20603 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20604 break;
20605
20606 if (is_redundant_typedef (decl))
20607 gen_type_die (TREE_TYPE (decl), context_die);
20608 else
20609 /* Output a DIE to represent the typedef itself. */
20610 gen_typedef_die (decl, context_die);
20611 break;
20612
20613 case LABEL_DECL:
20614 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20615 gen_label_die (decl, context_die);
20616 break;
20617
20618 case VAR_DECL:
20619 case RESULT_DECL:
20620 /* If we are in terse mode, don't generate any DIEs to represent any
20621 variable declarations or definitions. */
20622 if (debug_info_level <= DINFO_LEVEL_TERSE)
20623 break;
20624
20625 /* Output any DIEs that are needed to specify the type of this data
20626 object. */
20627 if (decl_by_reference_p (decl_or_origin))
20628 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20629 else
20630 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20631
20632 /* And its containing type. */
20633 class_origin = decl_class_context (decl_or_origin);
20634 if (class_origin != NULL_TREE)
20635 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20636
20637 /* And its containing namespace. */
20638 context_die = declare_in_namespace (decl_or_origin, context_die);
20639
20640 /* Now output the DIE to represent the data object itself. This gets
20641 complicated because of the possibility that the VAR_DECL really
20642 represents an inlined instance of a formal parameter for an inline
20643 function. */
20644 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20645 if (ultimate_origin != NULL_TREE
20646 && TREE_CODE (ultimate_origin) == PARM_DECL)
20647 gen_formal_parameter_die (decl, origin,
20648 true /* Emit name attribute. */,
20649 context_die);
20650 else
20651 gen_variable_die (decl, origin, context_die);
20652 break;
20653
20654 case FIELD_DECL:
20655 /* Ignore the nameless fields that are used to skip bits but handle C++
20656 anonymous unions and structs. */
20657 if (DECL_NAME (decl) != NULL_TREE
20658 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20659 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20660 {
20661 gen_type_die (member_declared_type (decl), context_die);
20662 gen_field_die (decl, context_die);
20663 }
20664 break;
20665
20666 case PARM_DECL:
20667 if (DECL_BY_REFERENCE (decl_or_origin))
20668 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20669 else
20670 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20671 return gen_formal_parameter_die (decl, origin,
20672 true /* Emit name attribute. */,
20673 context_die);
20674
20675 case NAMESPACE_DECL:
20676 case IMPORTED_DECL:
20677 if (dwarf_version >= 3 || !dwarf_strict)
20678 gen_namespace_die (decl, context_die);
20679 break;
20680
20681 case NAMELIST_DECL:
20682 gen_namelist_decl (DECL_NAME (decl), context_die,
20683 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20684 break;
20685
20686 default:
20687 /* Probably some frontend-internal decl. Assume we don't care. */
20688 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20689 break;
20690 }
20691
20692 return NULL;
20693 }
20694 \f
20695 /* Output debug information for global decl DECL. Called from toplev.c after
20696 compilation proper has finished. */
20697
20698 static void
20699 dwarf2out_global_decl (tree decl)
20700 {
20701 /* Output DWARF2 information for file-scope tentative data object
20702 declarations, file-scope (extern) function declarations (which
20703 had no corresponding body) and file-scope tagged type declarations
20704 and definitions which have not yet been forced out. */
20705 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20706 dwarf2out_decl (decl);
20707 }
20708
20709 /* Output debug information for type decl DECL. Called from toplev.c
20710 and from language front ends (to record built-in types). */
20711 static void
20712 dwarf2out_type_decl (tree decl, int local)
20713 {
20714 if (!local)
20715 dwarf2out_decl (decl);
20716 }
20717
20718 /* Output debug information for imported module or decl DECL.
20719 NAME is non-NULL name in the lexical block if the decl has been renamed.
20720 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20721 that DECL belongs to.
20722 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20723 static void
20724 dwarf2out_imported_module_or_decl_1 (tree decl,
20725 tree name,
20726 tree lexical_block,
20727 dw_die_ref lexical_block_die)
20728 {
20729 expanded_location xloc;
20730 dw_die_ref imported_die = NULL;
20731 dw_die_ref at_import_die;
20732
20733 if (TREE_CODE (decl) == IMPORTED_DECL)
20734 {
20735 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20736 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20737 gcc_assert (decl);
20738 }
20739 else
20740 xloc = expand_location (input_location);
20741
20742 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20743 {
20744 at_import_die = force_type_die (TREE_TYPE (decl));
20745 /* For namespace N { typedef void T; } using N::T; base_type_die
20746 returns NULL, but DW_TAG_imported_declaration requires
20747 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20748 if (!at_import_die)
20749 {
20750 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20751 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20752 at_import_die = lookup_type_die (TREE_TYPE (decl));
20753 gcc_assert (at_import_die);
20754 }
20755 }
20756 else
20757 {
20758 at_import_die = lookup_decl_die (decl);
20759 if (!at_import_die)
20760 {
20761 /* If we're trying to avoid duplicate debug info, we may not have
20762 emitted the member decl for this field. Emit it now. */
20763 if (TREE_CODE (decl) == FIELD_DECL)
20764 {
20765 tree type = DECL_CONTEXT (decl);
20766
20767 if (TYPE_CONTEXT (type)
20768 && TYPE_P (TYPE_CONTEXT (type))
20769 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20770 DINFO_USAGE_DIR_USE))
20771 return;
20772 gen_type_die_for_member (type, decl,
20773 get_context_die (TYPE_CONTEXT (type)));
20774 }
20775 if (TREE_CODE (decl) == NAMELIST_DECL)
20776 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20777 get_context_die (DECL_CONTEXT (decl)),
20778 NULL_TREE);
20779 else
20780 at_import_die = force_decl_die (decl);
20781 }
20782 }
20783
20784 if (TREE_CODE (decl) == NAMESPACE_DECL)
20785 {
20786 if (dwarf_version >= 3 || !dwarf_strict)
20787 imported_die = new_die (DW_TAG_imported_module,
20788 lexical_block_die,
20789 lexical_block);
20790 else
20791 return;
20792 }
20793 else
20794 imported_die = new_die (DW_TAG_imported_declaration,
20795 lexical_block_die,
20796 lexical_block);
20797
20798 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20799 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20800 if (name)
20801 add_AT_string (imported_die, DW_AT_name,
20802 IDENTIFIER_POINTER (name));
20803 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20804 }
20805
20806 /* Output debug information for imported module or decl DECL.
20807 NAME is non-NULL name in context if the decl has been renamed.
20808 CHILD is true if decl is one of the renamed decls as part of
20809 importing whole module. */
20810
20811 static void
20812 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20813 bool child)
20814 {
20815 /* dw_die_ref at_import_die; */
20816 dw_die_ref scope_die;
20817
20818 if (debug_info_level <= DINFO_LEVEL_TERSE)
20819 return;
20820
20821 gcc_assert (decl);
20822
20823 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20824 We need decl DIE for reference and scope die. First, get DIE for the decl
20825 itself. */
20826
20827 /* Get the scope die for decl context. Use comp_unit_die for global module
20828 or decl. If die is not found for non globals, force new die. */
20829 if (context
20830 && TYPE_P (context)
20831 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20832 return;
20833
20834 if (!(dwarf_version >= 3 || !dwarf_strict))
20835 return;
20836
20837 scope_die = get_context_die (context);
20838
20839 if (child)
20840 {
20841 gcc_assert (scope_die->die_child);
20842 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20843 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20844 scope_die = scope_die->die_child;
20845 }
20846
20847 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20848 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20849
20850 }
20851
20852 /* Output debug information for namelists. */
20853
20854 static dw_die_ref
20855 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20856 {
20857 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20858 tree value;
20859 unsigned i;
20860
20861 if (debug_info_level <= DINFO_LEVEL_TERSE)
20862 return NULL;
20863
20864 gcc_assert (scope_die != NULL);
20865 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20866 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20867
20868 /* If there are no item_decls, we have a nondefining namelist, e.g.
20869 with USE association; hence, set DW_AT_declaration. */
20870 if (item_decls == NULL_TREE)
20871 {
20872 add_AT_flag (nml_die, DW_AT_declaration, 1);
20873 return nml_die;
20874 }
20875
20876 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20877 {
20878 nml_item_ref_die = lookup_decl_die (value);
20879 if (!nml_item_ref_die)
20880 nml_item_ref_die = force_decl_die (value);
20881
20882 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20883 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20884 }
20885 return nml_die;
20886 }
20887
20888
20889 /* Write the debugging output for DECL. */
20890
20891 void
20892 dwarf2out_decl (tree decl)
20893 {
20894 dw_die_ref context_die = comp_unit_die ();
20895
20896 switch (TREE_CODE (decl))
20897 {
20898 case ERROR_MARK:
20899 return;
20900
20901 case FUNCTION_DECL:
20902 /* What we would really like to do here is to filter out all mere
20903 file-scope declarations of file-scope functions which are never
20904 referenced later within this translation unit (and keep all of ones
20905 that *are* referenced later on) but we aren't clairvoyant, so we have
20906 no idea which functions will be referenced in the future (i.e. later
20907 on within the current translation unit). So here we just ignore all
20908 file-scope function declarations which are not also definitions. If
20909 and when the debugger needs to know something about these functions,
20910 it will have to hunt around and find the DWARF information associated
20911 with the definition of the function.
20912
20913 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20914 nodes represent definitions and which ones represent mere
20915 declarations. We have to check DECL_INITIAL instead. That's because
20916 the C front-end supports some weird semantics for "extern inline"
20917 function definitions. These can get inlined within the current
20918 translation unit (and thus, we need to generate Dwarf info for their
20919 abstract instances so that the Dwarf info for the concrete inlined
20920 instances can have something to refer to) but the compiler never
20921 generates any out-of-lines instances of such things (despite the fact
20922 that they *are* definitions).
20923
20924 The important point is that the C front-end marks these "extern
20925 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20926 them anyway. Note that the C++ front-end also plays some similar games
20927 for inline function definitions appearing within include files which
20928 also contain `#pragma interface' pragmas.
20929
20930 If we are called from dwarf2out_abstract_function output a DIE
20931 anyway. We can end up here this way with early inlining and LTO
20932 where the inlined function is output in a different LTRANS unit
20933 or not at all. */
20934 if (DECL_INITIAL (decl) == NULL_TREE
20935 && ! DECL_ABSTRACT (decl))
20936 return;
20937
20938 /* If we're a nested function, initially use a parent of NULL; if we're
20939 a plain function, this will be fixed up in decls_for_scope. If
20940 we're a method, it will be ignored, since we already have a DIE. */
20941 if (decl_function_context (decl)
20942 /* But if we're in terse mode, we don't care about scope. */
20943 && debug_info_level > DINFO_LEVEL_TERSE)
20944 context_die = NULL;
20945 break;
20946
20947 case VAR_DECL:
20948 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20949 declaration and if the declaration was never even referenced from
20950 within this entire compilation unit. We suppress these DIEs in
20951 order to save space in the .debug section (by eliminating entries
20952 which are probably useless). Note that we must not suppress
20953 block-local extern declarations (whether used or not) because that
20954 would screw-up the debugger's name lookup mechanism and cause it to
20955 miss things which really ought to be in scope at a given point. */
20956 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20957 return;
20958
20959 /* For local statics lookup proper context die. */
20960 if (TREE_STATIC (decl)
20961 && DECL_CONTEXT (decl)
20962 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20963 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20964
20965 /* If we are in terse mode, don't generate any DIEs to represent any
20966 variable declarations or definitions. */
20967 if (debug_info_level <= DINFO_LEVEL_TERSE)
20968 return;
20969 break;
20970
20971 case CONST_DECL:
20972 if (debug_info_level <= DINFO_LEVEL_TERSE)
20973 return;
20974 if (!is_fortran () && !is_ada ())
20975 return;
20976 if (TREE_STATIC (decl) && decl_function_context (decl))
20977 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20978 break;
20979
20980 case NAMESPACE_DECL:
20981 case IMPORTED_DECL:
20982 if (debug_info_level <= DINFO_LEVEL_TERSE)
20983 return;
20984 if (lookup_decl_die (decl) != NULL)
20985 return;
20986 break;
20987
20988 case TYPE_DECL:
20989 /* Don't emit stubs for types unless they are needed by other DIEs. */
20990 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20991 return;
20992
20993 /* Don't bother trying to generate any DIEs to represent any of the
20994 normal built-in types for the language we are compiling. */
20995 if (DECL_IS_BUILTIN (decl))
20996 return;
20997
20998 /* If we are in terse mode, don't generate any DIEs for types. */
20999 if (debug_info_level <= DINFO_LEVEL_TERSE)
21000 return;
21001
21002 /* If we're a function-scope tag, initially use a parent of NULL;
21003 this will be fixed up in decls_for_scope. */
21004 if (decl_function_context (decl))
21005 context_die = NULL;
21006
21007 break;
21008
21009 case NAMELIST_DECL:
21010 break;
21011
21012 default:
21013 return;
21014 }
21015
21016 gen_decl_die (decl, NULL, context_die);
21017 }
21018
21019 /* Write the debugging output for DECL. */
21020
21021 static void
21022 dwarf2out_function_decl (tree decl)
21023 {
21024 dwarf2out_decl (decl);
21025 call_arg_locations = NULL;
21026 call_arg_loc_last = NULL;
21027 call_site_count = -1;
21028 tail_call_site_count = -1;
21029 block_map.release ();
21030 htab_empty (decl_loc_table);
21031 htab_empty (cached_dw_loc_list_table);
21032 }
21033
21034 /* Output a marker (i.e. a label) for the beginning of the generated code for
21035 a lexical block. */
21036
21037 static void
21038 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21039 unsigned int blocknum)
21040 {
21041 switch_to_section (current_function_section ());
21042 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21043 }
21044
21045 /* Output a marker (i.e. a label) for the end of the generated code for a
21046 lexical block. */
21047
21048 static void
21049 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21050 {
21051 switch_to_section (current_function_section ());
21052 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21053 }
21054
21055 /* Returns nonzero if it is appropriate not to emit any debugging
21056 information for BLOCK, because it doesn't contain any instructions.
21057
21058 Don't allow this for blocks with nested functions or local classes
21059 as we would end up with orphans, and in the presence of scheduling
21060 we may end up calling them anyway. */
21061
21062 static bool
21063 dwarf2out_ignore_block (const_tree block)
21064 {
21065 tree decl;
21066 unsigned int i;
21067
21068 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21069 if (TREE_CODE (decl) == FUNCTION_DECL
21070 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21071 return 0;
21072 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21073 {
21074 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21075 if (TREE_CODE (decl) == FUNCTION_DECL
21076 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21077 return 0;
21078 }
21079
21080 return 1;
21081 }
21082
21083 /* Hash table routines for file_hash. */
21084
21085 static int
21086 file_table_eq (const void *p1_p, const void *p2_p)
21087 {
21088 const struct dwarf_file_data *const p1 =
21089 (const struct dwarf_file_data *) p1_p;
21090 const char *const p2 = (const char *) p2_p;
21091 return filename_cmp (p1->filename, p2) == 0;
21092 }
21093
21094 static hashval_t
21095 file_table_hash (const void *p_p)
21096 {
21097 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21098 return htab_hash_string (p->filename);
21099 }
21100
21101 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21102 dwarf2out.c) and return its "index". The index of each (known) filename is
21103 just a unique number which is associated with only that one filename. We
21104 need such numbers for the sake of generating labels (in the .debug_sfnames
21105 section) and references to those files numbers (in the .debug_srcinfo
21106 and.debug_macinfo sections). If the filename given as an argument is not
21107 found in our current list, add it to the list and assign it the next
21108 available unique index number. In order to speed up searches, we remember
21109 the index of the filename was looked up last. This handles the majority of
21110 all searches. */
21111
21112 static struct dwarf_file_data *
21113 lookup_filename (const char *file_name)
21114 {
21115 void ** slot;
21116 struct dwarf_file_data * created;
21117
21118 /* Check to see if the file name that was searched on the previous
21119 call matches this file name. If so, return the index. */
21120 if (file_table_last_lookup
21121 && (file_name == file_table_last_lookup->filename
21122 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21123 return file_table_last_lookup;
21124
21125 /* Didn't match the previous lookup, search the table. */
21126 slot = htab_find_slot_with_hash (file_table, file_name,
21127 htab_hash_string (file_name), INSERT);
21128 if (*slot)
21129 return (struct dwarf_file_data *) *slot;
21130
21131 created = ggc_alloc<dwarf_file_data> ();
21132 created->filename = file_name;
21133 created->emitted_number = 0;
21134 *slot = created;
21135 return created;
21136 }
21137
21138 /* If the assembler will construct the file table, then translate the compiler
21139 internal file table number into the assembler file table number, and emit
21140 a .file directive if we haven't already emitted one yet. The file table
21141 numbers are different because we prune debug info for unused variables and
21142 types, which may include filenames. */
21143
21144 static int
21145 maybe_emit_file (struct dwarf_file_data * fd)
21146 {
21147 if (! fd->emitted_number)
21148 {
21149 if (last_emitted_file)
21150 fd->emitted_number = last_emitted_file->emitted_number + 1;
21151 else
21152 fd->emitted_number = 1;
21153 last_emitted_file = fd;
21154
21155 if (DWARF2_ASM_LINE_DEBUG_INFO)
21156 {
21157 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21158 output_quoted_string (asm_out_file,
21159 remap_debug_filename (fd->filename));
21160 fputc ('\n', asm_out_file);
21161 }
21162 }
21163
21164 return fd->emitted_number;
21165 }
21166
21167 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21168 That generation should happen after function debug info has been
21169 generated. The value of the attribute is the constant value of ARG. */
21170
21171 static void
21172 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21173 {
21174 die_arg_entry entry;
21175
21176 if (!die || !arg)
21177 return;
21178
21179 if (!tmpl_value_parm_die_table)
21180 vec_alloc (tmpl_value_parm_die_table, 32);
21181
21182 entry.die = die;
21183 entry.arg = arg;
21184 vec_safe_push (tmpl_value_parm_die_table, entry);
21185 }
21186
21187 /* Return TRUE if T is an instance of generic type, FALSE
21188 otherwise. */
21189
21190 static bool
21191 generic_type_p (tree t)
21192 {
21193 if (t == NULL_TREE || !TYPE_P (t))
21194 return false;
21195 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21196 }
21197
21198 /* Schedule the generation of the generic parameter dies for the
21199 instance of generic type T. The proper generation itself is later
21200 done by gen_scheduled_generic_parms_dies. */
21201
21202 static void
21203 schedule_generic_params_dies_gen (tree t)
21204 {
21205 if (!generic_type_p (t))
21206 return;
21207
21208 if (!generic_type_instances)
21209 vec_alloc (generic_type_instances, 256);
21210
21211 vec_safe_push (generic_type_instances, t);
21212 }
21213
21214 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21215 by append_entry_to_tmpl_value_parm_die_table. This function must
21216 be called after function DIEs have been generated. */
21217
21218 static void
21219 gen_remaining_tmpl_value_param_die_attribute (void)
21220 {
21221 if (tmpl_value_parm_die_table)
21222 {
21223 unsigned i;
21224 die_arg_entry *e;
21225
21226 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21227 tree_add_const_value_attribute (e->die, e->arg);
21228 }
21229 }
21230
21231 /* Generate generic parameters DIEs for instances of generic types
21232 that have been previously scheduled by
21233 schedule_generic_params_dies_gen. This function must be called
21234 after all the types of the CU have been laid out. */
21235
21236 static void
21237 gen_scheduled_generic_parms_dies (void)
21238 {
21239 unsigned i;
21240 tree t;
21241
21242 if (!generic_type_instances)
21243 return;
21244
21245 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21246 if (COMPLETE_TYPE_P (t))
21247 gen_generic_params_dies (t);
21248 }
21249
21250
21251 /* Replace DW_AT_name for the decl with name. */
21252
21253 static void
21254 dwarf2out_set_name (tree decl, tree name)
21255 {
21256 dw_die_ref die;
21257 dw_attr_ref attr;
21258 const char *dname;
21259
21260 die = TYPE_SYMTAB_DIE (decl);
21261 if (!die)
21262 return;
21263
21264 dname = dwarf2_name (name, 0);
21265 if (!dname)
21266 return;
21267
21268 attr = get_AT (die, DW_AT_name);
21269 if (attr)
21270 {
21271 struct indirect_string_node *node;
21272
21273 node = find_AT_string (dname);
21274 /* replace the string. */
21275 attr->dw_attr_val.v.val_str = node;
21276 }
21277
21278 else
21279 add_name_attribute (die, dname);
21280 }
21281
21282 /* True if before or during processing of the first function being emitted. */
21283 static bool in_first_function_p = true;
21284 /* True if loc_note during dwarf2out_var_location call might still be
21285 before first real instruction at address equal to .Ltext0. */
21286 static bool maybe_at_text_label_p = true;
21287 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21288 static unsigned int first_loclabel_num_not_at_text_label;
21289
21290 /* Called by the final INSN scan whenever we see a var location. We
21291 use it to drop labels in the right places, and throw the location in
21292 our lookup table. */
21293
21294 static void
21295 dwarf2out_var_location (rtx loc_note)
21296 {
21297 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21298 struct var_loc_node *newloc;
21299 rtx next_real, next_note;
21300 static const char *last_label;
21301 static const char *last_postcall_label;
21302 static bool last_in_cold_section_p;
21303 static rtx expected_next_loc_note;
21304 tree decl;
21305 bool var_loc_p;
21306
21307 if (!NOTE_P (loc_note))
21308 {
21309 if (CALL_P (loc_note))
21310 {
21311 call_site_count++;
21312 if (SIBLING_CALL_P (loc_note))
21313 tail_call_site_count++;
21314 }
21315 return;
21316 }
21317
21318 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21319 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21320 return;
21321
21322 /* Optimize processing a large consecutive sequence of location
21323 notes so we don't spend too much time in next_real_insn. If the
21324 next insn is another location note, remember the next_real_insn
21325 calculation for next time. */
21326 next_real = cached_next_real_insn;
21327 if (next_real)
21328 {
21329 if (expected_next_loc_note != loc_note)
21330 next_real = NULL_RTX;
21331 }
21332
21333 next_note = NEXT_INSN (loc_note);
21334 if (! next_note
21335 || INSN_DELETED_P (next_note)
21336 || ! NOTE_P (next_note)
21337 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21338 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21339 next_note = NULL_RTX;
21340
21341 if (! next_real)
21342 next_real = next_real_insn (loc_note);
21343
21344 if (next_note)
21345 {
21346 expected_next_loc_note = next_note;
21347 cached_next_real_insn = next_real;
21348 }
21349 else
21350 cached_next_real_insn = NULL_RTX;
21351
21352 /* If there are no instructions which would be affected by this note,
21353 don't do anything. */
21354 if (var_loc_p
21355 && next_real == NULL_RTX
21356 && !NOTE_DURING_CALL_P (loc_note))
21357 return;
21358
21359 if (next_real == NULL_RTX)
21360 next_real = get_last_insn ();
21361
21362 /* If there were any real insns between note we processed last time
21363 and this note (or if it is the first note), clear
21364 last_{,postcall_}label so that they are not reused this time. */
21365 if (last_var_location_insn == NULL_RTX
21366 || last_var_location_insn != next_real
21367 || last_in_cold_section_p != in_cold_section_p)
21368 {
21369 last_label = NULL;
21370 last_postcall_label = NULL;
21371 }
21372
21373 if (var_loc_p)
21374 {
21375 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21376 newloc = add_var_loc_to_decl (decl, loc_note,
21377 NOTE_DURING_CALL_P (loc_note)
21378 ? last_postcall_label : last_label);
21379 if (newloc == NULL)
21380 return;
21381 }
21382 else
21383 {
21384 decl = NULL_TREE;
21385 newloc = NULL;
21386 }
21387
21388 /* If there were no real insns between note we processed last time
21389 and this note, use the label we emitted last time. Otherwise
21390 create a new label and emit it. */
21391 if (last_label == NULL)
21392 {
21393 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21394 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21395 loclabel_num++;
21396 last_label = ggc_strdup (loclabel);
21397 /* See if loclabel might be equal to .Ltext0. If yes,
21398 bump first_loclabel_num_not_at_text_label. */
21399 if (!have_multiple_function_sections
21400 && in_first_function_p
21401 && maybe_at_text_label_p)
21402 {
21403 static rtx last_start;
21404 rtx insn;
21405 for (insn = loc_note; insn; insn = previous_insn (insn))
21406 if (insn == last_start)
21407 break;
21408 else if (!NONDEBUG_INSN_P (insn))
21409 continue;
21410 else
21411 {
21412 rtx body = PATTERN (insn);
21413 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21414 continue;
21415 /* Inline asm could occupy zero bytes. */
21416 else if (GET_CODE (body) == ASM_INPUT
21417 || asm_noperands (body) >= 0)
21418 continue;
21419 #ifdef HAVE_attr_length
21420 else if (get_attr_min_length (insn) == 0)
21421 continue;
21422 #endif
21423 else
21424 {
21425 /* Assume insn has non-zero length. */
21426 maybe_at_text_label_p = false;
21427 break;
21428 }
21429 }
21430 if (maybe_at_text_label_p)
21431 {
21432 last_start = loc_note;
21433 first_loclabel_num_not_at_text_label = loclabel_num;
21434 }
21435 }
21436 }
21437
21438 if (!var_loc_p)
21439 {
21440 struct call_arg_loc_node *ca_loc
21441 = ggc_cleared_alloc<call_arg_loc_node> ();
21442 rtx prev = prev_real_insn (loc_note), x;
21443 ca_loc->call_arg_loc_note = loc_note;
21444 ca_loc->next = NULL;
21445 ca_loc->label = last_label;
21446 gcc_assert (prev
21447 && (CALL_P (prev)
21448 || (NONJUMP_INSN_P (prev)
21449 && GET_CODE (PATTERN (prev)) == SEQUENCE
21450 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21451 if (!CALL_P (prev))
21452 prev = XVECEXP (PATTERN (prev), 0, 0);
21453 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21454 x = get_call_rtx_from (PATTERN (prev));
21455 if (x)
21456 {
21457 x = XEXP (XEXP (x, 0), 0);
21458 if (GET_CODE (x) == SYMBOL_REF
21459 && SYMBOL_REF_DECL (x)
21460 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21461 ca_loc->symbol_ref = x;
21462 }
21463 ca_loc->block = insn_scope (prev);
21464 if (call_arg_locations)
21465 call_arg_loc_last->next = ca_loc;
21466 else
21467 call_arg_locations = ca_loc;
21468 call_arg_loc_last = ca_loc;
21469 }
21470 else if (!NOTE_DURING_CALL_P (loc_note))
21471 newloc->label = last_label;
21472 else
21473 {
21474 if (!last_postcall_label)
21475 {
21476 sprintf (loclabel, "%s-1", last_label);
21477 last_postcall_label = ggc_strdup (loclabel);
21478 }
21479 newloc->label = last_postcall_label;
21480 }
21481
21482 last_var_location_insn = next_real;
21483 last_in_cold_section_p = in_cold_section_p;
21484 }
21485
21486 /* Note in one location list that text section has changed. */
21487
21488 static int
21489 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21490 {
21491 var_loc_list *list = (var_loc_list *) *slot;
21492 if (list->first)
21493 list->last_before_switch
21494 = list->last->next ? list->last->next : list->last;
21495 return 1;
21496 }
21497
21498 /* Note in all location lists that text section has changed. */
21499
21500 static void
21501 var_location_switch_text_section (void)
21502 {
21503 if (decl_loc_table == NULL)
21504 return;
21505
21506 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21507 }
21508
21509 /* Create a new line number table. */
21510
21511 static dw_line_info_table *
21512 new_line_info_table (void)
21513 {
21514 dw_line_info_table *table;
21515
21516 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21517 table->file_num = 1;
21518 table->line_num = 1;
21519 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21520
21521 return table;
21522 }
21523
21524 /* Lookup the "current" table into which we emit line info, so
21525 that we don't have to do it for every source line. */
21526
21527 static void
21528 set_cur_line_info_table (section *sec)
21529 {
21530 dw_line_info_table *table;
21531
21532 if (sec == text_section)
21533 table = text_section_line_info;
21534 else if (sec == cold_text_section)
21535 {
21536 table = cold_text_section_line_info;
21537 if (!table)
21538 {
21539 cold_text_section_line_info = table = new_line_info_table ();
21540 table->end_label = cold_end_label;
21541 }
21542 }
21543 else
21544 {
21545 const char *end_label;
21546
21547 if (flag_reorder_blocks_and_partition)
21548 {
21549 if (in_cold_section_p)
21550 end_label = crtl->subsections.cold_section_end_label;
21551 else
21552 end_label = crtl->subsections.hot_section_end_label;
21553 }
21554 else
21555 {
21556 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21557 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21558 current_function_funcdef_no);
21559 end_label = ggc_strdup (label);
21560 }
21561
21562 table = new_line_info_table ();
21563 table->end_label = end_label;
21564
21565 vec_safe_push (separate_line_info, table);
21566 }
21567
21568 if (DWARF2_ASM_LINE_DEBUG_INFO)
21569 table->is_stmt = (cur_line_info_table
21570 ? cur_line_info_table->is_stmt
21571 : DWARF_LINE_DEFAULT_IS_STMT_START);
21572 cur_line_info_table = table;
21573 }
21574
21575
21576 /* We need to reset the locations at the beginning of each
21577 function. We can't do this in the end_function hook, because the
21578 declarations that use the locations won't have been output when
21579 that hook is called. Also compute have_multiple_function_sections here. */
21580
21581 static void
21582 dwarf2out_begin_function (tree fun)
21583 {
21584 section *sec = function_section (fun);
21585
21586 if (sec != text_section)
21587 have_multiple_function_sections = true;
21588
21589 if (flag_reorder_blocks_and_partition && !cold_text_section)
21590 {
21591 gcc_assert (current_function_decl == fun);
21592 cold_text_section = unlikely_text_section ();
21593 switch_to_section (cold_text_section);
21594 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21595 switch_to_section (sec);
21596 }
21597
21598 dwarf2out_note_section_used ();
21599 call_site_count = 0;
21600 tail_call_site_count = 0;
21601
21602 set_cur_line_info_table (sec);
21603 }
21604
21605 /* Helper function of dwarf2out_end_function, called only after emitting
21606 the very first function into assembly. Check if some .debug_loc range
21607 might end with a .LVL* label that could be equal to .Ltext0.
21608 In that case we must force using absolute addresses in .debug_loc ranges,
21609 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21610 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21611 list terminator.
21612 Set have_multiple_function_sections to true in that case and
21613 terminate htab traversal. */
21614
21615 static int
21616 find_empty_loc_ranges_at_text_label (void **slot, void *)
21617 {
21618 var_loc_list *entry;
21619 struct var_loc_node *node;
21620
21621 entry = (var_loc_list *) *slot;
21622 node = entry->first;
21623 if (node && node->next && node->next->label)
21624 {
21625 unsigned int i;
21626 const char *label = node->next->label;
21627 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21628
21629 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21630 {
21631 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21632 if (strcmp (label, loclabel) == 0)
21633 {
21634 have_multiple_function_sections = true;
21635 return 0;
21636 }
21637 }
21638 }
21639 return 1;
21640 }
21641
21642 /* Hook called after emitting a function into assembly.
21643 This does something only for the very first function emitted. */
21644
21645 static void
21646 dwarf2out_end_function (unsigned int)
21647 {
21648 if (in_first_function_p
21649 && !have_multiple_function_sections
21650 && first_loclabel_num_not_at_text_label
21651 && decl_loc_table)
21652 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21653 NULL);
21654 in_first_function_p = false;
21655 maybe_at_text_label_p = false;
21656 }
21657
21658 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21659
21660 static void
21661 push_dw_line_info_entry (dw_line_info_table *table,
21662 enum dw_line_info_opcode opcode, unsigned int val)
21663 {
21664 dw_line_info_entry e;
21665 e.opcode = opcode;
21666 e.val = val;
21667 vec_safe_push (table->entries, e);
21668 }
21669
21670 /* Output a label to mark the beginning of a source code line entry
21671 and record information relating to this source line, in
21672 'line_info_table' for later output of the .debug_line section. */
21673 /* ??? The discriminator parameter ought to be unsigned. */
21674
21675 static void
21676 dwarf2out_source_line (unsigned int line, const char *filename,
21677 int discriminator, bool is_stmt)
21678 {
21679 unsigned int file_num;
21680 dw_line_info_table *table;
21681
21682 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21683 return;
21684
21685 /* The discriminator column was added in dwarf4. Simplify the below
21686 by simply removing it if we're not supposed to output it. */
21687 if (dwarf_version < 4 && dwarf_strict)
21688 discriminator = 0;
21689
21690 table = cur_line_info_table;
21691 file_num = maybe_emit_file (lookup_filename (filename));
21692
21693 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21694 the debugger has used the second (possibly duplicate) line number
21695 at the beginning of the function to mark the end of the prologue.
21696 We could eliminate any other duplicates within the function. For
21697 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21698 that second line number entry. */
21699 /* Recall that this end-of-prologue indication is *not* the same thing
21700 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21701 to which the hook corresponds, follows the last insn that was
21702 emitted by gen_prologue. What we need is to precede the first insn
21703 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21704 insn that corresponds to something the user wrote. These may be
21705 very different locations once scheduling is enabled. */
21706
21707 if (0 && file_num == table->file_num
21708 && line == table->line_num
21709 && discriminator == table->discrim_num
21710 && is_stmt == table->is_stmt)
21711 return;
21712
21713 switch_to_section (current_function_section ());
21714
21715 /* If requested, emit something human-readable. */
21716 if (flag_debug_asm)
21717 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21718
21719 if (DWARF2_ASM_LINE_DEBUG_INFO)
21720 {
21721 /* Emit the .loc directive understood by GNU as. */
21722 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21723 file_num, line, is_stmt, discriminator */
21724 fputs ("\t.loc ", asm_out_file);
21725 fprint_ul (asm_out_file, file_num);
21726 putc (' ', asm_out_file);
21727 fprint_ul (asm_out_file, line);
21728 putc (' ', asm_out_file);
21729 putc ('0', asm_out_file);
21730
21731 if (is_stmt != table->is_stmt)
21732 {
21733 fputs (" is_stmt ", asm_out_file);
21734 putc (is_stmt ? '1' : '0', asm_out_file);
21735 }
21736 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21737 {
21738 gcc_assert (discriminator > 0);
21739 fputs (" discriminator ", asm_out_file);
21740 fprint_ul (asm_out_file, (unsigned long) discriminator);
21741 }
21742 putc ('\n', asm_out_file);
21743 }
21744 else
21745 {
21746 unsigned int label_num = ++line_info_label_num;
21747
21748 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21749
21750 push_dw_line_info_entry (table, LI_set_address, label_num);
21751 if (file_num != table->file_num)
21752 push_dw_line_info_entry (table, LI_set_file, file_num);
21753 if (discriminator != table->discrim_num)
21754 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21755 if (is_stmt != table->is_stmt)
21756 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21757 push_dw_line_info_entry (table, LI_set_line, line);
21758 }
21759
21760 table->file_num = file_num;
21761 table->line_num = line;
21762 table->discrim_num = discriminator;
21763 table->is_stmt = is_stmt;
21764 table->in_use = true;
21765 }
21766
21767 /* Record the beginning of a new source file. */
21768
21769 static void
21770 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21771 {
21772 if (flag_eliminate_dwarf2_dups)
21773 {
21774 /* Record the beginning of the file for break_out_includes. */
21775 dw_die_ref bincl_die;
21776
21777 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21778 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21779 }
21780
21781 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21782 {
21783 macinfo_entry e;
21784 e.code = DW_MACINFO_start_file;
21785 e.lineno = lineno;
21786 e.info = ggc_strdup (filename);
21787 vec_safe_push (macinfo_table, e);
21788 }
21789 }
21790
21791 /* Record the end of a source file. */
21792
21793 static void
21794 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21795 {
21796 if (flag_eliminate_dwarf2_dups)
21797 /* Record the end of the file for break_out_includes. */
21798 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21799
21800 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21801 {
21802 macinfo_entry e;
21803 e.code = DW_MACINFO_end_file;
21804 e.lineno = lineno;
21805 e.info = NULL;
21806 vec_safe_push (macinfo_table, e);
21807 }
21808 }
21809
21810 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21811 the tail part of the directive line, i.e. the part which is past the
21812 initial whitespace, #, whitespace, directive-name, whitespace part. */
21813
21814 static void
21815 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21816 const char *buffer ATTRIBUTE_UNUSED)
21817 {
21818 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21819 {
21820 macinfo_entry e;
21821 /* Insert a dummy first entry to be able to optimize the whole
21822 predefined macro block using DW_MACRO_GNU_transparent_include. */
21823 if (macinfo_table->is_empty () && lineno <= 1)
21824 {
21825 e.code = 0;
21826 e.lineno = 0;
21827 e.info = NULL;
21828 vec_safe_push (macinfo_table, e);
21829 }
21830 e.code = DW_MACINFO_define;
21831 e.lineno = lineno;
21832 e.info = ggc_strdup (buffer);
21833 vec_safe_push (macinfo_table, e);
21834 }
21835 }
21836
21837 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21838 the tail part of the directive line, i.e. the part which is past the
21839 initial whitespace, #, whitespace, directive-name, whitespace part. */
21840
21841 static void
21842 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21843 const char *buffer ATTRIBUTE_UNUSED)
21844 {
21845 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21846 {
21847 macinfo_entry e;
21848 /* Insert a dummy first entry to be able to optimize the whole
21849 predefined macro block using DW_MACRO_GNU_transparent_include. */
21850 if (macinfo_table->is_empty () && lineno <= 1)
21851 {
21852 e.code = 0;
21853 e.lineno = 0;
21854 e.info = NULL;
21855 vec_safe_push (macinfo_table, e);
21856 }
21857 e.code = DW_MACINFO_undef;
21858 e.lineno = lineno;
21859 e.info = ggc_strdup (buffer);
21860 vec_safe_push (macinfo_table, e);
21861 }
21862 }
21863
21864 /* Helpers to manipulate hash table of CUs. */
21865
21866 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21867 {
21868 typedef macinfo_entry value_type;
21869 typedef macinfo_entry compare_type;
21870 static inline hashval_t hash (const value_type *);
21871 static inline bool equal (const value_type *, const compare_type *);
21872 };
21873
21874 inline hashval_t
21875 macinfo_entry_hasher::hash (const value_type *entry)
21876 {
21877 return htab_hash_string (entry->info);
21878 }
21879
21880 inline bool
21881 macinfo_entry_hasher::equal (const value_type *entry1,
21882 const compare_type *entry2)
21883 {
21884 return !strcmp (entry1->info, entry2->info);
21885 }
21886
21887 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21888
21889 /* Output a single .debug_macinfo entry. */
21890
21891 static void
21892 output_macinfo_op (macinfo_entry *ref)
21893 {
21894 int file_num;
21895 size_t len;
21896 struct indirect_string_node *node;
21897 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21898 struct dwarf_file_data *fd;
21899
21900 switch (ref->code)
21901 {
21902 case DW_MACINFO_start_file:
21903 fd = lookup_filename (ref->info);
21904 file_num = maybe_emit_file (fd);
21905 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21906 dw2_asm_output_data_uleb128 (ref->lineno,
21907 "Included from line number %lu",
21908 (unsigned long) ref->lineno);
21909 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21910 break;
21911 case DW_MACINFO_end_file:
21912 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21913 break;
21914 case DW_MACINFO_define:
21915 case DW_MACINFO_undef:
21916 len = strlen (ref->info) + 1;
21917 if (!dwarf_strict
21918 && len > DWARF_OFFSET_SIZE
21919 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21920 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21921 {
21922 ref->code = ref->code == DW_MACINFO_define
21923 ? DW_MACRO_GNU_define_indirect
21924 : DW_MACRO_GNU_undef_indirect;
21925 output_macinfo_op (ref);
21926 return;
21927 }
21928 dw2_asm_output_data (1, ref->code,
21929 ref->code == DW_MACINFO_define
21930 ? "Define macro" : "Undefine macro");
21931 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21932 (unsigned long) ref->lineno);
21933 dw2_asm_output_nstring (ref->info, -1, "The macro");
21934 break;
21935 case DW_MACRO_GNU_define_indirect:
21936 case DW_MACRO_GNU_undef_indirect:
21937 node = find_AT_string (ref->info);
21938 gcc_assert (node
21939 && ((node->form == DW_FORM_strp)
21940 || (node->form == DW_FORM_GNU_str_index)));
21941 dw2_asm_output_data (1, ref->code,
21942 ref->code == DW_MACRO_GNU_define_indirect
21943 ? "Define macro indirect"
21944 : "Undefine macro indirect");
21945 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21946 (unsigned long) ref->lineno);
21947 if (node->form == DW_FORM_strp)
21948 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21949 debug_str_section, "The macro: \"%s\"",
21950 ref->info);
21951 else
21952 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21953 ref->info);
21954 break;
21955 case DW_MACRO_GNU_transparent_include:
21956 dw2_asm_output_data (1, ref->code, "Transparent include");
21957 ASM_GENERATE_INTERNAL_LABEL (label,
21958 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21960 break;
21961 default:
21962 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21963 ASM_COMMENT_START, (unsigned long) ref->code);
21964 break;
21965 }
21966 }
21967
21968 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21969 other compilation unit .debug_macinfo sections. IDX is the first
21970 index of a define/undef, return the number of ops that should be
21971 emitted in a comdat .debug_macinfo section and emit
21972 a DW_MACRO_GNU_transparent_include entry referencing it.
21973 If the define/undef entry should be emitted normally, return 0. */
21974
21975 static unsigned
21976 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21977 macinfo_hash_type **macinfo_htab)
21978 {
21979 macinfo_entry *first, *second, *cur, *inc;
21980 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21981 unsigned char checksum[16];
21982 struct md5_ctx ctx;
21983 char *grp_name, *tail;
21984 const char *base;
21985 unsigned int i, count, encoded_filename_len, linebuf_len;
21986 macinfo_entry **slot;
21987
21988 first = &(*macinfo_table)[idx];
21989 second = &(*macinfo_table)[idx + 1];
21990
21991 /* Optimize only if there are at least two consecutive define/undef ops,
21992 and either all of them are before first DW_MACINFO_start_file
21993 with lineno {0,1} (i.e. predefined macro block), or all of them are
21994 in some included header file. */
21995 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21996 return 0;
21997 if (vec_safe_is_empty (files))
21998 {
21999 if (first->lineno > 1 || second->lineno > 1)
22000 return 0;
22001 }
22002 else if (first->lineno == 0)
22003 return 0;
22004
22005 /* Find the last define/undef entry that can be grouped together
22006 with first and at the same time compute md5 checksum of their
22007 codes, linenumbers and strings. */
22008 md5_init_ctx (&ctx);
22009 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22010 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22011 break;
22012 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22013 break;
22014 else
22015 {
22016 unsigned char code = cur->code;
22017 md5_process_bytes (&code, 1, &ctx);
22018 checksum_uleb128 (cur->lineno, &ctx);
22019 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22020 }
22021 md5_finish_ctx (&ctx, checksum);
22022 count = i - idx;
22023
22024 /* From the containing include filename (if any) pick up just
22025 usable characters from its basename. */
22026 if (vec_safe_is_empty (files))
22027 base = "";
22028 else
22029 base = lbasename (files->last ().info);
22030 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22031 if (ISIDNUM (base[i]) || base[i] == '.')
22032 encoded_filename_len++;
22033 /* Count . at the end. */
22034 if (encoded_filename_len)
22035 encoded_filename_len++;
22036
22037 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22038 linebuf_len = strlen (linebuf);
22039
22040 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22041 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22042 + 16 * 2 + 1);
22043 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22044 tail = grp_name + 4;
22045 if (encoded_filename_len)
22046 {
22047 for (i = 0; base[i]; i++)
22048 if (ISIDNUM (base[i]) || base[i] == '.')
22049 *tail++ = base[i];
22050 *tail++ = '.';
22051 }
22052 memcpy (tail, linebuf, linebuf_len);
22053 tail += linebuf_len;
22054 *tail++ = '.';
22055 for (i = 0; i < 16; i++)
22056 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22057
22058 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22059 in the empty vector entry before the first define/undef. */
22060 inc = &(*macinfo_table)[idx - 1];
22061 inc->code = DW_MACRO_GNU_transparent_include;
22062 inc->lineno = 0;
22063 inc->info = ggc_strdup (grp_name);
22064 if (!*macinfo_htab)
22065 *macinfo_htab = new macinfo_hash_type (10);
22066 /* Avoid emitting duplicates. */
22067 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22068 if (*slot != NULL)
22069 {
22070 inc->code = 0;
22071 inc->info = NULL;
22072 /* If such an entry has been used before, just emit
22073 a DW_MACRO_GNU_transparent_include op. */
22074 inc = *slot;
22075 output_macinfo_op (inc);
22076 /* And clear all macinfo_entry in the range to avoid emitting them
22077 in the second pass. */
22078 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22079 {
22080 cur->code = 0;
22081 cur->info = NULL;
22082 }
22083 }
22084 else
22085 {
22086 *slot = inc;
22087 inc->lineno = (*macinfo_htab)->elements ();
22088 output_macinfo_op (inc);
22089 }
22090 return count;
22091 }
22092
22093 /* Save any strings needed by the macinfo table in the debug str
22094 table. All strings must be collected into the table by the time
22095 index_string is called. */
22096
22097 static void
22098 save_macinfo_strings (void)
22099 {
22100 unsigned len;
22101 unsigned i;
22102 macinfo_entry *ref;
22103
22104 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22105 {
22106 switch (ref->code)
22107 {
22108 /* Match the logic in output_macinfo_op to decide on
22109 indirect strings. */
22110 case DW_MACINFO_define:
22111 case DW_MACINFO_undef:
22112 len = strlen (ref->info) + 1;
22113 if (!dwarf_strict
22114 && len > DWARF_OFFSET_SIZE
22115 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22116 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22117 set_indirect_string (find_AT_string (ref->info));
22118 break;
22119 case DW_MACRO_GNU_define_indirect:
22120 case DW_MACRO_GNU_undef_indirect:
22121 set_indirect_string (find_AT_string (ref->info));
22122 break;
22123 default:
22124 break;
22125 }
22126 }
22127 }
22128
22129 /* Output macinfo section(s). */
22130
22131 static void
22132 output_macinfo (void)
22133 {
22134 unsigned i;
22135 unsigned long length = vec_safe_length (macinfo_table);
22136 macinfo_entry *ref;
22137 vec<macinfo_entry, va_gc> *files = NULL;
22138 macinfo_hash_type *macinfo_htab = NULL;
22139
22140 if (! length)
22141 return;
22142
22143 /* output_macinfo* uses these interchangeably. */
22144 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22145 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22146 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22147 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22148
22149 /* For .debug_macro emit the section header. */
22150 if (!dwarf_strict)
22151 {
22152 dw2_asm_output_data (2, 4, "DWARF macro version number");
22153 if (DWARF_OFFSET_SIZE == 8)
22154 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22155 else
22156 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22157 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22158 (!dwarf_split_debug_info ? debug_line_section_label
22159 : debug_skeleton_line_section_label),
22160 debug_line_section, NULL);
22161 }
22162
22163 /* In the first loop, it emits the primary .debug_macinfo section
22164 and after each emitted op the macinfo_entry is cleared.
22165 If a longer range of define/undef ops can be optimized using
22166 DW_MACRO_GNU_transparent_include, the
22167 DW_MACRO_GNU_transparent_include op is emitted and kept in
22168 the vector before the first define/undef in the range and the
22169 whole range of define/undef ops is not emitted and kept. */
22170 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22171 {
22172 switch (ref->code)
22173 {
22174 case DW_MACINFO_start_file:
22175 vec_safe_push (files, *ref);
22176 break;
22177 case DW_MACINFO_end_file:
22178 if (!vec_safe_is_empty (files))
22179 files->pop ();
22180 break;
22181 case DW_MACINFO_define:
22182 case DW_MACINFO_undef:
22183 if (!dwarf_strict
22184 && HAVE_COMDAT_GROUP
22185 && vec_safe_length (files) != 1
22186 && i > 0
22187 && i + 1 < length
22188 && (*macinfo_table)[i - 1].code == 0)
22189 {
22190 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22191 if (count)
22192 {
22193 i += count - 1;
22194 continue;
22195 }
22196 }
22197 break;
22198 case 0:
22199 /* A dummy entry may be inserted at the beginning to be able
22200 to optimize the whole block of predefined macros. */
22201 if (i == 0)
22202 continue;
22203 default:
22204 break;
22205 }
22206 output_macinfo_op (ref);
22207 ref->info = NULL;
22208 ref->code = 0;
22209 }
22210
22211 if (!macinfo_htab)
22212 return;
22213
22214 delete macinfo_htab;
22215 macinfo_htab = NULL;
22216
22217 /* If any DW_MACRO_GNU_transparent_include were used, on those
22218 DW_MACRO_GNU_transparent_include entries terminate the
22219 current chain and switch to a new comdat .debug_macinfo
22220 section and emit the define/undef entries within it. */
22221 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22222 switch (ref->code)
22223 {
22224 case 0:
22225 continue;
22226 case DW_MACRO_GNU_transparent_include:
22227 {
22228 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22229 tree comdat_key = get_identifier (ref->info);
22230 /* Terminate the previous .debug_macinfo section. */
22231 dw2_asm_output_data (1, 0, "End compilation unit");
22232 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22233 SECTION_DEBUG
22234 | SECTION_LINKONCE,
22235 comdat_key);
22236 ASM_GENERATE_INTERNAL_LABEL (label,
22237 DEBUG_MACRO_SECTION_LABEL,
22238 ref->lineno);
22239 ASM_OUTPUT_LABEL (asm_out_file, label);
22240 ref->code = 0;
22241 ref->info = NULL;
22242 dw2_asm_output_data (2, 4, "DWARF macro version number");
22243 if (DWARF_OFFSET_SIZE == 8)
22244 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22245 else
22246 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22247 }
22248 break;
22249 case DW_MACINFO_define:
22250 case DW_MACINFO_undef:
22251 output_macinfo_op (ref);
22252 ref->code = 0;
22253 ref->info = NULL;
22254 break;
22255 default:
22256 gcc_unreachable ();
22257 }
22258 }
22259
22260 /* Set up for Dwarf output at the start of compilation. */
22261
22262 static void
22263 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22264 {
22265 /* Allocate the file_table. */
22266 file_table = htab_create_ggc (50, file_table_hash,
22267 file_table_eq, NULL);
22268
22269 /* Allocate the decl_die_table. */
22270 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22271 decl_die_table_eq, NULL);
22272
22273 /* Allocate the decl_loc_table. */
22274 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22275 decl_loc_table_eq, NULL);
22276
22277 /* Allocate the cached_dw_loc_list_table. */
22278 cached_dw_loc_list_table
22279 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22280 cached_dw_loc_list_table_eq, NULL);
22281
22282 /* Allocate the initial hunk of the decl_scope_table. */
22283 vec_alloc (decl_scope_table, 256);
22284
22285 /* Allocate the initial hunk of the abbrev_die_table. */
22286 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22287 (ABBREV_DIE_TABLE_INCREMENT);
22288 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22289 /* Zero-th entry is allocated, but unused. */
22290 abbrev_die_table_in_use = 1;
22291
22292 /* Allocate the pubtypes and pubnames vectors. */
22293 vec_alloc (pubname_table, 32);
22294 vec_alloc (pubtype_table, 32);
22295
22296 vec_alloc (incomplete_types, 64);
22297
22298 vec_alloc (used_rtx_array, 32);
22299
22300 if (!dwarf_split_debug_info)
22301 {
22302 debug_info_section = get_section (DEBUG_INFO_SECTION,
22303 SECTION_DEBUG, NULL);
22304 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22305 SECTION_DEBUG, NULL);
22306 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22307 SECTION_DEBUG, NULL);
22308 }
22309 else
22310 {
22311 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22312 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22313 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22314 SECTION_DEBUG | SECTION_EXCLUDE,
22315 NULL);
22316 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22317 SECTION_DEBUG, NULL);
22318 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22319 SECTION_DEBUG, NULL);
22320 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22321 SECTION_DEBUG, NULL);
22322 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22323 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22324
22325 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22326 the main .o, but the skeleton_line goes into the split off dwo. */
22327 debug_skeleton_line_section
22328 = get_section (DEBUG_DWO_LINE_SECTION,
22329 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22330 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22331 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22332 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22333 SECTION_DEBUG | SECTION_EXCLUDE,
22334 NULL);
22335 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22336 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22337 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22338 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22339 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22340 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22341 }
22342 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22343 SECTION_DEBUG, NULL);
22344 debug_macinfo_section = get_section (dwarf_strict
22345 ? DEBUG_MACINFO_SECTION
22346 : DEBUG_MACRO_SECTION,
22347 DEBUG_MACRO_SECTION_FLAGS, NULL);
22348 debug_line_section = get_section (DEBUG_LINE_SECTION,
22349 SECTION_DEBUG, NULL);
22350 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22351 SECTION_DEBUG, NULL);
22352 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22353 SECTION_DEBUG, NULL);
22354 debug_str_section = get_section (DEBUG_STR_SECTION,
22355 DEBUG_STR_SECTION_FLAGS, NULL);
22356 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22357 SECTION_DEBUG, NULL);
22358 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22359 SECTION_DEBUG, NULL);
22360
22361 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22362 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22363 DEBUG_ABBREV_SECTION_LABEL, 0);
22364 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22365 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22366 COLD_TEXT_SECTION_LABEL, 0);
22367 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22368
22369 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22370 DEBUG_INFO_SECTION_LABEL, 0);
22371 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22372 DEBUG_LINE_SECTION_LABEL, 0);
22373 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22374 DEBUG_RANGES_SECTION_LABEL, 0);
22375 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22376 DEBUG_ADDR_SECTION_LABEL, 0);
22377 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22378 dwarf_strict
22379 ? DEBUG_MACINFO_SECTION_LABEL
22380 : DEBUG_MACRO_SECTION_LABEL, 0);
22381 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22382
22383 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22384 vec_alloc (macinfo_table, 64);
22385
22386 switch_to_section (text_section);
22387 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22388
22389 /* Make sure the line number table for .text always exists. */
22390 text_section_line_info = new_line_info_table ();
22391 text_section_line_info->end_label = text_end_label;
22392 }
22393
22394 /* Called before compile () starts outputtting functions, variables
22395 and toplevel asms into assembly. */
22396
22397 static void
22398 dwarf2out_assembly_start (void)
22399 {
22400 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22401 && dwarf2out_do_cfi_asm ()
22402 && (!(flag_unwind_tables || flag_exceptions)
22403 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22404 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22405 }
22406
22407 /* A helper function for dwarf2out_finish called through
22408 htab_traverse. Assign a string its index. All strings must be
22409 collected into the table by the time index_string is called,
22410 because the indexing code relies on htab_traverse to traverse nodes
22411 in the same order for each run. */
22412
22413 static int
22414 index_string (void **h, void *v)
22415 {
22416 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22417 unsigned int *index = (unsigned int *) v;
22418
22419 find_string_form (node);
22420 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22421 {
22422 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22423 node->index = *index;
22424 *index += 1;
22425 }
22426 return 1;
22427 }
22428
22429 /* A helper function for output_indirect_strings called through
22430 htab_traverse. Output the offset to a string and update the
22431 current offset. */
22432
22433 static int
22434 output_index_string_offset (void **h, void *v)
22435 {
22436 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22437 unsigned int *offset = (unsigned int *) v;
22438
22439 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22440 {
22441 /* Assert that this node has been assigned an index. */
22442 gcc_assert (node->index != NO_INDEX_ASSIGNED
22443 && node->index != NOT_INDEXED);
22444 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22445 "indexed string 0x%x: %s", node->index, node->str);
22446 *offset += strlen (node->str) + 1;
22447 }
22448 return 1;
22449 }
22450
22451 /* A helper function for dwarf2out_finish called through
22452 htab_traverse. Output the indexed string. */
22453
22454 static int
22455 output_index_string (void **h, void *v)
22456 {
22457 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22458 unsigned int *cur_idx = (unsigned int *) v;
22459
22460 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22461 {
22462 /* Assert that the strings are output in the same order as their
22463 indexes were assigned. */
22464 gcc_assert (*cur_idx == node->index);
22465 assemble_string (node->str, strlen (node->str) + 1);
22466 *cur_idx += 1;
22467 }
22468 return 1;
22469 }
22470
22471 /* A helper function for dwarf2out_finish called through
22472 htab_traverse. Emit one queued .debug_str string. */
22473
22474 static int
22475 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22476 {
22477 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22478
22479 node->form = find_string_form (node);
22480 if (node->form == DW_FORM_strp && node->refcount > 0)
22481 {
22482 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22483 assemble_string (node->str, strlen (node->str) + 1);
22484 }
22485
22486 return 1;
22487 }
22488
22489 /* Output the indexed string table. */
22490
22491 static void
22492 output_indirect_strings (void)
22493 {
22494 switch_to_section (debug_str_section);
22495 if (!dwarf_split_debug_info)
22496 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22497 else
22498 {
22499 unsigned int offset = 0;
22500 unsigned int cur_idx = 0;
22501
22502 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22503
22504 switch_to_section (debug_str_offsets_section);
22505 htab_traverse_noresize (debug_str_hash,
22506 output_index_string_offset,
22507 &offset);
22508 switch_to_section (debug_str_dwo_section);
22509 htab_traverse_noresize (debug_str_hash,
22510 output_index_string,
22511 &cur_idx);
22512 }
22513 }
22514
22515 /* Callback for htab_traverse to assign an index to an entry in the
22516 table, and to write that entry to the .debug_addr section. */
22517
22518 static int
22519 output_addr_table_entry (void **slot, void *data)
22520 {
22521 addr_table_entry *entry = (addr_table_entry *) *slot;
22522 unsigned int *cur_index = (unsigned int *)data;
22523
22524 if (entry->refcount == 0)
22525 {
22526 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22527 || entry->index == NOT_INDEXED);
22528 return 1;
22529 }
22530
22531 gcc_assert (entry->index == *cur_index);
22532 (*cur_index)++;
22533
22534 switch (entry->kind)
22535 {
22536 case ate_kind_rtx:
22537 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22538 "0x%x", entry->index);
22539 break;
22540 case ate_kind_rtx_dtprel:
22541 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22542 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22543 DWARF2_ADDR_SIZE,
22544 entry->addr.rtl);
22545 fputc ('\n', asm_out_file);
22546 break;
22547 case ate_kind_label:
22548 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22549 "0x%x", entry->index);
22550 break;
22551 default:
22552 gcc_unreachable ();
22553 }
22554 return 1;
22555 }
22556
22557 /* Produce the .debug_addr section. */
22558
22559 static void
22560 output_addr_table (void)
22561 {
22562 unsigned int index = 0;
22563 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22564 return;
22565
22566 switch_to_section (debug_addr_section);
22567 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22568 }
22569
22570 #if ENABLE_ASSERT_CHECKING
22571 /* Verify that all marks are clear. */
22572
22573 static void
22574 verify_marks_clear (dw_die_ref die)
22575 {
22576 dw_die_ref c;
22577
22578 gcc_assert (! die->die_mark);
22579 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22580 }
22581 #endif /* ENABLE_ASSERT_CHECKING */
22582
22583 /* Clear the marks for a die and its children.
22584 Be cool if the mark isn't set. */
22585
22586 static void
22587 prune_unmark_dies (dw_die_ref die)
22588 {
22589 dw_die_ref c;
22590
22591 if (die->die_mark)
22592 die->die_mark = 0;
22593 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22594 }
22595
22596 /* Given DIE that we're marking as used, find any other dies
22597 it references as attributes and mark them as used. */
22598
22599 static void
22600 prune_unused_types_walk_attribs (dw_die_ref die)
22601 {
22602 dw_attr_ref a;
22603 unsigned ix;
22604
22605 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22606 {
22607 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22608 {
22609 /* A reference to another DIE.
22610 Make sure that it will get emitted.
22611 If it was broken out into a comdat group, don't follow it. */
22612 if (! AT_ref (a)->comdat_type_p
22613 || a->dw_attr == DW_AT_specification)
22614 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22615 }
22616 /* Set the string's refcount to 0 so that prune_unused_types_mark
22617 accounts properly for it. */
22618 if (AT_class (a) == dw_val_class_str)
22619 a->dw_attr_val.v.val_str->refcount = 0;
22620 }
22621 }
22622
22623 /* Mark the generic parameters and arguments children DIEs of DIE. */
22624
22625 static void
22626 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22627 {
22628 dw_die_ref c;
22629
22630 if (die == NULL || die->die_child == NULL)
22631 return;
22632 c = die->die_child;
22633 do
22634 {
22635 if (is_template_parameter (c))
22636 prune_unused_types_mark (c, 1);
22637 c = c->die_sib;
22638 } while (c && c != die->die_child);
22639 }
22640
22641 /* Mark DIE as being used. If DOKIDS is true, then walk down
22642 to DIE's children. */
22643
22644 static void
22645 prune_unused_types_mark (dw_die_ref die, int dokids)
22646 {
22647 dw_die_ref c;
22648
22649 if (die->die_mark == 0)
22650 {
22651 /* We haven't done this node yet. Mark it as used. */
22652 die->die_mark = 1;
22653 /* If this is the DIE of a generic type instantiation,
22654 mark the children DIEs that describe its generic parms and
22655 args. */
22656 prune_unused_types_mark_generic_parms_dies (die);
22657
22658 /* We also have to mark its parents as used.
22659 (But we don't want to mark our parent's kids due to this,
22660 unless it is a class.) */
22661 if (die->die_parent)
22662 prune_unused_types_mark (die->die_parent,
22663 class_scope_p (die->die_parent));
22664
22665 /* Mark any referenced nodes. */
22666 prune_unused_types_walk_attribs (die);
22667
22668 /* If this node is a specification,
22669 also mark the definition, if it exists. */
22670 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22671 prune_unused_types_mark (die->die_definition, 1);
22672 }
22673
22674 if (dokids && die->die_mark != 2)
22675 {
22676 /* We need to walk the children, but haven't done so yet.
22677 Remember that we've walked the kids. */
22678 die->die_mark = 2;
22679
22680 /* If this is an array type, we need to make sure our
22681 kids get marked, even if they're types. If we're
22682 breaking out types into comdat sections, do this
22683 for all type definitions. */
22684 if (die->die_tag == DW_TAG_array_type
22685 || (use_debug_types
22686 && is_type_die (die) && ! is_declaration_die (die)))
22687 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22688 else
22689 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22690 }
22691 }
22692
22693 /* For local classes, look if any static member functions were emitted
22694 and if so, mark them. */
22695
22696 static void
22697 prune_unused_types_walk_local_classes (dw_die_ref die)
22698 {
22699 dw_die_ref c;
22700
22701 if (die->die_mark == 2)
22702 return;
22703
22704 switch (die->die_tag)
22705 {
22706 case DW_TAG_structure_type:
22707 case DW_TAG_union_type:
22708 case DW_TAG_class_type:
22709 break;
22710
22711 case DW_TAG_subprogram:
22712 if (!get_AT_flag (die, DW_AT_declaration)
22713 || die->die_definition != NULL)
22714 prune_unused_types_mark (die, 1);
22715 return;
22716
22717 default:
22718 return;
22719 }
22720
22721 /* Mark children. */
22722 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22723 }
22724
22725 /* Walk the tree DIE and mark types that we actually use. */
22726
22727 static void
22728 prune_unused_types_walk (dw_die_ref die)
22729 {
22730 dw_die_ref c;
22731
22732 /* Don't do anything if this node is already marked and
22733 children have been marked as well. */
22734 if (die->die_mark == 2)
22735 return;
22736
22737 switch (die->die_tag)
22738 {
22739 case DW_TAG_structure_type:
22740 case DW_TAG_union_type:
22741 case DW_TAG_class_type:
22742 if (die->die_perennial_p)
22743 break;
22744
22745 for (c = die->die_parent; c; c = c->die_parent)
22746 if (c->die_tag == DW_TAG_subprogram)
22747 break;
22748
22749 /* Finding used static member functions inside of classes
22750 is needed just for local classes, because for other classes
22751 static member function DIEs with DW_AT_specification
22752 are emitted outside of the DW_TAG_*_type. If we ever change
22753 it, we'd need to call this even for non-local classes. */
22754 if (c)
22755 prune_unused_types_walk_local_classes (die);
22756
22757 /* It's a type node --- don't mark it. */
22758 return;
22759
22760 case DW_TAG_const_type:
22761 case DW_TAG_packed_type:
22762 case DW_TAG_pointer_type:
22763 case DW_TAG_reference_type:
22764 case DW_TAG_rvalue_reference_type:
22765 case DW_TAG_volatile_type:
22766 case DW_TAG_typedef:
22767 case DW_TAG_array_type:
22768 case DW_TAG_interface_type:
22769 case DW_TAG_friend:
22770 case DW_TAG_variant_part:
22771 case DW_TAG_enumeration_type:
22772 case DW_TAG_subroutine_type:
22773 case DW_TAG_string_type:
22774 case DW_TAG_set_type:
22775 case DW_TAG_subrange_type:
22776 case DW_TAG_ptr_to_member_type:
22777 case DW_TAG_file_type:
22778 if (die->die_perennial_p)
22779 break;
22780
22781 /* It's a type node --- don't mark it. */
22782 return;
22783
22784 default:
22785 /* Mark everything else. */
22786 break;
22787 }
22788
22789 if (die->die_mark == 0)
22790 {
22791 die->die_mark = 1;
22792
22793 /* Now, mark any dies referenced from here. */
22794 prune_unused_types_walk_attribs (die);
22795 }
22796
22797 die->die_mark = 2;
22798
22799 /* Mark children. */
22800 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22801 }
22802
22803 /* Increment the string counts on strings referred to from DIE's
22804 attributes. */
22805
22806 static void
22807 prune_unused_types_update_strings (dw_die_ref die)
22808 {
22809 dw_attr_ref a;
22810 unsigned ix;
22811
22812 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22813 if (AT_class (a) == dw_val_class_str)
22814 {
22815 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22816 s->refcount++;
22817 /* Avoid unnecessarily putting strings that are used less than
22818 twice in the hash table. */
22819 if (s->refcount
22820 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22821 {
22822 void ** slot;
22823 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22824 htab_hash_string (s->str),
22825 INSERT);
22826 gcc_assert (*slot == NULL);
22827 *slot = s;
22828 }
22829 }
22830 }
22831
22832 /* Remove from the tree DIE any dies that aren't marked. */
22833
22834 static void
22835 prune_unused_types_prune (dw_die_ref die)
22836 {
22837 dw_die_ref c;
22838
22839 gcc_assert (die->die_mark);
22840 prune_unused_types_update_strings (die);
22841
22842 if (! die->die_child)
22843 return;
22844
22845 c = die->die_child;
22846 do {
22847 dw_die_ref prev = c;
22848 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22849 if (c == die->die_child)
22850 {
22851 /* No marked children between 'prev' and the end of the list. */
22852 if (prev == c)
22853 /* No marked children at all. */
22854 die->die_child = NULL;
22855 else
22856 {
22857 prev->die_sib = c->die_sib;
22858 die->die_child = prev;
22859 }
22860 return;
22861 }
22862
22863 if (c != prev->die_sib)
22864 prev->die_sib = c;
22865 prune_unused_types_prune (c);
22866 } while (c != die->die_child);
22867 }
22868
22869 /* Remove dies representing declarations that we never use. */
22870
22871 static void
22872 prune_unused_types (void)
22873 {
22874 unsigned int i;
22875 limbo_die_node *node;
22876 comdat_type_node *ctnode;
22877 pubname_ref pub;
22878 dw_die_ref base_type;
22879
22880 #if ENABLE_ASSERT_CHECKING
22881 /* All the marks should already be clear. */
22882 verify_marks_clear (comp_unit_die ());
22883 for (node = limbo_die_list; node; node = node->next)
22884 verify_marks_clear (node->die);
22885 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22886 verify_marks_clear (ctnode->root_die);
22887 #endif /* ENABLE_ASSERT_CHECKING */
22888
22889 /* Mark types that are used in global variables. */
22890 premark_types_used_by_global_vars ();
22891
22892 /* Set the mark on nodes that are actually used. */
22893 prune_unused_types_walk (comp_unit_die ());
22894 for (node = limbo_die_list; node; node = node->next)
22895 prune_unused_types_walk (node->die);
22896 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22897 {
22898 prune_unused_types_walk (ctnode->root_die);
22899 prune_unused_types_mark (ctnode->type_die, 1);
22900 }
22901
22902 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22903 are unusual in that they are pubnames that are the children of pubtypes.
22904 They should only be marked via their parent DW_TAG_enumeration_type die,
22905 not as roots in themselves. */
22906 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22907 if (pub->die->die_tag != DW_TAG_enumerator)
22908 prune_unused_types_mark (pub->die, 1);
22909 for (i = 0; base_types.iterate (i, &base_type); i++)
22910 prune_unused_types_mark (base_type, 1);
22911
22912 if (debug_str_hash)
22913 htab_empty (debug_str_hash);
22914 if (skeleton_debug_str_hash)
22915 htab_empty (skeleton_debug_str_hash);
22916 prune_unused_types_prune (comp_unit_die ());
22917 for (node = limbo_die_list; node; node = node->next)
22918 prune_unused_types_prune (node->die);
22919 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22920 prune_unused_types_prune (ctnode->root_die);
22921
22922 /* Leave the marks clear. */
22923 prune_unmark_dies (comp_unit_die ());
22924 for (node = limbo_die_list; node; node = node->next)
22925 prune_unmark_dies (node->die);
22926 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22927 prune_unmark_dies (ctnode->root_die);
22928 }
22929
22930 /* Set the parameter to true if there are any relative pathnames in
22931 the file table. */
22932 static int
22933 file_table_relative_p (void ** slot, void *param)
22934 {
22935 bool *p = (bool *) param;
22936 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22937 if (!IS_ABSOLUTE_PATH (d->filename))
22938 {
22939 *p = true;
22940 return 0;
22941 }
22942 return 1;
22943 }
22944
22945 /* Helpers to manipulate hash table of comdat type units. */
22946
22947 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22948 {
22949 typedef comdat_type_node value_type;
22950 typedef comdat_type_node compare_type;
22951 static inline hashval_t hash (const value_type *);
22952 static inline bool equal (const value_type *, const compare_type *);
22953 };
22954
22955 inline hashval_t
22956 comdat_type_hasher::hash (const value_type *type_node)
22957 {
22958 hashval_t h;
22959 memcpy (&h, type_node->signature, sizeof (h));
22960 return h;
22961 }
22962
22963 inline bool
22964 comdat_type_hasher::equal (const value_type *type_node_1,
22965 const compare_type *type_node_2)
22966 {
22967 return (! memcmp (type_node_1->signature, type_node_2->signature,
22968 DWARF_TYPE_SIGNATURE_SIZE));
22969 }
22970
22971 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22972 to the location it would have been added, should we know its
22973 DECL_ASSEMBLER_NAME when we added other attributes. This will
22974 probably improve compactness of debug info, removing equivalent
22975 abbrevs, and hide any differences caused by deferring the
22976 computation of the assembler name, triggered by e.g. PCH. */
22977
22978 static inline void
22979 move_linkage_attr (dw_die_ref die)
22980 {
22981 unsigned ix = vec_safe_length (die->die_attr);
22982 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22983
22984 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22985 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22986
22987 while (--ix > 0)
22988 {
22989 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22990
22991 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22992 break;
22993 }
22994
22995 if (ix != vec_safe_length (die->die_attr) - 1)
22996 {
22997 die->die_attr->pop ();
22998 die->die_attr->quick_insert (ix, linkage);
22999 }
23000 }
23001
23002 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23003 referenced from typed stack ops and count how often they are used. */
23004
23005 static void
23006 mark_base_types (dw_loc_descr_ref loc)
23007 {
23008 dw_die_ref base_type = NULL;
23009
23010 for (; loc; loc = loc->dw_loc_next)
23011 {
23012 switch (loc->dw_loc_opc)
23013 {
23014 case DW_OP_GNU_regval_type:
23015 case DW_OP_GNU_deref_type:
23016 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23017 break;
23018 case DW_OP_GNU_convert:
23019 case DW_OP_GNU_reinterpret:
23020 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23021 continue;
23022 /* FALLTHRU */
23023 case DW_OP_GNU_const_type:
23024 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23025 break;
23026 case DW_OP_GNU_entry_value:
23027 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23028 continue;
23029 default:
23030 continue;
23031 }
23032 gcc_assert (base_type->die_parent == comp_unit_die ());
23033 if (base_type->die_mark)
23034 base_type->die_mark++;
23035 else
23036 {
23037 base_types.safe_push (base_type);
23038 base_type->die_mark = 1;
23039 }
23040 }
23041 }
23042
23043 /* Comparison function for sorting marked base types. */
23044
23045 static int
23046 base_type_cmp (const void *x, const void *y)
23047 {
23048 dw_die_ref dx = *(const dw_die_ref *) x;
23049 dw_die_ref dy = *(const dw_die_ref *) y;
23050 unsigned int byte_size1, byte_size2;
23051 unsigned int encoding1, encoding2;
23052 if (dx->die_mark > dy->die_mark)
23053 return -1;
23054 if (dx->die_mark < dy->die_mark)
23055 return 1;
23056 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23057 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23058 if (byte_size1 < byte_size2)
23059 return 1;
23060 if (byte_size1 > byte_size2)
23061 return -1;
23062 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23063 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23064 if (encoding1 < encoding2)
23065 return 1;
23066 if (encoding1 > encoding2)
23067 return -1;
23068 return 0;
23069 }
23070
23071 /* Move base types marked by mark_base_types as early as possible
23072 in the CU, sorted by decreasing usage count both to make the
23073 uleb128 references as small as possible and to make sure they
23074 will have die_offset already computed by calc_die_sizes when
23075 sizes of typed stack loc ops is computed. */
23076
23077 static void
23078 move_marked_base_types (void)
23079 {
23080 unsigned int i;
23081 dw_die_ref base_type, die, c;
23082
23083 if (base_types.is_empty ())
23084 return;
23085
23086 /* Sort by decreasing usage count, they will be added again in that
23087 order later on. */
23088 base_types.qsort (base_type_cmp);
23089 die = comp_unit_die ();
23090 c = die->die_child;
23091 do
23092 {
23093 dw_die_ref prev = c;
23094 c = c->die_sib;
23095 while (c->die_mark)
23096 {
23097 remove_child_with_prev (c, prev);
23098 /* As base types got marked, there must be at least
23099 one node other than DW_TAG_base_type. */
23100 gcc_assert (c != c->die_sib);
23101 c = c->die_sib;
23102 }
23103 }
23104 while (c != die->die_child);
23105 gcc_assert (die->die_child);
23106 c = die->die_child;
23107 for (i = 0; base_types.iterate (i, &base_type); i++)
23108 {
23109 base_type->die_mark = 0;
23110 base_type->die_sib = c->die_sib;
23111 c->die_sib = base_type;
23112 c = base_type;
23113 }
23114 }
23115
23116 /* Helper function for resolve_addr, attempt to resolve
23117 one CONST_STRING, return non-zero if not successful. Similarly verify that
23118 SYMBOL_REFs refer to variables emitted in the current CU. */
23119
23120 static int
23121 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23122 {
23123 rtx rtl = *addr;
23124
23125 if (GET_CODE (rtl) == CONST_STRING)
23126 {
23127 size_t len = strlen (XSTR (rtl, 0)) + 1;
23128 tree t = build_string (len, XSTR (rtl, 0));
23129 tree tlen = size_int (len - 1);
23130 TREE_TYPE (t)
23131 = build_array_type (char_type_node, build_index_type (tlen));
23132 rtl = lookup_constant_def (t);
23133 if (!rtl || !MEM_P (rtl))
23134 return 1;
23135 rtl = XEXP (rtl, 0);
23136 if (GET_CODE (rtl) == SYMBOL_REF
23137 && SYMBOL_REF_DECL (rtl)
23138 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23139 return 1;
23140 vec_safe_push (used_rtx_array, rtl);
23141 *addr = rtl;
23142 return 0;
23143 }
23144
23145 if (GET_CODE (rtl) == SYMBOL_REF
23146 && SYMBOL_REF_DECL (rtl))
23147 {
23148 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23149 {
23150 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23151 return 1;
23152 }
23153 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23154 return 1;
23155 }
23156
23157 if (GET_CODE (rtl) == CONST
23158 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23159 return 1;
23160
23161 return 0;
23162 }
23163
23164 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23165 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23166 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23167
23168 static rtx
23169 string_cst_pool_decl (tree t)
23170 {
23171 rtx rtl = output_constant_def (t, 1);
23172 unsigned char *array;
23173 dw_loc_descr_ref l;
23174 tree decl;
23175 size_t len;
23176 dw_die_ref ref;
23177
23178 if (!rtl || !MEM_P (rtl))
23179 return NULL_RTX;
23180 rtl = XEXP (rtl, 0);
23181 if (GET_CODE (rtl) != SYMBOL_REF
23182 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23183 return NULL_RTX;
23184
23185 decl = SYMBOL_REF_DECL (rtl);
23186 if (!lookup_decl_die (decl))
23187 {
23188 len = TREE_STRING_LENGTH (t);
23189 vec_safe_push (used_rtx_array, rtl);
23190 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23191 array = ggc_vec_alloc<unsigned char> (len);
23192 memcpy (array, TREE_STRING_POINTER (t), len);
23193 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23194 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23195 l->dw_loc_oprnd2.v.val_vec.length = len;
23196 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23197 l->dw_loc_oprnd2.v.val_vec.array = array;
23198 add_AT_loc (ref, DW_AT_location, l);
23199 equate_decl_number_to_die (decl, ref);
23200 }
23201 return rtl;
23202 }
23203
23204 /* Helper function of resolve_addr_in_expr. LOC is
23205 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23206 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23207 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23208 with DW_OP_GNU_implicit_pointer if possible
23209 and return true, if unsuccessful, return false. */
23210
23211 static bool
23212 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23213 {
23214 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23215 HOST_WIDE_INT offset = 0;
23216 dw_die_ref ref = NULL;
23217 tree decl;
23218
23219 if (GET_CODE (rtl) == CONST
23220 && GET_CODE (XEXP (rtl, 0)) == PLUS
23221 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23222 {
23223 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23224 rtl = XEXP (XEXP (rtl, 0), 0);
23225 }
23226 if (GET_CODE (rtl) == CONST_STRING)
23227 {
23228 size_t len = strlen (XSTR (rtl, 0)) + 1;
23229 tree t = build_string (len, XSTR (rtl, 0));
23230 tree tlen = size_int (len - 1);
23231
23232 TREE_TYPE (t)
23233 = build_array_type (char_type_node, build_index_type (tlen));
23234 rtl = string_cst_pool_decl (t);
23235 if (!rtl)
23236 return false;
23237 }
23238 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23239 {
23240 decl = SYMBOL_REF_DECL (rtl);
23241 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23242 {
23243 ref = lookup_decl_die (decl);
23244 if (ref && (get_AT (ref, DW_AT_location)
23245 || get_AT (ref, DW_AT_const_value)))
23246 {
23247 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23248 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23249 loc->dw_loc_oprnd1.val_entry = NULL;
23250 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23251 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23252 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23253 loc->dw_loc_oprnd2.v.val_int = offset;
23254 return true;
23255 }
23256 }
23257 }
23258 return false;
23259 }
23260
23261 /* Helper function for resolve_addr, handle one location
23262 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23263 the location list couldn't be resolved. */
23264
23265 static bool
23266 resolve_addr_in_expr (dw_loc_descr_ref loc)
23267 {
23268 dw_loc_descr_ref keep = NULL;
23269 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23270 switch (loc->dw_loc_opc)
23271 {
23272 case DW_OP_addr:
23273 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23274 {
23275 if ((prev == NULL
23276 || prev->dw_loc_opc == DW_OP_piece
23277 || prev->dw_loc_opc == DW_OP_bit_piece)
23278 && loc->dw_loc_next
23279 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23280 && !dwarf_strict
23281 && optimize_one_addr_into_implicit_ptr (loc))
23282 break;
23283 return false;
23284 }
23285 break;
23286 case DW_OP_GNU_addr_index:
23287 case DW_OP_GNU_const_index:
23288 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23289 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23290 {
23291 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23292 if (resolve_one_addr (&rtl, NULL))
23293 return false;
23294 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23295 loc->dw_loc_oprnd1.val_entry =
23296 add_addr_table_entry (rtl, ate_kind_rtx);
23297 }
23298 break;
23299 case DW_OP_const4u:
23300 case DW_OP_const8u:
23301 if (loc->dtprel
23302 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23303 return false;
23304 break;
23305 case DW_OP_plus_uconst:
23306 if (size_of_loc_descr (loc)
23307 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23308 + 1
23309 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23310 {
23311 dw_loc_descr_ref repl
23312 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23313 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23314 add_loc_descr (&repl, loc->dw_loc_next);
23315 *loc = *repl;
23316 }
23317 break;
23318 case DW_OP_implicit_value:
23319 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23320 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23321 return false;
23322 break;
23323 case DW_OP_GNU_implicit_pointer:
23324 case DW_OP_GNU_parameter_ref:
23325 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23326 {
23327 dw_die_ref ref
23328 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23329 if (ref == NULL)
23330 return false;
23331 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23332 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23333 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23334 }
23335 break;
23336 case DW_OP_GNU_const_type:
23337 case DW_OP_GNU_regval_type:
23338 case DW_OP_GNU_deref_type:
23339 case DW_OP_GNU_convert:
23340 case DW_OP_GNU_reinterpret:
23341 while (loc->dw_loc_next
23342 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23343 {
23344 dw_die_ref base1, base2;
23345 unsigned enc1, enc2, size1, size2;
23346 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23347 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23348 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23349 else if (loc->dw_loc_oprnd1.val_class
23350 == dw_val_class_unsigned_const)
23351 break;
23352 else
23353 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23354 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23355 == dw_val_class_unsigned_const)
23356 break;
23357 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23358 gcc_assert (base1->die_tag == DW_TAG_base_type
23359 && base2->die_tag == DW_TAG_base_type);
23360 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23361 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23362 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23363 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23364 if (size1 == size2
23365 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23366 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23367 && loc != keep)
23368 || enc1 == enc2))
23369 {
23370 /* Optimize away next DW_OP_GNU_convert after
23371 adjusting LOC's base type die reference. */
23372 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23373 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23374 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23375 else
23376 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23377 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23378 continue;
23379 }
23380 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23381 point typed stack entry. */
23382 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23383 keep = loc->dw_loc_next;
23384 break;
23385 }
23386 break;
23387 default:
23388 break;
23389 }
23390 return true;
23391 }
23392
23393 /* Helper function of resolve_addr. DIE had DW_AT_location of
23394 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23395 and DW_OP_addr couldn't be resolved. resolve_addr has already
23396 removed the DW_AT_location attribute. This function attempts to
23397 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23398 to it or DW_AT_const_value attribute, if possible. */
23399
23400 static void
23401 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23402 {
23403 if (TREE_CODE (decl) != VAR_DECL
23404 || lookup_decl_die (decl) != die
23405 || DECL_EXTERNAL (decl)
23406 || !TREE_STATIC (decl)
23407 || DECL_INITIAL (decl) == NULL_TREE
23408 || DECL_P (DECL_INITIAL (decl))
23409 || get_AT (die, DW_AT_const_value))
23410 return;
23411
23412 tree init = DECL_INITIAL (decl);
23413 HOST_WIDE_INT offset = 0;
23414 /* For variables that have been optimized away and thus
23415 don't have a memory location, see if we can emit
23416 DW_AT_const_value instead. */
23417 if (tree_add_const_value_attribute (die, init))
23418 return;
23419 if (dwarf_strict)
23420 return;
23421 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23422 and ADDR_EXPR refers to a decl that has DW_AT_location or
23423 DW_AT_const_value (but isn't addressable, otherwise
23424 resolving the original DW_OP_addr wouldn't fail), see if
23425 we can add DW_OP_GNU_implicit_pointer. */
23426 STRIP_NOPS (init);
23427 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23428 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23429 {
23430 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23431 init = TREE_OPERAND (init, 0);
23432 STRIP_NOPS (init);
23433 }
23434 if (TREE_CODE (init) != ADDR_EXPR)
23435 return;
23436 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23437 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23438 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23439 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23440 && TREE_OPERAND (init, 0) != decl))
23441 {
23442 dw_die_ref ref;
23443 dw_loc_descr_ref l;
23444
23445 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23446 {
23447 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23448 if (!rtl)
23449 return;
23450 decl = SYMBOL_REF_DECL (rtl);
23451 }
23452 else
23453 decl = TREE_OPERAND (init, 0);
23454 ref = lookup_decl_die (decl);
23455 if (ref == NULL
23456 || (!get_AT (ref, DW_AT_location)
23457 && !get_AT (ref, DW_AT_const_value)))
23458 return;
23459 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23460 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23461 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23462 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23463 add_AT_loc (die, DW_AT_location, l);
23464 }
23465 }
23466
23467 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23468 an address in .rodata section if the string literal is emitted there,
23469 or remove the containing location list or replace DW_AT_const_value
23470 with DW_AT_location and empty location expression, if it isn't found
23471 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23472 to something that has been emitted in the current CU. */
23473
23474 static void
23475 resolve_addr (dw_die_ref die)
23476 {
23477 dw_die_ref c;
23478 dw_attr_ref a;
23479 dw_loc_list_ref *curr, *start, loc;
23480 unsigned ix;
23481
23482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23483 switch (AT_class (a))
23484 {
23485 case dw_val_class_loc_list:
23486 start = curr = AT_loc_list_ptr (a);
23487 loc = *curr;
23488 gcc_assert (loc);
23489 /* The same list can be referenced more than once. See if we have
23490 already recorded the result from a previous pass. */
23491 if (loc->replaced)
23492 *curr = loc->dw_loc_next;
23493 else if (!loc->resolved_addr)
23494 {
23495 /* As things stand, we do not expect or allow one die to
23496 reference a suffix of another die's location list chain.
23497 References must be identical or completely separate.
23498 There is therefore no need to cache the result of this
23499 pass on any list other than the first; doing so
23500 would lead to unnecessary writes. */
23501 while (*curr)
23502 {
23503 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23504 if (!resolve_addr_in_expr ((*curr)->expr))
23505 {
23506 dw_loc_list_ref next = (*curr)->dw_loc_next;
23507 dw_loc_descr_ref l = (*curr)->expr;
23508
23509 if (next && (*curr)->ll_symbol)
23510 {
23511 gcc_assert (!next->ll_symbol);
23512 next->ll_symbol = (*curr)->ll_symbol;
23513 }
23514 if (dwarf_split_debug_info)
23515 remove_loc_list_addr_table_entries (l);
23516 *curr = next;
23517 }
23518 else
23519 {
23520 mark_base_types ((*curr)->expr);
23521 curr = &(*curr)->dw_loc_next;
23522 }
23523 }
23524 if (loc == *start)
23525 loc->resolved_addr = 1;
23526 else
23527 {
23528 loc->replaced = 1;
23529 loc->dw_loc_next = *start;
23530 }
23531 }
23532 if (!*start)
23533 {
23534 remove_AT (die, a->dw_attr);
23535 ix--;
23536 }
23537 break;
23538 case dw_val_class_loc:
23539 {
23540 dw_loc_descr_ref l = AT_loc (a);
23541 /* For -gdwarf-2 don't attempt to optimize
23542 DW_AT_data_member_location containing
23543 DW_OP_plus_uconst - older consumers might
23544 rely on it being that op instead of a more complex,
23545 but shorter, location description. */
23546 if ((dwarf_version > 2
23547 || a->dw_attr != DW_AT_data_member_location
23548 || l == NULL
23549 || l->dw_loc_opc != DW_OP_plus_uconst
23550 || l->dw_loc_next != NULL)
23551 && !resolve_addr_in_expr (l))
23552 {
23553 if (dwarf_split_debug_info)
23554 remove_loc_list_addr_table_entries (l);
23555 if (l != NULL
23556 && l->dw_loc_next == NULL
23557 && l->dw_loc_opc == DW_OP_addr
23558 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23559 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23560 && a->dw_attr == DW_AT_location)
23561 {
23562 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23563 remove_AT (die, a->dw_attr);
23564 ix--;
23565 optimize_location_into_implicit_ptr (die, decl);
23566 break;
23567 }
23568 remove_AT (die, a->dw_attr);
23569 ix--;
23570 }
23571 else
23572 mark_base_types (l);
23573 }
23574 break;
23575 case dw_val_class_addr:
23576 if (a->dw_attr == DW_AT_const_value
23577 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23578 {
23579 if (AT_index (a) != NOT_INDEXED)
23580 remove_addr_table_entry (a->dw_attr_val.val_entry);
23581 remove_AT (die, a->dw_attr);
23582 ix--;
23583 }
23584 if (die->die_tag == DW_TAG_GNU_call_site
23585 && a->dw_attr == DW_AT_abstract_origin)
23586 {
23587 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23588 dw_die_ref tdie = lookup_decl_die (tdecl);
23589 if (tdie == NULL
23590 && DECL_EXTERNAL (tdecl)
23591 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23592 {
23593 force_decl_die (tdecl);
23594 tdie = lookup_decl_die (tdecl);
23595 }
23596 if (tdie)
23597 {
23598 a->dw_attr_val.val_class = dw_val_class_die_ref;
23599 a->dw_attr_val.v.val_die_ref.die = tdie;
23600 a->dw_attr_val.v.val_die_ref.external = 0;
23601 }
23602 else
23603 {
23604 if (AT_index (a) != NOT_INDEXED)
23605 remove_addr_table_entry (a->dw_attr_val.val_entry);
23606 remove_AT (die, a->dw_attr);
23607 ix--;
23608 }
23609 }
23610 break;
23611 default:
23612 break;
23613 }
23614
23615 FOR_EACH_CHILD (die, c, resolve_addr (c));
23616 }
23617 \f
23618 /* Helper routines for optimize_location_lists.
23619 This pass tries to share identical local lists in .debug_loc
23620 section. */
23621
23622 /* Iteratively hash operands of LOC opcode. */
23623
23624 static hashval_t
23625 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23626 {
23627 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23628 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23629
23630 switch (loc->dw_loc_opc)
23631 {
23632 case DW_OP_const4u:
23633 case DW_OP_const8u:
23634 if (loc->dtprel)
23635 goto hash_addr;
23636 /* FALLTHRU */
23637 case DW_OP_const1u:
23638 case DW_OP_const1s:
23639 case DW_OP_const2u:
23640 case DW_OP_const2s:
23641 case DW_OP_const4s:
23642 case DW_OP_const8s:
23643 case DW_OP_constu:
23644 case DW_OP_consts:
23645 case DW_OP_pick:
23646 case DW_OP_plus_uconst:
23647 case DW_OP_breg0:
23648 case DW_OP_breg1:
23649 case DW_OP_breg2:
23650 case DW_OP_breg3:
23651 case DW_OP_breg4:
23652 case DW_OP_breg5:
23653 case DW_OP_breg6:
23654 case DW_OP_breg7:
23655 case DW_OP_breg8:
23656 case DW_OP_breg9:
23657 case DW_OP_breg10:
23658 case DW_OP_breg11:
23659 case DW_OP_breg12:
23660 case DW_OP_breg13:
23661 case DW_OP_breg14:
23662 case DW_OP_breg15:
23663 case DW_OP_breg16:
23664 case DW_OP_breg17:
23665 case DW_OP_breg18:
23666 case DW_OP_breg19:
23667 case DW_OP_breg20:
23668 case DW_OP_breg21:
23669 case DW_OP_breg22:
23670 case DW_OP_breg23:
23671 case DW_OP_breg24:
23672 case DW_OP_breg25:
23673 case DW_OP_breg26:
23674 case DW_OP_breg27:
23675 case DW_OP_breg28:
23676 case DW_OP_breg29:
23677 case DW_OP_breg30:
23678 case DW_OP_breg31:
23679 case DW_OP_regx:
23680 case DW_OP_fbreg:
23681 case DW_OP_piece:
23682 case DW_OP_deref_size:
23683 case DW_OP_xderef_size:
23684 hash = iterative_hash_object (val1->v.val_int, hash);
23685 break;
23686 case DW_OP_skip:
23687 case DW_OP_bra:
23688 {
23689 int offset;
23690
23691 gcc_assert (val1->val_class == dw_val_class_loc);
23692 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23693 hash = iterative_hash_object (offset, hash);
23694 }
23695 break;
23696 case DW_OP_implicit_value:
23697 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23698 switch (val2->val_class)
23699 {
23700 case dw_val_class_const:
23701 hash = iterative_hash_object (val2->v.val_int, hash);
23702 break;
23703 case dw_val_class_vec:
23704 {
23705 unsigned int elt_size = val2->v.val_vec.elt_size;
23706 unsigned int len = val2->v.val_vec.length;
23707
23708 hash = iterative_hash_object (elt_size, hash);
23709 hash = iterative_hash_object (len, hash);
23710 hash = iterative_hash (val2->v.val_vec.array,
23711 len * elt_size, hash);
23712 }
23713 break;
23714 case dw_val_class_const_double:
23715 hash = iterative_hash_object (val2->v.val_double.low, hash);
23716 hash = iterative_hash_object (val2->v.val_double.high, hash);
23717 break;
23718 case dw_val_class_wide_int:
23719 hash = iterative_hash_object (*val2->v.val_wide, hash);
23720 break;
23721 case dw_val_class_addr:
23722 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23723 break;
23724 default:
23725 gcc_unreachable ();
23726 }
23727 break;
23728 case DW_OP_bregx:
23729 case DW_OP_bit_piece:
23730 hash = iterative_hash_object (val1->v.val_int, hash);
23731 hash = iterative_hash_object (val2->v.val_int, hash);
23732 break;
23733 case DW_OP_addr:
23734 hash_addr:
23735 if (loc->dtprel)
23736 {
23737 unsigned char dtprel = 0xd1;
23738 hash = iterative_hash_object (dtprel, hash);
23739 }
23740 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23741 break;
23742 case DW_OP_GNU_addr_index:
23743 case DW_OP_GNU_const_index:
23744 {
23745 if (loc->dtprel)
23746 {
23747 unsigned char dtprel = 0xd1;
23748 hash = iterative_hash_object (dtprel, hash);
23749 }
23750 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23751 }
23752 break;
23753 case DW_OP_GNU_implicit_pointer:
23754 hash = iterative_hash_object (val2->v.val_int, hash);
23755 break;
23756 case DW_OP_GNU_entry_value:
23757 hash = hash_loc_operands (val1->v.val_loc, hash);
23758 break;
23759 case DW_OP_GNU_regval_type:
23760 case DW_OP_GNU_deref_type:
23761 {
23762 unsigned int byte_size
23763 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23764 unsigned int encoding
23765 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23766 hash = iterative_hash_object (val1->v.val_int, hash);
23767 hash = iterative_hash_object (byte_size, hash);
23768 hash = iterative_hash_object (encoding, hash);
23769 }
23770 break;
23771 case DW_OP_GNU_convert:
23772 case DW_OP_GNU_reinterpret:
23773 if (val1->val_class == dw_val_class_unsigned_const)
23774 {
23775 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23776 break;
23777 }
23778 /* FALLTHRU */
23779 case DW_OP_GNU_const_type:
23780 {
23781 unsigned int byte_size
23782 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23783 unsigned int encoding
23784 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23785 hash = iterative_hash_object (byte_size, hash);
23786 hash = iterative_hash_object (encoding, hash);
23787 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23788 break;
23789 hash = iterative_hash_object (val2->val_class, hash);
23790 switch (val2->val_class)
23791 {
23792 case dw_val_class_const:
23793 hash = iterative_hash_object (val2->v.val_int, hash);
23794 break;
23795 case dw_val_class_vec:
23796 {
23797 unsigned int elt_size = val2->v.val_vec.elt_size;
23798 unsigned int len = val2->v.val_vec.length;
23799
23800 hash = iterative_hash_object (elt_size, hash);
23801 hash = iterative_hash_object (len, hash);
23802 hash = iterative_hash (val2->v.val_vec.array,
23803 len * elt_size, hash);
23804 }
23805 break;
23806 case dw_val_class_const_double:
23807 hash = iterative_hash_object (val2->v.val_double.low, hash);
23808 hash = iterative_hash_object (val2->v.val_double.high, hash);
23809 break;
23810 case dw_val_class_wide_int:
23811 hash = iterative_hash_object (*val2->v.val_wide, hash);
23812 break;
23813 default:
23814 gcc_unreachable ();
23815 }
23816 }
23817 break;
23818
23819 default:
23820 /* Other codes have no operands. */
23821 break;
23822 }
23823 return hash;
23824 }
23825
23826 /* Iteratively hash the whole DWARF location expression LOC. */
23827
23828 static inline hashval_t
23829 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23830 {
23831 dw_loc_descr_ref l;
23832 bool sizes_computed = false;
23833 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23834 size_of_locs (loc);
23835
23836 for (l = loc; l != NULL; l = l->dw_loc_next)
23837 {
23838 enum dwarf_location_atom opc = l->dw_loc_opc;
23839 hash = iterative_hash_object (opc, hash);
23840 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23841 {
23842 size_of_locs (loc);
23843 sizes_computed = true;
23844 }
23845 hash = hash_loc_operands (l, hash);
23846 }
23847 return hash;
23848 }
23849
23850 /* Compute hash of the whole location list LIST_HEAD. */
23851
23852 static inline void
23853 hash_loc_list (dw_loc_list_ref list_head)
23854 {
23855 dw_loc_list_ref curr = list_head;
23856 hashval_t hash = 0;
23857
23858 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23859 {
23860 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23861 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23862 if (curr->section)
23863 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23864 hash);
23865 hash = hash_locs (curr->expr, hash);
23866 }
23867 list_head->hash = hash;
23868 }
23869
23870 /* Return true if X and Y opcodes have the same operands. */
23871
23872 static inline bool
23873 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23874 {
23875 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23876 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23877 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23878 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23879
23880 switch (x->dw_loc_opc)
23881 {
23882 case DW_OP_const4u:
23883 case DW_OP_const8u:
23884 if (x->dtprel)
23885 goto hash_addr;
23886 /* FALLTHRU */
23887 case DW_OP_const1u:
23888 case DW_OP_const1s:
23889 case DW_OP_const2u:
23890 case DW_OP_const2s:
23891 case DW_OP_const4s:
23892 case DW_OP_const8s:
23893 case DW_OP_constu:
23894 case DW_OP_consts:
23895 case DW_OP_pick:
23896 case DW_OP_plus_uconst:
23897 case DW_OP_breg0:
23898 case DW_OP_breg1:
23899 case DW_OP_breg2:
23900 case DW_OP_breg3:
23901 case DW_OP_breg4:
23902 case DW_OP_breg5:
23903 case DW_OP_breg6:
23904 case DW_OP_breg7:
23905 case DW_OP_breg8:
23906 case DW_OP_breg9:
23907 case DW_OP_breg10:
23908 case DW_OP_breg11:
23909 case DW_OP_breg12:
23910 case DW_OP_breg13:
23911 case DW_OP_breg14:
23912 case DW_OP_breg15:
23913 case DW_OP_breg16:
23914 case DW_OP_breg17:
23915 case DW_OP_breg18:
23916 case DW_OP_breg19:
23917 case DW_OP_breg20:
23918 case DW_OP_breg21:
23919 case DW_OP_breg22:
23920 case DW_OP_breg23:
23921 case DW_OP_breg24:
23922 case DW_OP_breg25:
23923 case DW_OP_breg26:
23924 case DW_OP_breg27:
23925 case DW_OP_breg28:
23926 case DW_OP_breg29:
23927 case DW_OP_breg30:
23928 case DW_OP_breg31:
23929 case DW_OP_regx:
23930 case DW_OP_fbreg:
23931 case DW_OP_piece:
23932 case DW_OP_deref_size:
23933 case DW_OP_xderef_size:
23934 return valx1->v.val_int == valy1->v.val_int;
23935 case DW_OP_skip:
23936 case DW_OP_bra:
23937 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23938 can cause irrelevant differences in dw_loc_addr. */
23939 gcc_assert (valx1->val_class == dw_val_class_loc
23940 && valy1->val_class == dw_val_class_loc
23941 && (dwarf_split_debug_info
23942 || x->dw_loc_addr == y->dw_loc_addr));
23943 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23944 case DW_OP_implicit_value:
23945 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23946 || valx2->val_class != valy2->val_class)
23947 return false;
23948 switch (valx2->val_class)
23949 {
23950 case dw_val_class_const:
23951 return valx2->v.val_int == valy2->v.val_int;
23952 case dw_val_class_vec:
23953 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23954 && valx2->v.val_vec.length == valy2->v.val_vec.length
23955 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23956 valx2->v.val_vec.elt_size
23957 * valx2->v.val_vec.length) == 0;
23958 case dw_val_class_const_double:
23959 return valx2->v.val_double.low == valy2->v.val_double.low
23960 && valx2->v.val_double.high == valy2->v.val_double.high;
23961 case dw_val_class_wide_int:
23962 return *valx2->v.val_wide == *valy2->v.val_wide;
23963 case dw_val_class_addr:
23964 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23965 default:
23966 gcc_unreachable ();
23967 }
23968 case DW_OP_bregx:
23969 case DW_OP_bit_piece:
23970 return valx1->v.val_int == valy1->v.val_int
23971 && valx2->v.val_int == valy2->v.val_int;
23972 case DW_OP_addr:
23973 hash_addr:
23974 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23975 case DW_OP_GNU_addr_index:
23976 case DW_OP_GNU_const_index:
23977 {
23978 rtx ax1 = valx1->val_entry->addr.rtl;
23979 rtx ay1 = valy1->val_entry->addr.rtl;
23980 return rtx_equal_p (ax1, ay1);
23981 }
23982 case DW_OP_GNU_implicit_pointer:
23983 return valx1->val_class == dw_val_class_die_ref
23984 && valx1->val_class == valy1->val_class
23985 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23986 && valx2->v.val_int == valy2->v.val_int;
23987 case DW_OP_GNU_entry_value:
23988 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23989 case DW_OP_GNU_const_type:
23990 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23991 || valx2->val_class != valy2->val_class)
23992 return false;
23993 switch (valx2->val_class)
23994 {
23995 case dw_val_class_const:
23996 return valx2->v.val_int == valy2->v.val_int;
23997 case dw_val_class_vec:
23998 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23999 && valx2->v.val_vec.length == valy2->v.val_vec.length
24000 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24001 valx2->v.val_vec.elt_size
24002 * valx2->v.val_vec.length) == 0;
24003 case dw_val_class_const_double:
24004 return valx2->v.val_double.low == valy2->v.val_double.low
24005 && valx2->v.val_double.high == valy2->v.val_double.high;
24006 case dw_val_class_wide_int:
24007 return *valx2->v.val_wide == *valy2->v.val_wide;
24008 default:
24009 gcc_unreachable ();
24010 }
24011 case DW_OP_GNU_regval_type:
24012 case DW_OP_GNU_deref_type:
24013 return valx1->v.val_int == valy1->v.val_int
24014 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24015 case DW_OP_GNU_convert:
24016 case DW_OP_GNU_reinterpret:
24017 if (valx1->val_class != valy1->val_class)
24018 return false;
24019 if (valx1->val_class == dw_val_class_unsigned_const)
24020 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24021 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24022 case DW_OP_GNU_parameter_ref:
24023 return valx1->val_class == dw_val_class_die_ref
24024 && valx1->val_class == valy1->val_class
24025 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24026 default:
24027 /* Other codes have no operands. */
24028 return true;
24029 }
24030 }
24031
24032 /* Return true if DWARF location expressions X and Y are the same. */
24033
24034 static inline bool
24035 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24036 {
24037 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24038 if (x->dw_loc_opc != y->dw_loc_opc
24039 || x->dtprel != y->dtprel
24040 || !compare_loc_operands (x, y))
24041 break;
24042 return x == NULL && y == NULL;
24043 }
24044
24045 /* Hashtable helpers. */
24046
24047 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24048 {
24049 typedef dw_loc_list_struct value_type;
24050 typedef dw_loc_list_struct compare_type;
24051 static inline hashval_t hash (const value_type *);
24052 static inline bool equal (const value_type *, const compare_type *);
24053 };
24054
24055 /* Return precomputed hash of location list X. */
24056
24057 inline hashval_t
24058 loc_list_hasher::hash (const value_type *x)
24059 {
24060 return x->hash;
24061 }
24062
24063 /* Return true if location lists A and B are the same. */
24064
24065 inline bool
24066 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24067 {
24068 if (a == b)
24069 return 1;
24070 if (a->hash != b->hash)
24071 return 0;
24072 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24073 if (strcmp (a->begin, b->begin) != 0
24074 || strcmp (a->end, b->end) != 0
24075 || (a->section == NULL) != (b->section == NULL)
24076 || (a->section && strcmp (a->section, b->section) != 0)
24077 || !compare_locs (a->expr, b->expr))
24078 break;
24079 return a == NULL && b == NULL;
24080 }
24081
24082 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24083
24084
24085 /* Recursively optimize location lists referenced from DIE
24086 children and share them whenever possible. */
24087
24088 static void
24089 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24090 {
24091 dw_die_ref c;
24092 dw_attr_ref a;
24093 unsigned ix;
24094 dw_loc_list_struct **slot;
24095
24096 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24097 if (AT_class (a) == dw_val_class_loc_list)
24098 {
24099 dw_loc_list_ref list = AT_loc_list (a);
24100 /* TODO: perform some optimizations here, before hashing
24101 it and storing into the hash table. */
24102 hash_loc_list (list);
24103 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24104 if (*slot == NULL)
24105 *slot = list;
24106 else
24107 a->dw_attr_val.v.val_loc_list = *slot;
24108 }
24109
24110 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24111 }
24112
24113
24114 /* Recursively assign each location list a unique index into the debug_addr
24115 section. */
24116
24117 static void
24118 index_location_lists (dw_die_ref die)
24119 {
24120 dw_die_ref c;
24121 dw_attr_ref a;
24122 unsigned ix;
24123
24124 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24125 if (AT_class (a) == dw_val_class_loc_list)
24126 {
24127 dw_loc_list_ref list = AT_loc_list (a);
24128 dw_loc_list_ref curr;
24129 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24130 {
24131 /* Don't index an entry that has already been indexed
24132 or won't be output. */
24133 if (curr->begin_entry != NULL
24134 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24135 continue;
24136
24137 curr->begin_entry
24138 = add_addr_table_entry (xstrdup (curr->begin),
24139 ate_kind_label);
24140 }
24141 }
24142
24143 FOR_EACH_CHILD (die, c, index_location_lists (c));
24144 }
24145
24146 /* Optimize location lists referenced from DIE
24147 children and share them whenever possible. */
24148
24149 static void
24150 optimize_location_lists (dw_die_ref die)
24151 {
24152 loc_list_hash_type htab (500);
24153 optimize_location_lists_1 (die, &htab);
24154 }
24155 \f
24156 /* Output stuff that dwarf requires at the end of every file,
24157 and generate the DWARF-2 debugging info. */
24158
24159 static void
24160 dwarf2out_finish (const char *filename)
24161 {
24162 limbo_die_node *node, *next_node;
24163 comdat_type_node *ctnode;
24164 unsigned int i;
24165 dw_die_ref main_comp_unit_die;
24166
24167 /* PCH might result in DW_AT_producer string being restored from the
24168 header compilation, so always fill it with empty string initially
24169 and overwrite only here. */
24170 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24171 producer_string = gen_producer_string ();
24172 producer->dw_attr_val.v.val_str->refcount--;
24173 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24174
24175 gen_scheduled_generic_parms_dies ();
24176 gen_remaining_tmpl_value_param_die_attribute ();
24177
24178 /* Add the name for the main input file now. We delayed this from
24179 dwarf2out_init to avoid complications with PCH. */
24180 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24181 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24182 add_comp_dir_attribute (comp_unit_die ());
24183 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24184 {
24185 bool p = false;
24186 htab_traverse (file_table, file_table_relative_p, &p);
24187 if (p)
24188 add_comp_dir_attribute (comp_unit_die ());
24189 }
24190
24191 if (deferred_locations_list)
24192 for (i = 0; i < deferred_locations_list->length (); i++)
24193 {
24194 add_location_or_const_value_attribute (
24195 (*deferred_locations_list)[i].die,
24196 (*deferred_locations_list)[i].variable,
24197 false,
24198 DW_AT_location);
24199 }
24200
24201 /* Traverse the limbo die list, and add parent/child links. The only
24202 dies without parents that should be here are concrete instances of
24203 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24204 For concrete instances, we can get the parent die from the abstract
24205 instance. */
24206 for (node = limbo_die_list; node; node = next_node)
24207 {
24208 dw_die_ref die = node->die;
24209 next_node = node->next;
24210
24211 if (die->die_parent == NULL)
24212 {
24213 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24214
24215 if (origin && origin->die_parent)
24216 add_child_die (origin->die_parent, die);
24217 else if (is_cu_die (die))
24218 ;
24219 else if (seen_error ())
24220 /* It's OK to be confused by errors in the input. */
24221 add_child_die (comp_unit_die (), die);
24222 else
24223 {
24224 /* In certain situations, the lexical block containing a
24225 nested function can be optimized away, which results
24226 in the nested function die being orphaned. Likewise
24227 with the return type of that nested function. Force
24228 this to be a child of the containing function.
24229
24230 It may happen that even the containing function got fully
24231 inlined and optimized out. In that case we are lost and
24232 assign the empty child. This should not be big issue as
24233 the function is likely unreachable too. */
24234 gcc_assert (node->created_for);
24235
24236 if (DECL_P (node->created_for))
24237 origin = get_context_die (DECL_CONTEXT (node->created_for));
24238 else if (TYPE_P (node->created_for))
24239 origin = scope_die_for (node->created_for, comp_unit_die ());
24240 else
24241 origin = comp_unit_die ();
24242
24243 add_child_die (origin, die);
24244 }
24245 }
24246 }
24247
24248 limbo_die_list = NULL;
24249
24250 #if ENABLE_ASSERT_CHECKING
24251 {
24252 dw_die_ref die = comp_unit_die (), c;
24253 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24254 }
24255 #endif
24256 resolve_addr (comp_unit_die ());
24257 move_marked_base_types ();
24258
24259 for (node = deferred_asm_name; node; node = node->next)
24260 {
24261 tree decl = node->created_for;
24262 /* When generating LTO bytecode we can not generate new assembler
24263 names at this point and all important decls got theirs via
24264 free-lang-data. */
24265 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24266 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24267 {
24268 add_linkage_attr (node->die, decl);
24269 move_linkage_attr (node->die);
24270 }
24271 }
24272
24273 deferred_asm_name = NULL;
24274
24275 /* Walk through the list of incomplete types again, trying once more to
24276 emit full debugging info for them. */
24277 retry_incomplete_types ();
24278
24279 if (flag_eliminate_unused_debug_types)
24280 prune_unused_types ();
24281
24282 /* Generate separate COMDAT sections for type DIEs. */
24283 if (use_debug_types)
24284 {
24285 break_out_comdat_types (comp_unit_die ());
24286
24287 /* Each new type_unit DIE was added to the limbo die list when created.
24288 Since these have all been added to comdat_type_list, clear the
24289 limbo die list. */
24290 limbo_die_list = NULL;
24291
24292 /* For each new comdat type unit, copy declarations for incomplete
24293 types to make the new unit self-contained (i.e., no direct
24294 references to the main compile unit). */
24295 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24296 copy_decls_for_unworthy_types (ctnode->root_die);
24297 copy_decls_for_unworthy_types (comp_unit_die ());
24298
24299 /* In the process of copying declarations from one unit to another,
24300 we may have left some declarations behind that are no longer
24301 referenced. Prune them. */
24302 prune_unused_types ();
24303 }
24304
24305 /* Generate separate CUs for each of the include files we've seen.
24306 They will go into limbo_die_list. */
24307 if (flag_eliminate_dwarf2_dups)
24308 break_out_includes (comp_unit_die ());
24309
24310 /* Traverse the DIE's and add add sibling attributes to those DIE's
24311 that have children. */
24312 add_sibling_attributes (comp_unit_die ());
24313 for (node = limbo_die_list; node; node = node->next)
24314 add_sibling_attributes (node->die);
24315 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24316 add_sibling_attributes (ctnode->root_die);
24317
24318 /* When splitting DWARF info, we put some attributes in the
24319 skeleton compile_unit DIE that remains in the .o, while
24320 most attributes go in the DWO compile_unit_die. */
24321 if (dwarf_split_debug_info)
24322 main_comp_unit_die = gen_compile_unit_die (NULL);
24323 else
24324 main_comp_unit_die = comp_unit_die ();
24325
24326 /* Output a terminator label for the .text section. */
24327 switch_to_section (text_section);
24328 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24329 if (cold_text_section)
24330 {
24331 switch_to_section (cold_text_section);
24332 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24333 }
24334
24335 /* We can only use the low/high_pc attributes if all of the code was
24336 in .text. */
24337 if (!have_multiple_function_sections
24338 || (dwarf_version < 3 && dwarf_strict))
24339 {
24340 /* Don't add if the CU has no associated code. */
24341 if (text_section_used)
24342 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24343 text_end_label, true);
24344 }
24345 else
24346 {
24347 unsigned fde_idx;
24348 dw_fde_ref fde;
24349 bool range_list_added = false;
24350
24351 if (text_section_used)
24352 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24353 text_end_label, &range_list_added, true);
24354 if (cold_text_section_used)
24355 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24356 cold_end_label, &range_list_added, true);
24357
24358 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24359 {
24360 if (DECL_IGNORED_P (fde->decl))
24361 continue;
24362 if (!fde->in_std_section)
24363 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24364 fde->dw_fde_end, &range_list_added,
24365 true);
24366 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24367 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24368 fde->dw_fde_second_end, &range_list_added,
24369 true);
24370 }
24371
24372 if (range_list_added)
24373 {
24374 /* We need to give .debug_loc and .debug_ranges an appropriate
24375 "base address". Use zero so that these addresses become
24376 absolute. Historically, we've emitted the unexpected
24377 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24378 Emit both to give time for other tools to adapt. */
24379 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24380 if (! dwarf_strict && dwarf_version < 4)
24381 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24382
24383 add_ranges (NULL);
24384 }
24385 }
24386
24387 if (debug_info_level >= DINFO_LEVEL_TERSE)
24388 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24389 debug_line_section_label);
24390
24391 if (have_macinfo)
24392 add_AT_macptr (comp_unit_die (),
24393 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24394 macinfo_section_label);
24395
24396 if (dwarf_split_debug_info)
24397 {
24398 /* optimize_location_lists calculates the size of the lists,
24399 so index them first, and assign indices to the entries.
24400 Although optimize_location_lists will remove entries from
24401 the table, it only does so for duplicates, and therefore
24402 only reduces ref_counts to 1. */
24403 index_location_lists (comp_unit_die ());
24404
24405 if (addr_index_table != NULL)
24406 {
24407 unsigned int index = 0;
24408 htab_traverse_noresize (addr_index_table,
24409 index_addr_table_entry, &index);
24410 }
24411 }
24412
24413 if (have_location_lists)
24414 optimize_location_lists (comp_unit_die ());
24415
24416 save_macinfo_strings ();
24417
24418 if (dwarf_split_debug_info)
24419 {
24420 unsigned int index = 0;
24421
24422 /* Add attributes common to skeleton compile_units and
24423 type_units. Because these attributes include strings, it
24424 must be done before freezing the string table. Top-level
24425 skeleton die attrs are added when the skeleton type unit is
24426 created, so ensure it is created by this point. */
24427 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24428 (void) get_skeleton_type_unit ();
24429 htab_traverse_noresize (debug_str_hash, index_string, &index);
24430 }
24431
24432 /* Output all of the compilation units. We put the main one last so that
24433 the offsets are available to output_pubnames. */
24434 for (node = limbo_die_list; node; node = node->next)
24435 output_comp_unit (node->die, 0);
24436
24437 hash_table<comdat_type_hasher> comdat_type_table (100);
24438 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24439 {
24440 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24441
24442 /* Don't output duplicate types. */
24443 if (*slot != HTAB_EMPTY_ENTRY)
24444 continue;
24445
24446 /* Add a pointer to the line table for the main compilation unit
24447 so that the debugger can make sense of DW_AT_decl_file
24448 attributes. */
24449 if (debug_info_level >= DINFO_LEVEL_TERSE)
24450 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24451 (!dwarf_split_debug_info
24452 ? debug_line_section_label
24453 : debug_skeleton_line_section_label));
24454
24455 output_comdat_type_unit (ctnode);
24456 *slot = ctnode;
24457 }
24458
24459 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24460 both the main_cu and all skeleton TUs. Making this call unconditional
24461 would end up either adding a second copy of the AT_pubnames attribute, or
24462 requiring a special case in add_top_level_skeleton_die_attrs. */
24463 if (!dwarf_split_debug_info)
24464 add_AT_pubnames (comp_unit_die ());
24465
24466 if (dwarf_split_debug_info)
24467 {
24468 int mark;
24469 unsigned char checksum[16];
24470 struct md5_ctx ctx;
24471
24472 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24473 md5_init_ctx (&ctx);
24474 mark = 0;
24475 die_checksum (comp_unit_die (), &ctx, &mark);
24476 unmark_all_dies (comp_unit_die ());
24477 md5_finish_ctx (&ctx, checksum);
24478
24479 /* Use the first 8 bytes of the checksum as the dwo_id,
24480 and add it to both comp-unit DIEs. */
24481 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24482 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24483
24484 /* Add the base offset of the ranges table to the skeleton
24485 comp-unit DIE. */
24486 if (ranges_table_in_use)
24487 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24488 ranges_section_label);
24489
24490 switch_to_section (debug_addr_section);
24491 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24492 output_addr_table ();
24493 }
24494
24495 /* Output the main compilation unit if non-empty or if .debug_macinfo
24496 or .debug_macro will be emitted. */
24497 output_comp_unit (comp_unit_die (), have_macinfo);
24498
24499 if (dwarf_split_debug_info && info_section_emitted)
24500 output_skeleton_debug_sections (main_comp_unit_die);
24501
24502 /* Output the abbreviation table. */
24503 if (abbrev_die_table_in_use != 1)
24504 {
24505 switch_to_section (debug_abbrev_section);
24506 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24507 output_abbrev_section ();
24508 }
24509
24510 /* Output location list section if necessary. */
24511 if (have_location_lists)
24512 {
24513 /* Output the location lists info. */
24514 switch_to_section (debug_loc_section);
24515 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24516 output_location_lists (comp_unit_die ());
24517 }
24518
24519 output_pubtables ();
24520
24521 /* Output the address range information if a CU (.debug_info section)
24522 was emitted. We output an empty table even if we had no functions
24523 to put in it. This because the consumer has no way to tell the
24524 difference between an empty table that we omitted and failure to
24525 generate a table that would have contained data. */
24526 if (info_section_emitted)
24527 {
24528 unsigned long aranges_length = size_of_aranges ();
24529
24530 switch_to_section (debug_aranges_section);
24531 output_aranges (aranges_length);
24532 }
24533
24534 /* Output ranges section if necessary. */
24535 if (ranges_table_in_use)
24536 {
24537 switch_to_section (debug_ranges_section);
24538 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24539 output_ranges ();
24540 }
24541
24542 /* Have to end the macro section. */
24543 if (have_macinfo)
24544 {
24545 switch_to_section (debug_macinfo_section);
24546 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24547 output_macinfo ();
24548 dw2_asm_output_data (1, 0, "End compilation unit");
24549 }
24550
24551 /* Output the source line correspondence table. We must do this
24552 even if there is no line information. Otherwise, on an empty
24553 translation unit, we will generate a present, but empty,
24554 .debug_info section. IRIX 6.5 `nm' will then complain when
24555 examining the file. This is done late so that any filenames
24556 used by the debug_info section are marked as 'used'. */
24557 switch_to_section (debug_line_section);
24558 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24559 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24560 output_line_info (false);
24561
24562 if (dwarf_split_debug_info && info_section_emitted)
24563 {
24564 switch_to_section (debug_skeleton_line_section);
24565 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24566 output_line_info (true);
24567 }
24568
24569 /* If we emitted any indirect strings, output the string table too. */
24570 if (debug_str_hash || skeleton_debug_str_hash)
24571 output_indirect_strings ();
24572 }
24573
24574 #include "gt-dwarf2out.h"