re PR debug/65549 (crash in htab_hash_string with -flto -g)
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
123
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
128
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
131
132 /* Define this macro to be a nonzero value if the directory specifications
133 which are output in the debug info should end with a separator. */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136 from generating indirect strings in DWARF2 debug information, for instance
137 if your target is stuck with an old version of GDB that is unable to
138 process them properly or uses VMS Debug. */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
144
145 /* ??? Poison these here until it can be done generically. They've been
146 totally replaced in this file; make sure it stays that way. */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
152
153 /* The size of the target's pointer type. */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
157
158 /* Array of RTXes referenced by the debugging information, which therefore
159 must be kept around forever. */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
161
162 /* A pointer to the base of a list of incomplete types which might be
163 completed at some later time. incomplete_types_list needs to be a
164 vec<tree, va_gc> *because we want to tell the garbage collector about
165 it. */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
167
168 /* A pointer to the base of a table of references to declaration
169 scopes. This table is a display which tracks the nesting
170 of declaration scopes at the current scope and containing
171 scopes. This table is used to find the proper place to
172 define type declaration DIE's. */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
174
175 /* Pointers to various DWARF2 sections. */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193
194 /* Maximum size (in bytes) of an artificially generated label. */
195 #define MAX_ARTIFICIAL_LABEL_BYTES 30
196
197 /* According to the (draft) DWARF 3 specification, the initial length
198 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
199 bytes are 0xffffffff, followed by the length stored in the next 8
200 bytes.
201
202 However, the SGI/MIPS ABI uses an initial length which is equal to
203 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
204
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
208
209 /* Round SIZE up to the nearest BOUNDARY. */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
212
213 /* CIE identifier. */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
220
221
222 /* A vector for a table that contains frame description
223 information for each routine. */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
226
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
228
229 struct GTY((for_user)) indirect_string_node {
230 const char *str;
231 unsigned int refcount;
232 enum dwarf_form form;
233 char *label;
234 unsigned int index;
235 };
236
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
238 {
239 typedef const char *compare_type;
240
241 static hashval_t hash (indirect_string_node *);
242 static bool equal (indirect_string_node *, const char *);
243 };
244
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
246
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248 main object file, rather than the dwo, similar to the force_direct
249 parameter elsewhere but with additional complications:
250
251 1) The string is needed in both the main object file and the dwo.
252 That is, the comp_dir and dwo_name will appear in both places.
253
254 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255 DW_FORM_GNU_str_index.
256
257 3) GCC chooses the form to use late, depending on the size and
258 reference count.
259
260 Rather than forcing the all debug string handling functions and
261 callers to deal with these complications, simply use a separate,
262 special-cased string table for any attribute that should go in the
263 main object file. This limits the complexity to just the places
264 that need it. */
265
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
267
268 static GTY(()) int dw2_string_counter;
269
270 /* True if the compilation unit places functions in more than one section. */
271 static GTY(()) bool have_multiple_function_sections = false;
272
273 /* Whether the default text and cold text sections have been used at all. */
274
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
277
278 /* The default cold text section. */
279 static GTY(()) section *cold_text_section;
280
281 /* The DIE for C++14 'auto' in a function return type. */
282 static GTY(()) dw_die_ref auto_die;
283
284 /* The DIE for C++14 'decltype(auto)' in a function return type. */
285 static GTY(()) dw_die_ref decltype_auto_die;
286
287 /* Forward declarations for functions defined in this file. */
288
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292
293 /* Personality decl of current unit. Used only when assembler does not support
294 personality CFI. */
295 static GTY(()) rtx current_unit_personality;
296
297 /* Data and reference forms for relocatable data. */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
300
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION ".debug_frame"
303 #endif
304
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL "LFB"
307 #endif
308
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL "LFE"
311 #endif
312
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL "LPE"
315 #endif
316
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL "LEB"
319 #endif
320
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL "Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL "LSCIE"
325 #define CIE_END_LABEL "LECIE"
326 #define FDE_LABEL "LSFDE"
327 #define FDE_AFTER_SIZE_LABEL "LASFDE"
328 #define FDE_END_LABEL "LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
330 #define LINE_NUMBER_END_LABEL "LELT"
331 #define LN_PROLOG_AS_LABEL "LASLTP"
332 #define LN_PROLOG_END_LABEL "LELTP"
333 #define DIE_LABEL_PREFIX "DW"
334 \f
335 /* Match the base name of a file to the base name of a compilation unit. */
336
337 static int
338 matches_main_base (const char *path)
339 {
340 /* Cache the last query. */
341 static const char *last_path = NULL;
342 static int last_match = 0;
343 if (path != last_path)
344 {
345 const char *base;
346 int length = base_of_path (path, &base);
347 last_path = path;
348 last_match = (length == main_input_baselength
349 && memcmp (base, main_input_basename, length) == 0);
350 }
351 return last_match;
352 }
353
354 #ifdef DEBUG_DEBUG_STRUCT
355
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358 enum debug_struct_file criterion, int generic,
359 int matches, int result)
360 {
361 /* Find the type name. */
362 tree type_decl = TYPE_STUB_DECL (type);
363 tree t = type_decl;
364 const char *name = 0;
365 if (TREE_CODE (t) == TYPE_DECL)
366 t = DECL_NAME (t);
367 if (t)
368 name = IDENTIFIER_POINTER (t);
369
370 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
371 criterion,
372 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373 matches ? "bas" : "hdr",
374 generic ? "gen" : "ord",
375 usage == DINFO_USAGE_DFN ? ";" :
376 usage == DINFO_USAGE_DIR_USE ? "." : "*",
377 result,
378 (void*) type_decl, name);
379 return result;
380 }
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382 dump_struct_debug (type, usage, criterion, generic, matches, result)
383
384 #else
385
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 (result)
388
389 #endif
390
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392 of the number. */
393
394 static unsigned int
395 get_full_len (const wide_int &op)
396 {
397 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
398 / HOST_BITS_PER_WIDE_INT);
399 }
400
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
403 {
404 enum debug_struct_file criterion;
405 tree type_decl;
406 bool generic = lang_hooks.types.generic_p (type);
407
408 if (generic)
409 criterion = debug_struct_generic[usage];
410 else
411 criterion = debug_struct_ordinary[usage];
412
413 if (criterion == DINFO_STRUCT_FILE_NONE)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415 if (criterion == DINFO_STRUCT_FILE_ANY)
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
417
418 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
419
420 if (type_decl != NULL)
421 {
422 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
424
425 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427 }
428
429 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 }
431 \f
432 /* Return a pointer to a copy of the section string name S with all
433 attributes stripped off, and an asterisk prepended (for assemble_name). */
434
435 static inline char *
436 stripattributes (const char *s)
437 {
438 char *stripped = XNEWVEC (char, strlen (s) + 2);
439 char *p = stripped;
440
441 *p++ = '*';
442
443 while (*s && *s != ',')
444 *p++ = *s++;
445
446 *p = '\0';
447 return stripped;
448 }
449
450 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
451 switch to the data section instead, and write out a synthetic start label
452 for collect2 the first time around. */
453
454 static void
455 switch_to_eh_frame_section (bool back)
456 {
457 tree label;
458
459 #ifdef EH_FRAME_SECTION_NAME
460 if (eh_frame_section == 0)
461 {
462 int flags;
463
464 if (EH_TABLES_CAN_BE_READ_ONLY)
465 {
466 int fde_encoding;
467 int per_encoding;
468 int lsda_encoding;
469
470 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
471 /*global=*/0);
472 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
473 /*global=*/1);
474 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
475 /*global=*/0);
476 flags = ((! flag_pic
477 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
478 && (fde_encoding & 0x70) != DW_EH_PE_aligned
479 && (per_encoding & 0x70) != DW_EH_PE_absptr
480 && (per_encoding & 0x70) != DW_EH_PE_aligned
481 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
482 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
483 ? 0 : SECTION_WRITE);
484 }
485 else
486 flags = SECTION_WRITE;
487 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
488 }
489 #endif /* EH_FRAME_SECTION_NAME */
490
491 if (eh_frame_section)
492 switch_to_section (eh_frame_section);
493 else
494 {
495 /* We have no special eh_frame section. Put the information in
496 the data section and emit special labels to guide collect2. */
497 switch_to_section (data_section);
498
499 if (!back)
500 {
501 label = get_file_function_name ("F");
502 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
503 targetm.asm_out.globalize_label (asm_out_file,
504 IDENTIFIER_POINTER (label));
505 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
506 }
507 }
508 }
509
510 /* Switch [BACK] to the eh or debug frame table section, depending on
511 FOR_EH. */
512
513 static void
514 switch_to_frame_table_section (int for_eh, bool back)
515 {
516 if (for_eh)
517 switch_to_eh_frame_section (back);
518 else
519 {
520 if (!debug_frame_section)
521 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
522 SECTION_DEBUG, NULL);
523 switch_to_section (debug_frame_section);
524 }
525 }
526
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
528
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
531 {
532 switch (cfi)
533 {
534 case DW_CFA_nop:
535 case DW_CFA_GNU_window_save:
536 case DW_CFA_remember_state:
537 case DW_CFA_restore_state:
538 return dw_cfi_oprnd_unused;
539
540 case DW_CFA_set_loc:
541 case DW_CFA_advance_loc1:
542 case DW_CFA_advance_loc2:
543 case DW_CFA_advance_loc4:
544 case DW_CFA_MIPS_advance_loc8:
545 return dw_cfi_oprnd_addr;
546
547 case DW_CFA_offset:
548 case DW_CFA_offset_extended:
549 case DW_CFA_def_cfa:
550 case DW_CFA_offset_extended_sf:
551 case DW_CFA_def_cfa_sf:
552 case DW_CFA_restore:
553 case DW_CFA_restore_extended:
554 case DW_CFA_undefined:
555 case DW_CFA_same_value:
556 case DW_CFA_def_cfa_register:
557 case DW_CFA_register:
558 case DW_CFA_expression:
559 return dw_cfi_oprnd_reg_num;
560
561 case DW_CFA_def_cfa_offset:
562 case DW_CFA_GNU_args_size:
563 case DW_CFA_def_cfa_offset_sf:
564 return dw_cfi_oprnd_offset;
565
566 case DW_CFA_def_cfa_expression:
567 return dw_cfi_oprnd_loc;
568
569 default:
570 gcc_unreachable ();
571 }
572 }
573
574 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
575
576 enum dw_cfi_oprnd_type
577 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
578 {
579 switch (cfi)
580 {
581 case DW_CFA_def_cfa:
582 case DW_CFA_def_cfa_sf:
583 case DW_CFA_offset:
584 case DW_CFA_offset_extended_sf:
585 case DW_CFA_offset_extended:
586 return dw_cfi_oprnd_offset;
587
588 case DW_CFA_register:
589 return dw_cfi_oprnd_reg_num;
590
591 case DW_CFA_expression:
592 return dw_cfi_oprnd_loc;
593
594 default:
595 return dw_cfi_oprnd_unused;
596 }
597 }
598
599 /* Output one FDE. */
600
601 static void
602 output_fde (dw_fde_ref fde, bool for_eh, bool second,
603 char *section_start_label, int fde_encoding, char *augmentation,
604 bool any_lsda_needed, int lsda_encoding)
605 {
606 const char *begin, *end;
607 static unsigned int j;
608 char l1[20], l2[20];
609
610 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
611 /* empty */ 0);
612 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
613 for_eh + j);
614 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
615 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
616 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
617 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
618 " indicating 64-bit DWARF extension");
619 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
620 "FDE Length");
621 ASM_OUTPUT_LABEL (asm_out_file, l1);
622
623 if (for_eh)
624 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
625 else
626 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
627 debug_frame_section, "FDE CIE offset");
628
629 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
630 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
631
632 if (for_eh)
633 {
634 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
635 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
636 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
637 "FDE initial location");
638 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
639 end, begin, "FDE address range");
640 }
641 else
642 {
643 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
644 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645 }
646
647 if (augmentation[0])
648 {
649 if (any_lsda_needed)
650 {
651 int size = size_of_encoded_value (lsda_encoding);
652
653 if (lsda_encoding == DW_EH_PE_aligned)
654 {
655 int offset = ( 4 /* Length */
656 + 4 /* CIE offset */
657 + 2 * size_of_encoded_value (fde_encoding)
658 + 1 /* Augmentation size */ );
659 int pad = -offset & (PTR_SIZE - 1);
660
661 size += pad;
662 gcc_assert (size_of_uleb128 (size) == 1);
663 }
664
665 dw2_asm_output_data_uleb128 (size, "Augmentation size");
666
667 if (fde->uses_eh_lsda)
668 {
669 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
670 fde->funcdef_number);
671 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
672 gen_rtx_SYMBOL_REF (Pmode, l1),
673 false,
674 "Language Specific Data Area");
675 }
676 else
677 {
678 if (lsda_encoding == DW_EH_PE_aligned)
679 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
680 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
681 "Language Specific Data Area (none)");
682 }
683 }
684 else
685 dw2_asm_output_data_uleb128 (0, "Augmentation size");
686 }
687
688 /* Loop through the Call Frame Instructions associated with this FDE. */
689 fde->dw_fde_current_label = begin;
690 {
691 size_t from, until, i;
692
693 from = 0;
694 until = vec_safe_length (fde->dw_fde_cfi);
695
696 if (fde->dw_fde_second_begin == NULL)
697 ;
698 else if (!second)
699 until = fde->dw_fde_switch_cfi_index;
700 else
701 from = fde->dw_fde_switch_cfi_index;
702
703 for (i = from; i < until; i++)
704 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705 }
706
707 /* If we are to emit a ref/link from function bodies to their frame tables,
708 do it now. This is typically performed to make sure that tables
709 associated with functions are dragged with them and not discarded in
710 garbage collecting links. We need to do this on a per function basis to
711 cope with -ffunction-sections. */
712
713 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
714 /* Switch to the function section, emit the ref to the tables, and
715 switch *back* into the table section. */
716 switch_to_section (function_section (fde->decl));
717 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
718 switch_to_frame_table_section (for_eh, true);
719 #endif
720
721 /* Pad the FDE out to an address sized boundary. */
722 ASM_OUTPUT_ALIGN (asm_out_file,
723 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
724 ASM_OUTPUT_LABEL (asm_out_file, l2);
725
726 j += 2;
727 }
728
729 /* Return true if frame description entry FDE is needed for EH. */
730
731 static bool
732 fde_needed_for_eh_p (dw_fde_ref fde)
733 {
734 if (flag_asynchronous_unwind_tables)
735 return true;
736
737 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
738 return true;
739
740 if (fde->uses_eh_lsda)
741 return true;
742
743 /* If exceptions are enabled, we have collected nothrow info. */
744 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
745 return false;
746
747 return true;
748 }
749
750 /* Output the call frame information used to record information
751 that relates to calculating the frame pointer, and records the
752 location of saved registers. */
753
754 static void
755 output_call_frame_info (int for_eh)
756 {
757 unsigned int i;
758 dw_fde_ref fde;
759 dw_cfi_ref cfi;
760 char l1[20], l2[20], section_start_label[20];
761 bool any_lsda_needed = false;
762 char augmentation[6];
763 int augmentation_size;
764 int fde_encoding = DW_EH_PE_absptr;
765 int per_encoding = DW_EH_PE_absptr;
766 int lsda_encoding = DW_EH_PE_absptr;
767 int return_reg;
768 rtx personality = NULL;
769 int dw_cie_version;
770
771 /* Don't emit a CIE if there won't be any FDEs. */
772 if (!fde_vec)
773 return;
774
775 /* Nothing to do if the assembler's doing it all. */
776 if (dwarf2out_do_cfi_asm ())
777 return;
778
779 /* If we don't have any functions we'll want to unwind out of, don't emit
780 any EH unwind information. If we make FDEs linkonce, we may have to
781 emit an empty label for an FDE that wouldn't otherwise be emitted. We
782 want to avoid having an FDE kept around when the function it refers to
783 is discarded. Example where this matters: a primary function template
784 in C++ requires EH information, an explicit specialization doesn't. */
785 if (for_eh)
786 {
787 bool any_eh_needed = false;
788
789 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790 {
791 if (fde->uses_eh_lsda)
792 any_eh_needed = any_lsda_needed = true;
793 else if (fde_needed_for_eh_p (fde))
794 any_eh_needed = true;
795 else if (TARGET_USES_WEAK_UNWIND_INFO)
796 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
797 }
798
799 if (!any_eh_needed)
800 return;
801 }
802
803 /* We're going to be generating comments, so turn on app. */
804 if (flag_debug_asm)
805 app_enable ();
806
807 /* Switch to the proper frame section, first time. */
808 switch_to_frame_table_section (for_eh, false);
809
810 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
811 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812
813 /* Output the CIE. */
814 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
815 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
816 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
817 dw2_asm_output_data (4, 0xffffffff,
818 "Initial length escape value indicating 64-bit DWARF extension");
819 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
820 "Length of Common Information Entry");
821 ASM_OUTPUT_LABEL (asm_out_file, l1);
822
823 /* Now that the CIE pointer is PC-relative for EH,
824 use 0 to identify the CIE. */
825 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
826 (for_eh ? 0 : DWARF_CIE_ID),
827 "CIE Identifier Tag");
828
829 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830 use CIE version 1, unless that would produce incorrect results
831 due to overflowing the return register column. */
832 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833 dw_cie_version = 1;
834 if (return_reg >= 256 || dwarf_version > 2)
835 dw_cie_version = 3;
836 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
837
838 augmentation[0] = 0;
839 augmentation_size = 0;
840
841 personality = current_unit_personality;
842 if (for_eh)
843 {
844 char *p;
845
846 /* Augmentation:
847 z Indicates that a uleb128 is present to size the
848 augmentation section.
849 L Indicates the encoding (and thus presence) of
850 an LSDA pointer in the FDE augmentation.
851 R Indicates a non-default pointer encoding for
852 FDE code pointers.
853 P Indicates the presence of an encoding + language
854 personality routine in the CIE augmentation. */
855
856 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
859
860 p = augmentation + 1;
861 if (personality)
862 {
863 *p++ = 'P';
864 augmentation_size += 1 + size_of_encoded_value (per_encoding);
865 assemble_external_libcall (personality);
866 }
867 if (any_lsda_needed)
868 {
869 *p++ = 'L';
870 augmentation_size += 1;
871 }
872 if (fde_encoding != DW_EH_PE_absptr)
873 {
874 *p++ = 'R';
875 augmentation_size += 1;
876 }
877 if (p > augmentation + 1)
878 {
879 augmentation[0] = 'z';
880 *p = '\0';
881 }
882
883 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
884 if (personality && per_encoding == DW_EH_PE_aligned)
885 {
886 int offset = ( 4 /* Length */
887 + 4 /* CIE Id */
888 + 1 /* CIE version */
889 + strlen (augmentation) + 1 /* Augmentation */
890 + size_of_uleb128 (1) /* Code alignment */
891 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892 + 1 /* RA column */
893 + 1 /* Augmentation size */
894 + 1 /* Personality encoding */ );
895 int pad = -offset & (PTR_SIZE - 1);
896
897 augmentation_size += pad;
898
899 /* Augmentations should be small, so there's scarce need to
900 iterate for a solution. Die if we exceed one uleb128 byte. */
901 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
902 }
903 }
904
905 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906 if (dw_cie_version >= 4)
907 {
908 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909 dw2_asm_output_data (1, 0, "CIE Segment Size");
910 }
911 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913 "CIE Data Alignment Factor");
914
915 if (dw_cie_version == 1)
916 dw2_asm_output_data (1, return_reg, "CIE RA Column");
917 else
918 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
919
920 if (augmentation[0])
921 {
922 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923 if (personality)
924 {
925 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926 eh_data_format_name (per_encoding));
927 dw2_asm_output_encoded_addr_rtx (per_encoding,
928 personality,
929 true, NULL);
930 }
931
932 if (any_lsda_needed)
933 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934 eh_data_format_name (lsda_encoding));
935
936 if (fde_encoding != DW_EH_PE_absptr)
937 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938 eh_data_format_name (fde_encoding));
939 }
940
941 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942 output_cfi (cfi, NULL, for_eh);
943
944 /* Pad the CIE out to an address sized boundary. */
945 ASM_OUTPUT_ALIGN (asm_out_file,
946 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947 ASM_OUTPUT_LABEL (asm_out_file, l2);
948
949 /* Loop through all of the FDE's. */
950 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
951 {
952 unsigned int k;
953
954 /* Don't emit EH unwind info for leaf functions that don't need it. */
955 if (for_eh && !fde_needed_for_eh_p (fde))
956 continue;
957
958 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960 augmentation, any_lsda_needed, lsda_encoding);
961 }
962
963 if (for_eh && targetm.terminate_dw2_eh_frame_info)
964 dw2_asm_output_data (4, 0, "End of Table");
965
966 /* Turn off app to make assembly quicker. */
967 if (flag_debug_asm)
968 app_disable ();
969 }
970
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
972
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
975 {
976 int enc;
977 rtx ref;
978 rtx personality = get_personality_function (current_function_decl);
979
980 fprintf (asm_out_file, "\t.cfi_startproc\n");
981
982 if (personality)
983 {
984 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
985 ref = personality;
986
987 /* ??? The GAS support isn't entirely consistent. We have to
988 handle indirect support ourselves, but PC-relative is done
989 in the assembler. Further, the assembler can't handle any
990 of the weirder relocation types. */
991 if (enc & DW_EH_PE_indirect)
992 ref = dw2_force_const_mem (ref, true);
993
994 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
995 output_addr_const (asm_out_file, ref);
996 fputc ('\n', asm_out_file);
997 }
998
999 if (crtl->uses_eh_lsda)
1000 {
1001 char lab[20];
1002
1003 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1004 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1005 current_function_funcdef_no);
1006 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1007 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008
1009 if (enc & DW_EH_PE_indirect)
1010 ref = dw2_force_const_mem (ref, true);
1011
1012 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1013 output_addr_const (asm_out_file, ref);
1014 fputc ('\n', asm_out_file);
1015 }
1016 }
1017
1018 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1019 this allocation may be done before pass_final. */
1020
1021 dw_fde_ref
1022 dwarf2out_alloc_current_fde (void)
1023 {
1024 dw_fde_ref fde;
1025
1026 fde = ggc_cleared_alloc<dw_fde_node> ();
1027 fde->decl = current_function_decl;
1028 fde->funcdef_number = current_function_funcdef_no;
1029 fde->fde_index = vec_safe_length (fde_vec);
1030 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1031 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1032 fde->nothrow = crtl->nothrow;
1033 fde->drap_reg = INVALID_REGNUM;
1034 fde->vdrap_reg = INVALID_REGNUM;
1035
1036 /* Record the FDE associated with this function. */
1037 cfun->fde = fde;
1038 vec_safe_push (fde_vec, fde);
1039
1040 return fde;
1041 }
1042
1043 /* Output a marker (i.e. a label) for the beginning of a function, before
1044 the prologue. */
1045
1046 void
1047 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1048 const char *file ATTRIBUTE_UNUSED)
1049 {
1050 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 char * dup_label;
1052 dw_fde_ref fde;
1053 section *fnsec;
1054 bool do_frame;
1055
1056 current_function_func_begin_label = NULL;
1057
1058 do_frame = dwarf2out_do_frame ();
1059
1060 /* ??? current_function_func_begin_label is also used by except.c for
1061 call-site information. We must emit this label if it might be used. */
1062 if (!do_frame
1063 && (!flag_exceptions
1064 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065 return;
1066
1067 fnsec = function_section (current_function_decl);
1068 switch_to_section (fnsec);
1069 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 dup_label = xstrdup (label);
1074 current_function_func_begin_label = dup_label;
1075
1076 /* We can elide the fde allocation if we're not emitting debug info. */
1077 if (!do_frame)
1078 return;
1079
1080 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082 would include pass_dwarf2_frame. If we've not created the FDE yet,
1083 do so now. */
1084 fde = cfun->fde;
1085 if (fde == NULL)
1086 fde = dwarf2out_alloc_current_fde ();
1087
1088 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1089 fde->dw_fde_begin = dup_label;
1090 fde->dw_fde_current_label = dup_label;
1091 fde->in_std_section = (fnsec == text_section
1092 || (cold_text_section && fnsec == cold_text_section));
1093
1094 /* We only want to output line number information for the genuine dwarf2
1095 prologue case, not the eh frame case. */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097 if (file)
1098 dwarf2out_source_line (line, file, 0, true);
1099 #endif
1100
1101 if (dwarf2out_do_cfi_asm ())
1102 dwarf2out_do_cfi_startproc (false);
1103 else
1104 {
1105 rtx personality = get_personality_function (current_function_decl);
1106 if (!current_unit_personality)
1107 current_unit_personality = personality;
1108
1109 /* We cannot keep a current personality per function as without CFI
1110 asm, at the point where we emit the CFI data, there is no current
1111 function anymore. */
1112 if (personality && current_unit_personality != personality)
1113 sorry ("multiple EH personalities are supported only with assemblers "
1114 "supporting .cfi_personality directive");
1115 }
1116 }
1117
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119 for a function prologue. This gets called *after* the prologue code has
1120 been generated. */
1121
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124 const char *file ATTRIBUTE_UNUSED)
1125 {
1126 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127
1128 /* Output a label to mark the endpoint of the code generated for this
1129 function. */
1130 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131 current_function_funcdef_no);
1132 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133 current_function_funcdef_no);
1134 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1135 }
1136
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138 for a function epilogue. This gets called *before* the prologue code has
1139 been generated. */
1140
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143 const char *file ATTRIBUTE_UNUSED)
1144 {
1145 dw_fde_ref fde = cfun->fde;
1146 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147
1148 if (fde->dw_fde_vms_begin_epilogue)
1149 return;
1150
1151 /* Output a label to mark the endpoint of the code generated for this
1152 function. */
1153 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154 current_function_funcdef_no);
1155 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156 current_function_funcdef_no);
1157 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1158 }
1159
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161 for a function definition. This gets called *after* the epilogue code has
1162 been generated. */
1163
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166 const char *file ATTRIBUTE_UNUSED)
1167 {
1168 dw_fde_ref fde;
1169 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1170
1171 last_var_location_insn = NULL;
1172 cached_next_real_insn = NULL;
1173
1174 if (dwarf2out_do_cfi_asm ())
1175 fprintf (asm_out_file, "\t.cfi_endproc\n");
1176
1177 /* Output a label to mark the endpoint of the code generated for this
1178 function. */
1179 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180 current_function_funcdef_no);
1181 ASM_OUTPUT_LABEL (asm_out_file, label);
1182 fde = cfun->fde;
1183 gcc_assert (fde != NULL);
1184 if (fde->dw_fde_second_begin == NULL)
1185 fde->dw_fde_end = xstrdup (label);
1186 }
1187
1188 void
1189 dwarf2out_frame_finish (void)
1190 {
1191 /* Output call frame information. */
1192 if (targetm.debug_unwind_info () == UI_DWARF2)
1193 output_call_frame_info (0);
1194
1195 /* Output another copy for the unwinder. */
1196 if ((flag_unwind_tables || flag_exceptions)
1197 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1198 output_call_frame_info (1);
1199 }
1200
1201 /* Note that the current function section is being used for code. */
1202
1203 static void
1204 dwarf2out_note_section_used (void)
1205 {
1206 section *sec = current_function_section ();
1207 if (sec == text_section)
1208 text_section_used = true;
1209 else if (sec == cold_text_section)
1210 cold_text_section_used = true;
1211 }
1212
1213 static void var_location_switch_text_section (void);
1214 static void set_cur_line_info_table (section *);
1215
1216 void
1217 dwarf2out_switch_text_section (void)
1218 {
1219 section *sect;
1220 dw_fde_ref fde = cfun->fde;
1221
1222 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1223
1224 if (!in_cold_section_p)
1225 {
1226 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1227 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1228 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1229 }
1230 else
1231 {
1232 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1233 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1234 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1235 }
1236 have_multiple_function_sections = true;
1237
1238 /* There is no need to mark used sections when not debugging. */
1239 if (cold_text_section != NULL)
1240 dwarf2out_note_section_used ();
1241
1242 if (dwarf2out_do_cfi_asm ())
1243 fprintf (asm_out_file, "\t.cfi_endproc\n");
1244
1245 /* Now do the real section switch. */
1246 sect = current_function_section ();
1247 switch_to_section (sect);
1248
1249 fde->second_in_std_section
1250 = (sect == text_section
1251 || (cold_text_section && sect == cold_text_section));
1252
1253 if (dwarf2out_do_cfi_asm ())
1254 dwarf2out_do_cfi_startproc (true);
1255
1256 var_location_switch_text_section ();
1257
1258 if (cold_text_section != NULL)
1259 set_cur_line_info_table (sect);
1260 }
1261 \f
1262 /* And now, the subset of the debugging information support code necessary
1263 for emitting location expressions. */
1264
1265 /* Data about a single source file. */
1266 struct GTY((for_user)) dwarf_file_data {
1267 const char * filename;
1268 int emitted_number;
1269 };
1270
1271 typedef struct GTY(()) deferred_locations_struct
1272 {
1273 tree variable;
1274 dw_die_ref die;
1275 } deferred_locations;
1276
1277
1278 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1279
1280
1281 /* Describe an entry into the .debug_addr section. */
1282
1283 enum ate_kind {
1284 ate_kind_rtx,
1285 ate_kind_rtx_dtprel,
1286 ate_kind_label
1287 };
1288
1289 typedef struct GTY((for_user)) addr_table_entry_struct {
1290 enum ate_kind kind;
1291 unsigned int refcount;
1292 unsigned int index;
1293 union addr_table_entry_struct_union
1294 {
1295 rtx GTY ((tag ("0"))) rtl;
1296 char * GTY ((tag ("1"))) label;
1297 }
1298 GTY ((desc ("%1.kind"))) addr;
1299 }
1300 addr_table_entry;
1301
1302 /* Location lists are ranges + location descriptions for that range,
1303 so you can track variables that are in different places over
1304 their entire life. */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306 dw_loc_list_ref dw_loc_next;
1307 const char *begin; /* Label and addr_entry for start of range */
1308 addr_table_entry *begin_entry;
1309 const char *end; /* Label for end of range */
1310 char *ll_symbol; /* Label for beginning of location list.
1311 Only on head of list */
1312 const char *section; /* Section this loclist is relative to */
1313 dw_loc_descr_ref expr;
1314 hashval_t hash;
1315 /* True if all addresses in this and subsequent lists are known to be
1316 resolved. */
1317 bool resolved_addr;
1318 /* True if this list has been replaced by dw_loc_next. */
1319 bool replaced;
1320 bool emitted;
1321 /* True if the range should be emitted even if begin and end
1322 are the same. */
1323 bool force;
1324 } dw_loc_list_node;
1325
1326 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1327
1328 /* Convert a DWARF stack opcode into its string name. */
1329
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1332 {
1333 const char *name = get_DW_OP_name (op);
1334
1335 if (name != NULL)
1336 return name;
1337
1338 return "OP_<unknown>";
1339 }
1340
1341 /* Return a pointer to a newly allocated location description. Location
1342 descriptions are simple expression terms that can be strung
1343 together to form more complicated location (address) descriptions. */
1344
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347 unsigned HOST_WIDE_INT oprnd2)
1348 {
1349 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1350
1351 descr->dw_loc_opc = op;
1352 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd1.val_entry = NULL;
1354 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356 descr->dw_loc_oprnd2.val_entry = NULL;
1357 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1358
1359 return descr;
1360 }
1361
1362 /* Return a pointer to a newly allocated location description for
1363 REG and OFFSET. */
1364
1365 static inline dw_loc_descr_ref
1366 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1367 {
1368 if (reg <= 31)
1369 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1370 offset, 0);
1371 else
1372 return new_loc_descr (DW_OP_bregx, reg, offset);
1373 }
1374
1375 /* Add a location description term to a location description expression. */
1376
1377 static inline void
1378 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1379 {
1380 dw_loc_descr_ref *d;
1381
1382 /* Find the end of the chain. */
1383 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1384 ;
1385
1386 *d = descr;
1387 }
1388
1389 /* Compare two location operands for exact equality. */
1390
1391 static bool
1392 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1393 {
1394 if (a->val_class != b->val_class)
1395 return false;
1396 switch (a->val_class)
1397 {
1398 case dw_val_class_none:
1399 return true;
1400 case dw_val_class_addr:
1401 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1402
1403 case dw_val_class_offset:
1404 case dw_val_class_unsigned_const:
1405 case dw_val_class_const:
1406 case dw_val_class_range_list:
1407 case dw_val_class_lineptr:
1408 case dw_val_class_macptr:
1409 /* These are all HOST_WIDE_INT, signed or unsigned. */
1410 return a->v.val_unsigned == b->v.val_unsigned;
1411
1412 case dw_val_class_loc:
1413 return a->v.val_loc == b->v.val_loc;
1414 case dw_val_class_loc_list:
1415 return a->v.val_loc_list == b->v.val_loc_list;
1416 case dw_val_class_die_ref:
1417 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1418 case dw_val_class_fde_ref:
1419 return a->v.val_fde_index == b->v.val_fde_index;
1420 case dw_val_class_lbl_id:
1421 case dw_val_class_high_pc:
1422 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423 case dw_val_class_str:
1424 return a->v.val_str == b->v.val_str;
1425 case dw_val_class_flag:
1426 return a->v.val_flag == b->v.val_flag;
1427 case dw_val_class_file:
1428 return a->v.val_file == b->v.val_file;
1429 case dw_val_class_decl_ref:
1430 return a->v.val_decl_ref == b->v.val_decl_ref;
1431
1432 case dw_val_class_const_double:
1433 return (a->v.val_double.high == b->v.val_double.high
1434 && a->v.val_double.low == b->v.val_double.low);
1435
1436 case dw_val_class_wide_int:
1437 return *a->v.val_wide == *b->v.val_wide;
1438
1439 case dw_val_class_vec:
1440 {
1441 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1443
1444 return (a_len == b_len
1445 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1446 }
1447
1448 case dw_val_class_data8:
1449 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1450
1451 case dw_val_class_vms_delta:
1452 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1454 }
1455 gcc_unreachable ();
1456 }
1457
1458 /* Compare two location atoms for exact equality. */
1459
1460 static bool
1461 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462 {
1463 if (a->dw_loc_opc != b->dw_loc_opc)
1464 return false;
1465
1466 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467 address size, but since we always allocate cleared storage it
1468 should be zero for other types of locations. */
1469 if (a->dtprel != b->dtprel)
1470 return false;
1471
1472 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474 }
1475
1476 /* Compare two complete location expressions for exact equality. */
1477
1478 bool
1479 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1480 {
1481 while (1)
1482 {
1483 if (a == b)
1484 return true;
1485 if (a == NULL || b == NULL)
1486 return false;
1487 if (!loc_descr_equal_p_1 (a, b))
1488 return false;
1489
1490 a = a->dw_loc_next;
1491 b = b->dw_loc_next;
1492 }
1493 }
1494
1495
1496 /* Add a constant OFFSET to a location expression. */
1497
1498 static void
1499 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1500 {
1501 dw_loc_descr_ref loc;
1502 HOST_WIDE_INT *p;
1503
1504 gcc_assert (*list_head != NULL);
1505
1506 if (!offset)
1507 return;
1508
1509 /* Find the end of the chain. */
1510 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511 ;
1512
1513 p = NULL;
1514 if (loc->dw_loc_opc == DW_OP_fbreg
1515 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516 p = &loc->dw_loc_oprnd1.v.val_int;
1517 else if (loc->dw_loc_opc == DW_OP_bregx)
1518 p = &loc->dw_loc_oprnd2.v.val_int;
1519
1520 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521 offset. Don't optimize if an signed integer overflow would happen. */
1522 if (p != NULL
1523 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525 *p += offset;
1526
1527 else if (offset > 0)
1528 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1529
1530 else
1531 {
1532 loc->dw_loc_next = int_loc_descriptor (-offset);
1533 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 }
1535 }
1536
1537 /* Add a constant OFFSET to a location list. */
1538
1539 static void
1540 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1541 {
1542 dw_loc_list_ref d;
1543 for (d = list_head; d != NULL; d = d->dw_loc_next)
1544 loc_descr_plus_const (&d->expr, offset);
1545 }
1546
1547 #define DWARF_REF_SIZE \
1548 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1549
1550 static unsigned long int get_base_type_offset (dw_die_ref);
1551
1552 /* Return the size of a location descriptor. */
1553
1554 static unsigned long
1555 size_of_loc_descr (dw_loc_descr_ref loc)
1556 {
1557 unsigned long size = 1;
1558
1559 switch (loc->dw_loc_opc)
1560 {
1561 case DW_OP_addr:
1562 size += DWARF2_ADDR_SIZE;
1563 break;
1564 case DW_OP_GNU_addr_index:
1565 case DW_OP_GNU_const_index:
1566 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1568 break;
1569 case DW_OP_const1u:
1570 case DW_OP_const1s:
1571 size += 1;
1572 break;
1573 case DW_OP_const2u:
1574 case DW_OP_const2s:
1575 size += 2;
1576 break;
1577 case DW_OP_const4u:
1578 case DW_OP_const4s:
1579 size += 4;
1580 break;
1581 case DW_OP_const8u:
1582 case DW_OP_const8s:
1583 size += 8;
1584 break;
1585 case DW_OP_constu:
1586 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587 break;
1588 case DW_OP_consts:
1589 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590 break;
1591 case DW_OP_pick:
1592 size += 1;
1593 break;
1594 case DW_OP_plus_uconst:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 break;
1597 case DW_OP_skip:
1598 case DW_OP_bra:
1599 size += 2;
1600 break;
1601 case DW_OP_breg0:
1602 case DW_OP_breg1:
1603 case DW_OP_breg2:
1604 case DW_OP_breg3:
1605 case DW_OP_breg4:
1606 case DW_OP_breg5:
1607 case DW_OP_breg6:
1608 case DW_OP_breg7:
1609 case DW_OP_breg8:
1610 case DW_OP_breg9:
1611 case DW_OP_breg10:
1612 case DW_OP_breg11:
1613 case DW_OP_breg12:
1614 case DW_OP_breg13:
1615 case DW_OP_breg14:
1616 case DW_OP_breg15:
1617 case DW_OP_breg16:
1618 case DW_OP_breg17:
1619 case DW_OP_breg18:
1620 case DW_OP_breg19:
1621 case DW_OP_breg20:
1622 case DW_OP_breg21:
1623 case DW_OP_breg22:
1624 case DW_OP_breg23:
1625 case DW_OP_breg24:
1626 case DW_OP_breg25:
1627 case DW_OP_breg26:
1628 case DW_OP_breg27:
1629 case DW_OP_breg28:
1630 case DW_OP_breg29:
1631 case DW_OP_breg30:
1632 case DW_OP_breg31:
1633 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1634 break;
1635 case DW_OP_regx:
1636 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637 break;
1638 case DW_OP_fbreg:
1639 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1640 break;
1641 case DW_OP_bregx:
1642 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1643 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644 break;
1645 case DW_OP_piece:
1646 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647 break;
1648 case DW_OP_bit_piece:
1649 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1650 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1651 break;
1652 case DW_OP_deref_size:
1653 case DW_OP_xderef_size:
1654 size += 1;
1655 break;
1656 case DW_OP_call2:
1657 size += 2;
1658 break;
1659 case DW_OP_call4:
1660 size += 4;
1661 break;
1662 case DW_OP_call_ref:
1663 size += DWARF_REF_SIZE;
1664 break;
1665 case DW_OP_implicit_value:
1666 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1667 + loc->dw_loc_oprnd1.v.val_unsigned;
1668 break;
1669 case DW_OP_GNU_implicit_pointer:
1670 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1671 break;
1672 case DW_OP_GNU_entry_value:
1673 {
1674 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1675 size += size_of_uleb128 (op_size) + op_size;
1676 break;
1677 }
1678 case DW_OP_GNU_const_type:
1679 {
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1682 size += size_of_uleb128 (o) + 1;
1683 switch (loc->dw_loc_oprnd2.val_class)
1684 {
1685 case dw_val_class_vec:
1686 size += loc->dw_loc_oprnd2.v.val_vec.length
1687 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1688 break;
1689 case dw_val_class_const:
1690 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1691 break;
1692 case dw_val_class_const_double:
1693 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1694 break;
1695 case dw_val_class_wide_int:
1696 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1697 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1698 break;
1699 default:
1700 gcc_unreachable ();
1701 }
1702 break;
1703 }
1704 case DW_OP_GNU_regval_type:
1705 {
1706 unsigned long o
1707 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1709 + size_of_uleb128 (o);
1710 }
1711 break;
1712 case DW_OP_GNU_deref_type:
1713 {
1714 unsigned long o
1715 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1716 size += 1 + size_of_uleb128 (o);
1717 }
1718 break;
1719 case DW_OP_GNU_convert:
1720 case DW_OP_GNU_reinterpret:
1721 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1722 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1723 else
1724 {
1725 unsigned long o
1726 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1727 size += size_of_uleb128 (o);
1728 }
1729 break;
1730 case DW_OP_GNU_parameter_ref:
1731 size += 4;
1732 break;
1733 default:
1734 break;
1735 }
1736
1737 return size;
1738 }
1739
1740 /* Return the size of a series of location descriptors. */
1741
1742 unsigned long
1743 size_of_locs (dw_loc_descr_ref loc)
1744 {
1745 dw_loc_descr_ref l;
1746 unsigned long size;
1747
1748 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1749 field, to avoid writing to a PCH file. */
1750 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1751 {
1752 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1753 break;
1754 size += size_of_loc_descr (l);
1755 }
1756 if (! l)
1757 return size;
1758
1759 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1760 {
1761 l->dw_loc_addr = size;
1762 size += size_of_loc_descr (l);
1763 }
1764
1765 return size;
1766 }
1767
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1771
1772 /* Output location description stack opcode's operands (if any).
1773 The for_eh_or_skip parameter controls whether register numbers are
1774 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776 info). This should be suppressed for the cases that have not been converted
1777 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1778
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 {
1782 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784
1785 switch (loc->dw_loc_opc)
1786 {
1787 #ifdef DWARF2_DEBUGGING_INFO
1788 case DW_OP_const2u:
1789 case DW_OP_const2s:
1790 dw2_asm_output_data (2, val1->v.val_int, NULL);
1791 break;
1792 case DW_OP_const4u:
1793 if (loc->dtprel)
1794 {
1795 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797 val1->v.val_addr);
1798 fputc ('\n', asm_out_file);
1799 break;
1800 }
1801 /* FALLTHRU */
1802 case DW_OP_const4s:
1803 dw2_asm_output_data (4, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_const8u:
1806 if (loc->dtprel)
1807 {
1808 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810 val1->v.val_addr);
1811 fputc ('\n', asm_out_file);
1812 break;
1813 }
1814 /* FALLTHRU */
1815 case DW_OP_const8s:
1816 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817 dw2_asm_output_data (8, val1->v.val_int, NULL);
1818 break;
1819 case DW_OP_skip:
1820 case DW_OP_bra:
1821 {
1822 int offset;
1823
1824 gcc_assert (val1->val_class == dw_val_class_loc);
1825 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826
1827 dw2_asm_output_data (2, offset, NULL);
1828 }
1829 break;
1830 case DW_OP_implicit_value:
1831 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832 switch (val2->val_class)
1833 {
1834 case dw_val_class_const:
1835 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836 break;
1837 case dw_val_class_vec:
1838 {
1839 unsigned int elt_size = val2->v.val_vec.elt_size;
1840 unsigned int len = val2->v.val_vec.length;
1841 unsigned int i;
1842 unsigned char *p;
1843
1844 if (elt_size > sizeof (HOST_WIDE_INT))
1845 {
1846 elt_size /= 2;
1847 len *= 2;
1848 }
1849 for (i = 0, p = val2->v.val_vec.array;
1850 i < len;
1851 i++, p += elt_size)
1852 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853 "fp or vector constant word %u", i);
1854 }
1855 break;
1856 case dw_val_class_const_double:
1857 {
1858 unsigned HOST_WIDE_INT first, second;
1859
1860 if (WORDS_BIG_ENDIAN)
1861 {
1862 first = val2->v.val_double.high;
1863 second = val2->v.val_double.low;
1864 }
1865 else
1866 {
1867 first = val2->v.val_double.low;
1868 second = val2->v.val_double.high;
1869 }
1870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871 first, NULL);
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 second, NULL);
1874 }
1875 break;
1876 case dw_val_class_wide_int:
1877 {
1878 int i;
1879 int len = get_full_len (*val2->v.val_wide);
1880 if (WORDS_BIG_ENDIAN)
1881 for (i = len - 1; i >= 0; --i)
1882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883 val2->v.val_wide->elt (i), NULL);
1884 else
1885 for (i = 0; i < len; ++i)
1886 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887 val2->v.val_wide->elt (i), NULL);
1888 }
1889 break;
1890 case dw_val_class_addr:
1891 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1892 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893 break;
1894 default:
1895 gcc_unreachable ();
1896 }
1897 break;
1898 #else
1899 case DW_OP_const2u:
1900 case DW_OP_const2s:
1901 case DW_OP_const4u:
1902 case DW_OP_const4s:
1903 case DW_OP_const8u:
1904 case DW_OP_const8s:
1905 case DW_OP_skip:
1906 case DW_OP_bra:
1907 case DW_OP_implicit_value:
1908 /* We currently don't make any attempt to make sure these are
1909 aligned properly like we do for the main unwind info, so
1910 don't support emitting things larger than a byte if we're
1911 only doing unwinding. */
1912 gcc_unreachable ();
1913 #endif
1914 case DW_OP_const1u:
1915 case DW_OP_const1s:
1916 dw2_asm_output_data (1, val1->v.val_int, NULL);
1917 break;
1918 case DW_OP_constu:
1919 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920 break;
1921 case DW_OP_consts:
1922 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923 break;
1924 case DW_OP_pick:
1925 dw2_asm_output_data (1, val1->v.val_int, NULL);
1926 break;
1927 case DW_OP_plus_uconst:
1928 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929 break;
1930 case DW_OP_breg0:
1931 case DW_OP_breg1:
1932 case DW_OP_breg2:
1933 case DW_OP_breg3:
1934 case DW_OP_breg4:
1935 case DW_OP_breg5:
1936 case DW_OP_breg6:
1937 case DW_OP_breg7:
1938 case DW_OP_breg8:
1939 case DW_OP_breg9:
1940 case DW_OP_breg10:
1941 case DW_OP_breg11:
1942 case DW_OP_breg12:
1943 case DW_OP_breg13:
1944 case DW_OP_breg14:
1945 case DW_OP_breg15:
1946 case DW_OP_breg16:
1947 case DW_OP_breg17:
1948 case DW_OP_breg18:
1949 case DW_OP_breg19:
1950 case DW_OP_breg20:
1951 case DW_OP_breg21:
1952 case DW_OP_breg22:
1953 case DW_OP_breg23:
1954 case DW_OP_breg24:
1955 case DW_OP_breg25:
1956 case DW_OP_breg26:
1957 case DW_OP_breg27:
1958 case DW_OP_breg28:
1959 case DW_OP_breg29:
1960 case DW_OP_breg30:
1961 case DW_OP_breg31:
1962 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963 break;
1964 case DW_OP_regx:
1965 {
1966 unsigned r = val1->v.val_unsigned;
1967 if (for_eh_or_skip >= 0)
1968 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969 gcc_assert (size_of_uleb128 (r)
1970 == size_of_uleb128 (val1->v.val_unsigned));
1971 dw2_asm_output_data_uleb128 (r, NULL);
1972 }
1973 break;
1974 case DW_OP_fbreg:
1975 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976 break;
1977 case DW_OP_bregx:
1978 {
1979 unsigned r = val1->v.val_unsigned;
1980 if (for_eh_or_skip >= 0)
1981 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982 gcc_assert (size_of_uleb128 (r)
1983 == size_of_uleb128 (val1->v.val_unsigned));
1984 dw2_asm_output_data_uleb128 (r, NULL);
1985 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986 }
1987 break;
1988 case DW_OP_piece:
1989 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990 break;
1991 case DW_OP_bit_piece:
1992 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994 break;
1995 case DW_OP_deref_size:
1996 case DW_OP_xderef_size:
1997 dw2_asm_output_data (1, val1->v.val_int, NULL);
1998 break;
1999
2000 case DW_OP_addr:
2001 if (loc->dtprel)
2002 {
2003 if (targetm.asm_out.output_dwarf_dtprel)
2004 {
2005 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006 DWARF2_ADDR_SIZE,
2007 val1->v.val_addr);
2008 fputc ('\n', asm_out_file);
2009 }
2010 else
2011 gcc_unreachable ();
2012 }
2013 else
2014 {
2015 #ifdef DWARF2_DEBUGGING_INFO
2016 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018 gcc_unreachable ();
2019 #endif
2020 }
2021 break;
2022
2023 case DW_OP_GNU_addr_index:
2024 case DW_OP_GNU_const_index:
2025 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027 "(index into .debug_addr)");
2028 break;
2029
2030 case DW_OP_GNU_implicit_pointer:
2031 {
2032 char label[MAX_ARTIFICIAL_LABEL_BYTES
2033 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2034 gcc_assert (val1->val_class == dw_val_class_die_ref);
2035 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2036 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2037 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2038 }
2039 break;
2040
2041 case DW_OP_GNU_entry_value:
2042 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2043 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2044 break;
2045
2046 case DW_OP_GNU_const_type:
2047 {
2048 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2049 gcc_assert (o);
2050 dw2_asm_output_data_uleb128 (o, NULL);
2051 switch (val2->val_class)
2052 {
2053 case dw_val_class_const:
2054 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055 dw2_asm_output_data (1, l, NULL);
2056 dw2_asm_output_data (l, val2->v.val_int, NULL);
2057 break;
2058 case dw_val_class_vec:
2059 {
2060 unsigned int elt_size = val2->v.val_vec.elt_size;
2061 unsigned int len = val2->v.val_vec.length;
2062 unsigned int i;
2063 unsigned char *p;
2064
2065 l = len * elt_size;
2066 dw2_asm_output_data (1, l, NULL);
2067 if (elt_size > sizeof (HOST_WIDE_INT))
2068 {
2069 elt_size /= 2;
2070 len *= 2;
2071 }
2072 for (i = 0, p = val2->v.val_vec.array;
2073 i < len;
2074 i++, p += elt_size)
2075 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2076 "fp or vector constant word %u", i);
2077 }
2078 break;
2079 case dw_val_class_const_double:
2080 {
2081 unsigned HOST_WIDE_INT first, second;
2082 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2083
2084 dw2_asm_output_data (1, 2 * l, NULL);
2085 if (WORDS_BIG_ENDIAN)
2086 {
2087 first = val2->v.val_double.high;
2088 second = val2->v.val_double.low;
2089 }
2090 else
2091 {
2092 first = val2->v.val_double.low;
2093 second = val2->v.val_double.high;
2094 }
2095 dw2_asm_output_data (l, first, NULL);
2096 dw2_asm_output_data (l, second, NULL);
2097 }
2098 break;
2099 case dw_val_class_wide_int:
2100 {
2101 int i;
2102 int len = get_full_len (*val2->v.val_wide);
2103 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2104
2105 dw2_asm_output_data (1, len * l, NULL);
2106 if (WORDS_BIG_ENDIAN)
2107 for (i = len - 1; i >= 0; --i)
2108 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2109 else
2110 for (i = 0; i < len; ++i)
2111 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2112 }
2113 break;
2114 default:
2115 gcc_unreachable ();
2116 }
2117 }
2118 break;
2119 case DW_OP_GNU_regval_type:
2120 {
2121 unsigned r = val1->v.val_unsigned;
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 if (for_eh_or_skip >= 0)
2125 {
2126 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2127 gcc_assert (size_of_uleb128 (r)
2128 == size_of_uleb128 (val1->v.val_unsigned));
2129 }
2130 dw2_asm_output_data_uleb128 (r, NULL);
2131 dw2_asm_output_data_uleb128 (o, NULL);
2132 }
2133 break;
2134 case DW_OP_GNU_deref_type:
2135 {
2136 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137 gcc_assert (o);
2138 dw2_asm_output_data (1, val1->v.val_int, NULL);
2139 dw2_asm_output_data_uleb128 (o, NULL);
2140 }
2141 break;
2142 case DW_OP_GNU_convert:
2143 case DW_OP_GNU_reinterpret:
2144 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2145 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2146 else
2147 {
2148 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2149 gcc_assert (o);
2150 dw2_asm_output_data_uleb128 (o, NULL);
2151 }
2152 break;
2153
2154 case DW_OP_GNU_parameter_ref:
2155 {
2156 unsigned long o;
2157 gcc_assert (val1->val_class == dw_val_class_die_ref);
2158 o = get_ref_die_offset (val1->v.val_die_ref.die);
2159 dw2_asm_output_data (4, o, NULL);
2160 }
2161 break;
2162
2163 default:
2164 /* Other codes have no operands. */
2165 break;
2166 }
2167 }
2168
2169 /* Output a sequence of location operations.
2170 The for_eh_or_skip parameter controls whether register numbers are
2171 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2172 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2173 info). This should be suppressed for the cases that have not been converted
2174 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2175
2176 void
2177 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2178 {
2179 for (; loc != NULL; loc = loc->dw_loc_next)
2180 {
2181 enum dwarf_location_atom opc = loc->dw_loc_opc;
2182 /* Output the opcode. */
2183 if (for_eh_or_skip >= 0
2184 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2185 {
2186 unsigned r = (opc - DW_OP_breg0);
2187 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188 gcc_assert (r <= 31);
2189 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2190 }
2191 else if (for_eh_or_skip >= 0
2192 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2193 {
2194 unsigned r = (opc - DW_OP_reg0);
2195 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196 gcc_assert (r <= 31);
2197 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2198 }
2199
2200 dw2_asm_output_data (1, opc,
2201 "%s", dwarf_stack_op_name (opc));
2202
2203 /* Output the operand(s) (if any). */
2204 output_loc_operands (loc, for_eh_or_skip);
2205 }
2206 }
2207
2208 /* Output location description stack opcode's operands (if any).
2209 The output is single bytes on a line, suitable for .cfi_escape. */
2210
2211 static void
2212 output_loc_operands_raw (dw_loc_descr_ref loc)
2213 {
2214 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2215 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2216
2217 switch (loc->dw_loc_opc)
2218 {
2219 case DW_OP_addr:
2220 case DW_OP_GNU_addr_index:
2221 case DW_OP_GNU_const_index:
2222 case DW_OP_implicit_value:
2223 /* We cannot output addresses in .cfi_escape, only bytes. */
2224 gcc_unreachable ();
2225
2226 case DW_OP_const1u:
2227 case DW_OP_const1s:
2228 case DW_OP_pick:
2229 case DW_OP_deref_size:
2230 case DW_OP_xderef_size:
2231 fputc (',', asm_out_file);
2232 dw2_asm_output_data_raw (1, val1->v.val_int);
2233 break;
2234
2235 case DW_OP_const2u:
2236 case DW_OP_const2s:
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_raw (2, val1->v.val_int);
2239 break;
2240
2241 case DW_OP_const4u:
2242 case DW_OP_const4s:
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_raw (4, val1->v.val_int);
2245 break;
2246
2247 case DW_OP_const8u:
2248 case DW_OP_const8s:
2249 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_raw (8, val1->v.val_int);
2252 break;
2253
2254 case DW_OP_skip:
2255 case DW_OP_bra:
2256 {
2257 int offset;
2258
2259 gcc_assert (val1->val_class == dw_val_class_loc);
2260 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2261
2262 fputc (',', asm_out_file);
2263 dw2_asm_output_data_raw (2, offset);
2264 }
2265 break;
2266
2267 case DW_OP_regx:
2268 {
2269 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2270 gcc_assert (size_of_uleb128 (r)
2271 == size_of_uleb128 (val1->v.val_unsigned));
2272 fputc (',', asm_out_file);
2273 dw2_asm_output_data_uleb128_raw (r);
2274 }
2275 break;
2276
2277 case DW_OP_constu:
2278 case DW_OP_plus_uconst:
2279 case DW_OP_piece:
2280 fputc (',', asm_out_file);
2281 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2282 break;
2283
2284 case DW_OP_bit_piece:
2285 fputc (',', asm_out_file);
2286 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2287 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2288 break;
2289
2290 case DW_OP_consts:
2291 case DW_OP_breg0:
2292 case DW_OP_breg1:
2293 case DW_OP_breg2:
2294 case DW_OP_breg3:
2295 case DW_OP_breg4:
2296 case DW_OP_breg5:
2297 case DW_OP_breg6:
2298 case DW_OP_breg7:
2299 case DW_OP_breg8:
2300 case DW_OP_breg9:
2301 case DW_OP_breg10:
2302 case DW_OP_breg11:
2303 case DW_OP_breg12:
2304 case DW_OP_breg13:
2305 case DW_OP_breg14:
2306 case DW_OP_breg15:
2307 case DW_OP_breg16:
2308 case DW_OP_breg17:
2309 case DW_OP_breg18:
2310 case DW_OP_breg19:
2311 case DW_OP_breg20:
2312 case DW_OP_breg21:
2313 case DW_OP_breg22:
2314 case DW_OP_breg23:
2315 case DW_OP_breg24:
2316 case DW_OP_breg25:
2317 case DW_OP_breg26:
2318 case DW_OP_breg27:
2319 case DW_OP_breg28:
2320 case DW_OP_breg29:
2321 case DW_OP_breg30:
2322 case DW_OP_breg31:
2323 case DW_OP_fbreg:
2324 fputc (',', asm_out_file);
2325 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2326 break;
2327
2328 case DW_OP_bregx:
2329 {
2330 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2331 gcc_assert (size_of_uleb128 (r)
2332 == size_of_uleb128 (val1->v.val_unsigned));
2333 fputc (',', asm_out_file);
2334 dw2_asm_output_data_uleb128_raw (r);
2335 fputc (',', asm_out_file);
2336 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2337 }
2338 break;
2339
2340 case DW_OP_GNU_implicit_pointer:
2341 case DW_OP_GNU_entry_value:
2342 case DW_OP_GNU_const_type:
2343 case DW_OP_GNU_regval_type:
2344 case DW_OP_GNU_deref_type:
2345 case DW_OP_GNU_convert:
2346 case DW_OP_GNU_reinterpret:
2347 case DW_OP_GNU_parameter_ref:
2348 gcc_unreachable ();
2349 break;
2350
2351 default:
2352 /* Other codes have no operands. */
2353 break;
2354 }
2355 }
2356
2357 void
2358 output_loc_sequence_raw (dw_loc_descr_ref loc)
2359 {
2360 while (1)
2361 {
2362 enum dwarf_location_atom opc = loc->dw_loc_opc;
2363 /* Output the opcode. */
2364 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2365 {
2366 unsigned r = (opc - DW_OP_breg0);
2367 r = DWARF2_FRAME_REG_OUT (r, 1);
2368 gcc_assert (r <= 31);
2369 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2370 }
2371 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2372 {
2373 unsigned r = (opc - DW_OP_reg0);
2374 r = DWARF2_FRAME_REG_OUT (r, 1);
2375 gcc_assert (r <= 31);
2376 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2377 }
2378 /* Output the opcode. */
2379 fprintf (asm_out_file, "%#x", opc);
2380 output_loc_operands_raw (loc);
2381
2382 if (!loc->dw_loc_next)
2383 break;
2384 loc = loc->dw_loc_next;
2385
2386 fputc (',', asm_out_file);
2387 }
2388 }
2389
2390 /* This function builds a dwarf location descriptor sequence from a
2391 dw_cfa_location, adding the given OFFSET to the result of the
2392 expression. */
2393
2394 struct dw_loc_descr_node *
2395 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2396 {
2397 struct dw_loc_descr_node *head, *tmp;
2398
2399 offset += cfa->offset;
2400
2401 if (cfa->indirect)
2402 {
2403 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2404 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2405 head->dw_loc_oprnd1.val_entry = NULL;
2406 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2407 add_loc_descr (&head, tmp);
2408 if (offset != 0)
2409 {
2410 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2411 add_loc_descr (&head, tmp);
2412 }
2413 }
2414 else
2415 head = new_reg_loc_descr (cfa->reg, offset);
2416
2417 return head;
2418 }
2419
2420 /* This function builds a dwarf location descriptor sequence for
2421 the address at OFFSET from the CFA when stack is aligned to
2422 ALIGNMENT byte. */
2423
2424 struct dw_loc_descr_node *
2425 build_cfa_aligned_loc (dw_cfa_location *cfa,
2426 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2427 {
2428 struct dw_loc_descr_node *head;
2429 unsigned int dwarf_fp
2430 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2431
2432 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2433 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2434 {
2435 head = new_reg_loc_descr (dwarf_fp, 0);
2436 add_loc_descr (&head, int_loc_descriptor (alignment));
2437 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2438 loc_descr_plus_const (&head, offset);
2439 }
2440 else
2441 head = new_reg_loc_descr (dwarf_fp, offset);
2442 return head;
2443 }
2444 \f
2445 /* And now, the support for symbolic debugging information. */
2446
2447 /* .debug_str support. */
2448
2449 static void dwarf2out_init (const char *);
2450 static void dwarf2out_finish (const char *);
2451 static void dwarf2out_assembly_start (void);
2452 static void dwarf2out_define (unsigned int, const char *);
2453 static void dwarf2out_undef (unsigned int, const char *);
2454 static void dwarf2out_start_source_file (unsigned, const char *);
2455 static void dwarf2out_end_source_file (unsigned);
2456 static void dwarf2out_function_decl (tree);
2457 static void dwarf2out_begin_block (unsigned, unsigned);
2458 static void dwarf2out_end_block (unsigned, unsigned);
2459 static bool dwarf2out_ignore_block (const_tree);
2460 static void dwarf2out_global_decl (tree);
2461 static void dwarf2out_type_decl (tree, int);
2462 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2463 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2464 dw_die_ref);
2465 static void dwarf2out_abstract_function (tree);
2466 static void dwarf2out_var_location (rtx_insn *);
2467 static void dwarf2out_begin_function (tree);
2468 static void dwarf2out_end_function (unsigned int);
2469 static void dwarf2out_set_name (tree, tree);
2470
2471 /* The debug hooks structure. */
2472
2473 const struct gcc_debug_hooks dwarf2_debug_hooks =
2474 {
2475 dwarf2out_init,
2476 dwarf2out_finish,
2477 dwarf2out_assembly_start,
2478 dwarf2out_define,
2479 dwarf2out_undef,
2480 dwarf2out_start_source_file,
2481 dwarf2out_end_source_file,
2482 dwarf2out_begin_block,
2483 dwarf2out_end_block,
2484 dwarf2out_ignore_block,
2485 dwarf2out_source_line,
2486 dwarf2out_begin_prologue,
2487 #if VMS_DEBUGGING_INFO
2488 dwarf2out_vms_end_prologue,
2489 dwarf2out_vms_begin_epilogue,
2490 #else
2491 debug_nothing_int_charstar,
2492 debug_nothing_int_charstar,
2493 #endif
2494 dwarf2out_end_epilogue,
2495 dwarf2out_begin_function,
2496 dwarf2out_end_function, /* end_function */
2497 dwarf2out_function_decl, /* function_decl */
2498 dwarf2out_global_decl,
2499 dwarf2out_type_decl, /* type_decl */
2500 dwarf2out_imported_module_or_decl,
2501 debug_nothing_tree, /* deferred_inline_function */
2502 /* The DWARF 2 backend tries to reduce debugging bloat by not
2503 emitting the abstract description of inline functions until
2504 something tries to reference them. */
2505 dwarf2out_abstract_function, /* outlining_inline_function */
2506 debug_nothing_rtx_code_label, /* label */
2507 debug_nothing_int, /* handle_pch */
2508 dwarf2out_var_location,
2509 dwarf2out_switch_text_section,
2510 dwarf2out_set_name,
2511 1, /* start_end_main_source_file */
2512 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2513 };
2514 \f
2515 /* NOTE: In the comments in this file, many references are made to
2516 "Debugging Information Entries". This term is abbreviated as `DIE'
2517 throughout the remainder of this file. */
2518
2519 /* An internal representation of the DWARF output is built, and then
2520 walked to generate the DWARF debugging info. The walk of the internal
2521 representation is done after the entire program has been compiled.
2522 The types below are used to describe the internal representation. */
2523
2524 /* Whether to put type DIEs into their own section .debug_types instead
2525 of making them part of the .debug_info section. Only supported for
2526 Dwarf V4 or higher and the user didn't disable them through
2527 -fno-debug-types-section. It is more efficient to put them in a
2528 separate comdat sections since the linker will then be able to
2529 remove duplicates. But not all tools support .debug_types sections
2530 yet. */
2531
2532 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2533
2534 /* Various DIE's use offsets relative to the beginning of the
2535 .debug_info section to refer to each other. */
2536
2537 typedef long int dw_offset;
2538
2539 /* Define typedefs here to avoid circular dependencies. */
2540
2541 typedef struct dw_attr_struct *dw_attr_ref;
2542 typedef struct dw_line_info_struct *dw_line_info_ref;
2543 typedef struct pubname_struct *pubname_ref;
2544 typedef struct dw_ranges_struct *dw_ranges_ref;
2545 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2546 typedef struct comdat_type_struct *comdat_type_node_ref;
2547
2548 /* The entries in the line_info table more-or-less mirror the opcodes
2549 that are used in the real dwarf line table. Arrays of these entries
2550 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2551 supported. */
2552
2553 enum dw_line_info_opcode {
2554 /* Emit DW_LNE_set_address; the operand is the label index. */
2555 LI_set_address,
2556
2557 /* Emit a row to the matrix with the given line. This may be done
2558 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2559 special opcodes. */
2560 LI_set_line,
2561
2562 /* Emit a DW_LNS_set_file. */
2563 LI_set_file,
2564
2565 /* Emit a DW_LNS_set_column. */
2566 LI_set_column,
2567
2568 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2569 LI_negate_stmt,
2570
2571 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2572 LI_set_prologue_end,
2573 LI_set_epilogue_begin,
2574
2575 /* Emit a DW_LNE_set_discriminator. */
2576 LI_set_discriminator
2577 };
2578
2579 typedef struct GTY(()) dw_line_info_struct {
2580 enum dw_line_info_opcode opcode;
2581 unsigned int val;
2582 } dw_line_info_entry;
2583
2584
2585 typedef struct GTY(()) dw_line_info_table_struct {
2586 /* The label that marks the end of this section. */
2587 const char *end_label;
2588
2589 /* The values for the last row of the matrix, as collected in the table.
2590 These are used to minimize the changes to the next row. */
2591 unsigned int file_num;
2592 unsigned int line_num;
2593 unsigned int column_num;
2594 int discrim_num;
2595 bool is_stmt;
2596 bool in_use;
2597
2598 vec<dw_line_info_entry, va_gc> *entries;
2599 } dw_line_info_table;
2600
2601 typedef dw_line_info_table *dw_line_info_table_p;
2602
2603
2604 /* Each DIE attribute has a field specifying the attribute kind,
2605 a link to the next attribute in the chain, and an attribute value.
2606 Attributes are typically linked below the DIE they modify. */
2607
2608 typedef struct GTY(()) dw_attr_struct {
2609 enum dwarf_attribute dw_attr;
2610 dw_val_node dw_attr_val;
2611 }
2612 dw_attr_node;
2613
2614
2615 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2616 The children of each node form a circular list linked by
2617 die_sib. die_child points to the node *before* the "first" child node. */
2618
2619 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2620 union die_symbol_or_type_node
2621 {
2622 const char * GTY ((tag ("0"))) die_symbol;
2623 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2624 }
2625 GTY ((desc ("%0.comdat_type_p"))) die_id;
2626 vec<dw_attr_node, va_gc> *die_attr;
2627 dw_die_ref die_parent;
2628 dw_die_ref die_child;
2629 dw_die_ref die_sib;
2630 dw_die_ref die_definition; /* ref from a specification to its definition */
2631 dw_offset die_offset;
2632 unsigned long die_abbrev;
2633 int die_mark;
2634 unsigned int decl_id;
2635 enum dwarf_tag die_tag;
2636 /* Die is used and must not be pruned as unused. */
2637 BOOL_BITFIELD die_perennial_p : 1;
2638 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2639 /* Lots of spare bits. */
2640 }
2641 die_node;
2642
2643 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2644 #define FOR_EACH_CHILD(die, c, expr) do { \
2645 c = die->die_child; \
2646 if (c) do { \
2647 c = c->die_sib; \
2648 expr; \
2649 } while (c != die->die_child); \
2650 } while (0)
2651
2652 /* The pubname structure */
2653
2654 typedef struct GTY(()) pubname_struct {
2655 dw_die_ref die;
2656 const char *name;
2657 }
2658 pubname_entry;
2659
2660
2661 struct GTY(()) dw_ranges_struct {
2662 /* If this is positive, it's a block number, otherwise it's a
2663 bitwise-negated index into dw_ranges_by_label. */
2664 int num;
2665 };
2666
2667 /* A structure to hold a macinfo entry. */
2668
2669 typedef struct GTY(()) macinfo_struct {
2670 unsigned char code;
2671 unsigned HOST_WIDE_INT lineno;
2672 const char *info;
2673 }
2674 macinfo_entry;
2675
2676
2677 struct GTY(()) dw_ranges_by_label_struct {
2678 const char *begin;
2679 const char *end;
2680 };
2681
2682 /* The comdat type node structure. */
2683 typedef struct GTY(()) comdat_type_struct
2684 {
2685 dw_die_ref root_die;
2686 dw_die_ref type_die;
2687 dw_die_ref skeleton_die;
2688 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2689 struct comdat_type_struct *next;
2690 }
2691 comdat_type_node;
2692
2693 /* The limbo die list structure. */
2694 typedef struct GTY(()) limbo_die_struct {
2695 dw_die_ref die;
2696 tree created_for;
2697 struct limbo_die_struct *next;
2698 }
2699 limbo_die_node;
2700
2701 typedef struct skeleton_chain_struct
2702 {
2703 dw_die_ref old_die;
2704 dw_die_ref new_die;
2705 struct skeleton_chain_struct *parent;
2706 }
2707 skeleton_chain_node;
2708
2709 /* Define a macro which returns nonzero for a TYPE_DECL which was
2710 implicitly generated for a type.
2711
2712 Note that, unlike the C front-end (which generates a NULL named
2713 TYPE_DECL node for each complete tagged type, each array type,
2714 and each function type node created) the C++ front-end generates
2715 a _named_ TYPE_DECL node for each tagged type node created.
2716 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2717 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2718 front-end, but for each type, tagged or not. */
2719
2720 #define TYPE_DECL_IS_STUB(decl) \
2721 (DECL_NAME (decl) == NULL_TREE \
2722 || (DECL_ARTIFICIAL (decl) \
2723 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2724 /* This is necessary for stub decls that \
2725 appear in nested inline functions. */ \
2726 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2727 && (decl_ultimate_origin (decl) \
2728 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2729
2730 /* Information concerning the compilation unit's programming
2731 language, and compiler version. */
2732
2733 /* Fixed size portion of the DWARF compilation unit header. */
2734 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2735 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2736
2737 /* Fixed size portion of the DWARF comdat type unit header. */
2738 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2739 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2740 + DWARF_OFFSET_SIZE)
2741
2742 /* Fixed size portion of public names info. */
2743 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2744
2745 /* Fixed size portion of the address range info. */
2746 #define DWARF_ARANGES_HEADER_SIZE \
2747 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2748 DWARF2_ADDR_SIZE * 2) \
2749 - DWARF_INITIAL_LENGTH_SIZE)
2750
2751 /* Size of padding portion in the address range info. It must be
2752 aligned to twice the pointer size. */
2753 #define DWARF_ARANGES_PAD_SIZE \
2754 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2755 DWARF2_ADDR_SIZE * 2) \
2756 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2757
2758 /* Use assembler line directives if available. */
2759 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2760 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2761 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2762 #else
2763 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2764 #endif
2765 #endif
2766
2767 /* Minimum line offset in a special line info. opcode.
2768 This value was chosen to give a reasonable range of values. */
2769 #define DWARF_LINE_BASE -10
2770
2771 /* First special line opcode - leave room for the standard opcodes. */
2772 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2773
2774 /* Range of line offsets in a special line info. opcode. */
2775 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2776
2777 /* Flag that indicates the initial value of the is_stmt_start flag.
2778 In the present implementation, we do not mark any lines as
2779 the beginning of a source statement, because that information
2780 is not made available by the GCC front-end. */
2781 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2782
2783 /* Maximum number of operations per instruction bundle. */
2784 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2785 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2786 #endif
2787
2788 /* This location is used by calc_die_sizes() to keep track
2789 the offset of each DIE within the .debug_info section. */
2790 static unsigned long next_die_offset;
2791
2792 /* Record the root of the DIE's built for the current compilation unit. */
2793 static GTY(()) dw_die_ref single_comp_unit_die;
2794
2795 /* A list of type DIEs that have been separated into comdat sections. */
2796 static GTY(()) comdat_type_node *comdat_type_list;
2797
2798 /* A list of DIEs with a NULL parent waiting to be relocated. */
2799 static GTY(()) limbo_die_node *limbo_die_list;
2800
2801 /* A list of DIEs for which we may have to generate
2802 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2803 static GTY(()) limbo_die_node *deferred_asm_name;
2804
2805 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2806 {
2807 typedef const char *compare_type;
2808
2809 static hashval_t hash (dwarf_file_data *);
2810 static bool equal (dwarf_file_data *, const char *);
2811 };
2812
2813 /* Filenames referenced by this compilation unit. */
2814 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2815
2816 struct decl_die_hasher : ggc_hasher<die_node *>
2817 {
2818 typedef tree compare_type;
2819
2820 static hashval_t hash (die_node *);
2821 static bool equal (die_node *, tree);
2822 };
2823 /* A hash table of references to DIE's that describe declarations.
2824 The key is a DECL_UID() which is a unique number identifying each decl. */
2825 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2826
2827 struct block_die_hasher : ggc_hasher<die_struct *>
2828 {
2829 static hashval_t hash (die_struct *);
2830 static bool equal (die_struct *, die_struct *);
2831 };
2832
2833 /* A hash table of references to DIE's that describe COMMON blocks.
2834 The key is DECL_UID() ^ die_parent. */
2835 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2836
2837 typedef struct GTY(()) die_arg_entry_struct {
2838 dw_die_ref die;
2839 tree arg;
2840 } die_arg_entry;
2841
2842
2843 /* Node of the variable location list. */
2844 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2845 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2846 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2847 in mode of the EXPR_LIST node and first EXPR_LIST operand
2848 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2849 location or NULL for padding. For larger bitsizes,
2850 mode is 0 and first operand is a CONCAT with bitsize
2851 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2852 NULL as second operand. */
2853 rtx GTY (()) loc;
2854 const char * GTY (()) label;
2855 struct var_loc_node * GTY (()) next;
2856 };
2857
2858 /* Variable location list. */
2859 struct GTY ((for_user)) var_loc_list_def {
2860 struct var_loc_node * GTY (()) first;
2861
2862 /* Pointer to the last but one or last element of the
2863 chained list. If the list is empty, both first and
2864 last are NULL, if the list contains just one node
2865 or the last node certainly is not redundant, it points
2866 to the last node, otherwise points to the last but one.
2867 Do not mark it for GC because it is marked through the chain. */
2868 struct var_loc_node * GTY ((skip ("%h"))) last;
2869
2870 /* Pointer to the last element before section switch,
2871 if NULL, either sections weren't switched or first
2872 is after section switch. */
2873 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2874
2875 /* DECL_UID of the variable decl. */
2876 unsigned int decl_id;
2877 };
2878 typedef struct var_loc_list_def var_loc_list;
2879
2880 /* Call argument location list. */
2881 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2882 rtx GTY (()) call_arg_loc_note;
2883 const char * GTY (()) label;
2884 tree GTY (()) block;
2885 bool tail_call_p;
2886 rtx GTY (()) symbol_ref;
2887 struct call_arg_loc_node * GTY (()) next;
2888 };
2889
2890
2891 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2892 {
2893 typedef const_tree compare_type;
2894
2895 static hashval_t hash (var_loc_list *);
2896 static bool equal (var_loc_list *, const_tree);
2897 };
2898
2899 /* Table of decl location linked lists. */
2900 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2901
2902 /* Head and tail of call_arg_loc chain. */
2903 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2904 static struct call_arg_loc_node *call_arg_loc_last;
2905
2906 /* Number of call sites in the current function. */
2907 static int call_site_count = -1;
2908 /* Number of tail call sites in the current function. */
2909 static int tail_call_site_count = -1;
2910
2911 /* A cached location list. */
2912 struct GTY ((for_user)) cached_dw_loc_list_def {
2913 /* The DECL_UID of the decl that this entry describes. */
2914 unsigned int decl_id;
2915
2916 /* The cached location list. */
2917 dw_loc_list_ref loc_list;
2918 };
2919 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2920
2921 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2922 {
2923
2924 typedef const_tree compare_type;
2925
2926 static hashval_t hash (cached_dw_loc_list *);
2927 static bool equal (cached_dw_loc_list *, const_tree);
2928 };
2929
2930 /* Table of cached location lists. */
2931 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2932
2933 /* A pointer to the base of a list of references to DIE's that
2934 are uniquely identified by their tag, presence/absence of
2935 children DIE's, and list of attribute/value pairs. */
2936 static GTY((length ("abbrev_die_table_allocated")))
2937 dw_die_ref *abbrev_die_table;
2938
2939 /* Number of elements currently allocated for abbrev_die_table. */
2940 static GTY(()) unsigned abbrev_die_table_allocated;
2941
2942 /* Number of elements in type_die_table currently in use. */
2943 static GTY(()) unsigned abbrev_die_table_in_use;
2944
2945 /* Size (in elements) of increments by which we may expand the
2946 abbrev_die_table. */
2947 #define ABBREV_DIE_TABLE_INCREMENT 256
2948
2949 /* A global counter for generating labels for line number data. */
2950 static unsigned int line_info_label_num;
2951
2952 /* The current table to which we should emit line number information
2953 for the current function. This will be set up at the beginning of
2954 assembly for the function. */
2955 static dw_line_info_table *cur_line_info_table;
2956
2957 /* The two default tables of line number info. */
2958 static GTY(()) dw_line_info_table *text_section_line_info;
2959 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2960
2961 /* The set of all non-default tables of line number info. */
2962 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2963
2964 /* A flag to tell pubnames/types export if there is an info section to
2965 refer to. */
2966 static bool info_section_emitted;
2967
2968 /* A pointer to the base of a table that contains a list of publicly
2969 accessible names. */
2970 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2971
2972 /* A pointer to the base of a table that contains a list of publicly
2973 accessible types. */
2974 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2975
2976 /* A pointer to the base of a table that contains a list of macro
2977 defines/undefines (and file start/end markers). */
2978 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2979
2980 /* True if .debug_macinfo or .debug_macros section is going to be
2981 emitted. */
2982 #define have_macinfo \
2983 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2984 && !macinfo_table->is_empty ())
2985
2986 /* Array of dies for which we should generate .debug_ranges info. */
2987 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2988
2989 /* Number of elements currently allocated for ranges_table. */
2990 static GTY(()) unsigned ranges_table_allocated;
2991
2992 /* Number of elements in ranges_table currently in use. */
2993 static GTY(()) unsigned ranges_table_in_use;
2994
2995 /* Array of pairs of labels referenced in ranges_table. */
2996 static GTY ((length ("ranges_by_label_allocated")))
2997 dw_ranges_by_label_ref ranges_by_label;
2998
2999 /* Number of elements currently allocated for ranges_by_label. */
3000 static GTY(()) unsigned ranges_by_label_allocated;
3001
3002 /* Number of elements in ranges_by_label currently in use. */
3003 static GTY(()) unsigned ranges_by_label_in_use;
3004
3005 /* Size (in elements) of increments by which we may expand the
3006 ranges_table. */
3007 #define RANGES_TABLE_INCREMENT 64
3008
3009 /* Whether we have location lists that need outputting */
3010 static GTY(()) bool have_location_lists;
3011
3012 /* Unique label counter. */
3013 static GTY(()) unsigned int loclabel_num;
3014
3015 /* Unique label counter for point-of-call tables. */
3016 static GTY(()) unsigned int poc_label_num;
3017
3018 /* The last file entry emitted by maybe_emit_file(). */
3019 static GTY(()) struct dwarf_file_data * last_emitted_file;
3020
3021 /* Number of internal labels generated by gen_internal_sym(). */
3022 static GTY(()) int label_num;
3023
3024 /* Cached result of previous call to lookup_filename. */
3025 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3026
3027 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3028
3029 /* Instances of generic types for which we need to generate debug
3030 info that describe their generic parameters and arguments. That
3031 generation needs to happen once all types are properly laid out so
3032 we do it at the end of compilation. */
3033 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3034
3035 /* Offset from the "steady-state frame pointer" to the frame base,
3036 within the current function. */
3037 static HOST_WIDE_INT frame_pointer_fb_offset;
3038 static bool frame_pointer_fb_offset_valid;
3039
3040 static vec<dw_die_ref> base_types;
3041
3042 /* Flags to represent a set of attribute classes for attributes that represent
3043 a scalar value (bounds, pointers, ...). */
3044 enum dw_scalar_form
3045 {
3046 dw_scalar_form_constant = 0x01,
3047 dw_scalar_form_exprloc = 0x02,
3048 dw_scalar_form_reference = 0x04
3049 };
3050
3051 /* Forward declarations for functions defined in this file. */
3052
3053 static int is_pseudo_reg (const_rtx);
3054 static tree type_main_variant (tree);
3055 static int is_tagged_type (const_tree);
3056 static const char *dwarf_tag_name (unsigned);
3057 static const char *dwarf_attr_name (unsigned);
3058 static const char *dwarf_form_name (unsigned);
3059 static tree decl_ultimate_origin (const_tree);
3060 static tree decl_class_context (tree);
3061 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3062 static inline enum dw_val_class AT_class (dw_attr_ref);
3063 static inline unsigned int AT_index (dw_attr_ref);
3064 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3065 static inline unsigned AT_flag (dw_attr_ref);
3066 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3067 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3068 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3069 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3070 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3071 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3072 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3073 unsigned int, unsigned char *);
3074 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3075 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3076 static inline const char *AT_string (dw_attr_ref);
3077 static enum dwarf_form AT_string_form (dw_attr_ref);
3078 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3079 static void add_AT_specification (dw_die_ref, dw_die_ref);
3080 static inline dw_die_ref AT_ref (dw_attr_ref);
3081 static inline int AT_ref_external (dw_attr_ref);
3082 static inline void set_AT_ref_external (dw_attr_ref, int);
3083 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3084 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3085 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3086 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3087 dw_loc_list_ref);
3088 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3089 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3090 static void remove_addr_table_entry (addr_table_entry *);
3091 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3092 static inline rtx AT_addr (dw_attr_ref);
3093 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3094 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3095 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3096 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3097 unsigned HOST_WIDE_INT);
3098 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3099 unsigned long, bool);
3100 static inline const char *AT_lbl (dw_attr_ref);
3101 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3102 static const char *get_AT_low_pc (dw_die_ref);
3103 static const char *get_AT_hi_pc (dw_die_ref);
3104 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3105 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3106 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3107 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3108 static bool is_cxx (void);
3109 static bool is_fortran (void);
3110 static bool is_ada (void);
3111 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3112 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3113 static void add_child_die (dw_die_ref, dw_die_ref);
3114 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3115 static dw_die_ref lookup_type_die (tree);
3116 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3117 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3118 static void equate_type_number_to_die (tree, dw_die_ref);
3119 static dw_die_ref lookup_decl_die (tree);
3120 static var_loc_list *lookup_decl_loc (const_tree);
3121 static void equate_decl_number_to_die (tree, dw_die_ref);
3122 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3123 static void print_spaces (FILE *);
3124 static void print_die (dw_die_ref, FILE *);
3125 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3126 static dw_die_ref pop_compile_unit (dw_die_ref);
3127 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3128 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3129 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3130 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3131 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3132 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3133 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3134 struct md5_ctx *, int *);
3135 struct checksum_attributes;
3136 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3137 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3138 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3139 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3140 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3141 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3142 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3143 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3144 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3145 static void compute_section_prefix (dw_die_ref);
3146 static int is_type_die (dw_die_ref);
3147 static int is_comdat_die (dw_die_ref);
3148 static int is_symbol_die (dw_die_ref);
3149 static inline bool is_template_instantiation (dw_die_ref);
3150 static void assign_symbol_names (dw_die_ref);
3151 static void break_out_includes (dw_die_ref);
3152 static int is_declaration_die (dw_die_ref);
3153 static int should_move_die_to_comdat (dw_die_ref);
3154 static dw_die_ref clone_as_declaration (dw_die_ref);
3155 static dw_die_ref clone_die (dw_die_ref);
3156 static dw_die_ref clone_tree (dw_die_ref);
3157 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3158 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3159 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3160 static dw_die_ref generate_skeleton (dw_die_ref);
3161 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3162 dw_die_ref,
3163 dw_die_ref);
3164 static void break_out_comdat_types (dw_die_ref);
3165 static void copy_decls_for_unworthy_types (dw_die_ref);
3166
3167 static void add_sibling_attributes (dw_die_ref);
3168 static void output_location_lists (dw_die_ref);
3169 static int constant_size (unsigned HOST_WIDE_INT);
3170 static unsigned long size_of_die (dw_die_ref);
3171 static void calc_die_sizes (dw_die_ref);
3172 static void calc_base_type_die_sizes (void);
3173 static void mark_dies (dw_die_ref);
3174 static void unmark_dies (dw_die_ref);
3175 static void unmark_all_dies (dw_die_ref);
3176 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3177 static unsigned long size_of_aranges (void);
3178 static enum dwarf_form value_format (dw_attr_ref);
3179 static void output_value_format (dw_attr_ref);
3180 static void output_abbrev_section (void);
3181 static void output_die_abbrevs (unsigned long, dw_die_ref);
3182 static void output_die_symbol (dw_die_ref);
3183 static void output_die (dw_die_ref);
3184 static void output_compilation_unit_header (void);
3185 static void output_comp_unit (dw_die_ref, int);
3186 static void output_comdat_type_unit (comdat_type_node *);
3187 static const char *dwarf2_name (tree, int);
3188 static void add_pubname (tree, dw_die_ref);
3189 static void add_enumerator_pubname (const char *, dw_die_ref);
3190 static void add_pubname_string (const char *, dw_die_ref);
3191 static void add_pubtype (tree, dw_die_ref);
3192 static void output_pubnames (vec<pubname_entry, va_gc> *);
3193 static void output_aranges (unsigned long);
3194 static unsigned int add_ranges_num (int);
3195 static unsigned int add_ranges (const_tree);
3196 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3197 bool *, bool);
3198 static void output_ranges (void);
3199 static dw_line_info_table *new_line_info_table (void);
3200 static void output_line_info (bool);
3201 static void output_file_names (void);
3202 static dw_die_ref base_type_die (tree);
3203 static int is_base_type (tree);
3204 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3205 static int decl_quals (const_tree);
3206 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3207 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3208 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3209 static int type_is_enum (const_tree);
3210 static unsigned int dbx_reg_number (const_rtx);
3211 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3212 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3213 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3214 enum var_init_status);
3215 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3216 enum var_init_status);
3217 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3218 enum var_init_status);
3219 static int is_based_loc (const_rtx);
3220 static bool resolve_one_addr (rtx *);
3221 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3222 enum var_init_status);
3223 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3224 enum var_init_status);
3225 struct loc_descr_context;
3226 static dw_loc_list_ref loc_list_from_tree (tree, int,
3227 const struct loc_descr_context *);
3228 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3229 const struct loc_descr_context *);
3230 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3231 static tree field_type (const_tree);
3232 static unsigned int simple_type_align_in_bits (const_tree);
3233 static unsigned int simple_decl_align_in_bits (const_tree);
3234 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3235 static HOST_WIDE_INT field_byte_offset (const_tree);
3236 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3237 dw_loc_list_ref);
3238 static void add_data_member_location_attribute (dw_die_ref, tree);
3239 static bool add_const_value_attribute (dw_die_ref, rtx);
3240 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3241 static void insert_wide_int (const wide_int &, unsigned char *, int);
3242 static void insert_float (const_rtx, unsigned char *);
3243 static rtx rtl_for_decl_location (tree);
3244 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3245 enum dwarf_attribute);
3246 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3247 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3248 static void add_name_attribute (dw_die_ref, const char *);
3249 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3250 static void add_comp_dir_attribute (dw_die_ref);
3251 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3252 const struct loc_descr_context *);
3253 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3254 const struct loc_descr_context *);
3255 static void add_subscript_info (dw_die_ref, tree, bool);
3256 static void add_byte_size_attribute (dw_die_ref, tree);
3257 static void add_bit_offset_attribute (dw_die_ref, tree);
3258 static void add_bit_size_attribute (dw_die_ref, tree);
3259 static void add_prototyped_attribute (dw_die_ref, tree);
3260 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3261 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3262 static void add_src_coords_attributes (dw_die_ref, tree);
3263 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3264 static void push_decl_scope (tree);
3265 static void pop_decl_scope (void);
3266 static dw_die_ref scope_die_for (tree, dw_die_ref);
3267 static inline int local_scope_p (dw_die_ref);
3268 static inline int class_scope_p (dw_die_ref);
3269 static inline int class_or_namespace_scope_p (dw_die_ref);
3270 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3271 static void add_calling_convention_attribute (dw_die_ref, tree);
3272 static const char *type_tag (const_tree);
3273 static tree member_declared_type (const_tree);
3274 #if 0
3275 static const char *decl_start_label (tree);
3276 #endif
3277 static void gen_array_type_die (tree, dw_die_ref);
3278 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3279 #if 0
3280 static void gen_entry_point_die (tree, dw_die_ref);
3281 #endif
3282 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3283 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3284 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3285 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3286 static void gen_formal_types_die (tree, dw_die_ref);
3287 static void gen_subprogram_die (tree, dw_die_ref);
3288 static void gen_variable_die (tree, tree, dw_die_ref);
3289 static void gen_const_die (tree, dw_die_ref);
3290 static void gen_label_die (tree, dw_die_ref);
3291 static void gen_lexical_block_die (tree, dw_die_ref);
3292 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3293 static void gen_field_die (tree, dw_die_ref);
3294 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3295 static dw_die_ref gen_compile_unit_die (const char *);
3296 static void gen_inheritance_die (tree, tree, dw_die_ref);
3297 static void gen_member_die (tree, dw_die_ref);
3298 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3299 enum debug_info_usage);
3300 static void gen_subroutine_type_die (tree, dw_die_ref);
3301 static void gen_typedef_die (tree, dw_die_ref);
3302 static void gen_type_die (tree, dw_die_ref);
3303 static void gen_block_die (tree, dw_die_ref);
3304 static void decls_for_scope (tree, dw_die_ref);
3305 static inline int is_redundant_typedef (const_tree);
3306 static bool is_naming_typedef_decl (const_tree);
3307 static inline dw_die_ref get_context_die (tree);
3308 static void gen_namespace_die (tree, dw_die_ref);
3309 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3310 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3311 static dw_die_ref force_decl_die (tree);
3312 static dw_die_ref force_type_die (tree);
3313 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3314 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3315 static struct dwarf_file_data * lookup_filename (const char *);
3316 static void retry_incomplete_types (void);
3317 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3318 static void gen_generic_params_dies (tree);
3319 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3320 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3321 static void splice_child_die (dw_die_ref, dw_die_ref);
3322 static int file_info_cmp (const void *, const void *);
3323 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3324 const char *, const char *);
3325 static void output_loc_list (dw_loc_list_ref);
3326 static char *gen_internal_sym (const char *);
3327 static bool want_pubnames (void);
3328
3329 static void prune_unmark_dies (dw_die_ref);
3330 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3331 static void prune_unused_types_mark (dw_die_ref, int);
3332 static void prune_unused_types_walk (dw_die_ref);
3333 static void prune_unused_types_walk_attribs (dw_die_ref);
3334 static void prune_unused_types_prune (dw_die_ref);
3335 static void prune_unused_types (void);
3336 static int maybe_emit_file (struct dwarf_file_data *fd);
3337 static inline const char *AT_vms_delta1 (dw_attr_ref);
3338 static inline const char *AT_vms_delta2 (dw_attr_ref);
3339 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3340 const char *, const char *);
3341 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3342 static void gen_remaining_tmpl_value_param_die_attribute (void);
3343 static bool generic_type_p (tree);
3344 static void schedule_generic_params_dies_gen (tree t);
3345 static void gen_scheduled_generic_parms_dies (void);
3346
3347 static const char *comp_dir_string (void);
3348
3349 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3350
3351 /* enum for tracking thread-local variables whose address is really an offset
3352 relative to the TLS pointer, which will need link-time relocation, but will
3353 not need relocation by the DWARF consumer. */
3354
3355 enum dtprel_bool
3356 {
3357 dtprel_false = 0,
3358 dtprel_true = 1
3359 };
3360
3361 /* Return the operator to use for an address of a variable. For dtprel_true, we
3362 use DW_OP_const*. For regular variables, which need both link-time
3363 relocation and consumer-level relocation (e.g., to account for shared objects
3364 loaded at a random address), we use DW_OP_addr*. */
3365
3366 static inline enum dwarf_location_atom
3367 dw_addr_op (enum dtprel_bool dtprel)
3368 {
3369 if (dtprel == dtprel_true)
3370 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3371 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3372 else
3373 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3374 }
3375
3376 /* Return a pointer to a newly allocated address location description. If
3377 dwarf_split_debug_info is true, then record the address with the appropriate
3378 relocation. */
3379 static inline dw_loc_descr_ref
3380 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3381 {
3382 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3383
3384 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3385 ref->dw_loc_oprnd1.v.val_addr = addr;
3386 ref->dtprel = dtprel;
3387 if (dwarf_split_debug_info)
3388 ref->dw_loc_oprnd1.val_entry
3389 = add_addr_table_entry (addr,
3390 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3391 else
3392 ref->dw_loc_oprnd1.val_entry = NULL;
3393
3394 return ref;
3395 }
3396
3397 /* Section names used to hold DWARF debugging information. */
3398
3399 #ifndef DEBUG_INFO_SECTION
3400 #define DEBUG_INFO_SECTION ".debug_info"
3401 #endif
3402 #ifndef DEBUG_DWO_INFO_SECTION
3403 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3404 #endif
3405 #ifndef DEBUG_ABBREV_SECTION
3406 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3407 #endif
3408 #ifndef DEBUG_DWO_ABBREV_SECTION
3409 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3410 #endif
3411 #ifndef DEBUG_ARANGES_SECTION
3412 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3413 #endif
3414 #ifndef DEBUG_ADDR_SECTION
3415 #define DEBUG_ADDR_SECTION ".debug_addr"
3416 #endif
3417 #ifndef DEBUG_NORM_MACINFO_SECTION
3418 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3419 #endif
3420 #ifndef DEBUG_DWO_MACINFO_SECTION
3421 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3422 #endif
3423 #ifndef DEBUG_MACINFO_SECTION
3424 #define DEBUG_MACINFO_SECTION \
3425 (!dwarf_split_debug_info \
3426 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3427 #endif
3428 #ifndef DEBUG_NORM_MACRO_SECTION
3429 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3430 #endif
3431 #ifndef DEBUG_DWO_MACRO_SECTION
3432 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3433 #endif
3434 #ifndef DEBUG_MACRO_SECTION
3435 #define DEBUG_MACRO_SECTION \
3436 (!dwarf_split_debug_info \
3437 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3438 #endif
3439 #ifndef DEBUG_LINE_SECTION
3440 #define DEBUG_LINE_SECTION ".debug_line"
3441 #endif
3442 #ifndef DEBUG_DWO_LINE_SECTION
3443 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3444 #endif
3445 #ifndef DEBUG_LOC_SECTION
3446 #define DEBUG_LOC_SECTION ".debug_loc"
3447 #endif
3448 #ifndef DEBUG_DWO_LOC_SECTION
3449 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3450 #endif
3451 #ifndef DEBUG_PUBNAMES_SECTION
3452 #define DEBUG_PUBNAMES_SECTION \
3453 ((debug_generate_pub_sections == 2) \
3454 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3455 #endif
3456 #ifndef DEBUG_PUBTYPES_SECTION
3457 #define DEBUG_PUBTYPES_SECTION \
3458 ((debug_generate_pub_sections == 2) \
3459 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3460 #endif
3461 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3462 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3463 #ifndef DEBUG_STR_OFFSETS_SECTION
3464 #define DEBUG_STR_OFFSETS_SECTION \
3465 (!dwarf_split_debug_info \
3466 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3467 #endif
3468 #ifndef DEBUG_STR_DWO_SECTION
3469 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3470 #endif
3471 #ifndef DEBUG_STR_SECTION
3472 #define DEBUG_STR_SECTION ".debug_str"
3473 #endif
3474 #ifndef DEBUG_RANGES_SECTION
3475 #define DEBUG_RANGES_SECTION ".debug_ranges"
3476 #endif
3477
3478 /* Standard ELF section names for compiled code and data. */
3479 #ifndef TEXT_SECTION_NAME
3480 #define TEXT_SECTION_NAME ".text"
3481 #endif
3482
3483 /* Section flags for .debug_macinfo/.debug_macro section. */
3484 #define DEBUG_MACRO_SECTION_FLAGS \
3485 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3486
3487 /* Section flags for .debug_str section. */
3488 #define DEBUG_STR_SECTION_FLAGS \
3489 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3490 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3491 : SECTION_DEBUG)
3492
3493 /* Section flags for .debug_str.dwo section. */
3494 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3495
3496 /* Labels we insert at beginning sections we can reference instead of
3497 the section names themselves. */
3498
3499 #ifndef TEXT_SECTION_LABEL
3500 #define TEXT_SECTION_LABEL "Ltext"
3501 #endif
3502 #ifndef COLD_TEXT_SECTION_LABEL
3503 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3504 #endif
3505 #ifndef DEBUG_LINE_SECTION_LABEL
3506 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3507 #endif
3508 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3509 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3510 #endif
3511 #ifndef DEBUG_INFO_SECTION_LABEL
3512 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3513 #endif
3514 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3515 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3516 #endif
3517 #ifndef DEBUG_ABBREV_SECTION_LABEL
3518 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3519 #endif
3520 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3521 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3522 #endif
3523 #ifndef DEBUG_ADDR_SECTION_LABEL
3524 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3525 #endif
3526 #ifndef DEBUG_LOC_SECTION_LABEL
3527 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3528 #endif
3529 #ifndef DEBUG_RANGES_SECTION_LABEL
3530 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3531 #endif
3532 #ifndef DEBUG_MACINFO_SECTION_LABEL
3533 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3534 #endif
3535 #ifndef DEBUG_MACRO_SECTION_LABEL
3536 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3537 #endif
3538 #define SKELETON_COMP_DIE_ABBREV 1
3539 #define SKELETON_TYPE_DIE_ABBREV 2
3540
3541 /* Definitions of defaults for formats and names of various special
3542 (artificial) labels which may be generated within this file (when the -g
3543 options is used and DWARF2_DEBUGGING_INFO is in effect.
3544 If necessary, these may be overridden from within the tm.h file, but
3545 typically, overriding these defaults is unnecessary. */
3546
3547 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3561
3562 #ifndef TEXT_END_LABEL
3563 #define TEXT_END_LABEL "Letext"
3564 #endif
3565 #ifndef COLD_END_LABEL
3566 #define COLD_END_LABEL "Letext_cold"
3567 #endif
3568 #ifndef BLOCK_BEGIN_LABEL
3569 #define BLOCK_BEGIN_LABEL "LBB"
3570 #endif
3571 #ifndef BLOCK_END_LABEL
3572 #define BLOCK_END_LABEL "LBE"
3573 #endif
3574 #ifndef LINE_CODE_LABEL
3575 #define LINE_CODE_LABEL "LM"
3576 #endif
3577
3578 \f
3579 /* Return the root of the DIE's built for the current compilation unit. */
3580 static dw_die_ref
3581 comp_unit_die (void)
3582 {
3583 if (!single_comp_unit_die)
3584 single_comp_unit_die = gen_compile_unit_die (NULL);
3585 return single_comp_unit_die;
3586 }
3587
3588 /* We allow a language front-end to designate a function that is to be
3589 called to "demangle" any name before it is put into a DIE. */
3590
3591 static const char *(*demangle_name_func) (const char *);
3592
3593 void
3594 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3595 {
3596 demangle_name_func = func;
3597 }
3598
3599 /* Test if rtl node points to a pseudo register. */
3600
3601 static inline int
3602 is_pseudo_reg (const_rtx rtl)
3603 {
3604 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3605 || (GET_CODE (rtl) == SUBREG
3606 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3607 }
3608
3609 /* Return a reference to a type, with its const and volatile qualifiers
3610 removed. */
3611
3612 static inline tree
3613 type_main_variant (tree type)
3614 {
3615 type = TYPE_MAIN_VARIANT (type);
3616
3617 /* ??? There really should be only one main variant among any group of
3618 variants of a given type (and all of the MAIN_VARIANT values for all
3619 members of the group should point to that one type) but sometimes the C
3620 front-end messes this up for array types, so we work around that bug
3621 here. */
3622 if (TREE_CODE (type) == ARRAY_TYPE)
3623 while (type != TYPE_MAIN_VARIANT (type))
3624 type = TYPE_MAIN_VARIANT (type);
3625
3626 return type;
3627 }
3628
3629 /* Return nonzero if the given type node represents a tagged type. */
3630
3631 static inline int
3632 is_tagged_type (const_tree type)
3633 {
3634 enum tree_code code = TREE_CODE (type);
3635
3636 return (code == RECORD_TYPE || code == UNION_TYPE
3637 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3638 }
3639
3640 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3641
3642 static void
3643 get_ref_die_offset_label (char *label, dw_die_ref ref)
3644 {
3645 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3646 }
3647
3648 /* Return die_offset of a DIE reference to a base type. */
3649
3650 static unsigned long int
3651 get_base_type_offset (dw_die_ref ref)
3652 {
3653 if (ref->die_offset)
3654 return ref->die_offset;
3655 if (comp_unit_die ()->die_abbrev)
3656 {
3657 calc_base_type_die_sizes ();
3658 gcc_assert (ref->die_offset);
3659 }
3660 return ref->die_offset;
3661 }
3662
3663 /* Return die_offset of a DIE reference other than base type. */
3664
3665 static unsigned long int
3666 get_ref_die_offset (dw_die_ref ref)
3667 {
3668 gcc_assert (ref->die_offset);
3669 return ref->die_offset;
3670 }
3671
3672 /* Convert a DIE tag into its string name. */
3673
3674 static const char *
3675 dwarf_tag_name (unsigned int tag)
3676 {
3677 const char *name = get_DW_TAG_name (tag);
3678
3679 if (name != NULL)
3680 return name;
3681
3682 return "DW_TAG_<unknown>";
3683 }
3684
3685 /* Convert a DWARF attribute code into its string name. */
3686
3687 static const char *
3688 dwarf_attr_name (unsigned int attr)
3689 {
3690 const char *name;
3691
3692 switch (attr)
3693 {
3694 #if VMS_DEBUGGING_INFO
3695 case DW_AT_HP_prologue:
3696 return "DW_AT_HP_prologue";
3697 #else
3698 case DW_AT_MIPS_loop_unroll_factor:
3699 return "DW_AT_MIPS_loop_unroll_factor";
3700 #endif
3701
3702 #if VMS_DEBUGGING_INFO
3703 case DW_AT_HP_epilogue:
3704 return "DW_AT_HP_epilogue";
3705 #else
3706 case DW_AT_MIPS_stride:
3707 return "DW_AT_MIPS_stride";
3708 #endif
3709 }
3710
3711 name = get_DW_AT_name (attr);
3712
3713 if (name != NULL)
3714 return name;
3715
3716 return "DW_AT_<unknown>";
3717 }
3718
3719 /* Convert a DWARF value form code into its string name. */
3720
3721 static const char *
3722 dwarf_form_name (unsigned int form)
3723 {
3724 const char *name = get_DW_FORM_name (form);
3725
3726 if (name != NULL)
3727 return name;
3728
3729 return "DW_FORM_<unknown>";
3730 }
3731 \f
3732 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3733 instance of an inlined instance of a decl which is local to an inline
3734 function, so we have to trace all of the way back through the origin chain
3735 to find out what sort of node actually served as the original seed for the
3736 given block. */
3737
3738 static tree
3739 decl_ultimate_origin (const_tree decl)
3740 {
3741 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3742 return NULL_TREE;
3743
3744 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3745 we're trying to output the abstract instance of this function. */
3746 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3747 return NULL_TREE;
3748
3749 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3750 most distant ancestor, this should never happen. */
3751 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3752
3753 return DECL_ABSTRACT_ORIGIN (decl);
3754 }
3755
3756 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3757 of a virtual function may refer to a base class, so we check the 'this'
3758 parameter. */
3759
3760 static tree
3761 decl_class_context (tree decl)
3762 {
3763 tree context = NULL_TREE;
3764
3765 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3766 context = DECL_CONTEXT (decl);
3767 else
3768 context = TYPE_MAIN_VARIANT
3769 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3770
3771 if (context && !TYPE_P (context))
3772 context = NULL_TREE;
3773
3774 return context;
3775 }
3776 \f
3777 /* Add an attribute/value pair to a DIE. */
3778
3779 static inline void
3780 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3781 {
3782 /* Maybe this should be an assert? */
3783 if (die == NULL)
3784 return;
3785
3786 vec_safe_reserve (die->die_attr, 1);
3787 vec_safe_push (die->die_attr, *attr);
3788 }
3789
3790 static inline enum dw_val_class
3791 AT_class (dw_attr_ref a)
3792 {
3793 return a->dw_attr_val.val_class;
3794 }
3795
3796 /* Return the index for any attribute that will be referenced with a
3797 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3798 are stored in dw_attr_val.v.val_str for reference counting
3799 pruning. */
3800
3801 static inline unsigned int
3802 AT_index (dw_attr_ref a)
3803 {
3804 if (AT_class (a) == dw_val_class_str)
3805 return a->dw_attr_val.v.val_str->index;
3806 else if (a->dw_attr_val.val_entry != NULL)
3807 return a->dw_attr_val.val_entry->index;
3808 return NOT_INDEXED;
3809 }
3810
3811 /* Add a flag value attribute to a DIE. */
3812
3813 static inline void
3814 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3815 {
3816 dw_attr_node attr;
3817
3818 attr.dw_attr = attr_kind;
3819 attr.dw_attr_val.val_class = dw_val_class_flag;
3820 attr.dw_attr_val.val_entry = NULL;
3821 attr.dw_attr_val.v.val_flag = flag;
3822 add_dwarf_attr (die, &attr);
3823 }
3824
3825 static inline unsigned
3826 AT_flag (dw_attr_ref a)
3827 {
3828 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3829 return a->dw_attr_val.v.val_flag;
3830 }
3831
3832 /* Add a signed integer attribute value to a DIE. */
3833
3834 static inline void
3835 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3836 {
3837 dw_attr_node attr;
3838
3839 attr.dw_attr = attr_kind;
3840 attr.dw_attr_val.val_class = dw_val_class_const;
3841 attr.dw_attr_val.val_entry = NULL;
3842 attr.dw_attr_val.v.val_int = int_val;
3843 add_dwarf_attr (die, &attr);
3844 }
3845
3846 static inline HOST_WIDE_INT
3847 AT_int (dw_attr_ref a)
3848 {
3849 gcc_assert (a && AT_class (a) == dw_val_class_const);
3850 return a->dw_attr_val.v.val_int;
3851 }
3852
3853 /* Add an unsigned integer attribute value to a DIE. */
3854
3855 static inline void
3856 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3857 unsigned HOST_WIDE_INT unsigned_val)
3858 {
3859 dw_attr_node attr;
3860
3861 attr.dw_attr = attr_kind;
3862 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3863 attr.dw_attr_val.val_entry = NULL;
3864 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3865 add_dwarf_attr (die, &attr);
3866 }
3867
3868 static inline unsigned HOST_WIDE_INT
3869 AT_unsigned (dw_attr_ref a)
3870 {
3871 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3872 return a->dw_attr_val.v.val_unsigned;
3873 }
3874
3875 /* Add an unsigned wide integer attribute value to a DIE. */
3876
3877 static inline void
3878 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3879 const wide_int& w)
3880 {
3881 dw_attr_node attr;
3882
3883 attr.dw_attr = attr_kind;
3884 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3885 attr.dw_attr_val.val_entry = NULL;
3886 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3887 *attr.dw_attr_val.v.val_wide = w;
3888 add_dwarf_attr (die, &attr);
3889 }
3890
3891 /* Add an unsigned double integer attribute value to a DIE. */
3892
3893 static inline void
3894 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3895 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3896 {
3897 dw_attr_node attr;
3898
3899 attr.dw_attr = attr_kind;
3900 attr.dw_attr_val.val_class = dw_val_class_const_double;
3901 attr.dw_attr_val.val_entry = NULL;
3902 attr.dw_attr_val.v.val_double.high = high;
3903 attr.dw_attr_val.v.val_double.low = low;
3904 add_dwarf_attr (die, &attr);
3905 }
3906
3907 /* Add a floating point attribute value to a DIE and return it. */
3908
3909 static inline void
3910 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3911 unsigned int length, unsigned int elt_size, unsigned char *array)
3912 {
3913 dw_attr_node attr;
3914
3915 attr.dw_attr = attr_kind;
3916 attr.dw_attr_val.val_class = dw_val_class_vec;
3917 attr.dw_attr_val.val_entry = NULL;
3918 attr.dw_attr_val.v.val_vec.length = length;
3919 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3920 attr.dw_attr_val.v.val_vec.array = array;
3921 add_dwarf_attr (die, &attr);
3922 }
3923
3924 /* Add an 8-byte data attribute value to a DIE. */
3925
3926 static inline void
3927 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3928 unsigned char data8[8])
3929 {
3930 dw_attr_node attr;
3931
3932 attr.dw_attr = attr_kind;
3933 attr.dw_attr_val.val_class = dw_val_class_data8;
3934 attr.dw_attr_val.val_entry = NULL;
3935 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3936 add_dwarf_attr (die, &attr);
3937 }
3938
3939 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3940 dwarf_split_debug_info, address attributes in dies destined for the
3941 final executable have force_direct set to avoid using indexed
3942 references. */
3943
3944 static inline void
3945 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3946 bool force_direct)
3947 {
3948 dw_attr_node attr;
3949 char * lbl_id;
3950
3951 lbl_id = xstrdup (lbl_low);
3952 attr.dw_attr = DW_AT_low_pc;
3953 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3954 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3955 if (dwarf_split_debug_info && !force_direct)
3956 attr.dw_attr_val.val_entry
3957 = add_addr_table_entry (lbl_id, ate_kind_label);
3958 else
3959 attr.dw_attr_val.val_entry = NULL;
3960 add_dwarf_attr (die, &attr);
3961
3962 attr.dw_attr = DW_AT_high_pc;
3963 if (dwarf_version < 4)
3964 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3965 else
3966 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3967 lbl_id = xstrdup (lbl_high);
3968 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3969 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3970 && dwarf_split_debug_info && !force_direct)
3971 attr.dw_attr_val.val_entry
3972 = add_addr_table_entry (lbl_id, ate_kind_label);
3973 else
3974 attr.dw_attr_val.val_entry = NULL;
3975 add_dwarf_attr (die, &attr);
3976 }
3977
3978 /* Hash and equality functions for debug_str_hash. */
3979
3980 hashval_t
3981 indirect_string_hasher::hash (indirect_string_node *x)
3982 {
3983 return htab_hash_string (x->str);
3984 }
3985
3986 bool
3987 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3988 {
3989 return strcmp (x1->str, x2) == 0;
3990 }
3991
3992 /* Add STR to the given string hash table. */
3993
3994 static struct indirect_string_node *
3995 find_AT_string_in_table (const char *str,
3996 hash_table<indirect_string_hasher> *table)
3997 {
3998 struct indirect_string_node *node;
3999
4000 indirect_string_node **slot
4001 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4002 if (*slot == NULL)
4003 {
4004 node = ggc_cleared_alloc<indirect_string_node> ();
4005 node->str = ggc_strdup (str);
4006 *slot = node;
4007 }
4008 else
4009 node = *slot;
4010
4011 node->refcount++;
4012 return node;
4013 }
4014
4015 /* Add STR to the indirect string hash table. */
4016
4017 static struct indirect_string_node *
4018 find_AT_string (const char *str)
4019 {
4020 if (! debug_str_hash)
4021 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4022
4023 return find_AT_string_in_table (str, debug_str_hash);
4024 }
4025
4026 /* Add a string attribute value to a DIE. */
4027
4028 static inline void
4029 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4030 {
4031 dw_attr_node attr;
4032 struct indirect_string_node *node;
4033
4034 node = find_AT_string (str);
4035
4036 attr.dw_attr = attr_kind;
4037 attr.dw_attr_val.val_class = dw_val_class_str;
4038 attr.dw_attr_val.val_entry = NULL;
4039 attr.dw_attr_val.v.val_str = node;
4040 add_dwarf_attr (die, &attr);
4041 }
4042
4043 static inline const char *
4044 AT_string (dw_attr_ref a)
4045 {
4046 gcc_assert (a && AT_class (a) == dw_val_class_str);
4047 return a->dw_attr_val.v.val_str->str;
4048 }
4049
4050 /* Call this function directly to bypass AT_string_form's logic to put
4051 the string inline in the die. */
4052
4053 static void
4054 set_indirect_string (struct indirect_string_node *node)
4055 {
4056 char label[32];
4057 /* Already indirect is a no op. */
4058 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4059 {
4060 gcc_assert (node->label);
4061 return;
4062 }
4063 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4064 ++dw2_string_counter;
4065 node->label = xstrdup (label);
4066
4067 if (!dwarf_split_debug_info)
4068 {
4069 node->form = DW_FORM_strp;
4070 node->index = NOT_INDEXED;
4071 }
4072 else
4073 {
4074 node->form = DW_FORM_GNU_str_index;
4075 node->index = NO_INDEX_ASSIGNED;
4076 }
4077 }
4078
4079 /* Find out whether a string should be output inline in DIE
4080 or out-of-line in .debug_str section. */
4081
4082 static enum dwarf_form
4083 find_string_form (struct indirect_string_node *node)
4084 {
4085 unsigned int len;
4086
4087 if (node->form)
4088 return node->form;
4089
4090 len = strlen (node->str) + 1;
4091
4092 /* If the string is shorter or equal to the size of the reference, it is
4093 always better to put it inline. */
4094 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4095 return node->form = DW_FORM_string;
4096
4097 /* If we cannot expect the linker to merge strings in .debug_str
4098 section, only put it into .debug_str if it is worth even in this
4099 single module. */
4100 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4101 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4102 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4103 return node->form = DW_FORM_string;
4104
4105 set_indirect_string (node);
4106
4107 return node->form;
4108 }
4109
4110 /* Find out whether the string referenced from the attribute should be
4111 output inline in DIE or out-of-line in .debug_str section. */
4112
4113 static enum dwarf_form
4114 AT_string_form (dw_attr_ref a)
4115 {
4116 gcc_assert (a && AT_class (a) == dw_val_class_str);
4117 return find_string_form (a->dw_attr_val.v.val_str);
4118 }
4119
4120 /* Add a DIE reference attribute value to a DIE. */
4121
4122 static inline void
4123 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4124 {
4125 dw_attr_node attr;
4126
4127 #ifdef ENABLE_CHECKING
4128 gcc_assert (targ_die != NULL);
4129 #else
4130 /* With LTO we can end up trying to reference something we didn't create
4131 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4132 if (targ_die == NULL)
4133 return;
4134 #endif
4135
4136 attr.dw_attr = attr_kind;
4137 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4138 attr.dw_attr_val.val_entry = NULL;
4139 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4140 attr.dw_attr_val.v.val_die_ref.external = 0;
4141 add_dwarf_attr (die, &attr);
4142 }
4143
4144 /* Change DIE reference REF to point to NEW_DIE instead. */
4145
4146 static inline void
4147 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4148 {
4149 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4150 ref->dw_attr_val.v.val_die_ref.die = new_die;
4151 ref->dw_attr_val.v.val_die_ref.external = 0;
4152 }
4153
4154 /* Add an AT_specification attribute to a DIE, and also make the back
4155 pointer from the specification to the definition. */
4156
4157 static inline void
4158 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4159 {
4160 add_AT_die_ref (die, DW_AT_specification, targ_die);
4161 gcc_assert (!targ_die->die_definition);
4162 targ_die->die_definition = die;
4163 }
4164
4165 static inline dw_die_ref
4166 AT_ref (dw_attr_ref a)
4167 {
4168 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4169 return a->dw_attr_val.v.val_die_ref.die;
4170 }
4171
4172 static inline int
4173 AT_ref_external (dw_attr_ref a)
4174 {
4175 if (a && AT_class (a) == dw_val_class_die_ref)
4176 return a->dw_attr_val.v.val_die_ref.external;
4177
4178 return 0;
4179 }
4180
4181 static inline void
4182 set_AT_ref_external (dw_attr_ref a, int i)
4183 {
4184 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4185 a->dw_attr_val.v.val_die_ref.external = i;
4186 }
4187
4188 /* Add an FDE reference attribute value to a DIE. */
4189
4190 static inline void
4191 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4192 {
4193 dw_attr_node attr;
4194
4195 attr.dw_attr = attr_kind;
4196 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4197 attr.dw_attr_val.val_entry = NULL;
4198 attr.dw_attr_val.v.val_fde_index = targ_fde;
4199 add_dwarf_attr (die, &attr);
4200 }
4201
4202 /* Add a location description attribute value to a DIE. */
4203
4204 static inline void
4205 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4206 {
4207 dw_attr_node attr;
4208
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_loc;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_loc = loc;
4213 add_dwarf_attr (die, &attr);
4214 }
4215
4216 static inline dw_loc_descr_ref
4217 AT_loc (dw_attr_ref a)
4218 {
4219 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4220 return a->dw_attr_val.v.val_loc;
4221 }
4222
4223 static inline void
4224 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4225 {
4226 dw_attr_node attr;
4227
4228 attr.dw_attr = attr_kind;
4229 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4230 attr.dw_attr_val.val_entry = NULL;
4231 attr.dw_attr_val.v.val_loc_list = loc_list;
4232 add_dwarf_attr (die, &attr);
4233 have_location_lists = true;
4234 }
4235
4236 static inline dw_loc_list_ref
4237 AT_loc_list (dw_attr_ref a)
4238 {
4239 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4240 return a->dw_attr_val.v.val_loc_list;
4241 }
4242
4243 static inline dw_loc_list_ref *
4244 AT_loc_list_ptr (dw_attr_ref a)
4245 {
4246 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4247 return &a->dw_attr_val.v.val_loc_list;
4248 }
4249
4250 struct addr_hasher : ggc_hasher<addr_table_entry *>
4251 {
4252 static hashval_t hash (addr_table_entry *);
4253 static bool equal (addr_table_entry *, addr_table_entry *);
4254 };
4255
4256 /* Table of entries into the .debug_addr section. */
4257
4258 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4259
4260 /* Hash an address_table_entry. */
4261
4262 hashval_t
4263 addr_hasher::hash (addr_table_entry *a)
4264 {
4265 inchash::hash hstate;
4266 switch (a->kind)
4267 {
4268 case ate_kind_rtx:
4269 hstate.add_int (0);
4270 break;
4271 case ate_kind_rtx_dtprel:
4272 hstate.add_int (1);
4273 break;
4274 case ate_kind_label:
4275 return htab_hash_string (a->addr.label);
4276 default:
4277 gcc_unreachable ();
4278 }
4279 inchash::add_rtx (a->addr.rtl, hstate);
4280 return hstate.end ();
4281 }
4282
4283 /* Determine equality for two address_table_entries. */
4284
4285 bool
4286 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4287 {
4288 if (a1->kind != a2->kind)
4289 return 0;
4290 switch (a1->kind)
4291 {
4292 case ate_kind_rtx:
4293 case ate_kind_rtx_dtprel:
4294 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4295 case ate_kind_label:
4296 return strcmp (a1->addr.label, a2->addr.label) == 0;
4297 default:
4298 gcc_unreachable ();
4299 }
4300 }
4301
4302 /* Initialize an addr_table_entry. */
4303
4304 void
4305 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4306 {
4307 e->kind = kind;
4308 switch (kind)
4309 {
4310 case ate_kind_rtx:
4311 case ate_kind_rtx_dtprel:
4312 e->addr.rtl = (rtx) addr;
4313 break;
4314 case ate_kind_label:
4315 e->addr.label = (char *) addr;
4316 break;
4317 }
4318 e->refcount = 0;
4319 e->index = NO_INDEX_ASSIGNED;
4320 }
4321
4322 /* Add attr to the address table entry to the table. Defer setting an
4323 index until output time. */
4324
4325 static addr_table_entry *
4326 add_addr_table_entry (void *addr, enum ate_kind kind)
4327 {
4328 addr_table_entry *node;
4329 addr_table_entry finder;
4330
4331 gcc_assert (dwarf_split_debug_info);
4332 if (! addr_index_table)
4333 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4334 init_addr_table_entry (&finder, kind, addr);
4335 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4336
4337 if (*slot == HTAB_EMPTY_ENTRY)
4338 {
4339 node = ggc_cleared_alloc<addr_table_entry> ();
4340 init_addr_table_entry (node, kind, addr);
4341 *slot = node;
4342 }
4343 else
4344 node = *slot;
4345
4346 node->refcount++;
4347 return node;
4348 }
4349
4350 /* Remove an entry from the addr table by decrementing its refcount.
4351 Strictly, decrementing the refcount would be enough, but the
4352 assertion that the entry is actually in the table has found
4353 bugs. */
4354
4355 static void
4356 remove_addr_table_entry (addr_table_entry *entry)
4357 {
4358 gcc_assert (dwarf_split_debug_info && addr_index_table);
4359 /* After an index is assigned, the table is frozen. */
4360 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4361 entry->refcount--;
4362 }
4363
4364 /* Given a location list, remove all addresses it refers to from the
4365 address_table. */
4366
4367 static void
4368 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4369 {
4370 for (; descr; descr = descr->dw_loc_next)
4371 if (descr->dw_loc_oprnd1.val_entry != NULL)
4372 {
4373 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4374 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4375 }
4376 }
4377
4378 /* A helper function for dwarf2out_finish called through
4379 htab_traverse. Assign an addr_table_entry its index. All entries
4380 must be collected into the table when this function is called,
4381 because the indexing code relies on htab_traverse to traverse nodes
4382 in the same order for each run. */
4383
4384 int
4385 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4386 {
4387 addr_table_entry *node = *h;
4388
4389 /* Don't index unreferenced nodes. */
4390 if (node->refcount == 0)
4391 return 1;
4392
4393 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4394 node->index = *index;
4395 *index += 1;
4396
4397 return 1;
4398 }
4399
4400 /* Add an address constant attribute value to a DIE. When using
4401 dwarf_split_debug_info, address attributes in dies destined for the
4402 final executable should be direct references--setting the parameter
4403 force_direct ensures this behavior. */
4404
4405 static inline void
4406 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4407 bool force_direct)
4408 {
4409 dw_attr_node attr;
4410
4411 attr.dw_attr = attr_kind;
4412 attr.dw_attr_val.val_class = dw_val_class_addr;
4413 attr.dw_attr_val.v.val_addr = addr;
4414 if (dwarf_split_debug_info && !force_direct)
4415 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4416 else
4417 attr.dw_attr_val.val_entry = NULL;
4418 add_dwarf_attr (die, &attr);
4419 }
4420
4421 /* Get the RTX from to an address DIE attribute. */
4422
4423 static inline rtx
4424 AT_addr (dw_attr_ref a)
4425 {
4426 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4427 return a->dw_attr_val.v.val_addr;
4428 }
4429
4430 /* Add a file attribute value to a DIE. */
4431
4432 static inline void
4433 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4434 struct dwarf_file_data *fd)
4435 {
4436 dw_attr_node attr;
4437
4438 attr.dw_attr = attr_kind;
4439 attr.dw_attr_val.val_class = dw_val_class_file;
4440 attr.dw_attr_val.val_entry = NULL;
4441 attr.dw_attr_val.v.val_file = fd;
4442 add_dwarf_attr (die, &attr);
4443 }
4444
4445 /* Get the dwarf_file_data from a file DIE attribute. */
4446
4447 static inline struct dwarf_file_data *
4448 AT_file (dw_attr_ref a)
4449 {
4450 gcc_assert (a && AT_class (a) == dw_val_class_file);
4451 return a->dw_attr_val.v.val_file;
4452 }
4453
4454 /* Add a vms delta attribute value to a DIE. */
4455
4456 static inline void
4457 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4458 const char *lbl1, const char *lbl2)
4459 {
4460 dw_attr_node attr;
4461
4462 attr.dw_attr = attr_kind;
4463 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4464 attr.dw_attr_val.val_entry = NULL;
4465 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4466 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4467 add_dwarf_attr (die, &attr);
4468 }
4469
4470 /* Add a label identifier attribute value to a DIE. */
4471
4472 static inline void
4473 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4474 const char *lbl_id)
4475 {
4476 dw_attr_node attr;
4477
4478 attr.dw_attr = attr_kind;
4479 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4480 attr.dw_attr_val.val_entry = NULL;
4481 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4482 if (dwarf_split_debug_info)
4483 attr.dw_attr_val.val_entry
4484 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4485 ate_kind_label);
4486 add_dwarf_attr (die, &attr);
4487 }
4488
4489 /* Add a section offset attribute value to a DIE, an offset into the
4490 debug_line section. */
4491
4492 static inline void
4493 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4494 const char *label)
4495 {
4496 dw_attr_node attr;
4497
4498 attr.dw_attr = attr_kind;
4499 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4500 attr.dw_attr_val.val_entry = NULL;
4501 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4502 add_dwarf_attr (die, &attr);
4503 }
4504
4505 /* Add a section offset attribute value to a DIE, an offset into the
4506 debug_macinfo section. */
4507
4508 static inline void
4509 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4510 const char *label)
4511 {
4512 dw_attr_node attr;
4513
4514 attr.dw_attr = attr_kind;
4515 attr.dw_attr_val.val_class = dw_val_class_macptr;
4516 attr.dw_attr_val.val_entry = NULL;
4517 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4518 add_dwarf_attr (die, &attr);
4519 }
4520
4521 /* Add an offset attribute value to a DIE. */
4522
4523 static inline void
4524 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4525 unsigned HOST_WIDE_INT offset)
4526 {
4527 dw_attr_node attr;
4528
4529 attr.dw_attr = attr_kind;
4530 attr.dw_attr_val.val_class = dw_val_class_offset;
4531 attr.dw_attr_val.val_entry = NULL;
4532 attr.dw_attr_val.v.val_offset = offset;
4533 add_dwarf_attr (die, &attr);
4534 }
4535
4536 /* Add a range_list attribute value to a DIE. When using
4537 dwarf_split_debug_info, address attributes in dies destined for the
4538 final executable should be direct references--setting the parameter
4539 force_direct ensures this behavior. */
4540
4541 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4542 #define RELOCATED_OFFSET (NULL)
4543
4544 static void
4545 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4546 long unsigned int offset, bool force_direct)
4547 {
4548 dw_attr_node attr;
4549
4550 attr.dw_attr = attr_kind;
4551 attr.dw_attr_val.val_class = dw_val_class_range_list;
4552 /* For the range_list attribute, use val_entry to store whether the
4553 offset should follow split-debug-info or normal semantics. This
4554 value is read in output_range_list_offset. */
4555 if (dwarf_split_debug_info && !force_direct)
4556 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4557 else
4558 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4559 attr.dw_attr_val.v.val_offset = offset;
4560 add_dwarf_attr (die, &attr);
4561 }
4562
4563 /* Return the start label of a delta attribute. */
4564
4565 static inline const char *
4566 AT_vms_delta1 (dw_attr_ref a)
4567 {
4568 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4569 return a->dw_attr_val.v.val_vms_delta.lbl1;
4570 }
4571
4572 /* Return the end label of a delta attribute. */
4573
4574 static inline const char *
4575 AT_vms_delta2 (dw_attr_ref a)
4576 {
4577 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4578 return a->dw_attr_val.v.val_vms_delta.lbl2;
4579 }
4580
4581 static inline const char *
4582 AT_lbl (dw_attr_ref a)
4583 {
4584 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4585 || AT_class (a) == dw_val_class_lineptr
4586 || AT_class (a) == dw_val_class_macptr
4587 || AT_class (a) == dw_val_class_high_pc));
4588 return a->dw_attr_val.v.val_lbl_id;
4589 }
4590
4591 /* Get the attribute of type attr_kind. */
4592
4593 static dw_attr_ref
4594 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4595 {
4596 dw_attr_ref a;
4597 unsigned ix;
4598 dw_die_ref spec = NULL;
4599
4600 if (! die)
4601 return NULL;
4602
4603 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4604 if (a->dw_attr == attr_kind)
4605 return a;
4606 else if (a->dw_attr == DW_AT_specification
4607 || a->dw_attr == DW_AT_abstract_origin)
4608 spec = AT_ref (a);
4609
4610 if (spec)
4611 return get_AT (spec, attr_kind);
4612
4613 return NULL;
4614 }
4615
4616 /* Returns the parent of the declaration of DIE. */
4617
4618 static dw_die_ref
4619 get_die_parent (dw_die_ref die)
4620 {
4621 dw_die_ref t;
4622
4623 if (!die)
4624 return NULL;
4625
4626 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4627 || (t = get_AT_ref (die, DW_AT_specification)))
4628 die = t;
4629
4630 return die->die_parent;
4631 }
4632
4633 /* Return the "low pc" attribute value, typically associated with a subprogram
4634 DIE. Return null if the "low pc" attribute is either not present, or if it
4635 cannot be represented as an assembler label identifier. */
4636
4637 static inline const char *
4638 get_AT_low_pc (dw_die_ref die)
4639 {
4640 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4641
4642 return a ? AT_lbl (a) : NULL;
4643 }
4644
4645 /* Return the "high pc" attribute value, typically associated with a subprogram
4646 DIE. Return null if the "high pc" attribute is either not present, or if it
4647 cannot be represented as an assembler label identifier. */
4648
4649 static inline const char *
4650 get_AT_hi_pc (dw_die_ref die)
4651 {
4652 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4653
4654 return a ? AT_lbl (a) : NULL;
4655 }
4656
4657 /* Return the value of the string attribute designated by ATTR_KIND, or
4658 NULL if it is not present. */
4659
4660 static inline const char *
4661 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4662 {
4663 dw_attr_ref a = get_AT (die, attr_kind);
4664
4665 return a ? AT_string (a) : NULL;
4666 }
4667
4668 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4669 if it is not present. */
4670
4671 static inline int
4672 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4673 {
4674 dw_attr_ref a = get_AT (die, attr_kind);
4675
4676 return a ? AT_flag (a) : 0;
4677 }
4678
4679 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4680 if it is not present. */
4681
4682 static inline unsigned
4683 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4684 {
4685 dw_attr_ref a = get_AT (die, attr_kind);
4686
4687 return a ? AT_unsigned (a) : 0;
4688 }
4689
4690 static inline dw_die_ref
4691 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4692 {
4693 dw_attr_ref a = get_AT (die, attr_kind);
4694
4695 return a ? AT_ref (a) : NULL;
4696 }
4697
4698 static inline struct dwarf_file_data *
4699 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4700 {
4701 dw_attr_ref a = get_AT (die, attr_kind);
4702
4703 return a ? AT_file (a) : NULL;
4704 }
4705
4706 /* Return TRUE if the language is C++. */
4707
4708 static inline bool
4709 is_cxx (void)
4710 {
4711 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4712
4713 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4714 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4715 }
4716
4717 /* Return TRUE if the language is Java. */
4718
4719 static inline bool
4720 is_java (void)
4721 {
4722 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4723
4724 return lang == DW_LANG_Java;
4725 }
4726
4727 /* Return TRUE if the language is Fortran. */
4728
4729 static inline bool
4730 is_fortran (void)
4731 {
4732 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4733
4734 return (lang == DW_LANG_Fortran77
4735 || lang == DW_LANG_Fortran90
4736 || lang == DW_LANG_Fortran95
4737 || lang == DW_LANG_Fortran03
4738 || lang == DW_LANG_Fortran08);
4739 }
4740
4741 /* Return TRUE if the language is Ada. */
4742
4743 static inline bool
4744 is_ada (void)
4745 {
4746 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4747
4748 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4749 }
4750
4751 /* Remove the specified attribute if present. */
4752
4753 static void
4754 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4755 {
4756 dw_attr_ref a;
4757 unsigned ix;
4758
4759 if (! die)
4760 return;
4761
4762 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4763 if (a->dw_attr == attr_kind)
4764 {
4765 if (AT_class (a) == dw_val_class_str)
4766 if (a->dw_attr_val.v.val_str->refcount)
4767 a->dw_attr_val.v.val_str->refcount--;
4768
4769 /* vec::ordered_remove should help reduce the number of abbrevs
4770 that are needed. */
4771 die->die_attr->ordered_remove (ix);
4772 return;
4773 }
4774 }
4775
4776 /* Remove CHILD from its parent. PREV must have the property that
4777 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4778
4779 static void
4780 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4781 {
4782 gcc_assert (child->die_parent == prev->die_parent);
4783 gcc_assert (prev->die_sib == child);
4784 if (prev == child)
4785 {
4786 gcc_assert (child->die_parent->die_child == child);
4787 prev = NULL;
4788 }
4789 else
4790 prev->die_sib = child->die_sib;
4791 if (child->die_parent->die_child == child)
4792 child->die_parent->die_child = prev;
4793 }
4794
4795 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4796 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4797
4798 static void
4799 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4800 {
4801 dw_die_ref parent = old_child->die_parent;
4802
4803 gcc_assert (parent == prev->die_parent);
4804 gcc_assert (prev->die_sib == old_child);
4805
4806 new_child->die_parent = parent;
4807 if (prev == old_child)
4808 {
4809 gcc_assert (parent->die_child == old_child);
4810 new_child->die_sib = new_child;
4811 }
4812 else
4813 {
4814 prev->die_sib = new_child;
4815 new_child->die_sib = old_child->die_sib;
4816 }
4817 if (old_child->die_parent->die_child == old_child)
4818 old_child->die_parent->die_child = new_child;
4819 }
4820
4821 /* Move all children from OLD_PARENT to NEW_PARENT. */
4822
4823 static void
4824 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4825 {
4826 dw_die_ref c;
4827 new_parent->die_child = old_parent->die_child;
4828 old_parent->die_child = NULL;
4829 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4830 }
4831
4832 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4833 matches TAG. */
4834
4835 static void
4836 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4837 {
4838 dw_die_ref c;
4839
4840 c = die->die_child;
4841 if (c) do {
4842 dw_die_ref prev = c;
4843 c = c->die_sib;
4844 while (c->die_tag == tag)
4845 {
4846 remove_child_with_prev (c, prev);
4847 /* Might have removed every child. */
4848 if (c == c->die_sib)
4849 return;
4850 c = c->die_sib;
4851 }
4852 } while (c != die->die_child);
4853 }
4854
4855 /* Add a CHILD_DIE as the last child of DIE. */
4856
4857 static void
4858 add_child_die (dw_die_ref die, dw_die_ref child_die)
4859 {
4860 /* FIXME this should probably be an assert. */
4861 if (! die || ! child_die)
4862 return;
4863 gcc_assert (die != child_die);
4864
4865 child_die->die_parent = die;
4866 if (die->die_child)
4867 {
4868 child_die->die_sib = die->die_child->die_sib;
4869 die->die_child->die_sib = child_die;
4870 }
4871 else
4872 child_die->die_sib = child_die;
4873 die->die_child = child_die;
4874 }
4875
4876 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4877 is the specification, to the end of PARENT's list of children.
4878 This is done by removing and re-adding it. */
4879
4880 static void
4881 splice_child_die (dw_die_ref parent, dw_die_ref child)
4882 {
4883 dw_die_ref p;
4884
4885 /* We want the declaration DIE from inside the class, not the
4886 specification DIE at toplevel. */
4887 if (child->die_parent != parent)
4888 {
4889 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4890
4891 if (tmp)
4892 child = tmp;
4893 }
4894
4895 gcc_assert (child->die_parent == parent
4896 || (child->die_parent
4897 == get_AT_ref (parent, DW_AT_specification)));
4898
4899 for (p = child->die_parent->die_child; ; p = p->die_sib)
4900 if (p->die_sib == child)
4901 {
4902 remove_child_with_prev (child, p);
4903 break;
4904 }
4905
4906 add_child_die (parent, child);
4907 }
4908
4909 /* Return a pointer to a newly created DIE node. */
4910
4911 static inline dw_die_ref
4912 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4913 {
4914 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4915
4916 die->die_tag = tag_value;
4917
4918 if (parent_die != NULL)
4919 add_child_die (parent_die, die);
4920 else
4921 {
4922 limbo_die_node *limbo_node;
4923
4924 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4925 limbo_node->die = die;
4926 limbo_node->created_for = t;
4927 limbo_node->next = limbo_die_list;
4928 limbo_die_list = limbo_node;
4929 }
4930
4931 return die;
4932 }
4933
4934 /* Return the DIE associated with the given type specifier. */
4935
4936 static inline dw_die_ref
4937 lookup_type_die (tree type)
4938 {
4939 return TYPE_SYMTAB_DIE (type);
4940 }
4941
4942 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4943 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4944 anonymous type instead the one of the naming typedef. */
4945
4946 static inline dw_die_ref
4947 strip_naming_typedef (tree type, dw_die_ref type_die)
4948 {
4949 if (type
4950 && TREE_CODE (type) == RECORD_TYPE
4951 && type_die
4952 && type_die->die_tag == DW_TAG_typedef
4953 && is_naming_typedef_decl (TYPE_NAME (type)))
4954 type_die = get_AT_ref (type_die, DW_AT_type);
4955 return type_die;
4956 }
4957
4958 /* Like lookup_type_die, but if type is an anonymous type named by a
4959 typedef[1], return the DIE of the anonymous type instead the one of
4960 the naming typedef. This is because in gen_typedef_die, we did
4961 equate the anonymous struct named by the typedef with the DIE of
4962 the naming typedef. So by default, lookup_type_die on an anonymous
4963 struct yields the DIE of the naming typedef.
4964
4965 [1]: Read the comment of is_naming_typedef_decl to learn about what
4966 a naming typedef is. */
4967
4968 static inline dw_die_ref
4969 lookup_type_die_strip_naming_typedef (tree type)
4970 {
4971 dw_die_ref die = lookup_type_die (type);
4972 return strip_naming_typedef (type, die);
4973 }
4974
4975 /* Equate a DIE to a given type specifier. */
4976
4977 static inline void
4978 equate_type_number_to_die (tree type, dw_die_ref type_die)
4979 {
4980 TYPE_SYMTAB_DIE (type) = type_die;
4981 }
4982
4983 /* Returns a hash value for X (which really is a die_struct). */
4984
4985 inline hashval_t
4986 decl_die_hasher::hash (die_node *x)
4987 {
4988 return (hashval_t) x->decl_id;
4989 }
4990
4991 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4992
4993 inline bool
4994 decl_die_hasher::equal (die_node *x, tree y)
4995 {
4996 return (x->decl_id == DECL_UID (y));
4997 }
4998
4999 /* Return the DIE associated with a given declaration. */
5000
5001 static inline dw_die_ref
5002 lookup_decl_die (tree decl)
5003 {
5004 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5005 }
5006
5007 /* Returns a hash value for X (which really is a var_loc_list). */
5008
5009 inline hashval_t
5010 decl_loc_hasher::hash (var_loc_list *x)
5011 {
5012 return (hashval_t) x->decl_id;
5013 }
5014
5015 /* Return nonzero if decl_id of var_loc_list X is the same as
5016 UID of decl *Y. */
5017
5018 inline bool
5019 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5020 {
5021 return (x->decl_id == DECL_UID (y));
5022 }
5023
5024 /* Return the var_loc list associated with a given declaration. */
5025
5026 static inline var_loc_list *
5027 lookup_decl_loc (const_tree decl)
5028 {
5029 if (!decl_loc_table)
5030 return NULL;
5031 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5032 }
5033
5034 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5035
5036 inline hashval_t
5037 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5038 {
5039 return (hashval_t) x->decl_id;
5040 }
5041
5042 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5043 UID of decl *Y. */
5044
5045 inline bool
5046 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5047 {
5048 return (x->decl_id == DECL_UID (y));
5049 }
5050
5051 /* Equate a DIE to a particular declaration. */
5052
5053 static void
5054 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5055 {
5056 unsigned int decl_id = DECL_UID (decl);
5057
5058 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5059 decl_die->decl_id = decl_id;
5060 }
5061
5062 /* Return how many bits covers PIECE EXPR_LIST. */
5063
5064 static HOST_WIDE_INT
5065 decl_piece_bitsize (rtx piece)
5066 {
5067 int ret = (int) GET_MODE (piece);
5068 if (ret)
5069 return ret;
5070 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5071 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5072 return INTVAL (XEXP (XEXP (piece, 0), 0));
5073 }
5074
5075 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5076
5077 static rtx *
5078 decl_piece_varloc_ptr (rtx piece)
5079 {
5080 if ((int) GET_MODE (piece))
5081 return &XEXP (piece, 0);
5082 else
5083 return &XEXP (XEXP (piece, 0), 1);
5084 }
5085
5086 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5087 Next is the chain of following piece nodes. */
5088
5089 static rtx_expr_list *
5090 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5091 {
5092 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5093 return alloc_EXPR_LIST (bitsize, loc_note, next);
5094 else
5095 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5096 GEN_INT (bitsize),
5097 loc_note), next);
5098 }
5099
5100 /* Return rtx that should be stored into loc field for
5101 LOC_NOTE and BITPOS/BITSIZE. */
5102
5103 static rtx
5104 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5105 HOST_WIDE_INT bitsize)
5106 {
5107 if (bitsize != -1)
5108 {
5109 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5110 if (bitpos != 0)
5111 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5112 }
5113 return loc_note;
5114 }
5115
5116 /* This function either modifies location piece list *DEST in
5117 place (if SRC and INNER is NULL), or copies location piece list
5118 *SRC to *DEST while modifying it. Location BITPOS is modified
5119 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5120 not copied and if needed some padding around it is added.
5121 When modifying in place, DEST should point to EXPR_LIST where
5122 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5123 to the start of the whole list and INNER points to the EXPR_LIST
5124 where earlier pieces cover PIECE_BITPOS bits. */
5125
5126 static void
5127 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5128 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5129 HOST_WIDE_INT bitsize, rtx loc_note)
5130 {
5131 HOST_WIDE_INT diff;
5132 bool copy = inner != NULL;
5133
5134 if (copy)
5135 {
5136 /* First copy all nodes preceding the current bitpos. */
5137 while (src != inner)
5138 {
5139 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5140 decl_piece_bitsize (*src), NULL_RTX);
5141 dest = &XEXP (*dest, 1);
5142 src = &XEXP (*src, 1);
5143 }
5144 }
5145 /* Add padding if needed. */
5146 if (bitpos != piece_bitpos)
5147 {
5148 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5149 copy ? NULL_RTX : *dest);
5150 dest = &XEXP (*dest, 1);
5151 }
5152 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5153 {
5154 gcc_assert (!copy);
5155 /* A piece with correct bitpos and bitsize already exist,
5156 just update the location for it and return. */
5157 *decl_piece_varloc_ptr (*dest) = loc_note;
5158 return;
5159 }
5160 /* Add the piece that changed. */
5161 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5162 dest = &XEXP (*dest, 1);
5163 /* Skip over pieces that overlap it. */
5164 diff = bitpos - piece_bitpos + bitsize;
5165 if (!copy)
5166 src = dest;
5167 while (diff > 0 && *src)
5168 {
5169 rtx piece = *src;
5170 diff -= decl_piece_bitsize (piece);
5171 if (copy)
5172 src = &XEXP (piece, 1);
5173 else
5174 {
5175 *src = XEXP (piece, 1);
5176 free_EXPR_LIST_node (piece);
5177 }
5178 }
5179 /* Add padding if needed. */
5180 if (diff < 0 && *src)
5181 {
5182 if (!copy)
5183 dest = src;
5184 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5185 dest = &XEXP (*dest, 1);
5186 }
5187 if (!copy)
5188 return;
5189 /* Finally copy all nodes following it. */
5190 while (*src)
5191 {
5192 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5193 decl_piece_bitsize (*src), NULL_RTX);
5194 dest = &XEXP (*dest, 1);
5195 src = &XEXP (*src, 1);
5196 }
5197 }
5198
5199 /* Add a variable location node to the linked list for DECL. */
5200
5201 static struct var_loc_node *
5202 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5203 {
5204 unsigned int decl_id;
5205 var_loc_list *temp;
5206 struct var_loc_node *loc = NULL;
5207 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5208
5209 if (TREE_CODE (decl) == VAR_DECL
5210 && DECL_HAS_DEBUG_EXPR_P (decl))
5211 {
5212 tree realdecl = DECL_DEBUG_EXPR (decl);
5213 if (handled_component_p (realdecl)
5214 || (TREE_CODE (realdecl) == MEM_REF
5215 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5216 {
5217 HOST_WIDE_INT maxsize;
5218 tree innerdecl;
5219 innerdecl
5220 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5221 if (!DECL_P (innerdecl)
5222 || DECL_IGNORED_P (innerdecl)
5223 || TREE_STATIC (innerdecl)
5224 || bitsize <= 0
5225 || bitpos + bitsize > 256
5226 || bitsize != maxsize)
5227 return NULL;
5228 decl = innerdecl;
5229 }
5230 }
5231
5232 decl_id = DECL_UID (decl);
5233 var_loc_list **slot
5234 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5235 if (*slot == NULL)
5236 {
5237 temp = ggc_cleared_alloc<var_loc_list> ();
5238 temp->decl_id = decl_id;
5239 *slot = temp;
5240 }
5241 else
5242 temp = *slot;
5243
5244 /* For PARM_DECLs try to keep around the original incoming value,
5245 even if that means we'll emit a zero-range .debug_loc entry. */
5246 if (temp->last
5247 && temp->first == temp->last
5248 && TREE_CODE (decl) == PARM_DECL
5249 && NOTE_P (temp->first->loc)
5250 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5251 && DECL_INCOMING_RTL (decl)
5252 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5253 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5254 == GET_CODE (DECL_INCOMING_RTL (decl))
5255 && prev_real_insn (temp->first->loc) == NULL_RTX
5256 && (bitsize != -1
5257 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5258 NOTE_VAR_LOCATION_LOC (loc_note))
5259 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5260 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5261 {
5262 loc = ggc_cleared_alloc<var_loc_node> ();
5263 temp->first->next = loc;
5264 temp->last = loc;
5265 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5266 }
5267 else if (temp->last)
5268 {
5269 struct var_loc_node *last = temp->last, *unused = NULL;
5270 rtx *piece_loc = NULL, last_loc_note;
5271 HOST_WIDE_INT piece_bitpos = 0;
5272 if (last->next)
5273 {
5274 last = last->next;
5275 gcc_assert (last->next == NULL);
5276 }
5277 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5278 {
5279 piece_loc = &last->loc;
5280 do
5281 {
5282 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5283 if (piece_bitpos + cur_bitsize > bitpos)
5284 break;
5285 piece_bitpos += cur_bitsize;
5286 piece_loc = &XEXP (*piece_loc, 1);
5287 }
5288 while (*piece_loc);
5289 }
5290 /* TEMP->LAST here is either pointer to the last but one or
5291 last element in the chained list, LAST is pointer to the
5292 last element. */
5293 if (label && strcmp (last->label, label) == 0)
5294 {
5295 /* For SRA optimized variables if there weren't any real
5296 insns since last note, just modify the last node. */
5297 if (piece_loc != NULL)
5298 {
5299 adjust_piece_list (piece_loc, NULL, NULL,
5300 bitpos, piece_bitpos, bitsize, loc_note);
5301 return NULL;
5302 }
5303 /* If the last note doesn't cover any instructions, remove it. */
5304 if (temp->last != last)
5305 {
5306 temp->last->next = NULL;
5307 unused = last;
5308 last = temp->last;
5309 gcc_assert (strcmp (last->label, label) != 0);
5310 }
5311 else
5312 {
5313 gcc_assert (temp->first == temp->last
5314 || (temp->first->next == temp->last
5315 && TREE_CODE (decl) == PARM_DECL));
5316 memset (temp->last, '\0', sizeof (*temp->last));
5317 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5318 return temp->last;
5319 }
5320 }
5321 if (bitsize == -1 && NOTE_P (last->loc))
5322 last_loc_note = last->loc;
5323 else if (piece_loc != NULL
5324 && *piece_loc != NULL_RTX
5325 && piece_bitpos == bitpos
5326 && decl_piece_bitsize (*piece_loc) == bitsize)
5327 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5328 else
5329 last_loc_note = NULL_RTX;
5330 /* If the current location is the same as the end of the list,
5331 and either both or neither of the locations is uninitialized,
5332 we have nothing to do. */
5333 if (last_loc_note == NULL_RTX
5334 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5335 NOTE_VAR_LOCATION_LOC (loc_note)))
5336 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5337 != NOTE_VAR_LOCATION_STATUS (loc_note))
5338 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5339 == VAR_INIT_STATUS_UNINITIALIZED)
5340 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5341 == VAR_INIT_STATUS_UNINITIALIZED))))
5342 {
5343 /* Add LOC to the end of list and update LAST. If the last
5344 element of the list has been removed above, reuse its
5345 memory for the new node, otherwise allocate a new one. */
5346 if (unused)
5347 {
5348 loc = unused;
5349 memset (loc, '\0', sizeof (*loc));
5350 }
5351 else
5352 loc = ggc_cleared_alloc<var_loc_node> ();
5353 if (bitsize == -1 || piece_loc == NULL)
5354 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5355 else
5356 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5357 bitpos, piece_bitpos, bitsize, loc_note);
5358 last->next = loc;
5359 /* Ensure TEMP->LAST will point either to the new last but one
5360 element of the chain, or to the last element in it. */
5361 if (last != temp->last)
5362 temp->last = last;
5363 }
5364 else if (unused)
5365 ggc_free (unused);
5366 }
5367 else
5368 {
5369 loc = ggc_cleared_alloc<var_loc_node> ();
5370 temp->first = loc;
5371 temp->last = loc;
5372 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5373 }
5374 return loc;
5375 }
5376 \f
5377 /* Keep track of the number of spaces used to indent the
5378 output of the debugging routines that print the structure of
5379 the DIE internal representation. */
5380 static int print_indent;
5381
5382 /* Indent the line the number of spaces given by print_indent. */
5383
5384 static inline void
5385 print_spaces (FILE *outfile)
5386 {
5387 fprintf (outfile, "%*s", print_indent, "");
5388 }
5389
5390 /* Print a type signature in hex. */
5391
5392 static inline void
5393 print_signature (FILE *outfile, char *sig)
5394 {
5395 int i;
5396
5397 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5398 fprintf (outfile, "%02x", sig[i] & 0xff);
5399 }
5400
5401 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5402
5403 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5404 RECURSE, output location descriptor operations. */
5405
5406 static void
5407 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5408 {
5409 switch (val->val_class)
5410 {
5411 case dw_val_class_addr:
5412 fprintf (outfile, "address");
5413 break;
5414 case dw_val_class_offset:
5415 fprintf (outfile, "offset");
5416 break;
5417 case dw_val_class_loc:
5418 fprintf (outfile, "location descriptor");
5419 if (val->v.val_loc == NULL)
5420 fprintf (outfile, " -> <null>\n");
5421 else if (recurse)
5422 {
5423 fprintf (outfile, ":\n");
5424 print_indent += 4;
5425 print_loc_descr (val->v.val_loc, outfile);
5426 print_indent -= 4;
5427 }
5428 else
5429 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5430 break;
5431 case dw_val_class_loc_list:
5432 fprintf (outfile, "location list -> label:%s",
5433 val->v.val_loc_list->ll_symbol);
5434 break;
5435 case dw_val_class_range_list:
5436 fprintf (outfile, "range list");
5437 break;
5438 case dw_val_class_const:
5439 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5440 break;
5441 case dw_val_class_unsigned_const:
5442 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5443 break;
5444 case dw_val_class_const_double:
5445 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5446 HOST_WIDE_INT_PRINT_UNSIGNED")",
5447 val->v.val_double.high,
5448 val->v.val_double.low);
5449 break;
5450 case dw_val_class_wide_int:
5451 {
5452 int i = val->v.val_wide->get_len ();
5453 fprintf (outfile, "constant (");
5454 gcc_assert (i > 0);
5455 if (val->v.val_wide->elt (i - 1) == 0)
5456 fprintf (outfile, "0x");
5457 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5458 val->v.val_wide->elt (--i));
5459 while (--i >= 0)
5460 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5461 val->v.val_wide->elt (i));
5462 fprintf (outfile, ")");
5463 break;
5464 }
5465 case dw_val_class_vec:
5466 fprintf (outfile, "floating-point or vector constant");
5467 break;
5468 case dw_val_class_flag:
5469 fprintf (outfile, "%u", val->v.val_flag);
5470 break;
5471 case dw_val_class_die_ref:
5472 if (val->v.val_die_ref.die != NULL)
5473 {
5474 dw_die_ref die = val->v.val_die_ref.die;
5475
5476 if (die->comdat_type_p)
5477 {
5478 fprintf (outfile, "die -> signature: ");
5479 print_signature (outfile,
5480 die->die_id.die_type_node->signature);
5481 }
5482 else if (die->die_id.die_symbol)
5483 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5484 else
5485 fprintf (outfile, "die -> %ld", die->die_offset);
5486 fprintf (outfile, " (%p)", (void *) die);
5487 }
5488 else
5489 fprintf (outfile, "die -> <null>");
5490 break;
5491 case dw_val_class_vms_delta:
5492 fprintf (outfile, "delta: @slotcount(%s-%s)",
5493 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5494 break;
5495 case dw_val_class_lbl_id:
5496 case dw_val_class_lineptr:
5497 case dw_val_class_macptr:
5498 case dw_val_class_high_pc:
5499 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5500 break;
5501 case dw_val_class_str:
5502 if (val->v.val_str->str != NULL)
5503 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5504 else
5505 fprintf (outfile, "<null>");
5506 break;
5507 case dw_val_class_file:
5508 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5509 val->v.val_file->emitted_number);
5510 break;
5511 case dw_val_class_data8:
5512 {
5513 int i;
5514
5515 for (i = 0; i < 8; i++)
5516 fprintf (outfile, "%02x", val->v.val_data8[i]);
5517 break;
5518 }
5519 default:
5520 break;
5521 }
5522 }
5523
5524 /* Likewise, for a DIE attribute. */
5525
5526 static void
5527 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5528 {
5529 print_dw_val (&a->dw_attr_val, recurse, outfile);
5530 }
5531
5532
5533 /* Print the list of operands in the LOC location description to OUTFILE. This
5534 routine is a debugging aid only. */
5535
5536 static void
5537 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5538 {
5539 dw_loc_descr_ref l = loc;
5540
5541 if (loc == NULL)
5542 {
5543 print_spaces (outfile);
5544 fprintf (outfile, "<null>\n");
5545 return;
5546 }
5547
5548 for (l = loc; l != NULL; l = l->dw_loc_next)
5549 {
5550 print_spaces (outfile);
5551 fprintf (outfile, "(%p) %s",
5552 (void *) l,
5553 dwarf_stack_op_name (l->dw_loc_opc));
5554 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5555 {
5556 fprintf (outfile, " ");
5557 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5558 }
5559 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5560 {
5561 fprintf (outfile, ", ");
5562 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5563 }
5564 fprintf (outfile, "\n");
5565 }
5566 }
5567
5568 /* Print the information associated with a given DIE, and its children.
5569 This routine is a debugging aid only. */
5570
5571 static void
5572 print_die (dw_die_ref die, FILE *outfile)
5573 {
5574 dw_attr_ref a;
5575 dw_die_ref c;
5576 unsigned ix;
5577
5578 print_spaces (outfile);
5579 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5580 die->die_offset, dwarf_tag_name (die->die_tag),
5581 (void*) die);
5582 print_spaces (outfile);
5583 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5584 fprintf (outfile, " offset: %ld", die->die_offset);
5585 fprintf (outfile, " mark: %d\n", die->die_mark);
5586
5587 if (die->comdat_type_p)
5588 {
5589 print_spaces (outfile);
5590 fprintf (outfile, " signature: ");
5591 print_signature (outfile, die->die_id.die_type_node->signature);
5592 fprintf (outfile, "\n");
5593 }
5594
5595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5596 {
5597 print_spaces (outfile);
5598 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5599
5600 print_attribute (a, true, outfile);
5601 fprintf (outfile, "\n");
5602 }
5603
5604 if (die->die_child != NULL)
5605 {
5606 print_indent += 4;
5607 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5608 print_indent -= 4;
5609 }
5610 if (print_indent == 0)
5611 fprintf (outfile, "\n");
5612 }
5613
5614 /* Print the list of operations in the LOC location description. */
5615
5616 DEBUG_FUNCTION void
5617 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5618 {
5619 print_loc_descr (loc, stderr);
5620 }
5621
5622 /* Print the information collected for a given DIE. */
5623
5624 DEBUG_FUNCTION void
5625 debug_dwarf_die (dw_die_ref die)
5626 {
5627 print_die (die, stderr);
5628 }
5629
5630 DEBUG_FUNCTION void
5631 debug (die_struct &ref)
5632 {
5633 print_die (&ref, stderr);
5634 }
5635
5636 DEBUG_FUNCTION void
5637 debug (die_struct *ptr)
5638 {
5639 if (ptr)
5640 debug (*ptr);
5641 else
5642 fprintf (stderr, "<nil>\n");
5643 }
5644
5645
5646 /* Print all DWARF information collected for the compilation unit.
5647 This routine is a debugging aid only. */
5648
5649 DEBUG_FUNCTION void
5650 debug_dwarf (void)
5651 {
5652 print_indent = 0;
5653 print_die (comp_unit_die (), stderr);
5654 }
5655 \f
5656 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5657 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5658 DIE that marks the start of the DIEs for this include file. */
5659
5660 static dw_die_ref
5661 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5662 {
5663 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5664 dw_die_ref new_unit = gen_compile_unit_die (filename);
5665
5666 new_unit->die_sib = old_unit;
5667 return new_unit;
5668 }
5669
5670 /* Close an include-file CU and reopen the enclosing one. */
5671
5672 static dw_die_ref
5673 pop_compile_unit (dw_die_ref old_unit)
5674 {
5675 dw_die_ref new_unit = old_unit->die_sib;
5676
5677 old_unit->die_sib = NULL;
5678 return new_unit;
5679 }
5680
5681 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5682 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5683 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5684
5685 /* Calculate the checksum of a location expression. */
5686
5687 static inline void
5688 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5689 {
5690 int tem;
5691 inchash::hash hstate;
5692 hashval_t hash;
5693
5694 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5695 CHECKSUM (tem);
5696 hash_loc_operands (loc, hstate);
5697 hash = hstate.end();
5698 CHECKSUM (hash);
5699 }
5700
5701 /* Calculate the checksum of an attribute. */
5702
5703 static void
5704 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5705 {
5706 dw_loc_descr_ref loc;
5707 rtx r;
5708
5709 CHECKSUM (at->dw_attr);
5710
5711 /* We don't care that this was compiled with a different compiler
5712 snapshot; if the output is the same, that's what matters. */
5713 if (at->dw_attr == DW_AT_producer)
5714 return;
5715
5716 switch (AT_class (at))
5717 {
5718 case dw_val_class_const:
5719 CHECKSUM (at->dw_attr_val.v.val_int);
5720 break;
5721 case dw_val_class_unsigned_const:
5722 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5723 break;
5724 case dw_val_class_const_double:
5725 CHECKSUM (at->dw_attr_val.v.val_double);
5726 break;
5727 case dw_val_class_wide_int:
5728 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5729 get_full_len (*at->dw_attr_val.v.val_wide)
5730 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5731 break;
5732 case dw_val_class_vec:
5733 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5734 (at->dw_attr_val.v.val_vec.length
5735 * at->dw_attr_val.v.val_vec.elt_size));
5736 break;
5737 case dw_val_class_flag:
5738 CHECKSUM (at->dw_attr_val.v.val_flag);
5739 break;
5740 case dw_val_class_str:
5741 CHECKSUM_STRING (AT_string (at));
5742 break;
5743
5744 case dw_val_class_addr:
5745 r = AT_addr (at);
5746 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5747 CHECKSUM_STRING (XSTR (r, 0));
5748 break;
5749
5750 case dw_val_class_offset:
5751 CHECKSUM (at->dw_attr_val.v.val_offset);
5752 break;
5753
5754 case dw_val_class_loc:
5755 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5756 loc_checksum (loc, ctx);
5757 break;
5758
5759 case dw_val_class_die_ref:
5760 die_checksum (AT_ref (at), ctx, mark);
5761 break;
5762
5763 case dw_val_class_fde_ref:
5764 case dw_val_class_vms_delta:
5765 case dw_val_class_lbl_id:
5766 case dw_val_class_lineptr:
5767 case dw_val_class_macptr:
5768 case dw_val_class_high_pc:
5769 break;
5770
5771 case dw_val_class_file:
5772 CHECKSUM_STRING (AT_file (at)->filename);
5773 break;
5774
5775 case dw_val_class_data8:
5776 CHECKSUM (at->dw_attr_val.v.val_data8);
5777 break;
5778
5779 default:
5780 break;
5781 }
5782 }
5783
5784 /* Calculate the checksum of a DIE. */
5785
5786 static void
5787 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5788 {
5789 dw_die_ref c;
5790 dw_attr_ref a;
5791 unsigned ix;
5792
5793 /* To avoid infinite recursion. */
5794 if (die->die_mark)
5795 {
5796 CHECKSUM (die->die_mark);
5797 return;
5798 }
5799 die->die_mark = ++(*mark);
5800
5801 CHECKSUM (die->die_tag);
5802
5803 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5804 attr_checksum (a, ctx, mark);
5805
5806 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5807 }
5808
5809 #undef CHECKSUM
5810 #undef CHECKSUM_BLOCK
5811 #undef CHECKSUM_STRING
5812
5813 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5814 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5815 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5816 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5817 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5818 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5819 #define CHECKSUM_ATTR(FOO) \
5820 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5821
5822 /* Calculate the checksum of a number in signed LEB128 format. */
5823
5824 static void
5825 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5826 {
5827 unsigned char byte;
5828 bool more;
5829
5830 while (1)
5831 {
5832 byte = (value & 0x7f);
5833 value >>= 7;
5834 more = !((value == 0 && (byte & 0x40) == 0)
5835 || (value == -1 && (byte & 0x40) != 0));
5836 if (more)
5837 byte |= 0x80;
5838 CHECKSUM (byte);
5839 if (!more)
5840 break;
5841 }
5842 }
5843
5844 /* Calculate the checksum of a number in unsigned LEB128 format. */
5845
5846 static void
5847 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5848 {
5849 while (1)
5850 {
5851 unsigned char byte = (value & 0x7f);
5852 value >>= 7;
5853 if (value != 0)
5854 /* More bytes to follow. */
5855 byte |= 0x80;
5856 CHECKSUM (byte);
5857 if (value == 0)
5858 break;
5859 }
5860 }
5861
5862 /* Checksum the context of the DIE. This adds the names of any
5863 surrounding namespaces or structures to the checksum. */
5864
5865 static void
5866 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5867 {
5868 const char *name;
5869 dw_die_ref spec;
5870 int tag = die->die_tag;
5871
5872 if (tag != DW_TAG_namespace
5873 && tag != DW_TAG_structure_type
5874 && tag != DW_TAG_class_type)
5875 return;
5876
5877 name = get_AT_string (die, DW_AT_name);
5878
5879 spec = get_AT_ref (die, DW_AT_specification);
5880 if (spec != NULL)
5881 die = spec;
5882
5883 if (die->die_parent != NULL)
5884 checksum_die_context (die->die_parent, ctx);
5885
5886 CHECKSUM_ULEB128 ('C');
5887 CHECKSUM_ULEB128 (tag);
5888 if (name != NULL)
5889 CHECKSUM_STRING (name);
5890 }
5891
5892 /* Calculate the checksum of a location expression. */
5893
5894 static inline void
5895 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5896 {
5897 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5898 were emitted as a DW_FORM_sdata instead of a location expression. */
5899 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5900 {
5901 CHECKSUM_ULEB128 (DW_FORM_sdata);
5902 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5903 return;
5904 }
5905
5906 /* Otherwise, just checksum the raw location expression. */
5907 while (loc != NULL)
5908 {
5909 inchash::hash hstate;
5910 hashval_t hash;
5911
5912 CHECKSUM_ULEB128 (loc->dtprel);
5913 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5914 hash_loc_operands (loc, hstate);
5915 hash = hstate.end ();
5916 CHECKSUM (hash);
5917 loc = loc->dw_loc_next;
5918 }
5919 }
5920
5921 /* Calculate the checksum of an attribute. */
5922
5923 static void
5924 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5925 struct md5_ctx *ctx, int *mark)
5926 {
5927 dw_loc_descr_ref loc;
5928 rtx r;
5929
5930 if (AT_class (at) == dw_val_class_die_ref)
5931 {
5932 dw_die_ref target_die = AT_ref (at);
5933
5934 /* For pointer and reference types, we checksum only the (qualified)
5935 name of the target type (if there is a name). For friend entries,
5936 we checksum only the (qualified) name of the target type or function.
5937 This allows the checksum to remain the same whether the target type
5938 is complete or not. */
5939 if ((at->dw_attr == DW_AT_type
5940 && (tag == DW_TAG_pointer_type
5941 || tag == DW_TAG_reference_type
5942 || tag == DW_TAG_rvalue_reference_type
5943 || tag == DW_TAG_ptr_to_member_type))
5944 || (at->dw_attr == DW_AT_friend
5945 && tag == DW_TAG_friend))
5946 {
5947 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5948
5949 if (name_attr != NULL)
5950 {
5951 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5952
5953 if (decl == NULL)
5954 decl = target_die;
5955 CHECKSUM_ULEB128 ('N');
5956 CHECKSUM_ULEB128 (at->dw_attr);
5957 if (decl->die_parent != NULL)
5958 checksum_die_context (decl->die_parent, ctx);
5959 CHECKSUM_ULEB128 ('E');
5960 CHECKSUM_STRING (AT_string (name_attr));
5961 return;
5962 }
5963 }
5964
5965 /* For all other references to another DIE, we check to see if the
5966 target DIE has already been visited. If it has, we emit a
5967 backward reference; if not, we descend recursively. */
5968 if (target_die->die_mark > 0)
5969 {
5970 CHECKSUM_ULEB128 ('R');
5971 CHECKSUM_ULEB128 (at->dw_attr);
5972 CHECKSUM_ULEB128 (target_die->die_mark);
5973 }
5974 else
5975 {
5976 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5977
5978 if (decl == NULL)
5979 decl = target_die;
5980 target_die->die_mark = ++(*mark);
5981 CHECKSUM_ULEB128 ('T');
5982 CHECKSUM_ULEB128 (at->dw_attr);
5983 if (decl->die_parent != NULL)
5984 checksum_die_context (decl->die_parent, ctx);
5985 die_checksum_ordered (target_die, ctx, mark);
5986 }
5987 return;
5988 }
5989
5990 CHECKSUM_ULEB128 ('A');
5991 CHECKSUM_ULEB128 (at->dw_attr);
5992
5993 switch (AT_class (at))
5994 {
5995 case dw_val_class_const:
5996 CHECKSUM_ULEB128 (DW_FORM_sdata);
5997 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5998 break;
5999
6000 case dw_val_class_unsigned_const:
6001 CHECKSUM_ULEB128 (DW_FORM_sdata);
6002 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6003 break;
6004
6005 case dw_val_class_const_double:
6006 CHECKSUM_ULEB128 (DW_FORM_block);
6007 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6008 CHECKSUM (at->dw_attr_val.v.val_double);
6009 break;
6010
6011 case dw_val_class_wide_int:
6012 CHECKSUM_ULEB128 (DW_FORM_block);
6013 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6014 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6015 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6016 get_full_len (*at->dw_attr_val.v.val_wide)
6017 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6018 break;
6019
6020 case dw_val_class_vec:
6021 CHECKSUM_ULEB128 (DW_FORM_block);
6022 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6023 * at->dw_attr_val.v.val_vec.elt_size);
6024 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6025 (at->dw_attr_val.v.val_vec.length
6026 * at->dw_attr_val.v.val_vec.elt_size));
6027 break;
6028
6029 case dw_val_class_flag:
6030 CHECKSUM_ULEB128 (DW_FORM_flag);
6031 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6032 break;
6033
6034 case dw_val_class_str:
6035 CHECKSUM_ULEB128 (DW_FORM_string);
6036 CHECKSUM_STRING (AT_string (at));
6037 break;
6038
6039 case dw_val_class_addr:
6040 r = AT_addr (at);
6041 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6042 CHECKSUM_ULEB128 (DW_FORM_string);
6043 CHECKSUM_STRING (XSTR (r, 0));
6044 break;
6045
6046 case dw_val_class_offset:
6047 CHECKSUM_ULEB128 (DW_FORM_sdata);
6048 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6049 break;
6050
6051 case dw_val_class_loc:
6052 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6053 loc_checksum_ordered (loc, ctx);
6054 break;
6055
6056 case dw_val_class_fde_ref:
6057 case dw_val_class_lbl_id:
6058 case dw_val_class_lineptr:
6059 case dw_val_class_macptr:
6060 case dw_val_class_high_pc:
6061 break;
6062
6063 case dw_val_class_file:
6064 CHECKSUM_ULEB128 (DW_FORM_string);
6065 CHECKSUM_STRING (AT_file (at)->filename);
6066 break;
6067
6068 case dw_val_class_data8:
6069 CHECKSUM (at->dw_attr_val.v.val_data8);
6070 break;
6071
6072 default:
6073 break;
6074 }
6075 }
6076
6077 struct checksum_attributes
6078 {
6079 dw_attr_ref at_name;
6080 dw_attr_ref at_type;
6081 dw_attr_ref at_friend;
6082 dw_attr_ref at_accessibility;
6083 dw_attr_ref at_address_class;
6084 dw_attr_ref at_allocated;
6085 dw_attr_ref at_artificial;
6086 dw_attr_ref at_associated;
6087 dw_attr_ref at_binary_scale;
6088 dw_attr_ref at_bit_offset;
6089 dw_attr_ref at_bit_size;
6090 dw_attr_ref at_bit_stride;
6091 dw_attr_ref at_byte_size;
6092 dw_attr_ref at_byte_stride;
6093 dw_attr_ref at_const_value;
6094 dw_attr_ref at_containing_type;
6095 dw_attr_ref at_count;
6096 dw_attr_ref at_data_location;
6097 dw_attr_ref at_data_member_location;
6098 dw_attr_ref at_decimal_scale;
6099 dw_attr_ref at_decimal_sign;
6100 dw_attr_ref at_default_value;
6101 dw_attr_ref at_digit_count;
6102 dw_attr_ref at_discr;
6103 dw_attr_ref at_discr_list;
6104 dw_attr_ref at_discr_value;
6105 dw_attr_ref at_encoding;
6106 dw_attr_ref at_endianity;
6107 dw_attr_ref at_explicit;
6108 dw_attr_ref at_is_optional;
6109 dw_attr_ref at_location;
6110 dw_attr_ref at_lower_bound;
6111 dw_attr_ref at_mutable;
6112 dw_attr_ref at_ordering;
6113 dw_attr_ref at_picture_string;
6114 dw_attr_ref at_prototyped;
6115 dw_attr_ref at_small;
6116 dw_attr_ref at_segment;
6117 dw_attr_ref at_string_length;
6118 dw_attr_ref at_threads_scaled;
6119 dw_attr_ref at_upper_bound;
6120 dw_attr_ref at_use_location;
6121 dw_attr_ref at_use_UTF8;
6122 dw_attr_ref at_variable_parameter;
6123 dw_attr_ref at_virtuality;
6124 dw_attr_ref at_visibility;
6125 dw_attr_ref at_vtable_elem_location;
6126 };
6127
6128 /* Collect the attributes that we will want to use for the checksum. */
6129
6130 static void
6131 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6132 {
6133 dw_attr_ref a;
6134 unsigned ix;
6135
6136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6137 {
6138 switch (a->dw_attr)
6139 {
6140 case DW_AT_name:
6141 attrs->at_name = a;
6142 break;
6143 case DW_AT_type:
6144 attrs->at_type = a;
6145 break;
6146 case DW_AT_friend:
6147 attrs->at_friend = a;
6148 break;
6149 case DW_AT_accessibility:
6150 attrs->at_accessibility = a;
6151 break;
6152 case DW_AT_address_class:
6153 attrs->at_address_class = a;
6154 break;
6155 case DW_AT_allocated:
6156 attrs->at_allocated = a;
6157 break;
6158 case DW_AT_artificial:
6159 attrs->at_artificial = a;
6160 break;
6161 case DW_AT_associated:
6162 attrs->at_associated = a;
6163 break;
6164 case DW_AT_binary_scale:
6165 attrs->at_binary_scale = a;
6166 break;
6167 case DW_AT_bit_offset:
6168 attrs->at_bit_offset = a;
6169 break;
6170 case DW_AT_bit_size:
6171 attrs->at_bit_size = a;
6172 break;
6173 case DW_AT_bit_stride:
6174 attrs->at_bit_stride = a;
6175 break;
6176 case DW_AT_byte_size:
6177 attrs->at_byte_size = a;
6178 break;
6179 case DW_AT_byte_stride:
6180 attrs->at_byte_stride = a;
6181 break;
6182 case DW_AT_const_value:
6183 attrs->at_const_value = a;
6184 break;
6185 case DW_AT_containing_type:
6186 attrs->at_containing_type = a;
6187 break;
6188 case DW_AT_count:
6189 attrs->at_count = a;
6190 break;
6191 case DW_AT_data_location:
6192 attrs->at_data_location = a;
6193 break;
6194 case DW_AT_data_member_location:
6195 attrs->at_data_member_location = a;
6196 break;
6197 case DW_AT_decimal_scale:
6198 attrs->at_decimal_scale = a;
6199 break;
6200 case DW_AT_decimal_sign:
6201 attrs->at_decimal_sign = a;
6202 break;
6203 case DW_AT_default_value:
6204 attrs->at_default_value = a;
6205 break;
6206 case DW_AT_digit_count:
6207 attrs->at_digit_count = a;
6208 break;
6209 case DW_AT_discr:
6210 attrs->at_discr = a;
6211 break;
6212 case DW_AT_discr_list:
6213 attrs->at_discr_list = a;
6214 break;
6215 case DW_AT_discr_value:
6216 attrs->at_discr_value = a;
6217 break;
6218 case DW_AT_encoding:
6219 attrs->at_encoding = a;
6220 break;
6221 case DW_AT_endianity:
6222 attrs->at_endianity = a;
6223 break;
6224 case DW_AT_explicit:
6225 attrs->at_explicit = a;
6226 break;
6227 case DW_AT_is_optional:
6228 attrs->at_is_optional = a;
6229 break;
6230 case DW_AT_location:
6231 attrs->at_location = a;
6232 break;
6233 case DW_AT_lower_bound:
6234 attrs->at_lower_bound = a;
6235 break;
6236 case DW_AT_mutable:
6237 attrs->at_mutable = a;
6238 break;
6239 case DW_AT_ordering:
6240 attrs->at_ordering = a;
6241 break;
6242 case DW_AT_picture_string:
6243 attrs->at_picture_string = a;
6244 break;
6245 case DW_AT_prototyped:
6246 attrs->at_prototyped = a;
6247 break;
6248 case DW_AT_small:
6249 attrs->at_small = a;
6250 break;
6251 case DW_AT_segment:
6252 attrs->at_segment = a;
6253 break;
6254 case DW_AT_string_length:
6255 attrs->at_string_length = a;
6256 break;
6257 case DW_AT_threads_scaled:
6258 attrs->at_threads_scaled = a;
6259 break;
6260 case DW_AT_upper_bound:
6261 attrs->at_upper_bound = a;
6262 break;
6263 case DW_AT_use_location:
6264 attrs->at_use_location = a;
6265 break;
6266 case DW_AT_use_UTF8:
6267 attrs->at_use_UTF8 = a;
6268 break;
6269 case DW_AT_variable_parameter:
6270 attrs->at_variable_parameter = a;
6271 break;
6272 case DW_AT_virtuality:
6273 attrs->at_virtuality = a;
6274 break;
6275 case DW_AT_visibility:
6276 attrs->at_visibility = a;
6277 break;
6278 case DW_AT_vtable_elem_location:
6279 attrs->at_vtable_elem_location = a;
6280 break;
6281 default:
6282 break;
6283 }
6284 }
6285 }
6286
6287 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6288
6289 static void
6290 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6291 {
6292 dw_die_ref c;
6293 dw_die_ref decl;
6294 struct checksum_attributes attrs;
6295
6296 CHECKSUM_ULEB128 ('D');
6297 CHECKSUM_ULEB128 (die->die_tag);
6298
6299 memset (&attrs, 0, sizeof (attrs));
6300
6301 decl = get_AT_ref (die, DW_AT_specification);
6302 if (decl != NULL)
6303 collect_checksum_attributes (&attrs, decl);
6304 collect_checksum_attributes (&attrs, die);
6305
6306 CHECKSUM_ATTR (attrs.at_name);
6307 CHECKSUM_ATTR (attrs.at_accessibility);
6308 CHECKSUM_ATTR (attrs.at_address_class);
6309 CHECKSUM_ATTR (attrs.at_allocated);
6310 CHECKSUM_ATTR (attrs.at_artificial);
6311 CHECKSUM_ATTR (attrs.at_associated);
6312 CHECKSUM_ATTR (attrs.at_binary_scale);
6313 CHECKSUM_ATTR (attrs.at_bit_offset);
6314 CHECKSUM_ATTR (attrs.at_bit_size);
6315 CHECKSUM_ATTR (attrs.at_bit_stride);
6316 CHECKSUM_ATTR (attrs.at_byte_size);
6317 CHECKSUM_ATTR (attrs.at_byte_stride);
6318 CHECKSUM_ATTR (attrs.at_const_value);
6319 CHECKSUM_ATTR (attrs.at_containing_type);
6320 CHECKSUM_ATTR (attrs.at_count);
6321 CHECKSUM_ATTR (attrs.at_data_location);
6322 CHECKSUM_ATTR (attrs.at_data_member_location);
6323 CHECKSUM_ATTR (attrs.at_decimal_scale);
6324 CHECKSUM_ATTR (attrs.at_decimal_sign);
6325 CHECKSUM_ATTR (attrs.at_default_value);
6326 CHECKSUM_ATTR (attrs.at_digit_count);
6327 CHECKSUM_ATTR (attrs.at_discr);
6328 CHECKSUM_ATTR (attrs.at_discr_list);
6329 CHECKSUM_ATTR (attrs.at_discr_value);
6330 CHECKSUM_ATTR (attrs.at_encoding);
6331 CHECKSUM_ATTR (attrs.at_endianity);
6332 CHECKSUM_ATTR (attrs.at_explicit);
6333 CHECKSUM_ATTR (attrs.at_is_optional);
6334 CHECKSUM_ATTR (attrs.at_location);
6335 CHECKSUM_ATTR (attrs.at_lower_bound);
6336 CHECKSUM_ATTR (attrs.at_mutable);
6337 CHECKSUM_ATTR (attrs.at_ordering);
6338 CHECKSUM_ATTR (attrs.at_picture_string);
6339 CHECKSUM_ATTR (attrs.at_prototyped);
6340 CHECKSUM_ATTR (attrs.at_small);
6341 CHECKSUM_ATTR (attrs.at_segment);
6342 CHECKSUM_ATTR (attrs.at_string_length);
6343 CHECKSUM_ATTR (attrs.at_threads_scaled);
6344 CHECKSUM_ATTR (attrs.at_upper_bound);
6345 CHECKSUM_ATTR (attrs.at_use_location);
6346 CHECKSUM_ATTR (attrs.at_use_UTF8);
6347 CHECKSUM_ATTR (attrs.at_variable_parameter);
6348 CHECKSUM_ATTR (attrs.at_virtuality);
6349 CHECKSUM_ATTR (attrs.at_visibility);
6350 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6351 CHECKSUM_ATTR (attrs.at_type);
6352 CHECKSUM_ATTR (attrs.at_friend);
6353
6354 /* Checksum the child DIEs. */
6355 c = die->die_child;
6356 if (c) do {
6357 dw_attr_ref name_attr;
6358
6359 c = c->die_sib;
6360 name_attr = get_AT (c, DW_AT_name);
6361 if (is_template_instantiation (c))
6362 {
6363 /* Ignore instantiations of member type and function templates. */
6364 }
6365 else if (name_attr != NULL
6366 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6367 {
6368 /* Use a shallow checksum for named nested types and member
6369 functions. */
6370 CHECKSUM_ULEB128 ('S');
6371 CHECKSUM_ULEB128 (c->die_tag);
6372 CHECKSUM_STRING (AT_string (name_attr));
6373 }
6374 else
6375 {
6376 /* Use a deep checksum for other children. */
6377 /* Mark this DIE so it gets processed when unmarking. */
6378 if (c->die_mark == 0)
6379 c->die_mark = -1;
6380 die_checksum_ordered (c, ctx, mark);
6381 }
6382 } while (c != die->die_child);
6383
6384 CHECKSUM_ULEB128 (0);
6385 }
6386
6387 /* Add a type name and tag to a hash. */
6388 static void
6389 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6390 {
6391 CHECKSUM_ULEB128 (tag);
6392 CHECKSUM_STRING (name);
6393 }
6394
6395 #undef CHECKSUM
6396 #undef CHECKSUM_STRING
6397 #undef CHECKSUM_ATTR
6398 #undef CHECKSUM_LEB128
6399 #undef CHECKSUM_ULEB128
6400
6401 /* Generate the type signature for DIE. This is computed by generating an
6402 MD5 checksum over the DIE's tag, its relevant attributes, and its
6403 children. Attributes that are references to other DIEs are processed
6404 by recursion, using the MARK field to prevent infinite recursion.
6405 If the DIE is nested inside a namespace or another type, we also
6406 need to include that context in the signature. The lower 64 bits
6407 of the resulting MD5 checksum comprise the signature. */
6408
6409 static void
6410 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6411 {
6412 int mark;
6413 const char *name;
6414 unsigned char checksum[16];
6415 struct md5_ctx ctx;
6416 dw_die_ref decl;
6417 dw_die_ref parent;
6418
6419 name = get_AT_string (die, DW_AT_name);
6420 decl = get_AT_ref (die, DW_AT_specification);
6421 parent = get_die_parent (die);
6422
6423 /* First, compute a signature for just the type name (and its surrounding
6424 context, if any. This is stored in the type unit DIE for link-time
6425 ODR (one-definition rule) checking. */
6426
6427 if (is_cxx () && name != NULL)
6428 {
6429 md5_init_ctx (&ctx);
6430
6431 /* Checksum the names of surrounding namespaces and structures. */
6432 if (parent != NULL)
6433 checksum_die_context (parent, &ctx);
6434
6435 /* Checksum the current DIE. */
6436 die_odr_checksum (die->die_tag, name, &ctx);
6437 md5_finish_ctx (&ctx, checksum);
6438
6439 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6440 }
6441
6442 /* Next, compute the complete type signature. */
6443
6444 md5_init_ctx (&ctx);
6445 mark = 1;
6446 die->die_mark = mark;
6447
6448 /* Checksum the names of surrounding namespaces and structures. */
6449 if (parent != NULL)
6450 checksum_die_context (parent, &ctx);
6451
6452 /* Checksum the DIE and its children. */
6453 die_checksum_ordered (die, &ctx, &mark);
6454 unmark_all_dies (die);
6455 md5_finish_ctx (&ctx, checksum);
6456
6457 /* Store the signature in the type node and link the type DIE and the
6458 type node together. */
6459 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6460 DWARF_TYPE_SIGNATURE_SIZE);
6461 die->comdat_type_p = true;
6462 die->die_id.die_type_node = type_node;
6463 type_node->type_die = die;
6464
6465 /* If the DIE is a specification, link its declaration to the type node
6466 as well. */
6467 if (decl != NULL)
6468 {
6469 decl->comdat_type_p = true;
6470 decl->die_id.die_type_node = type_node;
6471 }
6472 }
6473
6474 /* Do the location expressions look same? */
6475 static inline int
6476 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6477 {
6478 return loc1->dw_loc_opc == loc2->dw_loc_opc
6479 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6480 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6481 }
6482
6483 /* Do the values look the same? */
6484 static int
6485 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6486 {
6487 dw_loc_descr_ref loc1, loc2;
6488 rtx r1, r2;
6489
6490 if (v1->val_class != v2->val_class)
6491 return 0;
6492
6493 switch (v1->val_class)
6494 {
6495 case dw_val_class_const:
6496 return v1->v.val_int == v2->v.val_int;
6497 case dw_val_class_unsigned_const:
6498 return v1->v.val_unsigned == v2->v.val_unsigned;
6499 case dw_val_class_const_double:
6500 return v1->v.val_double.high == v2->v.val_double.high
6501 && v1->v.val_double.low == v2->v.val_double.low;
6502 case dw_val_class_wide_int:
6503 return *v1->v.val_wide == *v2->v.val_wide;
6504 case dw_val_class_vec:
6505 if (v1->v.val_vec.length != v2->v.val_vec.length
6506 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6507 return 0;
6508 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6509 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6510 return 0;
6511 return 1;
6512 case dw_val_class_flag:
6513 return v1->v.val_flag == v2->v.val_flag;
6514 case dw_val_class_str:
6515 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6516
6517 case dw_val_class_addr:
6518 r1 = v1->v.val_addr;
6519 r2 = v2->v.val_addr;
6520 if (GET_CODE (r1) != GET_CODE (r2))
6521 return 0;
6522 return !rtx_equal_p (r1, r2);
6523
6524 case dw_val_class_offset:
6525 return v1->v.val_offset == v2->v.val_offset;
6526
6527 case dw_val_class_loc:
6528 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6529 loc1 && loc2;
6530 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6531 if (!same_loc_p (loc1, loc2, mark))
6532 return 0;
6533 return !loc1 && !loc2;
6534
6535 case dw_val_class_die_ref:
6536 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6537
6538 case dw_val_class_fde_ref:
6539 case dw_val_class_vms_delta:
6540 case dw_val_class_lbl_id:
6541 case dw_val_class_lineptr:
6542 case dw_val_class_macptr:
6543 case dw_val_class_high_pc:
6544 return 1;
6545
6546 case dw_val_class_file:
6547 return v1->v.val_file == v2->v.val_file;
6548
6549 case dw_val_class_data8:
6550 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6551
6552 default:
6553 return 1;
6554 }
6555 }
6556
6557 /* Do the attributes look the same? */
6558
6559 static int
6560 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6561 {
6562 if (at1->dw_attr != at2->dw_attr)
6563 return 0;
6564
6565 /* We don't care that this was compiled with a different compiler
6566 snapshot; if the output is the same, that's what matters. */
6567 if (at1->dw_attr == DW_AT_producer)
6568 return 1;
6569
6570 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6571 }
6572
6573 /* Do the dies look the same? */
6574
6575 static int
6576 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6577 {
6578 dw_die_ref c1, c2;
6579 dw_attr_ref a1;
6580 unsigned ix;
6581
6582 /* To avoid infinite recursion. */
6583 if (die1->die_mark)
6584 return die1->die_mark == die2->die_mark;
6585 die1->die_mark = die2->die_mark = ++(*mark);
6586
6587 if (die1->die_tag != die2->die_tag)
6588 return 0;
6589
6590 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6591 return 0;
6592
6593 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6594 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6595 return 0;
6596
6597 c1 = die1->die_child;
6598 c2 = die2->die_child;
6599 if (! c1)
6600 {
6601 if (c2)
6602 return 0;
6603 }
6604 else
6605 for (;;)
6606 {
6607 if (!same_die_p (c1, c2, mark))
6608 return 0;
6609 c1 = c1->die_sib;
6610 c2 = c2->die_sib;
6611 if (c1 == die1->die_child)
6612 {
6613 if (c2 == die2->die_child)
6614 break;
6615 else
6616 return 0;
6617 }
6618 }
6619
6620 return 1;
6621 }
6622
6623 /* Do the dies look the same? Wrapper around same_die_p. */
6624
6625 static int
6626 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6627 {
6628 int mark = 0;
6629 int ret = same_die_p (die1, die2, &mark);
6630
6631 unmark_all_dies (die1);
6632 unmark_all_dies (die2);
6633
6634 return ret;
6635 }
6636
6637 /* The prefix to attach to symbols on DIEs in the current comdat debug
6638 info section. */
6639 static const char *comdat_symbol_id;
6640
6641 /* The index of the current symbol within the current comdat CU. */
6642 static unsigned int comdat_symbol_number;
6643
6644 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6645 children, and set comdat_symbol_id accordingly. */
6646
6647 static void
6648 compute_section_prefix (dw_die_ref unit_die)
6649 {
6650 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6651 const char *base = die_name ? lbasename (die_name) : "anonymous";
6652 char *name = XALLOCAVEC (char, strlen (base) + 64);
6653 char *p;
6654 int i, mark;
6655 unsigned char checksum[16];
6656 struct md5_ctx ctx;
6657
6658 /* Compute the checksum of the DIE, then append part of it as hex digits to
6659 the name filename of the unit. */
6660
6661 md5_init_ctx (&ctx);
6662 mark = 0;
6663 die_checksum (unit_die, &ctx, &mark);
6664 unmark_all_dies (unit_die);
6665 md5_finish_ctx (&ctx, checksum);
6666
6667 sprintf (name, "%s.", base);
6668 clean_symbol_name (name);
6669
6670 p = name + strlen (name);
6671 for (i = 0; i < 4; i++)
6672 {
6673 sprintf (p, "%.2x", checksum[i]);
6674 p += 2;
6675 }
6676
6677 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6678 comdat_symbol_number = 0;
6679 }
6680
6681 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6682
6683 static int
6684 is_type_die (dw_die_ref die)
6685 {
6686 switch (die->die_tag)
6687 {
6688 case DW_TAG_array_type:
6689 case DW_TAG_class_type:
6690 case DW_TAG_interface_type:
6691 case DW_TAG_enumeration_type:
6692 case DW_TAG_pointer_type:
6693 case DW_TAG_reference_type:
6694 case DW_TAG_rvalue_reference_type:
6695 case DW_TAG_string_type:
6696 case DW_TAG_structure_type:
6697 case DW_TAG_subroutine_type:
6698 case DW_TAG_union_type:
6699 case DW_TAG_ptr_to_member_type:
6700 case DW_TAG_set_type:
6701 case DW_TAG_subrange_type:
6702 case DW_TAG_base_type:
6703 case DW_TAG_const_type:
6704 case DW_TAG_file_type:
6705 case DW_TAG_packed_type:
6706 case DW_TAG_volatile_type:
6707 case DW_TAG_typedef:
6708 return 1;
6709 default:
6710 return 0;
6711 }
6712 }
6713
6714 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6715 Basically, we want to choose the bits that are likely to be shared between
6716 compilations (types) and leave out the bits that are specific to individual
6717 compilations (functions). */
6718
6719 static int
6720 is_comdat_die (dw_die_ref c)
6721 {
6722 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6723 we do for stabs. The advantage is a greater likelihood of sharing between
6724 objects that don't include headers in the same order (and therefore would
6725 put the base types in a different comdat). jason 8/28/00 */
6726
6727 if (c->die_tag == DW_TAG_base_type)
6728 return 0;
6729
6730 if (c->die_tag == DW_TAG_pointer_type
6731 || c->die_tag == DW_TAG_reference_type
6732 || c->die_tag == DW_TAG_rvalue_reference_type
6733 || c->die_tag == DW_TAG_const_type
6734 || c->die_tag == DW_TAG_volatile_type)
6735 {
6736 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6737
6738 return t ? is_comdat_die (t) : 0;
6739 }
6740
6741 return is_type_die (c);
6742 }
6743
6744 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6745 compilation unit. */
6746
6747 static int
6748 is_symbol_die (dw_die_ref c)
6749 {
6750 return (is_type_die (c)
6751 || is_declaration_die (c)
6752 || c->die_tag == DW_TAG_namespace
6753 || c->die_tag == DW_TAG_module);
6754 }
6755
6756 /* Returns true iff C is a compile-unit DIE. */
6757
6758 static inline bool
6759 is_cu_die (dw_die_ref c)
6760 {
6761 return c && c->die_tag == DW_TAG_compile_unit;
6762 }
6763
6764 /* Returns true iff C is a unit DIE of some sort. */
6765
6766 static inline bool
6767 is_unit_die (dw_die_ref c)
6768 {
6769 return c && (c->die_tag == DW_TAG_compile_unit
6770 || c->die_tag == DW_TAG_partial_unit
6771 || c->die_tag == DW_TAG_type_unit);
6772 }
6773
6774 /* Returns true iff C is a namespace DIE. */
6775
6776 static inline bool
6777 is_namespace_die (dw_die_ref c)
6778 {
6779 return c && c->die_tag == DW_TAG_namespace;
6780 }
6781
6782 /* Returns true iff C is a class or structure DIE. */
6783
6784 static inline bool
6785 is_class_die (dw_die_ref c)
6786 {
6787 return c && (c->die_tag == DW_TAG_class_type
6788 || c->die_tag == DW_TAG_structure_type);
6789 }
6790
6791 /* Return non-zero if this DIE is a template parameter. */
6792
6793 static inline bool
6794 is_template_parameter (dw_die_ref die)
6795 {
6796 switch (die->die_tag)
6797 {
6798 case DW_TAG_template_type_param:
6799 case DW_TAG_template_value_param:
6800 case DW_TAG_GNU_template_template_param:
6801 case DW_TAG_GNU_template_parameter_pack:
6802 return true;
6803 default:
6804 return false;
6805 }
6806 }
6807
6808 /* Return non-zero if this DIE represents a template instantiation. */
6809
6810 static inline bool
6811 is_template_instantiation (dw_die_ref die)
6812 {
6813 dw_die_ref c;
6814
6815 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6816 return false;
6817 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6818 return false;
6819 }
6820
6821 static char *
6822 gen_internal_sym (const char *prefix)
6823 {
6824 char buf[256];
6825
6826 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6827 return xstrdup (buf);
6828 }
6829
6830 /* Assign symbols to all worthy DIEs under DIE. */
6831
6832 static void
6833 assign_symbol_names (dw_die_ref die)
6834 {
6835 dw_die_ref c;
6836
6837 if (is_symbol_die (die) && !die->comdat_type_p)
6838 {
6839 if (comdat_symbol_id)
6840 {
6841 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6842
6843 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6844 comdat_symbol_id, comdat_symbol_number++);
6845 die->die_id.die_symbol = xstrdup (p);
6846 }
6847 else
6848 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6849 }
6850
6851 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6852 }
6853
6854 struct cu_hash_table_entry
6855 {
6856 dw_die_ref cu;
6857 unsigned min_comdat_num, max_comdat_num;
6858 struct cu_hash_table_entry *next;
6859 };
6860
6861 /* Helpers to manipulate hash table of CUs. */
6862
6863 struct cu_hash_table_entry_hasher
6864 {
6865 typedef cu_hash_table_entry *value_type;
6866 typedef die_struct *compare_type;
6867 static inline hashval_t hash (const cu_hash_table_entry *);
6868 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6869 static inline void remove (cu_hash_table_entry *);
6870 };
6871
6872 inline hashval_t
6873 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6874 {
6875 return htab_hash_string (entry->cu->die_id.die_symbol);
6876 }
6877
6878 inline bool
6879 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6880 const die_struct *entry2)
6881 {
6882 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6883 }
6884
6885 inline void
6886 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6887 {
6888 struct cu_hash_table_entry *next;
6889
6890 while (entry)
6891 {
6892 next = entry->next;
6893 free (entry);
6894 entry = next;
6895 }
6896 }
6897
6898 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6899
6900 /* Check whether we have already seen this CU and set up SYM_NUM
6901 accordingly. */
6902 static int
6903 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6904 {
6905 struct cu_hash_table_entry dummy;
6906 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6907
6908 dummy.max_comdat_num = 0;
6909
6910 slot = htable->find_slot_with_hash (cu,
6911 htab_hash_string (cu->die_id.die_symbol),
6912 INSERT);
6913 entry = *slot;
6914
6915 for (; entry; last = entry, entry = entry->next)
6916 {
6917 if (same_die_p_wrap (cu, entry->cu))
6918 break;
6919 }
6920
6921 if (entry)
6922 {
6923 *sym_num = entry->min_comdat_num;
6924 return 1;
6925 }
6926
6927 entry = XCNEW (struct cu_hash_table_entry);
6928 entry->cu = cu;
6929 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6930 entry->next = *slot;
6931 *slot = entry;
6932
6933 return 0;
6934 }
6935
6936 /* Record SYM_NUM to record of CU in HTABLE. */
6937 static void
6938 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6939 unsigned int sym_num)
6940 {
6941 struct cu_hash_table_entry **slot, *entry;
6942
6943 slot = htable->find_slot_with_hash (cu,
6944 htab_hash_string (cu->die_id.die_symbol),
6945 NO_INSERT);
6946 entry = *slot;
6947
6948 entry->max_comdat_num = sym_num;
6949 }
6950
6951 /* Traverse the DIE (which is always comp_unit_die), and set up
6952 additional compilation units for each of the include files we see
6953 bracketed by BINCL/EINCL. */
6954
6955 static void
6956 break_out_includes (dw_die_ref die)
6957 {
6958 dw_die_ref c;
6959 dw_die_ref unit = NULL;
6960 limbo_die_node *node, **pnode;
6961
6962 c = die->die_child;
6963 if (c) do {
6964 dw_die_ref prev = c;
6965 c = c->die_sib;
6966 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6967 || (unit && is_comdat_die (c)))
6968 {
6969 dw_die_ref next = c->die_sib;
6970
6971 /* This DIE is for a secondary CU; remove it from the main one. */
6972 remove_child_with_prev (c, prev);
6973
6974 if (c->die_tag == DW_TAG_GNU_BINCL)
6975 unit = push_new_compile_unit (unit, c);
6976 else if (c->die_tag == DW_TAG_GNU_EINCL)
6977 unit = pop_compile_unit (unit);
6978 else
6979 add_child_die (unit, c);
6980 c = next;
6981 if (c == die->die_child)
6982 break;
6983 }
6984 } while (c != die->die_child);
6985
6986 #if 0
6987 /* We can only use this in debugging, since the frontend doesn't check
6988 to make sure that we leave every include file we enter. */
6989 gcc_assert (!unit);
6990 #endif
6991
6992 assign_symbol_names (die);
6993 cu_hash_type cu_hash_table (10);
6994 for (node = limbo_die_list, pnode = &limbo_die_list;
6995 node;
6996 node = node->next)
6997 {
6998 int is_dupl;
6999
7000 compute_section_prefix (node->die);
7001 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7002 &comdat_symbol_number);
7003 assign_symbol_names (node->die);
7004 if (is_dupl)
7005 *pnode = node->next;
7006 else
7007 {
7008 pnode = &node->next;
7009 record_comdat_symbol_number (node->die, &cu_hash_table,
7010 comdat_symbol_number);
7011 }
7012 }
7013 }
7014
7015 /* Return non-zero if this DIE is a declaration. */
7016
7017 static int
7018 is_declaration_die (dw_die_ref die)
7019 {
7020 dw_attr_ref a;
7021 unsigned ix;
7022
7023 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7024 if (a->dw_attr == DW_AT_declaration)
7025 return 1;
7026
7027 return 0;
7028 }
7029
7030 /* Return non-zero if this DIE is nested inside a subprogram. */
7031
7032 static int
7033 is_nested_in_subprogram (dw_die_ref die)
7034 {
7035 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7036
7037 if (decl == NULL)
7038 decl = die;
7039 return local_scope_p (decl);
7040 }
7041
7042 /* Return non-zero if this DIE contains a defining declaration of a
7043 subprogram. */
7044
7045 static int
7046 contains_subprogram_definition (dw_die_ref die)
7047 {
7048 dw_die_ref c;
7049
7050 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7051 return 1;
7052 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7053 return 0;
7054 }
7055
7056 /* Return non-zero if this is a type DIE that should be moved to a
7057 COMDAT .debug_types section. */
7058
7059 static int
7060 should_move_die_to_comdat (dw_die_ref die)
7061 {
7062 switch (die->die_tag)
7063 {
7064 case DW_TAG_class_type:
7065 case DW_TAG_structure_type:
7066 case DW_TAG_enumeration_type:
7067 case DW_TAG_union_type:
7068 /* Don't move declarations, inlined instances, types nested in a
7069 subprogram, or types that contain subprogram definitions. */
7070 if (is_declaration_die (die)
7071 || get_AT (die, DW_AT_abstract_origin)
7072 || is_nested_in_subprogram (die)
7073 || contains_subprogram_definition (die))
7074 return 0;
7075 return 1;
7076 case DW_TAG_array_type:
7077 case DW_TAG_interface_type:
7078 case DW_TAG_pointer_type:
7079 case DW_TAG_reference_type:
7080 case DW_TAG_rvalue_reference_type:
7081 case DW_TAG_string_type:
7082 case DW_TAG_subroutine_type:
7083 case DW_TAG_ptr_to_member_type:
7084 case DW_TAG_set_type:
7085 case DW_TAG_subrange_type:
7086 case DW_TAG_base_type:
7087 case DW_TAG_const_type:
7088 case DW_TAG_file_type:
7089 case DW_TAG_packed_type:
7090 case DW_TAG_volatile_type:
7091 case DW_TAG_typedef:
7092 default:
7093 return 0;
7094 }
7095 }
7096
7097 /* Make a clone of DIE. */
7098
7099 static dw_die_ref
7100 clone_die (dw_die_ref die)
7101 {
7102 dw_die_ref clone;
7103 dw_attr_ref a;
7104 unsigned ix;
7105
7106 clone = ggc_cleared_alloc<die_node> ();
7107 clone->die_tag = die->die_tag;
7108
7109 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7110 add_dwarf_attr (clone, a);
7111
7112 return clone;
7113 }
7114
7115 /* Make a clone of the tree rooted at DIE. */
7116
7117 static dw_die_ref
7118 clone_tree (dw_die_ref die)
7119 {
7120 dw_die_ref c;
7121 dw_die_ref clone = clone_die (die);
7122
7123 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7124
7125 return clone;
7126 }
7127
7128 /* Make a clone of DIE as a declaration. */
7129
7130 static dw_die_ref
7131 clone_as_declaration (dw_die_ref die)
7132 {
7133 dw_die_ref clone;
7134 dw_die_ref decl;
7135 dw_attr_ref a;
7136 unsigned ix;
7137
7138 /* If the DIE is already a declaration, just clone it. */
7139 if (is_declaration_die (die))
7140 return clone_die (die);
7141
7142 /* If the DIE is a specification, just clone its declaration DIE. */
7143 decl = get_AT_ref (die, DW_AT_specification);
7144 if (decl != NULL)
7145 {
7146 clone = clone_die (decl);
7147 if (die->comdat_type_p)
7148 add_AT_die_ref (clone, DW_AT_signature, die);
7149 return clone;
7150 }
7151
7152 clone = ggc_cleared_alloc<die_node> ();
7153 clone->die_tag = die->die_tag;
7154
7155 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7156 {
7157 /* We don't want to copy over all attributes.
7158 For example we don't want DW_AT_byte_size because otherwise we will no
7159 longer have a declaration and GDB will treat it as a definition. */
7160
7161 switch (a->dw_attr)
7162 {
7163 case DW_AT_abstract_origin:
7164 case DW_AT_artificial:
7165 case DW_AT_containing_type:
7166 case DW_AT_external:
7167 case DW_AT_name:
7168 case DW_AT_type:
7169 case DW_AT_virtuality:
7170 case DW_AT_linkage_name:
7171 case DW_AT_MIPS_linkage_name:
7172 add_dwarf_attr (clone, a);
7173 break;
7174 case DW_AT_byte_size:
7175 default:
7176 break;
7177 }
7178 }
7179
7180 if (die->comdat_type_p)
7181 add_AT_die_ref (clone, DW_AT_signature, die);
7182
7183 add_AT_flag (clone, DW_AT_declaration, 1);
7184 return clone;
7185 }
7186
7187
7188 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7189
7190 struct decl_table_entry
7191 {
7192 dw_die_ref orig;
7193 dw_die_ref copy;
7194 };
7195
7196 /* Helpers to manipulate hash table of copied declarations. */
7197
7198 /* Hashtable helpers. */
7199
7200 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7201 {
7202 typedef decl_table_entry *value_type;
7203 typedef die_struct *compare_type;
7204 static inline hashval_t hash (const decl_table_entry *);
7205 static inline bool equal (const decl_table_entry *, const die_struct *);
7206 };
7207
7208 inline hashval_t
7209 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7210 {
7211 return htab_hash_pointer (entry->orig);
7212 }
7213
7214 inline bool
7215 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7216 const die_struct *entry2)
7217 {
7218 return entry1->orig == entry2;
7219 }
7220
7221 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7222
7223 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7224 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7225 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7226 to check if the ancestor has already been copied into UNIT. */
7227
7228 static dw_die_ref
7229 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7230 decl_hash_type *decl_table)
7231 {
7232 dw_die_ref parent = die->die_parent;
7233 dw_die_ref new_parent = unit;
7234 dw_die_ref copy;
7235 decl_table_entry **slot = NULL;
7236 struct decl_table_entry *entry = NULL;
7237
7238 if (decl_table)
7239 {
7240 /* Check if the entry has already been copied to UNIT. */
7241 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7242 INSERT);
7243 if (*slot != HTAB_EMPTY_ENTRY)
7244 {
7245 entry = *slot;
7246 return entry->copy;
7247 }
7248
7249 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7250 entry = XCNEW (struct decl_table_entry);
7251 entry->orig = die;
7252 entry->copy = NULL;
7253 *slot = entry;
7254 }
7255
7256 if (parent != NULL)
7257 {
7258 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7259 if (spec != NULL)
7260 parent = spec;
7261 if (!is_unit_die (parent))
7262 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7263 }
7264
7265 copy = clone_as_declaration (die);
7266 add_child_die (new_parent, copy);
7267
7268 if (decl_table)
7269 {
7270 /* Record the pointer to the copy. */
7271 entry->copy = copy;
7272 }
7273
7274 return copy;
7275 }
7276 /* Copy the declaration context to the new type unit DIE. This includes
7277 any surrounding namespace or type declarations. If the DIE has an
7278 AT_specification attribute, it also includes attributes and children
7279 attached to the specification, and returns a pointer to the original
7280 parent of the declaration DIE. Returns NULL otherwise. */
7281
7282 static dw_die_ref
7283 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7284 {
7285 dw_die_ref decl;
7286 dw_die_ref new_decl;
7287 dw_die_ref orig_parent = NULL;
7288
7289 decl = get_AT_ref (die, DW_AT_specification);
7290 if (decl == NULL)
7291 decl = die;
7292 else
7293 {
7294 unsigned ix;
7295 dw_die_ref c;
7296 dw_attr_ref a;
7297
7298 /* The original DIE will be changed to a declaration, and must
7299 be moved to be a child of the original declaration DIE. */
7300 orig_parent = decl->die_parent;
7301
7302 /* Copy the type node pointer from the new DIE to the original
7303 declaration DIE so we can forward references later. */
7304 decl->comdat_type_p = true;
7305 decl->die_id.die_type_node = die->die_id.die_type_node;
7306
7307 remove_AT (die, DW_AT_specification);
7308
7309 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7310 {
7311 if (a->dw_attr != DW_AT_name
7312 && a->dw_attr != DW_AT_declaration
7313 && a->dw_attr != DW_AT_external)
7314 add_dwarf_attr (die, a);
7315 }
7316
7317 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7318 }
7319
7320 if (decl->die_parent != NULL
7321 && !is_unit_die (decl->die_parent))
7322 {
7323 new_decl = copy_ancestor_tree (unit, decl, NULL);
7324 if (new_decl != NULL)
7325 {
7326 remove_AT (new_decl, DW_AT_signature);
7327 add_AT_specification (die, new_decl);
7328 }
7329 }
7330
7331 return orig_parent;
7332 }
7333
7334 /* Generate the skeleton ancestor tree for the given NODE, then clone
7335 the DIE and add the clone into the tree. */
7336
7337 static void
7338 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7339 {
7340 if (node->new_die != NULL)
7341 return;
7342
7343 node->new_die = clone_as_declaration (node->old_die);
7344
7345 if (node->parent != NULL)
7346 {
7347 generate_skeleton_ancestor_tree (node->parent);
7348 add_child_die (node->parent->new_die, node->new_die);
7349 }
7350 }
7351
7352 /* Generate a skeleton tree of DIEs containing any declarations that are
7353 found in the original tree. We traverse the tree looking for declaration
7354 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7355
7356 static void
7357 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7358 {
7359 skeleton_chain_node node;
7360 dw_die_ref c;
7361 dw_die_ref first;
7362 dw_die_ref prev = NULL;
7363 dw_die_ref next = NULL;
7364
7365 node.parent = parent;
7366
7367 first = c = parent->old_die->die_child;
7368 if (c)
7369 next = c->die_sib;
7370 if (c) do {
7371 if (prev == NULL || prev->die_sib == c)
7372 prev = c;
7373 c = next;
7374 next = (c == first ? NULL : c->die_sib);
7375 node.old_die = c;
7376 node.new_die = NULL;
7377 if (is_declaration_die (c))
7378 {
7379 if (is_template_instantiation (c))
7380 {
7381 /* Instantiated templates do not need to be cloned into the
7382 type unit. Just move the DIE and its children back to
7383 the skeleton tree (in the main CU). */
7384 remove_child_with_prev (c, prev);
7385 add_child_die (parent->new_die, c);
7386 c = prev;
7387 }
7388 else
7389 {
7390 /* Clone the existing DIE, move the original to the skeleton
7391 tree (which is in the main CU), and put the clone, with
7392 all the original's children, where the original came from
7393 (which is about to be moved to the type unit). */
7394 dw_die_ref clone = clone_die (c);
7395 move_all_children (c, clone);
7396
7397 /* If the original has a DW_AT_object_pointer attribute,
7398 it would now point to a child DIE just moved to the
7399 cloned tree, so we need to remove that attribute from
7400 the original. */
7401 remove_AT (c, DW_AT_object_pointer);
7402
7403 replace_child (c, clone, prev);
7404 generate_skeleton_ancestor_tree (parent);
7405 add_child_die (parent->new_die, c);
7406 node.new_die = c;
7407 c = clone;
7408 }
7409 }
7410 generate_skeleton_bottom_up (&node);
7411 } while (next != NULL);
7412 }
7413
7414 /* Wrapper function for generate_skeleton_bottom_up. */
7415
7416 static dw_die_ref
7417 generate_skeleton (dw_die_ref die)
7418 {
7419 skeleton_chain_node node;
7420
7421 node.old_die = die;
7422 node.new_die = NULL;
7423 node.parent = NULL;
7424
7425 /* If this type definition is nested inside another type,
7426 and is not an instantiation of a template, always leave
7427 at least a declaration in its place. */
7428 if (die->die_parent != NULL
7429 && is_type_die (die->die_parent)
7430 && !is_template_instantiation (die))
7431 node.new_die = clone_as_declaration (die);
7432
7433 generate_skeleton_bottom_up (&node);
7434 return node.new_die;
7435 }
7436
7437 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7438 declaration. The original DIE is moved to a new compile unit so that
7439 existing references to it follow it to the new location. If any of the
7440 original DIE's descendants is a declaration, we need to replace the
7441 original DIE with a skeleton tree and move the declarations back into the
7442 skeleton tree. */
7443
7444 static dw_die_ref
7445 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7446 dw_die_ref prev)
7447 {
7448 dw_die_ref skeleton, orig_parent;
7449
7450 /* Copy the declaration context to the type unit DIE. If the returned
7451 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7452 that DIE. */
7453 orig_parent = copy_declaration_context (unit, child);
7454
7455 skeleton = generate_skeleton (child);
7456 if (skeleton == NULL)
7457 remove_child_with_prev (child, prev);
7458 else
7459 {
7460 skeleton->comdat_type_p = true;
7461 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7462
7463 /* If the original DIE was a specification, we need to put
7464 the skeleton under the parent DIE of the declaration.
7465 This leaves the original declaration in the tree, but
7466 it will be pruned later since there are no longer any
7467 references to it. */
7468 if (orig_parent != NULL)
7469 {
7470 remove_child_with_prev (child, prev);
7471 add_child_die (orig_parent, skeleton);
7472 }
7473 else
7474 replace_child (child, skeleton, prev);
7475 }
7476
7477 return skeleton;
7478 }
7479
7480 /* Traverse the DIE and set up additional .debug_types sections for each
7481 type worthy of being placed in a COMDAT section. */
7482
7483 static void
7484 break_out_comdat_types (dw_die_ref die)
7485 {
7486 dw_die_ref c;
7487 dw_die_ref first;
7488 dw_die_ref prev = NULL;
7489 dw_die_ref next = NULL;
7490 dw_die_ref unit = NULL;
7491
7492 first = c = die->die_child;
7493 if (c)
7494 next = c->die_sib;
7495 if (c) do {
7496 if (prev == NULL || prev->die_sib == c)
7497 prev = c;
7498 c = next;
7499 next = (c == first ? NULL : c->die_sib);
7500 if (should_move_die_to_comdat (c))
7501 {
7502 dw_die_ref replacement;
7503 comdat_type_node_ref type_node;
7504
7505 /* Break out nested types into their own type units. */
7506 break_out_comdat_types (c);
7507
7508 /* Create a new type unit DIE as the root for the new tree, and
7509 add it to the list of comdat types. */
7510 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7511 add_AT_unsigned (unit, DW_AT_language,
7512 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7513 type_node = ggc_cleared_alloc<comdat_type_node> ();
7514 type_node->root_die = unit;
7515 type_node->next = comdat_type_list;
7516 comdat_type_list = type_node;
7517
7518 /* Generate the type signature. */
7519 generate_type_signature (c, type_node);
7520
7521 /* Copy the declaration context, attributes, and children of the
7522 declaration into the new type unit DIE, then remove this DIE
7523 from the main CU (or replace it with a skeleton if necessary). */
7524 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7525 type_node->skeleton_die = replacement;
7526
7527 /* Add the DIE to the new compunit. */
7528 add_child_die (unit, c);
7529
7530 if (replacement != NULL)
7531 c = replacement;
7532 }
7533 else if (c->die_tag == DW_TAG_namespace
7534 || c->die_tag == DW_TAG_class_type
7535 || c->die_tag == DW_TAG_structure_type
7536 || c->die_tag == DW_TAG_union_type)
7537 {
7538 /* Look for nested types that can be broken out. */
7539 break_out_comdat_types (c);
7540 }
7541 } while (next != NULL);
7542 }
7543
7544 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7545 Enter all the cloned children into the hash table decl_table. */
7546
7547 static dw_die_ref
7548 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7549 {
7550 dw_die_ref c;
7551 dw_die_ref clone;
7552 struct decl_table_entry *entry;
7553 decl_table_entry **slot;
7554
7555 if (die->die_tag == DW_TAG_subprogram)
7556 clone = clone_as_declaration (die);
7557 else
7558 clone = clone_die (die);
7559
7560 slot = decl_table->find_slot_with_hash (die,
7561 htab_hash_pointer (die), INSERT);
7562
7563 /* Assert that DIE isn't in the hash table yet. If it would be there
7564 before, the ancestors would be necessarily there as well, therefore
7565 clone_tree_partial wouldn't be called. */
7566 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7567
7568 entry = XCNEW (struct decl_table_entry);
7569 entry->orig = die;
7570 entry->copy = clone;
7571 *slot = entry;
7572
7573 if (die->die_tag != DW_TAG_subprogram)
7574 FOR_EACH_CHILD (die, c,
7575 add_child_die (clone, clone_tree_partial (c, decl_table)));
7576
7577 return clone;
7578 }
7579
7580 /* Walk the DIE and its children, looking for references to incomplete
7581 or trivial types that are unmarked (i.e., that are not in the current
7582 type_unit). */
7583
7584 static void
7585 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7586 {
7587 dw_die_ref c;
7588 dw_attr_ref a;
7589 unsigned ix;
7590
7591 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7592 {
7593 if (AT_class (a) == dw_val_class_die_ref)
7594 {
7595 dw_die_ref targ = AT_ref (a);
7596 decl_table_entry **slot;
7597 struct decl_table_entry *entry;
7598
7599 if (targ->die_mark != 0 || targ->comdat_type_p)
7600 continue;
7601
7602 slot = decl_table->find_slot_with_hash (targ,
7603 htab_hash_pointer (targ),
7604 INSERT);
7605
7606 if (*slot != HTAB_EMPTY_ENTRY)
7607 {
7608 /* TARG has already been copied, so we just need to
7609 modify the reference to point to the copy. */
7610 entry = *slot;
7611 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7612 }
7613 else
7614 {
7615 dw_die_ref parent = unit;
7616 dw_die_ref copy = clone_die (targ);
7617
7618 /* Record in DECL_TABLE that TARG has been copied.
7619 Need to do this now, before the recursive call,
7620 because DECL_TABLE may be expanded and SLOT
7621 would no longer be a valid pointer. */
7622 entry = XCNEW (struct decl_table_entry);
7623 entry->orig = targ;
7624 entry->copy = copy;
7625 *slot = entry;
7626
7627 /* If TARG is not a declaration DIE, we need to copy its
7628 children. */
7629 if (!is_declaration_die (targ))
7630 {
7631 FOR_EACH_CHILD (
7632 targ, c,
7633 add_child_die (copy,
7634 clone_tree_partial (c, decl_table)));
7635 }
7636
7637 /* Make sure the cloned tree is marked as part of the
7638 type unit. */
7639 mark_dies (copy);
7640
7641 /* If TARG has surrounding context, copy its ancestor tree
7642 into the new type unit. */
7643 if (targ->die_parent != NULL
7644 && !is_unit_die (targ->die_parent))
7645 parent = copy_ancestor_tree (unit, targ->die_parent,
7646 decl_table);
7647
7648 add_child_die (parent, copy);
7649 a->dw_attr_val.v.val_die_ref.die = copy;
7650
7651 /* Make sure the newly-copied DIE is walked. If it was
7652 installed in a previously-added context, it won't
7653 get visited otherwise. */
7654 if (parent != unit)
7655 {
7656 /* Find the highest point of the newly-added tree,
7657 mark each node along the way, and walk from there. */
7658 parent->die_mark = 1;
7659 while (parent->die_parent
7660 && parent->die_parent->die_mark == 0)
7661 {
7662 parent = parent->die_parent;
7663 parent->die_mark = 1;
7664 }
7665 copy_decls_walk (unit, parent, decl_table);
7666 }
7667 }
7668 }
7669 }
7670
7671 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7672 }
7673
7674 /* Copy declarations for "unworthy" types into the new comdat section.
7675 Incomplete types, modified types, and certain other types aren't broken
7676 out into comdat sections of their own, so they don't have a signature,
7677 and we need to copy the declaration into the same section so that we
7678 don't have an external reference. */
7679
7680 static void
7681 copy_decls_for_unworthy_types (dw_die_ref unit)
7682 {
7683 mark_dies (unit);
7684 decl_hash_type decl_table (10);
7685 copy_decls_walk (unit, unit, &decl_table);
7686 unmark_dies (unit);
7687 }
7688
7689 /* Traverse the DIE and add a sibling attribute if it may have the
7690 effect of speeding up access to siblings. To save some space,
7691 avoid generating sibling attributes for DIE's without children. */
7692
7693 static void
7694 add_sibling_attributes (dw_die_ref die)
7695 {
7696 dw_die_ref c;
7697
7698 if (! die->die_child)
7699 return;
7700
7701 if (die->die_parent && die != die->die_parent->die_child)
7702 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7703
7704 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7705 }
7706
7707 /* Output all location lists for the DIE and its children. */
7708
7709 static void
7710 output_location_lists (dw_die_ref die)
7711 {
7712 dw_die_ref c;
7713 dw_attr_ref a;
7714 unsigned ix;
7715
7716 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7717 if (AT_class (a) == dw_val_class_loc_list)
7718 output_loc_list (AT_loc_list (a));
7719
7720 FOR_EACH_CHILD (die, c, output_location_lists (c));
7721 }
7722
7723 /* We want to limit the number of external references, because they are
7724 larger than local references: a relocation takes multiple words, and
7725 even a sig8 reference is always eight bytes, whereas a local reference
7726 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7727 So if we encounter multiple external references to the same type DIE, we
7728 make a local typedef stub for it and redirect all references there.
7729
7730 This is the element of the hash table for keeping track of these
7731 references. */
7732
7733 struct external_ref
7734 {
7735 dw_die_ref type;
7736 dw_die_ref stub;
7737 unsigned n_refs;
7738 };
7739
7740 /* Hashtable helpers. */
7741
7742 struct external_ref_hasher : typed_free_remove <external_ref>
7743 {
7744 typedef external_ref *value_type;
7745 typedef external_ref *compare_type;
7746 static inline hashval_t hash (const external_ref *);
7747 static inline bool equal (const external_ref *, const external_ref *);
7748 };
7749
7750 inline hashval_t
7751 external_ref_hasher::hash (const external_ref *r)
7752 {
7753 dw_die_ref die = r->type;
7754 hashval_t h = 0;
7755
7756 /* We can't use the address of the DIE for hashing, because
7757 that will make the order of the stub DIEs non-deterministic. */
7758 if (! die->comdat_type_p)
7759 /* We have a symbol; use it to compute a hash. */
7760 h = htab_hash_string (die->die_id.die_symbol);
7761 else
7762 {
7763 /* We have a type signature; use a subset of the bits as the hash.
7764 The 8-byte signature is at least as large as hashval_t. */
7765 comdat_type_node_ref type_node = die->die_id.die_type_node;
7766 memcpy (&h, type_node->signature, sizeof (h));
7767 }
7768 return h;
7769 }
7770
7771 inline bool
7772 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7773 {
7774 return r1->type == r2->type;
7775 }
7776
7777 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7778
7779 /* Return a pointer to the external_ref for references to DIE. */
7780
7781 static struct external_ref *
7782 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7783 {
7784 struct external_ref ref, *ref_p;
7785 external_ref **slot;
7786
7787 ref.type = die;
7788 slot = map->find_slot (&ref, INSERT);
7789 if (*slot != HTAB_EMPTY_ENTRY)
7790 return *slot;
7791
7792 ref_p = XCNEW (struct external_ref);
7793 ref_p->type = die;
7794 *slot = ref_p;
7795 return ref_p;
7796 }
7797
7798 /* Subroutine of optimize_external_refs, below.
7799
7800 If we see a type skeleton, record it as our stub. If we see external
7801 references, remember how many we've seen. */
7802
7803 static void
7804 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7805 {
7806 dw_die_ref c;
7807 dw_attr_ref a;
7808 unsigned ix;
7809 struct external_ref *ref_p;
7810
7811 if (is_type_die (die)
7812 && (c = get_AT_ref (die, DW_AT_signature)))
7813 {
7814 /* This is a local skeleton; use it for local references. */
7815 ref_p = lookup_external_ref (map, c);
7816 ref_p->stub = die;
7817 }
7818
7819 /* Scan the DIE references, and remember any that refer to DIEs from
7820 other CUs (i.e. those which are not marked). */
7821 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7822 if (AT_class (a) == dw_val_class_die_ref
7823 && (c = AT_ref (a))->die_mark == 0
7824 && is_type_die (c))
7825 {
7826 ref_p = lookup_external_ref (map, c);
7827 ref_p->n_refs++;
7828 }
7829
7830 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7831 }
7832
7833 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7834 points to an external_ref, DATA is the CU we're processing. If we don't
7835 already have a local stub, and we have multiple refs, build a stub. */
7836
7837 int
7838 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7839 {
7840 struct external_ref *ref_p = *slot;
7841
7842 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7843 {
7844 /* We have multiple references to this type, so build a small stub.
7845 Both of these forms are a bit dodgy from the perspective of the
7846 DWARF standard, since technically they should have names. */
7847 dw_die_ref cu = data;
7848 dw_die_ref type = ref_p->type;
7849 dw_die_ref stub = NULL;
7850
7851 if (type->comdat_type_p)
7852 {
7853 /* If we refer to this type via sig8, use AT_signature. */
7854 stub = new_die (type->die_tag, cu, NULL_TREE);
7855 add_AT_die_ref (stub, DW_AT_signature, type);
7856 }
7857 else
7858 {
7859 /* Otherwise, use a typedef with no name. */
7860 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7861 add_AT_die_ref (stub, DW_AT_type, type);
7862 }
7863
7864 stub->die_mark++;
7865 ref_p->stub = stub;
7866 }
7867 return 1;
7868 }
7869
7870 /* DIE is a unit; look through all the DIE references to see if there are
7871 any external references to types, and if so, create local stubs for
7872 them which will be applied in build_abbrev_table. This is useful because
7873 references to local DIEs are smaller. */
7874
7875 static external_ref_hash_type *
7876 optimize_external_refs (dw_die_ref die)
7877 {
7878 external_ref_hash_type *map = new external_ref_hash_type (10);
7879 optimize_external_refs_1 (die, map);
7880 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7881 return map;
7882 }
7883
7884 /* The format of each DIE (and its attribute value pairs) is encoded in an
7885 abbreviation table. This routine builds the abbreviation table and assigns
7886 a unique abbreviation id for each abbreviation entry. The children of each
7887 die are visited recursively. */
7888
7889 static void
7890 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7891 {
7892 unsigned long abbrev_id;
7893 unsigned int n_alloc;
7894 dw_die_ref c;
7895 dw_attr_ref a;
7896 unsigned ix;
7897
7898 /* Scan the DIE references, and replace any that refer to
7899 DIEs from other CUs (i.e. those which are not marked) with
7900 the local stubs we built in optimize_external_refs. */
7901 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7902 if (AT_class (a) == dw_val_class_die_ref
7903 && (c = AT_ref (a))->die_mark == 0)
7904 {
7905 struct external_ref *ref_p;
7906 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7907
7908 ref_p = lookup_external_ref (extern_map, c);
7909 if (ref_p->stub && ref_p->stub != die)
7910 change_AT_die_ref (a, ref_p->stub);
7911 else
7912 /* We aren't changing this reference, so mark it external. */
7913 set_AT_ref_external (a, 1);
7914 }
7915
7916 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7917 {
7918 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7919 dw_attr_ref die_a, abbrev_a;
7920 unsigned ix;
7921 bool ok = true;
7922
7923 if (abbrev->die_tag != die->die_tag)
7924 continue;
7925 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7926 continue;
7927
7928 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7929 continue;
7930
7931 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7932 {
7933 abbrev_a = &(*abbrev->die_attr)[ix];
7934 if ((abbrev_a->dw_attr != die_a->dw_attr)
7935 || (value_format (abbrev_a) != value_format (die_a)))
7936 {
7937 ok = false;
7938 break;
7939 }
7940 }
7941 if (ok)
7942 break;
7943 }
7944
7945 if (abbrev_id >= abbrev_die_table_in_use)
7946 {
7947 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7948 {
7949 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7950 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7951 n_alloc);
7952
7953 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7954 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7955 abbrev_die_table_allocated = n_alloc;
7956 }
7957
7958 ++abbrev_die_table_in_use;
7959 abbrev_die_table[abbrev_id] = die;
7960 }
7961
7962 die->die_abbrev = abbrev_id;
7963 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7964 }
7965 \f
7966 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7967
7968 static int
7969 constant_size (unsigned HOST_WIDE_INT value)
7970 {
7971 int log;
7972
7973 if (value == 0)
7974 log = 0;
7975 else
7976 log = floor_log2 (value);
7977
7978 log = log / 8;
7979 log = 1 << (floor_log2 (log) + 1);
7980
7981 return log;
7982 }
7983
7984 /* Return the size of a DIE as it is represented in the
7985 .debug_info section. */
7986
7987 static unsigned long
7988 size_of_die (dw_die_ref die)
7989 {
7990 unsigned long size = 0;
7991 dw_attr_ref a;
7992 unsigned ix;
7993 enum dwarf_form form;
7994
7995 size += size_of_uleb128 (die->die_abbrev);
7996 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7997 {
7998 switch (AT_class (a))
7999 {
8000 case dw_val_class_addr:
8001 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8002 {
8003 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8004 size += size_of_uleb128 (AT_index (a));
8005 }
8006 else
8007 size += DWARF2_ADDR_SIZE;
8008 break;
8009 case dw_val_class_offset:
8010 size += DWARF_OFFSET_SIZE;
8011 break;
8012 case dw_val_class_loc:
8013 {
8014 unsigned long lsize = size_of_locs (AT_loc (a));
8015
8016 /* Block length. */
8017 if (dwarf_version >= 4)
8018 size += size_of_uleb128 (lsize);
8019 else
8020 size += constant_size (lsize);
8021 size += lsize;
8022 }
8023 break;
8024 case dw_val_class_loc_list:
8025 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8026 {
8027 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8028 size += size_of_uleb128 (AT_index (a));
8029 }
8030 else
8031 size += DWARF_OFFSET_SIZE;
8032 break;
8033 case dw_val_class_range_list:
8034 size += DWARF_OFFSET_SIZE;
8035 break;
8036 case dw_val_class_const:
8037 size += size_of_sleb128 (AT_int (a));
8038 break;
8039 case dw_val_class_unsigned_const:
8040 {
8041 int csize = constant_size (AT_unsigned (a));
8042 if (dwarf_version == 3
8043 && a->dw_attr == DW_AT_data_member_location
8044 && csize >= 4)
8045 size += size_of_uleb128 (AT_unsigned (a));
8046 else
8047 size += csize;
8048 }
8049 break;
8050 case dw_val_class_const_double:
8051 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8052 if (HOST_BITS_PER_WIDE_INT >= 64)
8053 size++; /* block */
8054 break;
8055 case dw_val_class_wide_int:
8056 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8057 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8058 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8059 > 64)
8060 size++; /* block */
8061 break;
8062 case dw_val_class_vec:
8063 size += constant_size (a->dw_attr_val.v.val_vec.length
8064 * a->dw_attr_val.v.val_vec.elt_size)
8065 + a->dw_attr_val.v.val_vec.length
8066 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8067 break;
8068 case dw_val_class_flag:
8069 if (dwarf_version >= 4)
8070 /* Currently all add_AT_flag calls pass in 1 as last argument,
8071 so DW_FORM_flag_present can be used. If that ever changes,
8072 we'll need to use DW_FORM_flag and have some optimization
8073 in build_abbrev_table that will change those to
8074 DW_FORM_flag_present if it is set to 1 in all DIEs using
8075 the same abbrev entry. */
8076 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8077 else
8078 size += 1;
8079 break;
8080 case dw_val_class_die_ref:
8081 if (AT_ref_external (a))
8082 {
8083 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8084 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8085 is sized by target address length, whereas in DWARF3
8086 it's always sized as an offset. */
8087 if (use_debug_types)
8088 size += DWARF_TYPE_SIGNATURE_SIZE;
8089 else if (dwarf_version == 2)
8090 size += DWARF2_ADDR_SIZE;
8091 else
8092 size += DWARF_OFFSET_SIZE;
8093 }
8094 else
8095 size += DWARF_OFFSET_SIZE;
8096 break;
8097 case dw_val_class_fde_ref:
8098 size += DWARF_OFFSET_SIZE;
8099 break;
8100 case dw_val_class_lbl_id:
8101 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8102 {
8103 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8104 size += size_of_uleb128 (AT_index (a));
8105 }
8106 else
8107 size += DWARF2_ADDR_SIZE;
8108 break;
8109 case dw_val_class_lineptr:
8110 case dw_val_class_macptr:
8111 size += DWARF_OFFSET_SIZE;
8112 break;
8113 case dw_val_class_str:
8114 form = AT_string_form (a);
8115 if (form == DW_FORM_strp)
8116 size += DWARF_OFFSET_SIZE;
8117 else if (form == DW_FORM_GNU_str_index)
8118 size += size_of_uleb128 (AT_index (a));
8119 else
8120 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8121 break;
8122 case dw_val_class_file:
8123 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8124 break;
8125 case dw_val_class_data8:
8126 size += 8;
8127 break;
8128 case dw_val_class_vms_delta:
8129 size += DWARF_OFFSET_SIZE;
8130 break;
8131 case dw_val_class_high_pc:
8132 size += DWARF2_ADDR_SIZE;
8133 break;
8134 default:
8135 gcc_unreachable ();
8136 }
8137 }
8138
8139 return size;
8140 }
8141
8142 /* Size the debugging information associated with a given DIE. Visits the
8143 DIE's children recursively. Updates the global variable next_die_offset, on
8144 each time through. Uses the current value of next_die_offset to update the
8145 die_offset field in each DIE. */
8146
8147 static void
8148 calc_die_sizes (dw_die_ref die)
8149 {
8150 dw_die_ref c;
8151
8152 gcc_assert (die->die_offset == 0
8153 || (unsigned long int) die->die_offset == next_die_offset);
8154 die->die_offset = next_die_offset;
8155 next_die_offset += size_of_die (die);
8156
8157 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8158
8159 if (die->die_child != NULL)
8160 /* Count the null byte used to terminate sibling lists. */
8161 next_die_offset += 1;
8162 }
8163
8164 /* Size just the base type children at the start of the CU.
8165 This is needed because build_abbrev needs to size locs
8166 and sizing of type based stack ops needs to know die_offset
8167 values for the base types. */
8168
8169 static void
8170 calc_base_type_die_sizes (void)
8171 {
8172 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8173 unsigned int i;
8174 dw_die_ref base_type;
8175 #if ENABLE_ASSERT_CHECKING
8176 dw_die_ref prev = comp_unit_die ()->die_child;
8177 #endif
8178
8179 die_offset += size_of_die (comp_unit_die ());
8180 for (i = 0; base_types.iterate (i, &base_type); i++)
8181 {
8182 #if ENABLE_ASSERT_CHECKING
8183 gcc_assert (base_type->die_offset == 0
8184 && prev->die_sib == base_type
8185 && base_type->die_child == NULL
8186 && base_type->die_abbrev);
8187 prev = base_type;
8188 #endif
8189 base_type->die_offset = die_offset;
8190 die_offset += size_of_die (base_type);
8191 }
8192 }
8193
8194 /* Set the marks for a die and its children. We do this so
8195 that we know whether or not a reference needs to use FORM_ref_addr; only
8196 DIEs in the same CU will be marked. We used to clear out the offset
8197 and use that as the flag, but ran into ordering problems. */
8198
8199 static void
8200 mark_dies (dw_die_ref die)
8201 {
8202 dw_die_ref c;
8203
8204 gcc_assert (!die->die_mark);
8205
8206 die->die_mark = 1;
8207 FOR_EACH_CHILD (die, c, mark_dies (c));
8208 }
8209
8210 /* Clear the marks for a die and its children. */
8211
8212 static void
8213 unmark_dies (dw_die_ref die)
8214 {
8215 dw_die_ref c;
8216
8217 if (! use_debug_types)
8218 gcc_assert (die->die_mark);
8219
8220 die->die_mark = 0;
8221 FOR_EACH_CHILD (die, c, unmark_dies (c));
8222 }
8223
8224 /* Clear the marks for a die, its children and referred dies. */
8225
8226 static void
8227 unmark_all_dies (dw_die_ref die)
8228 {
8229 dw_die_ref c;
8230 dw_attr_ref a;
8231 unsigned ix;
8232
8233 if (!die->die_mark)
8234 return;
8235 die->die_mark = 0;
8236
8237 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8238
8239 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8240 if (AT_class (a) == dw_val_class_die_ref)
8241 unmark_all_dies (AT_ref (a));
8242 }
8243
8244 /* Calculate if the entry should appear in the final output file. It may be
8245 from a pruned a type. */
8246
8247 static bool
8248 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8249 {
8250 /* By limiting gnu pubnames to definitions only, gold can generate a
8251 gdb index without entries for declarations, which don't include
8252 enough information to be useful. */
8253 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8254 return false;
8255
8256 if (table == pubname_table)
8257 {
8258 /* Enumerator names are part of the pubname table, but the
8259 parent DW_TAG_enumeration_type die may have been pruned.
8260 Don't output them if that is the case. */
8261 if (p->die->die_tag == DW_TAG_enumerator &&
8262 (p->die->die_parent == NULL
8263 || !p->die->die_parent->die_perennial_p))
8264 return false;
8265
8266 /* Everything else in the pubname table is included. */
8267 return true;
8268 }
8269
8270 /* The pubtypes table shouldn't include types that have been
8271 pruned. */
8272 return (p->die->die_offset != 0
8273 || !flag_eliminate_unused_debug_types);
8274 }
8275
8276 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8277 generated for the compilation unit. */
8278
8279 static unsigned long
8280 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8281 {
8282 unsigned long size;
8283 unsigned i;
8284 pubname_ref p;
8285 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8286
8287 size = DWARF_PUBNAMES_HEADER_SIZE;
8288 FOR_EACH_VEC_ELT (*names, i, p)
8289 if (include_pubname_in_output (names, p))
8290 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8291
8292 size += DWARF_OFFSET_SIZE;
8293 return size;
8294 }
8295
8296 /* Return the size of the information in the .debug_aranges section. */
8297
8298 static unsigned long
8299 size_of_aranges (void)
8300 {
8301 unsigned long size;
8302
8303 size = DWARF_ARANGES_HEADER_SIZE;
8304
8305 /* Count the address/length pair for this compilation unit. */
8306 if (text_section_used)
8307 size += 2 * DWARF2_ADDR_SIZE;
8308 if (cold_text_section_used)
8309 size += 2 * DWARF2_ADDR_SIZE;
8310 if (have_multiple_function_sections)
8311 {
8312 unsigned fde_idx;
8313 dw_fde_ref fde;
8314
8315 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8316 {
8317 if (DECL_IGNORED_P (fde->decl))
8318 continue;
8319 if (!fde->in_std_section)
8320 size += 2 * DWARF2_ADDR_SIZE;
8321 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8322 size += 2 * DWARF2_ADDR_SIZE;
8323 }
8324 }
8325
8326 /* Count the two zero words used to terminated the address range table. */
8327 size += 2 * DWARF2_ADDR_SIZE;
8328 return size;
8329 }
8330 \f
8331 /* Select the encoding of an attribute value. */
8332
8333 static enum dwarf_form
8334 value_format (dw_attr_ref a)
8335 {
8336 switch (AT_class (a))
8337 {
8338 case dw_val_class_addr:
8339 /* Only very few attributes allow DW_FORM_addr. */
8340 switch (a->dw_attr)
8341 {
8342 case DW_AT_low_pc:
8343 case DW_AT_high_pc:
8344 case DW_AT_entry_pc:
8345 case DW_AT_trampoline:
8346 return (AT_index (a) == NOT_INDEXED
8347 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8348 default:
8349 break;
8350 }
8351 switch (DWARF2_ADDR_SIZE)
8352 {
8353 case 1:
8354 return DW_FORM_data1;
8355 case 2:
8356 return DW_FORM_data2;
8357 case 4:
8358 return DW_FORM_data4;
8359 case 8:
8360 return DW_FORM_data8;
8361 default:
8362 gcc_unreachable ();
8363 }
8364 case dw_val_class_range_list:
8365 case dw_val_class_loc_list:
8366 if (dwarf_version >= 4)
8367 return DW_FORM_sec_offset;
8368 /* FALLTHRU */
8369 case dw_val_class_vms_delta:
8370 case dw_val_class_offset:
8371 switch (DWARF_OFFSET_SIZE)
8372 {
8373 case 4:
8374 return DW_FORM_data4;
8375 case 8:
8376 return DW_FORM_data8;
8377 default:
8378 gcc_unreachable ();
8379 }
8380 case dw_val_class_loc:
8381 if (dwarf_version >= 4)
8382 return DW_FORM_exprloc;
8383 switch (constant_size (size_of_locs (AT_loc (a))))
8384 {
8385 case 1:
8386 return DW_FORM_block1;
8387 case 2:
8388 return DW_FORM_block2;
8389 case 4:
8390 return DW_FORM_block4;
8391 default:
8392 gcc_unreachable ();
8393 }
8394 case dw_val_class_const:
8395 return DW_FORM_sdata;
8396 case dw_val_class_unsigned_const:
8397 switch (constant_size (AT_unsigned (a)))
8398 {
8399 case 1:
8400 return DW_FORM_data1;
8401 case 2:
8402 return DW_FORM_data2;
8403 case 4:
8404 /* In DWARF3 DW_AT_data_member_location with
8405 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8406 constant, so we need to use DW_FORM_udata if we need
8407 a large constant. */
8408 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8409 return DW_FORM_udata;
8410 return DW_FORM_data4;
8411 case 8:
8412 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8413 return DW_FORM_udata;
8414 return DW_FORM_data8;
8415 default:
8416 gcc_unreachable ();
8417 }
8418 case dw_val_class_const_double:
8419 switch (HOST_BITS_PER_WIDE_INT)
8420 {
8421 case 8:
8422 return DW_FORM_data2;
8423 case 16:
8424 return DW_FORM_data4;
8425 case 32:
8426 return DW_FORM_data8;
8427 case 64:
8428 default:
8429 return DW_FORM_block1;
8430 }
8431 case dw_val_class_wide_int:
8432 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8433 {
8434 case 8:
8435 return DW_FORM_data1;
8436 case 16:
8437 return DW_FORM_data2;
8438 case 32:
8439 return DW_FORM_data4;
8440 case 64:
8441 return DW_FORM_data8;
8442 default:
8443 return DW_FORM_block1;
8444 }
8445 case dw_val_class_vec:
8446 switch (constant_size (a->dw_attr_val.v.val_vec.length
8447 * a->dw_attr_val.v.val_vec.elt_size))
8448 {
8449 case 1:
8450 return DW_FORM_block1;
8451 case 2:
8452 return DW_FORM_block2;
8453 case 4:
8454 return DW_FORM_block4;
8455 default:
8456 gcc_unreachable ();
8457 }
8458 case dw_val_class_flag:
8459 if (dwarf_version >= 4)
8460 {
8461 /* Currently all add_AT_flag calls pass in 1 as last argument,
8462 so DW_FORM_flag_present can be used. If that ever changes,
8463 we'll need to use DW_FORM_flag and have some optimization
8464 in build_abbrev_table that will change those to
8465 DW_FORM_flag_present if it is set to 1 in all DIEs using
8466 the same abbrev entry. */
8467 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8468 return DW_FORM_flag_present;
8469 }
8470 return DW_FORM_flag;
8471 case dw_val_class_die_ref:
8472 if (AT_ref_external (a))
8473 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8474 else
8475 return DW_FORM_ref;
8476 case dw_val_class_fde_ref:
8477 return DW_FORM_data;
8478 case dw_val_class_lbl_id:
8479 return (AT_index (a) == NOT_INDEXED
8480 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8481 case dw_val_class_lineptr:
8482 case dw_val_class_macptr:
8483 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8484 case dw_val_class_str:
8485 return AT_string_form (a);
8486 case dw_val_class_file:
8487 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8488 {
8489 case 1:
8490 return DW_FORM_data1;
8491 case 2:
8492 return DW_FORM_data2;
8493 case 4:
8494 return DW_FORM_data4;
8495 default:
8496 gcc_unreachable ();
8497 }
8498
8499 case dw_val_class_data8:
8500 return DW_FORM_data8;
8501
8502 case dw_val_class_high_pc:
8503 switch (DWARF2_ADDR_SIZE)
8504 {
8505 case 1:
8506 return DW_FORM_data1;
8507 case 2:
8508 return DW_FORM_data2;
8509 case 4:
8510 return DW_FORM_data4;
8511 case 8:
8512 return DW_FORM_data8;
8513 default:
8514 gcc_unreachable ();
8515 }
8516
8517 default:
8518 gcc_unreachable ();
8519 }
8520 }
8521
8522 /* Output the encoding of an attribute value. */
8523
8524 static void
8525 output_value_format (dw_attr_ref a)
8526 {
8527 enum dwarf_form form = value_format (a);
8528
8529 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8530 }
8531
8532 /* Given a die and id, produce the appropriate abbreviations. */
8533
8534 static void
8535 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8536 {
8537 unsigned ix;
8538 dw_attr_ref a_attr;
8539
8540 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8541 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8542 dwarf_tag_name (abbrev->die_tag));
8543
8544 if (abbrev->die_child != NULL)
8545 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8546 else
8547 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8548
8549 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8550 {
8551 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8552 dwarf_attr_name (a_attr->dw_attr));
8553 output_value_format (a_attr);
8554 }
8555
8556 dw2_asm_output_data (1, 0, NULL);
8557 dw2_asm_output_data (1, 0, NULL);
8558 }
8559
8560
8561 /* Output the .debug_abbrev section which defines the DIE abbreviation
8562 table. */
8563
8564 static void
8565 output_abbrev_section (void)
8566 {
8567 unsigned long abbrev_id;
8568
8569 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8570 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8571
8572 /* Terminate the table. */
8573 dw2_asm_output_data (1, 0, NULL);
8574 }
8575
8576 /* Output a symbol we can use to refer to this DIE from another CU. */
8577
8578 static inline void
8579 output_die_symbol (dw_die_ref die)
8580 {
8581 const char *sym = die->die_id.die_symbol;
8582
8583 gcc_assert (!die->comdat_type_p);
8584
8585 if (sym == 0)
8586 return;
8587
8588 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8589 /* We make these global, not weak; if the target doesn't support
8590 .linkonce, it doesn't support combining the sections, so debugging
8591 will break. */
8592 targetm.asm_out.globalize_label (asm_out_file, sym);
8593
8594 ASM_OUTPUT_LABEL (asm_out_file, sym);
8595 }
8596
8597 /* Return a new location list, given the begin and end range, and the
8598 expression. */
8599
8600 static inline dw_loc_list_ref
8601 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8602 const char *section)
8603 {
8604 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8605
8606 retlist->begin = begin;
8607 retlist->begin_entry = NULL;
8608 retlist->end = end;
8609 retlist->expr = expr;
8610 retlist->section = section;
8611
8612 return retlist;
8613 }
8614
8615 /* Generate a new internal symbol for this location list node, if it
8616 hasn't got one yet. */
8617
8618 static inline void
8619 gen_llsym (dw_loc_list_ref list)
8620 {
8621 gcc_assert (!list->ll_symbol);
8622 list->ll_symbol = gen_internal_sym ("LLST");
8623 }
8624
8625 /* Output the location list given to us. */
8626
8627 static void
8628 output_loc_list (dw_loc_list_ref list_head)
8629 {
8630 dw_loc_list_ref curr = list_head;
8631
8632 if (list_head->emitted)
8633 return;
8634 list_head->emitted = true;
8635
8636 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8637
8638 /* Walk the location list, and output each range + expression. */
8639 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8640 {
8641 unsigned long size;
8642 /* Don't output an entry that starts and ends at the same address. */
8643 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8644 continue;
8645 size = size_of_locs (curr->expr);
8646 /* If the expression is too large, drop it on the floor. We could
8647 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8648 in the expression, but >= 64KB expressions for a single value
8649 in a single range are unlikely very useful. */
8650 if (size > 0xffff)
8651 continue;
8652 if (dwarf_split_debug_info)
8653 {
8654 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8655 "Location list start/length entry (%s)",
8656 list_head->ll_symbol);
8657 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8658 "Location list range start index (%s)",
8659 curr->begin);
8660 /* The length field is 4 bytes. If we ever need to support
8661 an 8-byte length, we can add a new DW_LLE code or fall back
8662 to DW_LLE_GNU_start_end_entry. */
8663 dw2_asm_output_delta (4, curr->end, curr->begin,
8664 "Location list range length (%s)",
8665 list_head->ll_symbol);
8666 }
8667 else if (!have_multiple_function_sections)
8668 {
8669 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8670 "Location list begin address (%s)",
8671 list_head->ll_symbol);
8672 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8673 "Location list end address (%s)",
8674 list_head->ll_symbol);
8675 }
8676 else
8677 {
8678 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8679 "Location list begin address (%s)",
8680 list_head->ll_symbol);
8681 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8682 "Location list end address (%s)",
8683 list_head->ll_symbol);
8684 }
8685
8686 /* Output the block length for this list of location operations. */
8687 gcc_assert (size <= 0xffff);
8688 dw2_asm_output_data (2, size, "%s", "Location expression size");
8689
8690 output_loc_sequence (curr->expr, -1);
8691 }
8692
8693 if (dwarf_split_debug_info)
8694 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8695 "Location list terminator (%s)",
8696 list_head->ll_symbol);
8697 else
8698 {
8699 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8700 "Location list terminator begin (%s)",
8701 list_head->ll_symbol);
8702 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8703 "Location list terminator end (%s)",
8704 list_head->ll_symbol);
8705 }
8706 }
8707
8708 /* Output a range_list offset into the debug_range section. Emit a
8709 relocated reference if val_entry is NULL, otherwise, emit an
8710 indirect reference. */
8711
8712 static void
8713 output_range_list_offset (dw_attr_ref a)
8714 {
8715 const char *name = dwarf_attr_name (a->dw_attr);
8716
8717 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8718 {
8719 char *p = strchr (ranges_section_label, '\0');
8720 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8721 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8722 debug_ranges_section, "%s", name);
8723 *p = '\0';
8724 }
8725 else
8726 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8727 "%s (offset from %s)", name, ranges_section_label);
8728 }
8729
8730 /* Output the offset into the debug_loc section. */
8731
8732 static void
8733 output_loc_list_offset (dw_attr_ref a)
8734 {
8735 char *sym = AT_loc_list (a)->ll_symbol;
8736
8737 gcc_assert (sym);
8738 if (dwarf_split_debug_info)
8739 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8740 "%s", dwarf_attr_name (a->dw_attr));
8741 else
8742 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8743 "%s", dwarf_attr_name (a->dw_attr));
8744 }
8745
8746 /* Output an attribute's index or value appropriately. */
8747
8748 static void
8749 output_attr_index_or_value (dw_attr_ref a)
8750 {
8751 const char *name = dwarf_attr_name (a->dw_attr);
8752
8753 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8754 {
8755 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8756 return;
8757 }
8758 switch (AT_class (a))
8759 {
8760 case dw_val_class_addr:
8761 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8762 break;
8763 case dw_val_class_high_pc:
8764 case dw_val_class_lbl_id:
8765 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8766 break;
8767 case dw_val_class_loc_list:
8768 output_loc_list_offset (a);
8769 break;
8770 default:
8771 gcc_unreachable ();
8772 }
8773 }
8774
8775 /* Output a type signature. */
8776
8777 static inline void
8778 output_signature (const char *sig, const char *name)
8779 {
8780 int i;
8781
8782 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8783 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8784 }
8785
8786 /* Output the DIE and its attributes. Called recursively to generate
8787 the definitions of each child DIE. */
8788
8789 static void
8790 output_die (dw_die_ref die)
8791 {
8792 dw_attr_ref a;
8793 dw_die_ref c;
8794 unsigned long size;
8795 unsigned ix;
8796
8797 /* If someone in another CU might refer to us, set up a symbol for
8798 them to point to. */
8799 if (! die->comdat_type_p && die->die_id.die_symbol)
8800 output_die_symbol (die);
8801
8802 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8803 (unsigned long)die->die_offset,
8804 dwarf_tag_name (die->die_tag));
8805
8806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8807 {
8808 const char *name = dwarf_attr_name (a->dw_attr);
8809
8810 switch (AT_class (a))
8811 {
8812 case dw_val_class_addr:
8813 output_attr_index_or_value (a);
8814 break;
8815
8816 case dw_val_class_offset:
8817 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8818 "%s", name);
8819 break;
8820
8821 case dw_val_class_range_list:
8822 output_range_list_offset (a);
8823 break;
8824
8825 case dw_val_class_loc:
8826 size = size_of_locs (AT_loc (a));
8827
8828 /* Output the block length for this list of location operations. */
8829 if (dwarf_version >= 4)
8830 dw2_asm_output_data_uleb128 (size, "%s", name);
8831 else
8832 dw2_asm_output_data (constant_size (size), size, "%s", name);
8833
8834 output_loc_sequence (AT_loc (a), -1);
8835 break;
8836
8837 case dw_val_class_const:
8838 /* ??? It would be slightly more efficient to use a scheme like is
8839 used for unsigned constants below, but gdb 4.x does not sign
8840 extend. Gdb 5.x does sign extend. */
8841 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8842 break;
8843
8844 case dw_val_class_unsigned_const:
8845 {
8846 int csize = constant_size (AT_unsigned (a));
8847 if (dwarf_version == 3
8848 && a->dw_attr == DW_AT_data_member_location
8849 && csize >= 4)
8850 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8851 else
8852 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8853 }
8854 break;
8855
8856 case dw_val_class_const_double:
8857 {
8858 unsigned HOST_WIDE_INT first, second;
8859
8860 if (HOST_BITS_PER_WIDE_INT >= 64)
8861 dw2_asm_output_data (1,
8862 HOST_BITS_PER_DOUBLE_INT
8863 / HOST_BITS_PER_CHAR,
8864 NULL);
8865
8866 if (WORDS_BIG_ENDIAN)
8867 {
8868 first = a->dw_attr_val.v.val_double.high;
8869 second = a->dw_attr_val.v.val_double.low;
8870 }
8871 else
8872 {
8873 first = a->dw_attr_val.v.val_double.low;
8874 second = a->dw_attr_val.v.val_double.high;
8875 }
8876
8877 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8878 first, "%s", name);
8879 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8880 second, NULL);
8881 }
8882 break;
8883
8884 case dw_val_class_wide_int:
8885 {
8886 int i;
8887 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8888 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8889 if (len * HOST_BITS_PER_WIDE_INT > 64)
8890 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8891 NULL);
8892
8893 if (WORDS_BIG_ENDIAN)
8894 for (i = len - 1; i >= 0; --i)
8895 {
8896 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8897 "%s", name);
8898 name = NULL;
8899 }
8900 else
8901 for (i = 0; i < len; ++i)
8902 {
8903 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8904 "%s", name);
8905 name = NULL;
8906 }
8907 }
8908 break;
8909
8910 case dw_val_class_vec:
8911 {
8912 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8913 unsigned int len = a->dw_attr_val.v.val_vec.length;
8914 unsigned int i;
8915 unsigned char *p;
8916
8917 dw2_asm_output_data (constant_size (len * elt_size),
8918 len * elt_size, "%s", name);
8919 if (elt_size > sizeof (HOST_WIDE_INT))
8920 {
8921 elt_size /= 2;
8922 len *= 2;
8923 }
8924 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8925 i < len;
8926 i++, p += elt_size)
8927 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8928 "fp or vector constant word %u", i);
8929 break;
8930 }
8931
8932 case dw_val_class_flag:
8933 if (dwarf_version >= 4)
8934 {
8935 /* Currently all add_AT_flag calls pass in 1 as last argument,
8936 so DW_FORM_flag_present can be used. If that ever changes,
8937 we'll need to use DW_FORM_flag and have some optimization
8938 in build_abbrev_table that will change those to
8939 DW_FORM_flag_present if it is set to 1 in all DIEs using
8940 the same abbrev entry. */
8941 gcc_assert (AT_flag (a) == 1);
8942 if (flag_debug_asm)
8943 fprintf (asm_out_file, "\t\t\t%s %s\n",
8944 ASM_COMMENT_START, name);
8945 break;
8946 }
8947 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8948 break;
8949
8950 case dw_val_class_loc_list:
8951 output_attr_index_or_value (a);
8952 break;
8953
8954 case dw_val_class_die_ref:
8955 if (AT_ref_external (a))
8956 {
8957 if (AT_ref (a)->comdat_type_p)
8958 {
8959 comdat_type_node_ref type_node =
8960 AT_ref (a)->die_id.die_type_node;
8961
8962 gcc_assert (type_node);
8963 output_signature (type_node->signature, name);
8964 }
8965 else
8966 {
8967 const char *sym = AT_ref (a)->die_id.die_symbol;
8968 int size;
8969
8970 gcc_assert (sym);
8971 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8972 length, whereas in DWARF3 it's always sized as an
8973 offset. */
8974 if (dwarf_version == 2)
8975 size = DWARF2_ADDR_SIZE;
8976 else
8977 size = DWARF_OFFSET_SIZE;
8978 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8979 name);
8980 }
8981 }
8982 else
8983 {
8984 gcc_assert (AT_ref (a)->die_offset);
8985 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8986 "%s", name);
8987 }
8988 break;
8989
8990 case dw_val_class_fde_ref:
8991 {
8992 char l1[20];
8993
8994 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8995 a->dw_attr_val.v.val_fde_index * 2);
8996 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8997 "%s", name);
8998 }
8999 break;
9000
9001 case dw_val_class_vms_delta:
9002 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9003 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9004 AT_vms_delta2 (a), AT_vms_delta1 (a),
9005 "%s", name);
9006 #else
9007 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9008 AT_vms_delta2 (a), AT_vms_delta1 (a),
9009 "%s", name);
9010 #endif
9011 break;
9012
9013 case dw_val_class_lbl_id:
9014 output_attr_index_or_value (a);
9015 break;
9016
9017 case dw_val_class_lineptr:
9018 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9019 debug_line_section, "%s", name);
9020 break;
9021
9022 case dw_val_class_macptr:
9023 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9024 debug_macinfo_section, "%s", name);
9025 break;
9026
9027 case dw_val_class_str:
9028 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9029 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9030 a->dw_attr_val.v.val_str->label,
9031 debug_str_section,
9032 "%s: \"%s\"", name, AT_string (a));
9033 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9034 dw2_asm_output_data_uleb128 (AT_index (a),
9035 "%s: \"%s\"", name, AT_string (a));
9036 else
9037 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9038 break;
9039
9040 case dw_val_class_file:
9041 {
9042 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9043
9044 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9045 a->dw_attr_val.v.val_file->filename);
9046 break;
9047 }
9048
9049 case dw_val_class_data8:
9050 {
9051 int i;
9052
9053 for (i = 0; i < 8; i++)
9054 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9055 i == 0 ? "%s" : NULL, name);
9056 break;
9057 }
9058
9059 case dw_val_class_high_pc:
9060 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9061 get_AT_low_pc (die), "DW_AT_high_pc");
9062 break;
9063
9064 default:
9065 gcc_unreachable ();
9066 }
9067 }
9068
9069 FOR_EACH_CHILD (die, c, output_die (c));
9070
9071 /* Add null byte to terminate sibling list. */
9072 if (die->die_child != NULL)
9073 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9074 (unsigned long) die->die_offset);
9075 }
9076
9077 /* Output the compilation unit that appears at the beginning of the
9078 .debug_info section, and precedes the DIE descriptions. */
9079
9080 static void
9081 output_compilation_unit_header (void)
9082 {
9083 /* We don't support actual DWARFv5 units yet, we just use some
9084 DWARFv5 draft DIE tags in DWARFv4 format. */
9085 int ver = dwarf_version < 5 ? dwarf_version : 4;
9086
9087 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9088 dw2_asm_output_data (4, 0xffffffff,
9089 "Initial length escape value indicating 64-bit DWARF extension");
9090 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9091 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9092 "Length of Compilation Unit Info");
9093 dw2_asm_output_data (2, ver, "DWARF version number");
9094 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9095 debug_abbrev_section,
9096 "Offset Into Abbrev. Section");
9097 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9098 }
9099
9100 /* Output the compilation unit DIE and its children. */
9101
9102 static void
9103 output_comp_unit (dw_die_ref die, int output_if_empty)
9104 {
9105 const char *secname, *oldsym;
9106 char *tmp;
9107
9108 /* Unless we are outputting main CU, we may throw away empty ones. */
9109 if (!output_if_empty && die->die_child == NULL)
9110 return;
9111
9112 /* Even if there are no children of this DIE, we must output the information
9113 about the compilation unit. Otherwise, on an empty translation unit, we
9114 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9115 will then complain when examining the file. First mark all the DIEs in
9116 this CU so we know which get local refs. */
9117 mark_dies (die);
9118
9119 external_ref_hash_type *extern_map = optimize_external_refs (die);
9120
9121 build_abbrev_table (die, extern_map);
9122
9123 delete extern_map;
9124
9125 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9126 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9127 calc_die_sizes (die);
9128
9129 oldsym = die->die_id.die_symbol;
9130 if (oldsym)
9131 {
9132 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9133
9134 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9135 secname = tmp;
9136 die->die_id.die_symbol = NULL;
9137 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9138 }
9139 else
9140 {
9141 switch_to_section (debug_info_section);
9142 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9143 info_section_emitted = true;
9144 }
9145
9146 /* Output debugging information. */
9147 output_compilation_unit_header ();
9148 output_die (die);
9149
9150 /* Leave the marks on the main CU, so we can check them in
9151 output_pubnames. */
9152 if (oldsym)
9153 {
9154 unmark_dies (die);
9155 die->die_id.die_symbol = oldsym;
9156 }
9157 }
9158
9159 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9160 and .debug_pubtypes. This is configured per-target, but can be
9161 overridden by the -gpubnames or -gno-pubnames options. */
9162
9163 static inline bool
9164 want_pubnames (void)
9165 {
9166 if (debug_info_level <= DINFO_LEVEL_TERSE)
9167 return false;
9168 if (debug_generate_pub_sections != -1)
9169 return debug_generate_pub_sections;
9170 return targetm.want_debug_pub_sections;
9171 }
9172
9173 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9174
9175 static void
9176 add_AT_pubnames (dw_die_ref die)
9177 {
9178 if (want_pubnames ())
9179 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9180 }
9181
9182 /* Add a string attribute value to a skeleton DIE. */
9183
9184 static inline void
9185 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9186 const char *str)
9187 {
9188 dw_attr_node attr;
9189 struct indirect_string_node *node;
9190
9191 if (! skeleton_debug_str_hash)
9192 skeleton_debug_str_hash
9193 = hash_table<indirect_string_hasher>::create_ggc (10);
9194
9195 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9196 find_string_form (node);
9197 if (node->form == DW_FORM_GNU_str_index)
9198 node->form = DW_FORM_strp;
9199
9200 attr.dw_attr = attr_kind;
9201 attr.dw_attr_val.val_class = dw_val_class_str;
9202 attr.dw_attr_val.val_entry = NULL;
9203 attr.dw_attr_val.v.val_str = node;
9204 add_dwarf_attr (die, &attr);
9205 }
9206
9207 /* Helper function to generate top-level dies for skeleton debug_info and
9208 debug_types. */
9209
9210 static void
9211 add_top_level_skeleton_die_attrs (dw_die_ref die)
9212 {
9213 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9214 const char *comp_dir = comp_dir_string ();
9215
9216 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9217 if (comp_dir != NULL)
9218 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9219 add_AT_pubnames (die);
9220 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9221 }
9222
9223 /* Output skeleton debug sections that point to the dwo file. */
9224
9225 static void
9226 output_skeleton_debug_sections (dw_die_ref comp_unit)
9227 {
9228 /* We don't support actual DWARFv5 units yet, we just use some
9229 DWARFv5 draft DIE tags in DWARFv4 format. */
9230 int ver = dwarf_version < 5 ? dwarf_version : 4;
9231
9232 /* These attributes will be found in the full debug_info section. */
9233 remove_AT (comp_unit, DW_AT_producer);
9234 remove_AT (comp_unit, DW_AT_language);
9235
9236 switch_to_section (debug_skeleton_info_section);
9237 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9238
9239 /* Produce the skeleton compilation-unit header. This one differs enough from
9240 a normal CU header that it's better not to call output_compilation_unit
9241 header. */
9242 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9243 dw2_asm_output_data (4, 0xffffffff,
9244 "Initial length escape value indicating 64-bit DWARF extension");
9245
9246 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9247 DWARF_COMPILE_UNIT_HEADER_SIZE
9248 - DWARF_INITIAL_LENGTH_SIZE
9249 + size_of_die (comp_unit),
9250 "Length of Compilation Unit Info");
9251 dw2_asm_output_data (2, ver, "DWARF version number");
9252 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9253 debug_abbrev_section,
9254 "Offset Into Abbrev. Section");
9255 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9256
9257 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9258 output_die (comp_unit);
9259
9260 /* Build the skeleton debug_abbrev section. */
9261 switch_to_section (debug_skeleton_abbrev_section);
9262 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9263
9264 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9265
9266 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9267 }
9268
9269 /* Output a comdat type unit DIE and its children. */
9270
9271 static void
9272 output_comdat_type_unit (comdat_type_node *node)
9273 {
9274 const char *secname;
9275 char *tmp;
9276 int i;
9277 #if defined (OBJECT_FORMAT_ELF)
9278 tree comdat_key;
9279 #endif
9280
9281 /* First mark all the DIEs in this CU so we know which get local refs. */
9282 mark_dies (node->root_die);
9283
9284 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9285
9286 build_abbrev_table (node->root_die, extern_map);
9287
9288 delete extern_map;
9289 extern_map = NULL;
9290
9291 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9292 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9293 calc_die_sizes (node->root_die);
9294
9295 #if defined (OBJECT_FORMAT_ELF)
9296 if (!dwarf_split_debug_info)
9297 secname = ".debug_types";
9298 else
9299 secname = ".debug_types.dwo";
9300
9301 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9302 sprintf (tmp, "wt.");
9303 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9304 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9305 comdat_key = get_identifier (tmp);
9306 targetm.asm_out.named_section (secname,
9307 SECTION_DEBUG | SECTION_LINKONCE,
9308 comdat_key);
9309 #else
9310 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9311 sprintf (tmp, ".gnu.linkonce.wt.");
9312 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9313 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9314 secname = tmp;
9315 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9316 #endif
9317
9318 /* Output debugging information. */
9319 output_compilation_unit_header ();
9320 output_signature (node->signature, "Type Signature");
9321 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9322 "Offset to Type DIE");
9323 output_die (node->root_die);
9324
9325 unmark_dies (node->root_die);
9326 }
9327
9328 /* Return the DWARF2/3 pubname associated with a decl. */
9329
9330 static const char *
9331 dwarf2_name (tree decl, int scope)
9332 {
9333 if (DECL_NAMELESS (decl))
9334 return NULL;
9335 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9336 }
9337
9338 /* Add a new entry to .debug_pubnames if appropriate. */
9339
9340 static void
9341 add_pubname_string (const char *str, dw_die_ref die)
9342 {
9343 pubname_entry e;
9344
9345 e.die = die;
9346 e.name = xstrdup (str);
9347 vec_safe_push (pubname_table, e);
9348 }
9349
9350 static void
9351 add_pubname (tree decl, dw_die_ref die)
9352 {
9353 if (!want_pubnames ())
9354 return;
9355
9356 /* Don't add items to the table when we expect that the consumer will have
9357 just read the enclosing die. For example, if the consumer is looking at a
9358 class_member, it will either be inside the class already, or will have just
9359 looked up the class to find the member. Either way, searching the class is
9360 faster than searching the index. */
9361 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9362 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9363 {
9364 const char *name = dwarf2_name (decl, 1);
9365
9366 if (name)
9367 add_pubname_string (name, die);
9368 }
9369 }
9370
9371 /* Add an enumerator to the pubnames section. */
9372
9373 static void
9374 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9375 {
9376 pubname_entry e;
9377
9378 gcc_assert (scope_name);
9379 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9380 e.die = die;
9381 vec_safe_push (pubname_table, e);
9382 }
9383
9384 /* Add a new entry to .debug_pubtypes if appropriate. */
9385
9386 static void
9387 add_pubtype (tree decl, dw_die_ref die)
9388 {
9389 pubname_entry e;
9390
9391 if (!want_pubnames ())
9392 return;
9393
9394 if ((TREE_PUBLIC (decl)
9395 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9396 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9397 {
9398 tree scope = NULL;
9399 const char *scope_name = "";
9400 const char *sep = is_cxx () ? "::" : ".";
9401 const char *name;
9402
9403 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9404 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9405 {
9406 scope_name = lang_hooks.dwarf_name (scope, 1);
9407 if (scope_name != NULL && scope_name[0] != '\0')
9408 scope_name = concat (scope_name, sep, NULL);
9409 else
9410 scope_name = "";
9411 }
9412
9413 if (TYPE_P (decl))
9414 name = type_tag (decl);
9415 else
9416 name = lang_hooks.dwarf_name (decl, 1);
9417
9418 /* If we don't have a name for the type, there's no point in adding
9419 it to the table. */
9420 if (name != NULL && name[0] != '\0')
9421 {
9422 e.die = die;
9423 e.name = concat (scope_name, name, NULL);
9424 vec_safe_push (pubtype_table, e);
9425 }
9426
9427 /* Although it might be more consistent to add the pubinfo for the
9428 enumerators as their dies are created, they should only be added if the
9429 enum type meets the criteria above. So rather than re-check the parent
9430 enum type whenever an enumerator die is created, just output them all
9431 here. This isn't protected by the name conditional because anonymous
9432 enums don't have names. */
9433 if (die->die_tag == DW_TAG_enumeration_type)
9434 {
9435 dw_die_ref c;
9436
9437 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9438 }
9439 }
9440 }
9441
9442 /* Output a single entry in the pubnames table. */
9443
9444 static void
9445 output_pubname (dw_offset die_offset, pubname_entry *entry)
9446 {
9447 dw_die_ref die = entry->die;
9448 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9449
9450 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9451
9452 if (debug_generate_pub_sections == 2)
9453 {
9454 /* This logic follows gdb's method for determining the value of the flag
9455 byte. */
9456 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9457 switch (die->die_tag)
9458 {
9459 case DW_TAG_typedef:
9460 case DW_TAG_base_type:
9461 case DW_TAG_subrange_type:
9462 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9463 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9464 break;
9465 case DW_TAG_enumerator:
9466 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9467 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9468 if (!is_cxx () && !is_java ())
9469 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9470 break;
9471 case DW_TAG_subprogram:
9472 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9473 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9474 if (!is_ada ())
9475 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9476 break;
9477 case DW_TAG_constant:
9478 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9479 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9480 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9481 break;
9482 case DW_TAG_variable:
9483 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9484 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9485 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9486 break;
9487 case DW_TAG_namespace:
9488 case DW_TAG_imported_declaration:
9489 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9490 break;
9491 case DW_TAG_class_type:
9492 case DW_TAG_interface_type:
9493 case DW_TAG_structure_type:
9494 case DW_TAG_union_type:
9495 case DW_TAG_enumeration_type:
9496 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9497 if (!is_cxx () && !is_java ())
9498 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9499 break;
9500 default:
9501 /* An unusual tag. Leave the flag-byte empty. */
9502 break;
9503 }
9504 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9505 "GDB-index flags");
9506 }
9507
9508 dw2_asm_output_nstring (entry->name, -1, "external name");
9509 }
9510
9511
9512 /* Output the public names table used to speed up access to externally
9513 visible names; or the public types table used to find type definitions. */
9514
9515 static void
9516 output_pubnames (vec<pubname_entry, va_gc> *names)
9517 {
9518 unsigned i;
9519 unsigned long pubnames_length = size_of_pubnames (names);
9520 pubname_ref pub;
9521
9522 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9523 dw2_asm_output_data (4, 0xffffffff,
9524 "Initial length escape value indicating 64-bit DWARF extension");
9525 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9526
9527 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9528 dw2_asm_output_data (2, 2, "DWARF Version");
9529
9530 if (dwarf_split_debug_info)
9531 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9532 debug_skeleton_info_section,
9533 "Offset of Compilation Unit Info");
9534 else
9535 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9536 debug_info_section,
9537 "Offset of Compilation Unit Info");
9538 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9539 "Compilation Unit Length");
9540
9541 FOR_EACH_VEC_ELT (*names, i, pub)
9542 {
9543 if (include_pubname_in_output (names, pub))
9544 {
9545 dw_offset die_offset = pub->die->die_offset;
9546
9547 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9548 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9549 gcc_assert (pub->die->die_mark);
9550
9551 /* If we're putting types in their own .debug_types sections,
9552 the .debug_pubtypes table will still point to the compile
9553 unit (not the type unit), so we want to use the offset of
9554 the skeleton DIE (if there is one). */
9555 if (pub->die->comdat_type_p && names == pubtype_table)
9556 {
9557 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9558
9559 if (type_node != NULL)
9560 die_offset = (type_node->skeleton_die != NULL
9561 ? type_node->skeleton_die->die_offset
9562 : comp_unit_die ()->die_offset);
9563 }
9564
9565 output_pubname (die_offset, pub);
9566 }
9567 }
9568
9569 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9570 }
9571
9572 /* Output public names and types tables if necessary. */
9573
9574 static void
9575 output_pubtables (void)
9576 {
9577 if (!want_pubnames () || !info_section_emitted)
9578 return;
9579
9580 switch_to_section (debug_pubnames_section);
9581 output_pubnames (pubname_table);
9582 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9583 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9584 simply won't look for the section. */
9585 switch_to_section (debug_pubtypes_section);
9586 output_pubnames (pubtype_table);
9587 }
9588
9589
9590 /* Output the information that goes into the .debug_aranges table.
9591 Namely, define the beginning and ending address range of the
9592 text section generated for this compilation unit. */
9593
9594 static void
9595 output_aranges (unsigned long aranges_length)
9596 {
9597 unsigned i;
9598
9599 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9600 dw2_asm_output_data (4, 0xffffffff,
9601 "Initial length escape value indicating 64-bit DWARF extension");
9602 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9603 "Length of Address Ranges Info");
9604 /* Version number for aranges is still 2, even up to DWARF5. */
9605 dw2_asm_output_data (2, 2, "DWARF Version");
9606 if (dwarf_split_debug_info)
9607 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9608 debug_skeleton_info_section,
9609 "Offset of Compilation Unit Info");
9610 else
9611 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9612 debug_info_section,
9613 "Offset of Compilation Unit Info");
9614 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9615 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9616
9617 /* We need to align to twice the pointer size here. */
9618 if (DWARF_ARANGES_PAD_SIZE)
9619 {
9620 /* Pad using a 2 byte words so that padding is correct for any
9621 pointer size. */
9622 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9623 2 * DWARF2_ADDR_SIZE);
9624 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9625 dw2_asm_output_data (2, 0, NULL);
9626 }
9627
9628 /* It is necessary not to output these entries if the sections were
9629 not used; if the sections were not used, the length will be 0 and
9630 the address may end up as 0 if the section is discarded by ld
9631 --gc-sections, leaving an invalid (0, 0) entry that can be
9632 confused with the terminator. */
9633 if (text_section_used)
9634 {
9635 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9636 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9637 text_section_label, "Length");
9638 }
9639 if (cold_text_section_used)
9640 {
9641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9642 "Address");
9643 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9644 cold_text_section_label, "Length");
9645 }
9646
9647 if (have_multiple_function_sections)
9648 {
9649 unsigned fde_idx;
9650 dw_fde_ref fde;
9651
9652 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9653 {
9654 if (DECL_IGNORED_P (fde->decl))
9655 continue;
9656 if (!fde->in_std_section)
9657 {
9658 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9659 "Address");
9660 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9661 fde->dw_fde_begin, "Length");
9662 }
9663 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9664 {
9665 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9666 "Address");
9667 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9668 fde->dw_fde_second_begin, "Length");
9669 }
9670 }
9671 }
9672
9673 /* Output the terminator words. */
9674 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9675 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9676 }
9677
9678 /* Add a new entry to .debug_ranges. Return the offset at which it
9679 was placed. */
9680
9681 static unsigned int
9682 add_ranges_num (int num)
9683 {
9684 unsigned int in_use = ranges_table_in_use;
9685
9686 if (in_use == ranges_table_allocated)
9687 {
9688 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9689 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9690 ranges_table_allocated);
9691 memset (ranges_table + ranges_table_in_use, 0,
9692 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9693 }
9694
9695 ranges_table[in_use].num = num;
9696 ranges_table_in_use = in_use + 1;
9697
9698 return in_use * 2 * DWARF2_ADDR_SIZE;
9699 }
9700
9701 /* Add a new entry to .debug_ranges corresponding to a block, or a
9702 range terminator if BLOCK is NULL. */
9703
9704 static unsigned int
9705 add_ranges (const_tree block)
9706 {
9707 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9708 }
9709
9710 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9711 When using dwarf_split_debug_info, address attributes in dies destined
9712 for the final executable should be direct references--setting the
9713 parameter force_direct ensures this behavior. */
9714
9715 static void
9716 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9717 bool *added, bool force_direct)
9718 {
9719 unsigned int in_use = ranges_by_label_in_use;
9720 unsigned int offset;
9721
9722 if (in_use == ranges_by_label_allocated)
9723 {
9724 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9725 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9726 ranges_by_label,
9727 ranges_by_label_allocated);
9728 memset (ranges_by_label + ranges_by_label_in_use, 0,
9729 RANGES_TABLE_INCREMENT
9730 * sizeof (struct dw_ranges_by_label_struct));
9731 }
9732
9733 ranges_by_label[in_use].begin = begin;
9734 ranges_by_label[in_use].end = end;
9735 ranges_by_label_in_use = in_use + 1;
9736
9737 offset = add_ranges_num (-(int)in_use - 1);
9738 if (!*added)
9739 {
9740 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9741 *added = true;
9742 }
9743 }
9744
9745 static void
9746 output_ranges (void)
9747 {
9748 unsigned i;
9749 static const char *const start_fmt = "Offset %#x";
9750 const char *fmt = start_fmt;
9751
9752 for (i = 0; i < ranges_table_in_use; i++)
9753 {
9754 int block_num = ranges_table[i].num;
9755
9756 if (block_num > 0)
9757 {
9758 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9759 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9760
9761 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9762 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9763
9764 /* If all code is in the text section, then the compilation
9765 unit base address defaults to DW_AT_low_pc, which is the
9766 base of the text section. */
9767 if (!have_multiple_function_sections)
9768 {
9769 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9770 text_section_label,
9771 fmt, i * 2 * DWARF2_ADDR_SIZE);
9772 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9773 text_section_label, NULL);
9774 }
9775
9776 /* Otherwise, the compilation unit base address is zero,
9777 which allows us to use absolute addresses, and not worry
9778 about whether the target supports cross-section
9779 arithmetic. */
9780 else
9781 {
9782 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9783 fmt, i * 2 * DWARF2_ADDR_SIZE);
9784 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9785 }
9786
9787 fmt = NULL;
9788 }
9789
9790 /* Negative block_num stands for an index into ranges_by_label. */
9791 else if (block_num < 0)
9792 {
9793 int lab_idx = - block_num - 1;
9794
9795 if (!have_multiple_function_sections)
9796 {
9797 gcc_unreachable ();
9798 #if 0
9799 /* If we ever use add_ranges_by_labels () for a single
9800 function section, all we have to do is to take out
9801 the #if 0 above. */
9802 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9803 ranges_by_label[lab_idx].begin,
9804 text_section_label,
9805 fmt, i * 2 * DWARF2_ADDR_SIZE);
9806 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9807 ranges_by_label[lab_idx].end,
9808 text_section_label, NULL);
9809 #endif
9810 }
9811 else
9812 {
9813 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9814 ranges_by_label[lab_idx].begin,
9815 fmt, i * 2 * DWARF2_ADDR_SIZE);
9816 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9817 ranges_by_label[lab_idx].end,
9818 NULL);
9819 }
9820 }
9821 else
9822 {
9823 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9824 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9825 fmt = start_fmt;
9826 }
9827 }
9828 }
9829
9830 /* Data structure containing information about input files. */
9831 struct file_info
9832 {
9833 const char *path; /* Complete file name. */
9834 const char *fname; /* File name part. */
9835 int length; /* Length of entire string. */
9836 struct dwarf_file_data * file_idx; /* Index in input file table. */
9837 int dir_idx; /* Index in directory table. */
9838 };
9839
9840 /* Data structure containing information about directories with source
9841 files. */
9842 struct dir_info
9843 {
9844 const char *path; /* Path including directory name. */
9845 int length; /* Path length. */
9846 int prefix; /* Index of directory entry which is a prefix. */
9847 int count; /* Number of files in this directory. */
9848 int dir_idx; /* Index of directory used as base. */
9849 };
9850
9851 /* Callback function for file_info comparison. We sort by looking at
9852 the directories in the path. */
9853
9854 static int
9855 file_info_cmp (const void *p1, const void *p2)
9856 {
9857 const struct file_info *const s1 = (const struct file_info *) p1;
9858 const struct file_info *const s2 = (const struct file_info *) p2;
9859 const unsigned char *cp1;
9860 const unsigned char *cp2;
9861
9862 /* Take care of file names without directories. We need to make sure that
9863 we return consistent values to qsort since some will get confused if
9864 we return the same value when identical operands are passed in opposite
9865 orders. So if neither has a directory, return 0 and otherwise return
9866 1 or -1 depending on which one has the directory. */
9867 if ((s1->path == s1->fname || s2->path == s2->fname))
9868 return (s2->path == s2->fname) - (s1->path == s1->fname);
9869
9870 cp1 = (const unsigned char *) s1->path;
9871 cp2 = (const unsigned char *) s2->path;
9872
9873 while (1)
9874 {
9875 ++cp1;
9876 ++cp2;
9877 /* Reached the end of the first path? If so, handle like above. */
9878 if ((cp1 == (const unsigned char *) s1->fname)
9879 || (cp2 == (const unsigned char *) s2->fname))
9880 return ((cp2 == (const unsigned char *) s2->fname)
9881 - (cp1 == (const unsigned char *) s1->fname));
9882
9883 /* Character of current path component the same? */
9884 else if (*cp1 != *cp2)
9885 return *cp1 - *cp2;
9886 }
9887 }
9888
9889 struct file_name_acquire_data
9890 {
9891 struct file_info *files;
9892 int used_files;
9893 int max_files;
9894 };
9895
9896 /* Traversal function for the hash table. */
9897
9898 int
9899 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9900 {
9901 struct dwarf_file_data *d = *slot;
9902 struct file_info *fi;
9903 const char *f;
9904
9905 gcc_assert (fnad->max_files >= d->emitted_number);
9906
9907 if (! d->emitted_number)
9908 return 1;
9909
9910 gcc_assert (fnad->max_files != fnad->used_files);
9911
9912 fi = fnad->files + fnad->used_files++;
9913
9914 /* Skip all leading "./". */
9915 f = d->filename;
9916 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9917 f += 2;
9918
9919 /* Create a new array entry. */
9920 fi->path = f;
9921 fi->length = strlen (f);
9922 fi->file_idx = d;
9923
9924 /* Search for the file name part. */
9925 f = strrchr (f, DIR_SEPARATOR);
9926 #if defined (DIR_SEPARATOR_2)
9927 {
9928 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9929
9930 if (g != NULL)
9931 {
9932 if (f == NULL || f < g)
9933 f = g;
9934 }
9935 }
9936 #endif
9937
9938 fi->fname = f == NULL ? fi->path : f + 1;
9939 return 1;
9940 }
9941
9942 /* Output the directory table and the file name table. We try to minimize
9943 the total amount of memory needed. A heuristic is used to avoid large
9944 slowdowns with many input files. */
9945
9946 static void
9947 output_file_names (void)
9948 {
9949 struct file_name_acquire_data fnad;
9950 int numfiles;
9951 struct file_info *files;
9952 struct dir_info *dirs;
9953 int *saved;
9954 int *savehere;
9955 int *backmap;
9956 int ndirs;
9957 int idx_offset;
9958 int i;
9959
9960 if (!last_emitted_file)
9961 {
9962 dw2_asm_output_data (1, 0, "End directory table");
9963 dw2_asm_output_data (1, 0, "End file name table");
9964 return;
9965 }
9966
9967 numfiles = last_emitted_file->emitted_number;
9968
9969 /* Allocate the various arrays we need. */
9970 files = XALLOCAVEC (struct file_info, numfiles);
9971 dirs = XALLOCAVEC (struct dir_info, numfiles);
9972
9973 fnad.files = files;
9974 fnad.used_files = 0;
9975 fnad.max_files = numfiles;
9976 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9977 gcc_assert (fnad.used_files == fnad.max_files);
9978
9979 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9980
9981 /* Find all the different directories used. */
9982 dirs[0].path = files[0].path;
9983 dirs[0].length = files[0].fname - files[0].path;
9984 dirs[0].prefix = -1;
9985 dirs[0].count = 1;
9986 dirs[0].dir_idx = 0;
9987 files[0].dir_idx = 0;
9988 ndirs = 1;
9989
9990 for (i = 1; i < numfiles; i++)
9991 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9992 && memcmp (dirs[ndirs - 1].path, files[i].path,
9993 dirs[ndirs - 1].length) == 0)
9994 {
9995 /* Same directory as last entry. */
9996 files[i].dir_idx = ndirs - 1;
9997 ++dirs[ndirs - 1].count;
9998 }
9999 else
10000 {
10001 int j;
10002
10003 /* This is a new directory. */
10004 dirs[ndirs].path = files[i].path;
10005 dirs[ndirs].length = files[i].fname - files[i].path;
10006 dirs[ndirs].count = 1;
10007 dirs[ndirs].dir_idx = ndirs;
10008 files[i].dir_idx = ndirs;
10009
10010 /* Search for a prefix. */
10011 dirs[ndirs].prefix = -1;
10012 for (j = 0; j < ndirs; j++)
10013 if (dirs[j].length < dirs[ndirs].length
10014 && dirs[j].length > 1
10015 && (dirs[ndirs].prefix == -1
10016 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10017 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10018 dirs[ndirs].prefix = j;
10019
10020 ++ndirs;
10021 }
10022
10023 /* Now to the actual work. We have to find a subset of the directories which
10024 allow expressing the file name using references to the directory table
10025 with the least amount of characters. We do not do an exhaustive search
10026 where we would have to check out every combination of every single
10027 possible prefix. Instead we use a heuristic which provides nearly optimal
10028 results in most cases and never is much off. */
10029 saved = XALLOCAVEC (int, ndirs);
10030 savehere = XALLOCAVEC (int, ndirs);
10031
10032 memset (saved, '\0', ndirs * sizeof (saved[0]));
10033 for (i = 0; i < ndirs; i++)
10034 {
10035 int j;
10036 int total;
10037
10038 /* We can always save some space for the current directory. But this
10039 does not mean it will be enough to justify adding the directory. */
10040 savehere[i] = dirs[i].length;
10041 total = (savehere[i] - saved[i]) * dirs[i].count;
10042
10043 for (j = i + 1; j < ndirs; j++)
10044 {
10045 savehere[j] = 0;
10046 if (saved[j] < dirs[i].length)
10047 {
10048 /* Determine whether the dirs[i] path is a prefix of the
10049 dirs[j] path. */
10050 int k;
10051
10052 k = dirs[j].prefix;
10053 while (k != -1 && k != (int) i)
10054 k = dirs[k].prefix;
10055
10056 if (k == (int) i)
10057 {
10058 /* Yes it is. We can possibly save some memory by
10059 writing the filenames in dirs[j] relative to
10060 dirs[i]. */
10061 savehere[j] = dirs[i].length;
10062 total += (savehere[j] - saved[j]) * dirs[j].count;
10063 }
10064 }
10065 }
10066
10067 /* Check whether we can save enough to justify adding the dirs[i]
10068 directory. */
10069 if (total > dirs[i].length + 1)
10070 {
10071 /* It's worthwhile adding. */
10072 for (j = i; j < ndirs; j++)
10073 if (savehere[j] > 0)
10074 {
10075 /* Remember how much we saved for this directory so far. */
10076 saved[j] = savehere[j];
10077
10078 /* Remember the prefix directory. */
10079 dirs[j].dir_idx = i;
10080 }
10081 }
10082 }
10083
10084 /* Emit the directory name table. */
10085 idx_offset = dirs[0].length > 0 ? 1 : 0;
10086 for (i = 1 - idx_offset; i < ndirs; i++)
10087 dw2_asm_output_nstring (dirs[i].path,
10088 dirs[i].length
10089 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10090 "Directory Entry: %#x", i + idx_offset);
10091
10092 dw2_asm_output_data (1, 0, "End directory table");
10093
10094 /* We have to emit them in the order of emitted_number since that's
10095 used in the debug info generation. To do this efficiently we
10096 generate a back-mapping of the indices first. */
10097 backmap = XALLOCAVEC (int, numfiles);
10098 for (i = 0; i < numfiles; i++)
10099 backmap[files[i].file_idx->emitted_number - 1] = i;
10100
10101 /* Now write all the file names. */
10102 for (i = 0; i < numfiles; i++)
10103 {
10104 int file_idx = backmap[i];
10105 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10106
10107 #ifdef VMS_DEBUGGING_INFO
10108 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10109
10110 /* Setting these fields can lead to debugger miscomparisons,
10111 but VMS Debug requires them to be set correctly. */
10112
10113 int ver;
10114 long long cdt;
10115 long siz;
10116 int maxfilelen = strlen (files[file_idx].path)
10117 + dirs[dir_idx].length
10118 + MAX_VMS_VERSION_LEN + 1;
10119 char *filebuf = XALLOCAVEC (char, maxfilelen);
10120
10121 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10122 snprintf (filebuf, maxfilelen, "%s;%d",
10123 files[file_idx].path + dirs[dir_idx].length, ver);
10124
10125 dw2_asm_output_nstring
10126 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10127
10128 /* Include directory index. */
10129 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10130
10131 /* Modification time. */
10132 dw2_asm_output_data_uleb128
10133 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10134 ? cdt : 0,
10135 NULL);
10136
10137 /* File length in bytes. */
10138 dw2_asm_output_data_uleb128
10139 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10140 ? siz : 0,
10141 NULL);
10142 #else
10143 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10144 "File Entry: %#x", (unsigned) i + 1);
10145
10146 /* Include directory index. */
10147 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10148
10149 /* Modification time. */
10150 dw2_asm_output_data_uleb128 (0, NULL);
10151
10152 /* File length in bytes. */
10153 dw2_asm_output_data_uleb128 (0, NULL);
10154 #endif /* VMS_DEBUGGING_INFO */
10155 }
10156
10157 dw2_asm_output_data (1, 0, "End file name table");
10158 }
10159
10160
10161 /* Output one line number table into the .debug_line section. */
10162
10163 static void
10164 output_one_line_info_table (dw_line_info_table *table)
10165 {
10166 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10167 unsigned int current_line = 1;
10168 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10169 dw_line_info_entry *ent;
10170 size_t i;
10171
10172 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10173 {
10174 switch (ent->opcode)
10175 {
10176 case LI_set_address:
10177 /* ??? Unfortunately, we have little choice here currently, and
10178 must always use the most general form. GCC does not know the
10179 address delta itself, so we can't use DW_LNS_advance_pc. Many
10180 ports do have length attributes which will give an upper bound
10181 on the address range. We could perhaps use length attributes
10182 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10183 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10184
10185 /* This can handle any delta. This takes
10186 4+DWARF2_ADDR_SIZE bytes. */
10187 dw2_asm_output_data (1, 0, "set address %s", line_label);
10188 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10189 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10190 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10191 break;
10192
10193 case LI_set_line:
10194 if (ent->val == current_line)
10195 {
10196 /* We still need to start a new row, so output a copy insn. */
10197 dw2_asm_output_data (1, DW_LNS_copy,
10198 "copy line %u", current_line);
10199 }
10200 else
10201 {
10202 int line_offset = ent->val - current_line;
10203 int line_delta = line_offset - DWARF_LINE_BASE;
10204
10205 current_line = ent->val;
10206 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10207 {
10208 /* This can handle deltas from -10 to 234, using the current
10209 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10210 This takes 1 byte. */
10211 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10212 "line %u", current_line);
10213 }
10214 else
10215 {
10216 /* This can handle any delta. This takes at least 4 bytes,
10217 depending on the value being encoded. */
10218 dw2_asm_output_data (1, DW_LNS_advance_line,
10219 "advance to line %u", current_line);
10220 dw2_asm_output_data_sleb128 (line_offset, NULL);
10221 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10222 }
10223 }
10224 break;
10225
10226 case LI_set_file:
10227 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10228 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10229 break;
10230
10231 case LI_set_column:
10232 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10233 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10234 break;
10235
10236 case LI_negate_stmt:
10237 current_is_stmt = !current_is_stmt;
10238 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10239 "is_stmt %d", current_is_stmt);
10240 break;
10241
10242 case LI_set_prologue_end:
10243 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10244 "set prologue end");
10245 break;
10246
10247 case LI_set_epilogue_begin:
10248 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10249 "set epilogue begin");
10250 break;
10251
10252 case LI_set_discriminator:
10253 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10254 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10255 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10256 dw2_asm_output_data_uleb128 (ent->val, NULL);
10257 break;
10258 }
10259 }
10260
10261 /* Emit debug info for the address of the end of the table. */
10262 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10263 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10264 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10265 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10266
10267 dw2_asm_output_data (1, 0, "end sequence");
10268 dw2_asm_output_data_uleb128 (1, NULL);
10269 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10270 }
10271
10272 /* Output the source line number correspondence information. This
10273 information goes into the .debug_line section. */
10274
10275 static void
10276 output_line_info (bool prologue_only)
10277 {
10278 char l1[20], l2[20], p1[20], p2[20];
10279 /* We don't support DWARFv5 line tables yet. */
10280 int ver = dwarf_version < 5 ? dwarf_version : 4;
10281 bool saw_one = false;
10282 int opc;
10283
10284 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10285 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10286 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10287 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10288
10289 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10290 dw2_asm_output_data (4, 0xffffffff,
10291 "Initial length escape value indicating 64-bit DWARF extension");
10292 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10293 "Length of Source Line Info");
10294 ASM_OUTPUT_LABEL (asm_out_file, l1);
10295
10296 dw2_asm_output_data (2, ver, "DWARF Version");
10297 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10298 ASM_OUTPUT_LABEL (asm_out_file, p1);
10299
10300 /* Define the architecture-dependent minimum instruction length (in bytes).
10301 In this implementation of DWARF, this field is used for information
10302 purposes only. Since GCC generates assembly language, we have no
10303 a priori knowledge of how many instruction bytes are generated for each
10304 source line, and therefore can use only the DW_LNE_set_address and
10305 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10306 this as '1', which is "correct enough" for all architectures,
10307 and don't let the target override. */
10308 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10309
10310 if (ver >= 4)
10311 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10312 "Maximum Operations Per Instruction");
10313 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10314 "Default is_stmt_start flag");
10315 dw2_asm_output_data (1, DWARF_LINE_BASE,
10316 "Line Base Value (Special Opcodes)");
10317 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10318 "Line Range Value (Special Opcodes)");
10319 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10320 "Special Opcode Base");
10321
10322 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10323 {
10324 int n_op_args;
10325 switch (opc)
10326 {
10327 case DW_LNS_advance_pc:
10328 case DW_LNS_advance_line:
10329 case DW_LNS_set_file:
10330 case DW_LNS_set_column:
10331 case DW_LNS_fixed_advance_pc:
10332 case DW_LNS_set_isa:
10333 n_op_args = 1;
10334 break;
10335 default:
10336 n_op_args = 0;
10337 break;
10338 }
10339
10340 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10341 opc, n_op_args);
10342 }
10343
10344 /* Write out the information about the files we use. */
10345 output_file_names ();
10346 ASM_OUTPUT_LABEL (asm_out_file, p2);
10347 if (prologue_only)
10348 {
10349 /* Output the marker for the end of the line number info. */
10350 ASM_OUTPUT_LABEL (asm_out_file, l2);
10351 return;
10352 }
10353
10354 if (separate_line_info)
10355 {
10356 dw_line_info_table *table;
10357 size_t i;
10358
10359 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10360 if (table->in_use)
10361 {
10362 output_one_line_info_table (table);
10363 saw_one = true;
10364 }
10365 }
10366 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10367 {
10368 output_one_line_info_table (cold_text_section_line_info);
10369 saw_one = true;
10370 }
10371
10372 /* ??? Some Darwin linkers crash on a .debug_line section with no
10373 sequences. Further, merely a DW_LNE_end_sequence entry is not
10374 sufficient -- the address column must also be initialized.
10375 Make sure to output at least one set_address/end_sequence pair,
10376 choosing .text since that section is always present. */
10377 if (text_section_line_info->in_use || !saw_one)
10378 output_one_line_info_table (text_section_line_info);
10379
10380 /* Output the marker for the end of the line number info. */
10381 ASM_OUTPUT_LABEL (asm_out_file, l2);
10382 }
10383 \f
10384 /* Given a pointer to a tree node for some base type, return a pointer to
10385 a DIE that describes the given type.
10386
10387 This routine must only be called for GCC type nodes that correspond to
10388 Dwarf base (fundamental) types. */
10389
10390 static dw_die_ref
10391 base_type_die (tree type)
10392 {
10393 dw_die_ref base_type_result;
10394 enum dwarf_type encoding;
10395
10396 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10397 return 0;
10398
10399 /* If this is a subtype that should not be emitted as a subrange type,
10400 use the base type. See subrange_type_for_debug_p. */
10401 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10402 type = TREE_TYPE (type);
10403
10404 switch (TREE_CODE (type))
10405 {
10406 case INTEGER_TYPE:
10407 if ((dwarf_version >= 4 || !dwarf_strict)
10408 && TYPE_NAME (type)
10409 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10410 && DECL_IS_BUILTIN (TYPE_NAME (type))
10411 && DECL_NAME (TYPE_NAME (type)))
10412 {
10413 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10414 if (strcmp (name, "char16_t") == 0
10415 || strcmp (name, "char32_t") == 0)
10416 {
10417 encoding = DW_ATE_UTF;
10418 break;
10419 }
10420 }
10421 if (TYPE_STRING_FLAG (type))
10422 {
10423 if (TYPE_UNSIGNED (type))
10424 encoding = DW_ATE_unsigned_char;
10425 else
10426 encoding = DW_ATE_signed_char;
10427 }
10428 else if (TYPE_UNSIGNED (type))
10429 encoding = DW_ATE_unsigned;
10430 else
10431 encoding = DW_ATE_signed;
10432 break;
10433
10434 case REAL_TYPE:
10435 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10436 {
10437 if (dwarf_version >= 3 || !dwarf_strict)
10438 encoding = DW_ATE_decimal_float;
10439 else
10440 encoding = DW_ATE_lo_user;
10441 }
10442 else
10443 encoding = DW_ATE_float;
10444 break;
10445
10446 case FIXED_POINT_TYPE:
10447 if (!(dwarf_version >= 3 || !dwarf_strict))
10448 encoding = DW_ATE_lo_user;
10449 else if (TYPE_UNSIGNED (type))
10450 encoding = DW_ATE_unsigned_fixed;
10451 else
10452 encoding = DW_ATE_signed_fixed;
10453 break;
10454
10455 /* Dwarf2 doesn't know anything about complex ints, so use
10456 a user defined type for it. */
10457 case COMPLEX_TYPE:
10458 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10459 encoding = DW_ATE_complex_float;
10460 else
10461 encoding = DW_ATE_lo_user;
10462 break;
10463
10464 case BOOLEAN_TYPE:
10465 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10466 encoding = DW_ATE_boolean;
10467 break;
10468
10469 default:
10470 /* No other TREE_CODEs are Dwarf fundamental types. */
10471 gcc_unreachable ();
10472 }
10473
10474 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10475
10476 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10477 int_size_in_bytes (type));
10478 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10479 add_pubtype (type, base_type_result);
10480
10481 return base_type_result;
10482 }
10483
10484 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10485 named 'auto' in its type: return true for it, false otherwise. */
10486
10487 static inline bool
10488 is_cxx_auto (tree type)
10489 {
10490 if (is_cxx ())
10491 {
10492 tree name = TYPE_IDENTIFIER (type);
10493 if (name == get_identifier ("auto")
10494 || name == get_identifier ("decltype(auto)"))
10495 return true;
10496 }
10497 return false;
10498 }
10499
10500 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10501 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10502
10503 static inline int
10504 is_base_type (tree type)
10505 {
10506 switch (TREE_CODE (type))
10507 {
10508 case ERROR_MARK:
10509 case VOID_TYPE:
10510 case INTEGER_TYPE:
10511 case REAL_TYPE:
10512 case FIXED_POINT_TYPE:
10513 case COMPLEX_TYPE:
10514 case BOOLEAN_TYPE:
10515 case POINTER_BOUNDS_TYPE:
10516 return 1;
10517
10518 case ARRAY_TYPE:
10519 case RECORD_TYPE:
10520 case UNION_TYPE:
10521 case QUAL_UNION_TYPE:
10522 case ENUMERAL_TYPE:
10523 case FUNCTION_TYPE:
10524 case METHOD_TYPE:
10525 case POINTER_TYPE:
10526 case REFERENCE_TYPE:
10527 case NULLPTR_TYPE:
10528 case OFFSET_TYPE:
10529 case LANG_TYPE:
10530 case VECTOR_TYPE:
10531 return 0;
10532
10533 default:
10534 if (is_cxx_auto (type))
10535 return 0;
10536 gcc_unreachable ();
10537 }
10538
10539 return 0;
10540 }
10541
10542 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10543 node, return the size in bits for the type if it is a constant, or else
10544 return the alignment for the type if the type's size is not constant, or
10545 else return BITS_PER_WORD if the type actually turns out to be an
10546 ERROR_MARK node. */
10547
10548 static inline unsigned HOST_WIDE_INT
10549 simple_type_size_in_bits (const_tree type)
10550 {
10551 if (TREE_CODE (type) == ERROR_MARK)
10552 return BITS_PER_WORD;
10553 else if (TYPE_SIZE (type) == NULL_TREE)
10554 return 0;
10555 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10556 return tree_to_uhwi (TYPE_SIZE (type));
10557 else
10558 return TYPE_ALIGN (type);
10559 }
10560
10561 /* Similarly, but return an offset_int instead of UHWI. */
10562
10563 static inline offset_int
10564 offset_int_type_size_in_bits (const_tree type)
10565 {
10566 if (TREE_CODE (type) == ERROR_MARK)
10567 return BITS_PER_WORD;
10568 else if (TYPE_SIZE (type) == NULL_TREE)
10569 return 0;
10570 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10571 return wi::to_offset (TYPE_SIZE (type));
10572 else
10573 return TYPE_ALIGN (type);
10574 }
10575
10576 /* Given a pointer to a tree node for a subrange type, return a pointer
10577 to a DIE that describes the given type. */
10578
10579 static dw_die_ref
10580 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10581 {
10582 dw_die_ref subrange_die;
10583 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10584
10585 if (context_die == NULL)
10586 context_die = comp_unit_die ();
10587
10588 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10589
10590 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10591 {
10592 /* The size of the subrange type and its base type do not match,
10593 so we need to generate a size attribute for the subrange type. */
10594 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10595 }
10596
10597 if (low)
10598 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10599 if (high)
10600 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10601
10602 return subrange_die;
10603 }
10604
10605 /* Returns the (const and/or volatile) cv_qualifiers associated with
10606 the decl node. This will normally be augmented with the
10607 cv_qualifiers of the underlying type in add_type_attribute. */
10608
10609 static int
10610 decl_quals (const_tree decl)
10611 {
10612 return ((TREE_READONLY (decl)
10613 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10614 | (TREE_THIS_VOLATILE (decl)
10615 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10616 }
10617
10618 /* Determine the TYPE whose qualifiers match the largest strict subset
10619 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10620 qualifiers outside QUAL_MASK. */
10621
10622 static int
10623 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10624 {
10625 tree t;
10626 int best_rank = 0, best_qual = 0, max_rank;
10627
10628 type_quals &= qual_mask;
10629 max_rank = popcount_hwi (type_quals) - 1;
10630
10631 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10632 t = TYPE_NEXT_VARIANT (t))
10633 {
10634 int q = TYPE_QUALS (t) & qual_mask;
10635
10636 if ((q & type_quals) == q && q != type_quals
10637 && check_base_type (t, type))
10638 {
10639 int rank = popcount_hwi (q);
10640
10641 if (rank > best_rank)
10642 {
10643 best_rank = rank;
10644 best_qual = q;
10645 }
10646 }
10647 }
10648
10649 return best_qual;
10650 }
10651
10652 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10653 entry that chains various modifiers in front of the given type. */
10654
10655 static dw_die_ref
10656 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10657 {
10658 enum tree_code code = TREE_CODE (type);
10659 dw_die_ref mod_type_die;
10660 dw_die_ref sub_die = NULL;
10661 tree item_type = NULL;
10662 tree qualified_type;
10663 tree name, low, high;
10664 dw_die_ref mod_scope;
10665 /* Only these cv-qualifiers are currently handled. */
10666 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10667 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10668
10669 if (code == ERROR_MARK)
10670 return NULL;
10671
10672 cv_quals &= cv_qual_mask;
10673
10674 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10675 tag modifier (and not an attribute) old consumers won't be able
10676 to handle it. */
10677 if (dwarf_version < 3)
10678 cv_quals &= ~TYPE_QUAL_RESTRICT;
10679
10680 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10681 if (dwarf_version < 5)
10682 cv_quals &= ~TYPE_QUAL_ATOMIC;
10683
10684 /* See if we already have the appropriately qualified variant of
10685 this type. */
10686 qualified_type = get_qualified_type (type, cv_quals);
10687
10688 if (qualified_type == sizetype
10689 && TYPE_NAME (qualified_type)
10690 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10691 {
10692 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10693
10694 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10695 && TYPE_PRECISION (t)
10696 == TYPE_PRECISION (qualified_type)
10697 && TYPE_UNSIGNED (t)
10698 == TYPE_UNSIGNED (qualified_type));
10699 qualified_type = t;
10700 }
10701
10702 /* If we do, then we can just use its DIE, if it exists. */
10703 if (qualified_type)
10704 {
10705 mod_type_die = lookup_type_die (qualified_type);
10706 if (mod_type_die)
10707 return mod_type_die;
10708 }
10709
10710 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10711
10712 /* Handle C typedef types. */
10713 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10714 && !DECL_ARTIFICIAL (name))
10715 {
10716 tree dtype = TREE_TYPE (name);
10717
10718 if (qualified_type == dtype)
10719 {
10720 /* For a named type, use the typedef. */
10721 gen_type_die (qualified_type, context_die);
10722 return lookup_type_die (qualified_type);
10723 }
10724 else
10725 {
10726 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10727 dquals &= cv_qual_mask;
10728 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10729 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10730 /* cv-unqualified version of named type. Just use
10731 the unnamed type to which it refers. */
10732 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10733 cv_quals, context_die);
10734 /* Else cv-qualified version of named type; fall through. */
10735 }
10736 }
10737
10738 mod_scope = scope_die_for (type, context_die);
10739
10740 if (cv_quals)
10741 {
10742 struct qual_info { int q; enum dwarf_tag t; };
10743 static const struct qual_info qual_info[] =
10744 {
10745 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10746 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10747 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10748 { TYPE_QUAL_CONST, DW_TAG_const_type },
10749 };
10750 int sub_quals;
10751 unsigned i;
10752
10753 /* Determine a lesser qualified type that most closely matches
10754 this one. Then generate DW_TAG_* entries for the remaining
10755 qualifiers. */
10756 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10757 cv_qual_mask);
10758 mod_type_die = modified_type_die (type, sub_quals, context_die);
10759
10760 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10761 if (qual_info[i].q & cv_quals & ~sub_quals)
10762 {
10763 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10764 if (mod_type_die)
10765 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10766 mod_type_die = d;
10767 }
10768 }
10769 else if (code == POINTER_TYPE)
10770 {
10771 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10772 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10773 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10774 item_type = TREE_TYPE (type);
10775 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10776 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10777 TYPE_ADDR_SPACE (item_type));
10778 }
10779 else if (code == REFERENCE_TYPE)
10780 {
10781 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10782 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10783 type);
10784 else
10785 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10786 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10787 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10788 item_type = TREE_TYPE (type);
10789 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10790 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10791 TYPE_ADDR_SPACE (item_type));
10792 }
10793 else if (code == INTEGER_TYPE
10794 && TREE_TYPE (type) != NULL_TREE
10795 && subrange_type_for_debug_p (type, &low, &high))
10796 {
10797 mod_type_die = subrange_type_die (type, low, high, context_die);
10798 item_type = TREE_TYPE (type);
10799 }
10800 else if (is_base_type (type))
10801 mod_type_die = base_type_die (type);
10802 else
10803 {
10804 gen_type_die (type, context_die);
10805
10806 /* We have to get the type_main_variant here (and pass that to the
10807 `lookup_type_die' routine) because the ..._TYPE node we have
10808 might simply be a *copy* of some original type node (where the
10809 copy was created to help us keep track of typedef names) and
10810 that copy might have a different TYPE_UID from the original
10811 ..._TYPE node. */
10812 if (TREE_CODE (type) != VECTOR_TYPE)
10813 return lookup_type_die (type_main_variant (type));
10814 else
10815 /* Vectors have the debugging information in the type,
10816 not the main variant. */
10817 return lookup_type_die (type);
10818 }
10819
10820 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10821 don't output a DW_TAG_typedef, since there isn't one in the
10822 user's program; just attach a DW_AT_name to the type.
10823 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10824 if the base type already has the same name. */
10825 if (name
10826 && ((TREE_CODE (name) != TYPE_DECL
10827 && (qualified_type == TYPE_MAIN_VARIANT (type)
10828 || (cv_quals == TYPE_UNQUALIFIED)))
10829 || (TREE_CODE (name) == TYPE_DECL
10830 && TREE_TYPE (name) == qualified_type
10831 && DECL_NAME (name))))
10832 {
10833 if (TREE_CODE (name) == TYPE_DECL)
10834 /* Could just call add_name_and_src_coords_attributes here,
10835 but since this is a builtin type it doesn't have any
10836 useful source coordinates anyway. */
10837 name = DECL_NAME (name);
10838 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10839 }
10840 /* This probably indicates a bug. */
10841 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10842 {
10843 name = TYPE_IDENTIFIER (type);
10844 add_name_attribute (mod_type_die,
10845 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10846 }
10847
10848 if (qualified_type)
10849 equate_type_number_to_die (qualified_type, mod_type_die);
10850
10851 if (item_type)
10852 /* We must do this after the equate_type_number_to_die call, in case
10853 this is a recursive type. This ensures that the modified_type_die
10854 recursion will terminate even if the type is recursive. Recursive
10855 types are possible in Ada. */
10856 sub_die = modified_type_die (item_type,
10857 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10858 context_die);
10859
10860 if (sub_die != NULL)
10861 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10862
10863 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10864 if (TYPE_ARTIFICIAL (type))
10865 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10866
10867 return mod_type_die;
10868 }
10869
10870 /* Generate DIEs for the generic parameters of T.
10871 T must be either a generic type or a generic function.
10872 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10873
10874 static void
10875 gen_generic_params_dies (tree t)
10876 {
10877 tree parms, args;
10878 int parms_num, i;
10879 dw_die_ref die = NULL;
10880 int non_default;
10881
10882 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10883 return;
10884
10885 if (TYPE_P (t))
10886 die = lookup_type_die (t);
10887 else if (DECL_P (t))
10888 die = lookup_decl_die (t);
10889
10890 gcc_assert (die);
10891
10892 parms = lang_hooks.get_innermost_generic_parms (t);
10893 if (!parms)
10894 /* T has no generic parameter. It means T is neither a generic type
10895 or function. End of story. */
10896 return;
10897
10898 parms_num = TREE_VEC_LENGTH (parms);
10899 args = lang_hooks.get_innermost_generic_args (t);
10900 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10901 non_default = int_cst_value (TREE_CHAIN (args));
10902 else
10903 non_default = TREE_VEC_LENGTH (args);
10904 for (i = 0; i < parms_num; i++)
10905 {
10906 tree parm, arg, arg_pack_elems;
10907 dw_die_ref parm_die;
10908
10909 parm = TREE_VEC_ELT (parms, i);
10910 arg = TREE_VEC_ELT (args, i);
10911 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10912 gcc_assert (parm && TREE_VALUE (parm) && arg);
10913
10914 if (parm && TREE_VALUE (parm) && arg)
10915 {
10916 /* If PARM represents a template parameter pack,
10917 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10918 by DW_TAG_template_*_parameter DIEs for the argument
10919 pack elements of ARG. Note that ARG would then be
10920 an argument pack. */
10921 if (arg_pack_elems)
10922 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10923 arg_pack_elems,
10924 die);
10925 else
10926 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10927 true /* emit name */, die);
10928 if (i >= non_default)
10929 add_AT_flag (parm_die, DW_AT_default_value, 1);
10930 }
10931 }
10932 }
10933
10934 /* Create and return a DIE for PARM which should be
10935 the representation of a generic type parameter.
10936 For instance, in the C++ front end, PARM would be a template parameter.
10937 ARG is the argument to PARM.
10938 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10939 name of the PARM.
10940 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10941 as a child node. */
10942
10943 static dw_die_ref
10944 generic_parameter_die (tree parm, tree arg,
10945 bool emit_name_p,
10946 dw_die_ref parent_die)
10947 {
10948 dw_die_ref tmpl_die = NULL;
10949 const char *name = NULL;
10950
10951 if (!parm || !DECL_NAME (parm) || !arg)
10952 return NULL;
10953
10954 /* We support non-type generic parameters and arguments,
10955 type generic parameters and arguments, as well as
10956 generic generic parameters (a.k.a. template template parameters in C++)
10957 and arguments. */
10958 if (TREE_CODE (parm) == PARM_DECL)
10959 /* PARM is a nontype generic parameter */
10960 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10961 else if (TREE_CODE (parm) == TYPE_DECL)
10962 /* PARM is a type generic parameter. */
10963 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10964 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10965 /* PARM is a generic generic parameter.
10966 Its DIE is a GNU extension. It shall have a
10967 DW_AT_name attribute to represent the name of the template template
10968 parameter, and a DW_AT_GNU_template_name attribute to represent the
10969 name of the template template argument. */
10970 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10971 parent_die, parm);
10972 else
10973 gcc_unreachable ();
10974
10975 if (tmpl_die)
10976 {
10977 tree tmpl_type;
10978
10979 /* If PARM is a generic parameter pack, it means we are
10980 emitting debug info for a template argument pack element.
10981 In other terms, ARG is a template argument pack element.
10982 In that case, we don't emit any DW_AT_name attribute for
10983 the die. */
10984 if (emit_name_p)
10985 {
10986 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10987 gcc_assert (name);
10988 add_AT_string (tmpl_die, DW_AT_name, name);
10989 }
10990
10991 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10992 {
10993 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10994 TMPL_DIE should have a child DW_AT_type attribute that is set
10995 to the type of the argument to PARM, which is ARG.
10996 If PARM is a type generic parameter, TMPL_DIE should have a
10997 child DW_AT_type that is set to ARG. */
10998 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10999 add_type_attribute (tmpl_die, tmpl_type,
11000 (TREE_THIS_VOLATILE (tmpl_type)
11001 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11002 parent_die);
11003 }
11004 else
11005 {
11006 /* So TMPL_DIE is a DIE representing a
11007 a generic generic template parameter, a.k.a template template
11008 parameter in C++ and arg is a template. */
11009
11010 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11011 to the name of the argument. */
11012 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11013 if (name)
11014 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11015 }
11016
11017 if (TREE_CODE (parm) == PARM_DECL)
11018 /* So PARM is a non-type generic parameter.
11019 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11020 attribute of TMPL_DIE which value represents the value
11021 of ARG.
11022 We must be careful here:
11023 The value of ARG might reference some function decls.
11024 We might currently be emitting debug info for a generic
11025 type and types are emitted before function decls, we don't
11026 know if the function decls referenced by ARG will actually be
11027 emitted after cgraph computations.
11028 So must defer the generation of the DW_AT_const_value to
11029 after cgraph is ready. */
11030 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11031 }
11032
11033 return tmpl_die;
11034 }
11035
11036 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11037 PARM_PACK must be a template parameter pack. The returned DIE
11038 will be child DIE of PARENT_DIE. */
11039
11040 static dw_die_ref
11041 template_parameter_pack_die (tree parm_pack,
11042 tree parm_pack_args,
11043 dw_die_ref parent_die)
11044 {
11045 dw_die_ref die;
11046 int j;
11047
11048 gcc_assert (parent_die && parm_pack);
11049
11050 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11051 add_name_and_src_coords_attributes (die, parm_pack);
11052 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11053 generic_parameter_die (parm_pack,
11054 TREE_VEC_ELT (parm_pack_args, j),
11055 false /* Don't emit DW_AT_name */,
11056 die);
11057 return die;
11058 }
11059
11060 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11061 an enumerated type. */
11062
11063 static inline int
11064 type_is_enum (const_tree type)
11065 {
11066 return TREE_CODE (type) == ENUMERAL_TYPE;
11067 }
11068
11069 /* Return the DBX register number described by a given RTL node. */
11070
11071 static unsigned int
11072 dbx_reg_number (const_rtx rtl)
11073 {
11074 unsigned regno = REGNO (rtl);
11075
11076 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11077
11078 #ifdef LEAF_REG_REMAP
11079 if (crtl->uses_only_leaf_regs)
11080 {
11081 int leaf_reg = LEAF_REG_REMAP (regno);
11082 if (leaf_reg != -1)
11083 regno = (unsigned) leaf_reg;
11084 }
11085 #endif
11086
11087 regno = DBX_REGISTER_NUMBER (regno);
11088 gcc_assert (regno != INVALID_REGNUM);
11089 return regno;
11090 }
11091
11092 /* Optionally add a DW_OP_piece term to a location description expression.
11093 DW_OP_piece is only added if the location description expression already
11094 doesn't end with DW_OP_piece. */
11095
11096 static void
11097 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11098 {
11099 dw_loc_descr_ref loc;
11100
11101 if (*list_head != NULL)
11102 {
11103 /* Find the end of the chain. */
11104 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11105 ;
11106
11107 if (loc->dw_loc_opc != DW_OP_piece)
11108 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11109 }
11110 }
11111
11112 /* Return a location descriptor that designates a machine register or
11113 zero if there is none. */
11114
11115 static dw_loc_descr_ref
11116 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11117 {
11118 rtx regs;
11119
11120 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11121 return 0;
11122
11123 /* We only use "frame base" when we're sure we're talking about the
11124 post-prologue local stack frame. We do this by *not* running
11125 register elimination until this point, and recognizing the special
11126 argument pointer and soft frame pointer rtx's.
11127 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11128 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11129 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11130 {
11131 dw_loc_descr_ref result = NULL;
11132
11133 if (dwarf_version >= 4 || !dwarf_strict)
11134 {
11135 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11136 initialized);
11137 if (result)
11138 add_loc_descr (&result,
11139 new_loc_descr (DW_OP_stack_value, 0, 0));
11140 }
11141 return result;
11142 }
11143
11144 regs = targetm.dwarf_register_span (rtl);
11145
11146 if (REG_NREGS (rtl) > 1 || regs)
11147 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11148 else
11149 {
11150 unsigned int dbx_regnum = dbx_reg_number (rtl);
11151 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11152 return 0;
11153 return one_reg_loc_descriptor (dbx_regnum, initialized);
11154 }
11155 }
11156
11157 /* Return a location descriptor that designates a machine register for
11158 a given hard register number. */
11159
11160 static dw_loc_descr_ref
11161 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11162 {
11163 dw_loc_descr_ref reg_loc_descr;
11164
11165 if (regno <= 31)
11166 reg_loc_descr
11167 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11168 else
11169 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11170
11171 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11172 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11173
11174 return reg_loc_descr;
11175 }
11176
11177 /* Given an RTL of a register, return a location descriptor that
11178 designates a value that spans more than one register. */
11179
11180 static dw_loc_descr_ref
11181 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11182 enum var_init_status initialized)
11183 {
11184 int size, i;
11185 dw_loc_descr_ref loc_result = NULL;
11186
11187 /* Simple, contiguous registers. */
11188 if (regs == NULL_RTX)
11189 {
11190 unsigned reg = REGNO (rtl);
11191 int nregs;
11192
11193 #ifdef LEAF_REG_REMAP
11194 if (crtl->uses_only_leaf_regs)
11195 {
11196 int leaf_reg = LEAF_REG_REMAP (reg);
11197 if (leaf_reg != -1)
11198 reg = (unsigned) leaf_reg;
11199 }
11200 #endif
11201
11202 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11203 nregs = REG_NREGS (rtl);
11204
11205 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11206
11207 loc_result = NULL;
11208 while (nregs--)
11209 {
11210 dw_loc_descr_ref t;
11211
11212 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11213 VAR_INIT_STATUS_INITIALIZED);
11214 add_loc_descr (&loc_result, t);
11215 add_loc_descr_op_piece (&loc_result, size);
11216 ++reg;
11217 }
11218 return loc_result;
11219 }
11220
11221 /* Now onto stupid register sets in non contiguous locations. */
11222
11223 gcc_assert (GET_CODE (regs) == PARALLEL);
11224
11225 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11226 loc_result = NULL;
11227
11228 for (i = 0; i < XVECLEN (regs, 0); ++i)
11229 {
11230 dw_loc_descr_ref t;
11231
11232 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11233 VAR_INIT_STATUS_INITIALIZED);
11234 add_loc_descr (&loc_result, t);
11235 add_loc_descr_op_piece (&loc_result, size);
11236 }
11237
11238 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11239 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11240 return loc_result;
11241 }
11242
11243 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11244
11245 /* Return a location descriptor that designates a constant i,
11246 as a compound operation from constant (i >> shift), constant shift
11247 and DW_OP_shl. */
11248
11249 static dw_loc_descr_ref
11250 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11251 {
11252 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11253 add_loc_descr (&ret, int_loc_descriptor (shift));
11254 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11255 return ret;
11256 }
11257
11258 /* Return a location descriptor that designates a constant. */
11259
11260 static dw_loc_descr_ref
11261 int_loc_descriptor (HOST_WIDE_INT i)
11262 {
11263 enum dwarf_location_atom op;
11264
11265 /* Pick the smallest representation of a constant, rather than just
11266 defaulting to the LEB encoding. */
11267 if (i >= 0)
11268 {
11269 int clz = clz_hwi (i);
11270 int ctz = ctz_hwi (i);
11271 if (i <= 31)
11272 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11273 else if (i <= 0xff)
11274 op = DW_OP_const1u;
11275 else if (i <= 0xffff)
11276 op = DW_OP_const2u;
11277 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11278 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11279 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11280 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11281 while DW_OP_const4u is 5 bytes. */
11282 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11283 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11284 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11285 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11286 while DW_OP_const4u is 5 bytes. */
11287 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11288 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11289 op = DW_OP_const4u;
11290 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11291 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11292 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11293 while DW_OP_constu of constant >= 0x100000000 takes at least
11294 6 bytes. */
11295 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11296 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11297 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11298 >= HOST_BITS_PER_WIDE_INT)
11299 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11300 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11301 while DW_OP_constu takes in this case at least 6 bytes. */
11302 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11303 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11304 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11305 && size_of_uleb128 (i) > 6)
11306 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11307 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11308 else
11309 op = DW_OP_constu;
11310 }
11311 else
11312 {
11313 if (i >= -0x80)
11314 op = DW_OP_const1s;
11315 else if (i >= -0x8000)
11316 op = DW_OP_const2s;
11317 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11318 {
11319 if (size_of_int_loc_descriptor (i) < 5)
11320 {
11321 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11322 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11323 return ret;
11324 }
11325 op = DW_OP_const4s;
11326 }
11327 else
11328 {
11329 if (size_of_int_loc_descriptor (i)
11330 < (unsigned long) 1 + size_of_sleb128 (i))
11331 {
11332 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11333 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11334 return ret;
11335 }
11336 op = DW_OP_consts;
11337 }
11338 }
11339
11340 return new_loc_descr (op, i, 0);
11341 }
11342
11343 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11344 without actually allocating it. */
11345
11346 static unsigned long
11347 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11348 {
11349 return size_of_int_loc_descriptor (i >> shift)
11350 + size_of_int_loc_descriptor (shift)
11351 + 1;
11352 }
11353
11354 /* Return size_of_locs (int_loc_descriptor (i)) without
11355 actually allocating it. */
11356
11357 static unsigned long
11358 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11359 {
11360 unsigned long s;
11361
11362 if (i >= 0)
11363 {
11364 int clz, ctz;
11365 if (i <= 31)
11366 return 1;
11367 else if (i <= 0xff)
11368 return 2;
11369 else if (i <= 0xffff)
11370 return 3;
11371 clz = clz_hwi (i);
11372 ctz = ctz_hwi (i);
11373 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11374 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11375 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11376 - clz - 5);
11377 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11378 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11379 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11380 - clz - 8);
11381 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11382 return 5;
11383 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11384 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11385 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11386 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11387 - clz - 8);
11388 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11389 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11390 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11391 - clz - 16);
11392 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11393 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11394 && s > 6)
11395 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11396 - clz - 32);
11397 else
11398 return 1 + s;
11399 }
11400 else
11401 {
11402 if (i >= -0x80)
11403 return 2;
11404 else if (i >= -0x8000)
11405 return 3;
11406 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11407 {
11408 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11409 {
11410 s = size_of_int_loc_descriptor (-i) + 1;
11411 if (s < 5)
11412 return s;
11413 }
11414 return 5;
11415 }
11416 else
11417 {
11418 unsigned long r = 1 + size_of_sleb128 (i);
11419 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11420 {
11421 s = size_of_int_loc_descriptor (-i) + 1;
11422 if (s < r)
11423 return s;
11424 }
11425 return r;
11426 }
11427 }
11428 }
11429
11430 /* Return loc description representing "address" of integer value.
11431 This can appear only as toplevel expression. */
11432
11433 static dw_loc_descr_ref
11434 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11435 {
11436 int litsize;
11437 dw_loc_descr_ref loc_result = NULL;
11438
11439 if (!(dwarf_version >= 4 || !dwarf_strict))
11440 return NULL;
11441
11442 litsize = size_of_int_loc_descriptor (i);
11443 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11444 is more compact. For DW_OP_stack_value we need:
11445 litsize + 1 (DW_OP_stack_value)
11446 and for DW_OP_implicit_value:
11447 1 (DW_OP_implicit_value) + 1 (length) + size. */
11448 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11449 {
11450 loc_result = int_loc_descriptor (i);
11451 add_loc_descr (&loc_result,
11452 new_loc_descr (DW_OP_stack_value, 0, 0));
11453 return loc_result;
11454 }
11455
11456 loc_result = new_loc_descr (DW_OP_implicit_value,
11457 size, 0);
11458 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11459 loc_result->dw_loc_oprnd2.v.val_int = i;
11460 return loc_result;
11461 }
11462
11463 /* Return a location descriptor that designates a base+offset location. */
11464
11465 static dw_loc_descr_ref
11466 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11467 enum var_init_status initialized)
11468 {
11469 unsigned int regno;
11470 dw_loc_descr_ref result;
11471 dw_fde_ref fde = cfun->fde;
11472
11473 /* We only use "frame base" when we're sure we're talking about the
11474 post-prologue local stack frame. We do this by *not* running
11475 register elimination until this point, and recognizing the special
11476 argument pointer and soft frame pointer rtx's. */
11477 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11478 {
11479 rtx elim = (ira_use_lra_p
11480 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11481 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11482
11483 if (elim != reg)
11484 {
11485 if (GET_CODE (elim) == PLUS)
11486 {
11487 offset += INTVAL (XEXP (elim, 1));
11488 elim = XEXP (elim, 0);
11489 }
11490 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11491 && (elim == hard_frame_pointer_rtx
11492 || elim == stack_pointer_rtx))
11493 || elim == (frame_pointer_needed
11494 ? hard_frame_pointer_rtx
11495 : stack_pointer_rtx));
11496
11497 /* If drap register is used to align stack, use frame
11498 pointer + offset to access stack variables. If stack
11499 is aligned without drap, use stack pointer + offset to
11500 access stack variables. */
11501 if (crtl->stack_realign_tried
11502 && reg == frame_pointer_rtx)
11503 {
11504 int base_reg
11505 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11506 ? HARD_FRAME_POINTER_REGNUM
11507 : REGNO (elim));
11508 return new_reg_loc_descr (base_reg, offset);
11509 }
11510
11511 gcc_assert (frame_pointer_fb_offset_valid);
11512 offset += frame_pointer_fb_offset;
11513 return new_loc_descr (DW_OP_fbreg, offset, 0);
11514 }
11515 }
11516
11517 regno = REGNO (reg);
11518 #ifdef LEAF_REG_REMAP
11519 if (crtl->uses_only_leaf_regs)
11520 {
11521 int leaf_reg = LEAF_REG_REMAP (regno);
11522 if (leaf_reg != -1)
11523 regno = (unsigned) leaf_reg;
11524 }
11525 #endif
11526 regno = DWARF_FRAME_REGNUM (regno);
11527
11528 if (!optimize && fde
11529 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11530 {
11531 /* Use cfa+offset to represent the location of arguments passed
11532 on the stack when drap is used to align stack.
11533 Only do this when not optimizing, for optimized code var-tracking
11534 is supposed to track where the arguments live and the register
11535 used as vdrap or drap in some spot might be used for something
11536 else in other part of the routine. */
11537 return new_loc_descr (DW_OP_fbreg, offset, 0);
11538 }
11539
11540 if (regno <= 31)
11541 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11542 offset, 0);
11543 else
11544 result = new_loc_descr (DW_OP_bregx, regno, offset);
11545
11546 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11547 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11548
11549 return result;
11550 }
11551
11552 /* Return true if this RTL expression describes a base+offset calculation. */
11553
11554 static inline int
11555 is_based_loc (const_rtx rtl)
11556 {
11557 return (GET_CODE (rtl) == PLUS
11558 && ((REG_P (XEXP (rtl, 0))
11559 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11560 && CONST_INT_P (XEXP (rtl, 1)))));
11561 }
11562
11563 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11564 failed. */
11565
11566 static dw_loc_descr_ref
11567 tls_mem_loc_descriptor (rtx mem)
11568 {
11569 tree base;
11570 dw_loc_descr_ref loc_result;
11571
11572 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11573 return NULL;
11574
11575 base = get_base_address (MEM_EXPR (mem));
11576 if (base == NULL
11577 || TREE_CODE (base) != VAR_DECL
11578 || !DECL_THREAD_LOCAL_P (base))
11579 return NULL;
11580
11581 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11582 if (loc_result == NULL)
11583 return NULL;
11584
11585 if (MEM_OFFSET (mem))
11586 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11587
11588 return loc_result;
11589 }
11590
11591 /* Output debug info about reason why we failed to expand expression as dwarf
11592 expression. */
11593
11594 static void
11595 expansion_failed (tree expr, rtx rtl, char const *reason)
11596 {
11597 if (dump_file && (dump_flags & TDF_DETAILS))
11598 {
11599 fprintf (dump_file, "Failed to expand as dwarf: ");
11600 if (expr)
11601 print_generic_expr (dump_file, expr, dump_flags);
11602 if (rtl)
11603 {
11604 fprintf (dump_file, "\n");
11605 print_rtl (dump_file, rtl);
11606 }
11607 fprintf (dump_file, "\nReason: %s\n", reason);
11608 }
11609 }
11610
11611 /* Helper function for const_ok_for_output. */
11612
11613 static bool
11614 const_ok_for_output_1 (rtx rtl)
11615 {
11616 if (GET_CODE (rtl) == UNSPEC)
11617 {
11618 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11619 we can't express it in the debug info. */
11620 #ifdef ENABLE_CHECKING
11621 /* Don't complain about TLS UNSPECs, those are just too hard to
11622 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11623 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11624 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11625 if (XVECLEN (rtl, 0) == 0
11626 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11627 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11628 inform (current_function_decl
11629 ? DECL_SOURCE_LOCATION (current_function_decl)
11630 : UNKNOWN_LOCATION,
11631 #if NUM_UNSPEC_VALUES > 0
11632 "non-delegitimized UNSPEC %s (%d) found in variable location",
11633 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11634 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11635 XINT (rtl, 1));
11636 #else
11637 "non-delegitimized UNSPEC %d found in variable location",
11638 XINT (rtl, 1));
11639 #endif
11640 #endif
11641 expansion_failed (NULL_TREE, rtl,
11642 "UNSPEC hasn't been delegitimized.\n");
11643 return false;
11644 }
11645
11646 if (targetm.const_not_ok_for_debug_p (rtl))
11647 {
11648 expansion_failed (NULL_TREE, rtl,
11649 "Expression rejected for debug by the backend.\n");
11650 return false;
11651 }
11652
11653 /* FIXME: Refer to PR60655. It is possible for simplification
11654 of rtl expressions in var tracking to produce such expressions.
11655 We should really identify / validate expressions
11656 enclosed in CONST that can be handled by assemblers on various
11657 targets and only handle legitimate cases here. */
11658 if (GET_CODE (rtl) != SYMBOL_REF)
11659 {
11660 if (GET_CODE (rtl) == NOT)
11661 return false;
11662 return true;
11663 }
11664
11665 if (CONSTANT_POOL_ADDRESS_P (rtl))
11666 {
11667 bool marked;
11668 get_pool_constant_mark (rtl, &marked);
11669 /* If all references to this pool constant were optimized away,
11670 it was not output and thus we can't represent it. */
11671 if (!marked)
11672 {
11673 expansion_failed (NULL_TREE, rtl,
11674 "Constant was removed from constant pool.\n");
11675 return false;
11676 }
11677 }
11678
11679 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11680 return false;
11681
11682 /* Avoid references to external symbols in debug info, on several targets
11683 the linker might even refuse to link when linking a shared library,
11684 and in many other cases the relocations for .debug_info/.debug_loc are
11685 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11686 to be defined within the same shared library or executable are fine. */
11687 if (SYMBOL_REF_EXTERNAL_P (rtl))
11688 {
11689 tree decl = SYMBOL_REF_DECL (rtl);
11690
11691 if (decl == NULL || !targetm.binds_local_p (decl))
11692 {
11693 expansion_failed (NULL_TREE, rtl,
11694 "Symbol not defined in current TU.\n");
11695 return false;
11696 }
11697 }
11698
11699 return true;
11700 }
11701
11702 /* Return true if constant RTL can be emitted in DW_OP_addr or
11703 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11704 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11705
11706 static bool
11707 const_ok_for_output (rtx rtl)
11708 {
11709 if (GET_CODE (rtl) == SYMBOL_REF)
11710 return const_ok_for_output_1 (rtl);
11711
11712 if (GET_CODE (rtl) == CONST)
11713 {
11714 subrtx_var_iterator::array_type array;
11715 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11716 if (!const_ok_for_output_1 (*iter))
11717 return false;
11718 return true;
11719 }
11720
11721 return true;
11722 }
11723
11724 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11725 if possible, NULL otherwise. */
11726
11727 static dw_die_ref
11728 base_type_for_mode (machine_mode mode, bool unsignedp)
11729 {
11730 dw_die_ref type_die;
11731 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11732
11733 if (type == NULL)
11734 return NULL;
11735 switch (TREE_CODE (type))
11736 {
11737 case INTEGER_TYPE:
11738 case REAL_TYPE:
11739 break;
11740 default:
11741 return NULL;
11742 }
11743 type_die = lookup_type_die (type);
11744 if (!type_die)
11745 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11746 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11747 return NULL;
11748 return type_die;
11749 }
11750
11751 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11752 type matching MODE, or, if MODE is narrower than or as wide as
11753 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11754 possible. */
11755
11756 static dw_loc_descr_ref
11757 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11758 {
11759 machine_mode outer_mode = mode;
11760 dw_die_ref type_die;
11761 dw_loc_descr_ref cvt;
11762
11763 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11764 {
11765 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11766 return op;
11767 }
11768 type_die = base_type_for_mode (outer_mode, 1);
11769 if (type_die == NULL)
11770 return NULL;
11771 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11772 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11773 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11774 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11775 add_loc_descr (&op, cvt);
11776 return op;
11777 }
11778
11779 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11780
11781 static dw_loc_descr_ref
11782 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11783 dw_loc_descr_ref op1)
11784 {
11785 dw_loc_descr_ref ret = op0;
11786 add_loc_descr (&ret, op1);
11787 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11788 if (STORE_FLAG_VALUE != 1)
11789 {
11790 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11792 }
11793 return ret;
11794 }
11795
11796 /* Return location descriptor for signed comparison OP RTL. */
11797
11798 static dw_loc_descr_ref
11799 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11800 machine_mode mem_mode)
11801 {
11802 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11803 dw_loc_descr_ref op0, op1;
11804 int shift;
11805
11806 if (op_mode == VOIDmode)
11807 op_mode = GET_MODE (XEXP (rtl, 1));
11808 if (op_mode == VOIDmode)
11809 return NULL;
11810
11811 if (dwarf_strict
11812 && (GET_MODE_CLASS (op_mode) != MODE_INT
11813 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11814 return NULL;
11815
11816 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11817 VAR_INIT_STATUS_INITIALIZED);
11818 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11819 VAR_INIT_STATUS_INITIALIZED);
11820
11821 if (op0 == NULL || op1 == NULL)
11822 return NULL;
11823
11824 if (GET_MODE_CLASS (op_mode) != MODE_INT
11825 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11826 return compare_loc_descriptor (op, op0, op1);
11827
11828 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11829 {
11830 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11831 dw_loc_descr_ref cvt;
11832
11833 if (type_die == NULL)
11834 return NULL;
11835 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11836 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11837 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11838 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11839 add_loc_descr (&op0, cvt);
11840 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11841 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11842 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11843 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11844 add_loc_descr (&op1, cvt);
11845 return compare_loc_descriptor (op, op0, op1);
11846 }
11847
11848 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11849 /* For eq/ne, if the operands are known to be zero-extended,
11850 there is no need to do the fancy shifting up. */
11851 if (op == DW_OP_eq || op == DW_OP_ne)
11852 {
11853 dw_loc_descr_ref last0, last1;
11854 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11855 ;
11856 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11857 ;
11858 /* deref_size zero extends, and for constants we can check
11859 whether they are zero extended or not. */
11860 if (((last0->dw_loc_opc == DW_OP_deref_size
11861 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11862 || (CONST_INT_P (XEXP (rtl, 0))
11863 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11864 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11865 && ((last1->dw_loc_opc == DW_OP_deref_size
11866 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11867 || (CONST_INT_P (XEXP (rtl, 1))
11868 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11869 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11870 return compare_loc_descriptor (op, op0, op1);
11871
11872 /* EQ/NE comparison against constant in narrower type than
11873 DWARF2_ADDR_SIZE can be performed either as
11874 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11875 DW_OP_{eq,ne}
11876 or
11877 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11878 DW_OP_{eq,ne}. Pick whatever is shorter. */
11879 if (CONST_INT_P (XEXP (rtl, 1))
11880 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11881 && (size_of_int_loc_descriptor (shift) + 1
11882 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11883 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11884 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11885 & GET_MODE_MASK (op_mode))))
11886 {
11887 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11888 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11889 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11890 & GET_MODE_MASK (op_mode));
11891 return compare_loc_descriptor (op, op0, op1);
11892 }
11893 }
11894 add_loc_descr (&op0, int_loc_descriptor (shift));
11895 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11896 if (CONST_INT_P (XEXP (rtl, 1)))
11897 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11898 else
11899 {
11900 add_loc_descr (&op1, int_loc_descriptor (shift));
11901 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11902 }
11903 return compare_loc_descriptor (op, op0, op1);
11904 }
11905
11906 /* Return location descriptor for unsigned comparison OP RTL. */
11907
11908 static dw_loc_descr_ref
11909 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11910 machine_mode mem_mode)
11911 {
11912 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11913 dw_loc_descr_ref op0, op1;
11914
11915 if (op_mode == VOIDmode)
11916 op_mode = GET_MODE (XEXP (rtl, 1));
11917 if (op_mode == VOIDmode)
11918 return NULL;
11919 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11920 return NULL;
11921
11922 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11923 return NULL;
11924
11925 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11926 VAR_INIT_STATUS_INITIALIZED);
11927 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11928 VAR_INIT_STATUS_INITIALIZED);
11929
11930 if (op0 == NULL || op1 == NULL)
11931 return NULL;
11932
11933 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11934 {
11935 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11936 dw_loc_descr_ref last0, last1;
11937 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11938 ;
11939 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11940 ;
11941 if (CONST_INT_P (XEXP (rtl, 0)))
11942 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11943 /* deref_size zero extends, so no need to mask it again. */
11944 else if (last0->dw_loc_opc != DW_OP_deref_size
11945 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11946 {
11947 add_loc_descr (&op0, int_loc_descriptor (mask));
11948 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11949 }
11950 if (CONST_INT_P (XEXP (rtl, 1)))
11951 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11952 /* deref_size zero extends, so no need to mask it again. */
11953 else if (last1->dw_loc_opc != DW_OP_deref_size
11954 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11955 {
11956 add_loc_descr (&op1, int_loc_descriptor (mask));
11957 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11958 }
11959 }
11960 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11961 {
11962 HOST_WIDE_INT bias = 1;
11963 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11964 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11965 if (CONST_INT_P (XEXP (rtl, 1)))
11966 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11967 + INTVAL (XEXP (rtl, 1)));
11968 else
11969 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11970 bias, 0));
11971 }
11972 return compare_loc_descriptor (op, op0, op1);
11973 }
11974
11975 /* Return location descriptor for {U,S}{MIN,MAX}. */
11976
11977 static dw_loc_descr_ref
11978 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11979 machine_mode mem_mode)
11980 {
11981 enum dwarf_location_atom op;
11982 dw_loc_descr_ref op0, op1, ret;
11983 dw_loc_descr_ref bra_node, drop_node;
11984
11985 if (dwarf_strict
11986 && (GET_MODE_CLASS (mode) != MODE_INT
11987 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11988 return NULL;
11989
11990 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11991 VAR_INIT_STATUS_INITIALIZED);
11992 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11993 VAR_INIT_STATUS_INITIALIZED);
11994
11995 if (op0 == NULL || op1 == NULL)
11996 return NULL;
11997
11998 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11999 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12000 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12001 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12002 {
12003 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12004 {
12005 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12006 add_loc_descr (&op0, int_loc_descriptor (mask));
12007 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12008 add_loc_descr (&op1, int_loc_descriptor (mask));
12009 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12010 }
12011 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12012 {
12013 HOST_WIDE_INT bias = 1;
12014 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12015 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12016 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12017 }
12018 }
12019 else if (GET_MODE_CLASS (mode) == MODE_INT
12020 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12021 {
12022 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12023 add_loc_descr (&op0, int_loc_descriptor (shift));
12024 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12025 add_loc_descr (&op1, int_loc_descriptor (shift));
12026 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12027 }
12028 else if (GET_MODE_CLASS (mode) == MODE_INT
12029 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12030 {
12031 dw_die_ref type_die = base_type_for_mode (mode, 0);
12032 dw_loc_descr_ref cvt;
12033 if (type_die == NULL)
12034 return NULL;
12035 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12036 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12037 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12038 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12039 add_loc_descr (&op0, cvt);
12040 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12041 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12042 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12043 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12044 add_loc_descr (&op1, cvt);
12045 }
12046
12047 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12048 op = DW_OP_lt;
12049 else
12050 op = DW_OP_gt;
12051 ret = op0;
12052 add_loc_descr (&ret, op1);
12053 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12054 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12055 add_loc_descr (&ret, bra_node);
12056 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12057 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12058 add_loc_descr (&ret, drop_node);
12059 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12060 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12061 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12062 && GET_MODE_CLASS (mode) == MODE_INT
12063 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12064 ret = convert_descriptor_to_mode (mode, ret);
12065 return ret;
12066 }
12067
12068 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12069 but after converting arguments to type_die, afterwards
12070 convert back to unsigned. */
12071
12072 static dw_loc_descr_ref
12073 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12074 machine_mode mode, machine_mode mem_mode)
12075 {
12076 dw_loc_descr_ref cvt, op0, op1;
12077
12078 if (type_die == NULL)
12079 return NULL;
12080 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12081 VAR_INIT_STATUS_INITIALIZED);
12082 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12083 VAR_INIT_STATUS_INITIALIZED);
12084 if (op0 == NULL || op1 == NULL)
12085 return NULL;
12086 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12087 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12088 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12089 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12090 add_loc_descr (&op0, cvt);
12091 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12092 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12093 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12094 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12095 add_loc_descr (&op1, cvt);
12096 add_loc_descr (&op0, op1);
12097 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12098 return convert_descriptor_to_mode (mode, op0);
12099 }
12100
12101 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12102 const0 is DW_OP_lit0 or corresponding typed constant,
12103 const1 is DW_OP_lit1 or corresponding typed constant
12104 and constMSB is constant with just the MSB bit set
12105 for the mode):
12106 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12107 L1: const0 DW_OP_swap
12108 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12109 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12110 L3: DW_OP_drop
12111 L4: DW_OP_nop
12112
12113 CTZ is similar:
12114 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12115 L1: const0 DW_OP_swap
12116 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12117 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12118 L3: DW_OP_drop
12119 L4: DW_OP_nop
12120
12121 FFS is similar:
12122 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12123 L1: const1 DW_OP_swap
12124 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12125 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12126 L3: DW_OP_drop
12127 L4: DW_OP_nop */
12128
12129 static dw_loc_descr_ref
12130 clz_loc_descriptor (rtx rtl, machine_mode mode,
12131 machine_mode mem_mode)
12132 {
12133 dw_loc_descr_ref op0, ret, tmp;
12134 HOST_WIDE_INT valv;
12135 dw_loc_descr_ref l1jump, l1label;
12136 dw_loc_descr_ref l2jump, l2label;
12137 dw_loc_descr_ref l3jump, l3label;
12138 dw_loc_descr_ref l4jump, l4label;
12139 rtx msb;
12140
12141 if (GET_MODE_CLASS (mode) != MODE_INT
12142 || GET_MODE (XEXP (rtl, 0)) != mode)
12143 return NULL;
12144
12145 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12146 VAR_INIT_STATUS_INITIALIZED);
12147 if (op0 == NULL)
12148 return NULL;
12149 ret = op0;
12150 if (GET_CODE (rtl) == CLZ)
12151 {
12152 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12153 valv = GET_MODE_BITSIZE (mode);
12154 }
12155 else if (GET_CODE (rtl) == FFS)
12156 valv = 0;
12157 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12158 valv = GET_MODE_BITSIZE (mode);
12159 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12160 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12161 add_loc_descr (&ret, l1jump);
12162 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12163 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12164 VAR_INIT_STATUS_INITIALIZED);
12165 if (tmp == NULL)
12166 return NULL;
12167 add_loc_descr (&ret, tmp);
12168 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12169 add_loc_descr (&ret, l4jump);
12170 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12171 ? const1_rtx : const0_rtx,
12172 mode, mem_mode,
12173 VAR_INIT_STATUS_INITIALIZED);
12174 if (l1label == NULL)
12175 return NULL;
12176 add_loc_descr (&ret, l1label);
12177 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12178 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12179 add_loc_descr (&ret, l2label);
12180 if (GET_CODE (rtl) != CLZ)
12181 msb = const1_rtx;
12182 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12183 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12184 << (GET_MODE_BITSIZE (mode) - 1));
12185 else
12186 msb = immed_wide_int_const
12187 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12188 GET_MODE_PRECISION (mode)), mode);
12189 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12190 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12191 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12192 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12193 else
12194 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12195 VAR_INIT_STATUS_INITIALIZED);
12196 if (tmp == NULL)
12197 return NULL;
12198 add_loc_descr (&ret, tmp);
12199 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12200 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12201 add_loc_descr (&ret, l3jump);
12202 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12203 VAR_INIT_STATUS_INITIALIZED);
12204 if (tmp == NULL)
12205 return NULL;
12206 add_loc_descr (&ret, tmp);
12207 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12208 ? DW_OP_shl : DW_OP_shr, 0, 0));
12209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12210 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12211 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12212 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12213 add_loc_descr (&ret, l2jump);
12214 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12215 add_loc_descr (&ret, l3label);
12216 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12217 add_loc_descr (&ret, l4label);
12218 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12219 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12220 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12221 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12222 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12223 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12224 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12225 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12226 return ret;
12227 }
12228
12229 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12230 const1 is DW_OP_lit1 or corresponding typed constant):
12231 const0 DW_OP_swap
12232 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12233 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12234 L2: DW_OP_drop
12235
12236 PARITY is similar:
12237 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12238 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12239 L2: DW_OP_drop */
12240
12241 static dw_loc_descr_ref
12242 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12243 machine_mode mem_mode)
12244 {
12245 dw_loc_descr_ref op0, ret, tmp;
12246 dw_loc_descr_ref l1jump, l1label;
12247 dw_loc_descr_ref l2jump, l2label;
12248
12249 if (GET_MODE_CLASS (mode) != MODE_INT
12250 || GET_MODE (XEXP (rtl, 0)) != mode)
12251 return NULL;
12252
12253 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12254 VAR_INIT_STATUS_INITIALIZED);
12255 if (op0 == NULL)
12256 return NULL;
12257 ret = op0;
12258 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12259 VAR_INIT_STATUS_INITIALIZED);
12260 if (tmp == NULL)
12261 return NULL;
12262 add_loc_descr (&ret, tmp);
12263 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12264 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12265 add_loc_descr (&ret, l1label);
12266 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12267 add_loc_descr (&ret, l2jump);
12268 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12269 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12270 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12271 VAR_INIT_STATUS_INITIALIZED);
12272 if (tmp == NULL)
12273 return NULL;
12274 add_loc_descr (&ret, tmp);
12275 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12276 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12277 ? DW_OP_plus : DW_OP_xor, 0, 0));
12278 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12279 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12280 VAR_INIT_STATUS_INITIALIZED);
12281 add_loc_descr (&ret, tmp);
12282 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12283 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12284 add_loc_descr (&ret, l1jump);
12285 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12286 add_loc_descr (&ret, l2label);
12287 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12288 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12289 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12290 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12291 return ret;
12292 }
12293
12294 /* BSWAP (constS is initial shift count, either 56 or 24):
12295 constS const0
12296 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12297 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12298 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12299 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12300 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12301
12302 static dw_loc_descr_ref
12303 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12304 machine_mode mem_mode)
12305 {
12306 dw_loc_descr_ref op0, ret, tmp;
12307 dw_loc_descr_ref l1jump, l1label;
12308 dw_loc_descr_ref l2jump, l2label;
12309
12310 if (GET_MODE_CLASS (mode) != MODE_INT
12311 || BITS_PER_UNIT != 8
12312 || (GET_MODE_BITSIZE (mode) != 32
12313 && GET_MODE_BITSIZE (mode) != 64))
12314 return NULL;
12315
12316 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12317 VAR_INIT_STATUS_INITIALIZED);
12318 if (op0 == NULL)
12319 return NULL;
12320
12321 ret = op0;
12322 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12323 mode, mem_mode,
12324 VAR_INIT_STATUS_INITIALIZED);
12325 if (tmp == NULL)
12326 return NULL;
12327 add_loc_descr (&ret, tmp);
12328 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12329 VAR_INIT_STATUS_INITIALIZED);
12330 if (tmp == NULL)
12331 return NULL;
12332 add_loc_descr (&ret, tmp);
12333 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12334 add_loc_descr (&ret, l1label);
12335 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12336 mode, mem_mode,
12337 VAR_INIT_STATUS_INITIALIZED);
12338 add_loc_descr (&ret, tmp);
12339 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12340 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12341 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12342 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12343 VAR_INIT_STATUS_INITIALIZED);
12344 if (tmp == NULL)
12345 return NULL;
12346 add_loc_descr (&ret, tmp);
12347 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12348 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12349 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12350 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12351 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12352 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12353 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12354 VAR_INIT_STATUS_INITIALIZED);
12355 add_loc_descr (&ret, tmp);
12356 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12357 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12358 add_loc_descr (&ret, l2jump);
12359 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12360 VAR_INIT_STATUS_INITIALIZED);
12361 add_loc_descr (&ret, tmp);
12362 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12363 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12364 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12365 add_loc_descr (&ret, l1jump);
12366 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12367 add_loc_descr (&ret, l2label);
12368 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12369 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12370 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12371 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12372 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12373 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12374 return ret;
12375 }
12376
12377 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12378 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12379 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12380 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12381
12382 ROTATERT is similar:
12383 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12384 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12385 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12386
12387 static dw_loc_descr_ref
12388 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12389 machine_mode mem_mode)
12390 {
12391 rtx rtlop1 = XEXP (rtl, 1);
12392 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12393 int i;
12394
12395 if (GET_MODE_CLASS (mode) != MODE_INT)
12396 return NULL;
12397
12398 if (GET_MODE (rtlop1) != VOIDmode
12399 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12400 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12401 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12402 VAR_INIT_STATUS_INITIALIZED);
12403 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12404 VAR_INIT_STATUS_INITIALIZED);
12405 if (op0 == NULL || op1 == NULL)
12406 return NULL;
12407 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12408 for (i = 0; i < 2; i++)
12409 {
12410 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12411 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12412 mode, mem_mode,
12413 VAR_INIT_STATUS_INITIALIZED);
12414 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12415 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12416 ? DW_OP_const4u
12417 : HOST_BITS_PER_WIDE_INT == 64
12418 ? DW_OP_const8u : DW_OP_constu,
12419 GET_MODE_MASK (mode), 0);
12420 else
12421 mask[i] = NULL;
12422 if (mask[i] == NULL)
12423 return NULL;
12424 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12425 }
12426 ret = op0;
12427 add_loc_descr (&ret, op1);
12428 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12429 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12430 if (GET_CODE (rtl) == ROTATERT)
12431 {
12432 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12433 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12434 GET_MODE_BITSIZE (mode), 0));
12435 }
12436 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12437 if (mask[0] != NULL)
12438 add_loc_descr (&ret, mask[0]);
12439 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12440 if (mask[1] != NULL)
12441 {
12442 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12443 add_loc_descr (&ret, mask[1]);
12444 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12445 }
12446 if (GET_CODE (rtl) == ROTATE)
12447 {
12448 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12450 GET_MODE_BITSIZE (mode), 0));
12451 }
12452 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12453 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12454 return ret;
12455 }
12456
12457 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12458 for DEBUG_PARAMETER_REF RTL. */
12459
12460 static dw_loc_descr_ref
12461 parameter_ref_descriptor (rtx rtl)
12462 {
12463 dw_loc_descr_ref ret;
12464 dw_die_ref ref;
12465
12466 if (dwarf_strict)
12467 return NULL;
12468 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12469 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12470 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12471 if (ref)
12472 {
12473 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12474 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12475 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12476 }
12477 else
12478 {
12479 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12480 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12481 }
12482 return ret;
12483 }
12484
12485 /* The following routine converts the RTL for a variable or parameter
12486 (resident in memory) into an equivalent Dwarf representation of a
12487 mechanism for getting the address of that same variable onto the top of a
12488 hypothetical "address evaluation" stack.
12489
12490 When creating memory location descriptors, we are effectively transforming
12491 the RTL for a memory-resident object into its Dwarf postfix expression
12492 equivalent. This routine recursively descends an RTL tree, turning
12493 it into Dwarf postfix code as it goes.
12494
12495 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12496
12497 MEM_MODE is the mode of the memory reference, needed to handle some
12498 autoincrement addressing modes.
12499
12500 Return 0 if we can't represent the location. */
12501
12502 dw_loc_descr_ref
12503 mem_loc_descriptor (rtx rtl, machine_mode mode,
12504 machine_mode mem_mode,
12505 enum var_init_status initialized)
12506 {
12507 dw_loc_descr_ref mem_loc_result = NULL;
12508 enum dwarf_location_atom op;
12509 dw_loc_descr_ref op0, op1;
12510 rtx inner = NULL_RTX;
12511
12512 if (mode == VOIDmode)
12513 mode = GET_MODE (rtl);
12514
12515 /* Note that for a dynamically sized array, the location we will generate a
12516 description of here will be the lowest numbered location which is
12517 actually within the array. That's *not* necessarily the same as the
12518 zeroth element of the array. */
12519
12520 rtl = targetm.delegitimize_address (rtl);
12521
12522 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12523 return NULL;
12524
12525 switch (GET_CODE (rtl))
12526 {
12527 case POST_INC:
12528 case POST_DEC:
12529 case POST_MODIFY:
12530 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12531
12532 case SUBREG:
12533 /* The case of a subreg may arise when we have a local (register)
12534 variable or a formal (register) parameter which doesn't quite fill
12535 up an entire register. For now, just assume that it is
12536 legitimate to make the Dwarf info refer to the whole register which
12537 contains the given subreg. */
12538 if (!subreg_lowpart_p (rtl))
12539 break;
12540 inner = SUBREG_REG (rtl);
12541 case TRUNCATE:
12542 if (inner == NULL_RTX)
12543 inner = XEXP (rtl, 0);
12544 if (GET_MODE_CLASS (mode) == MODE_INT
12545 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12546 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12547 #ifdef POINTERS_EXTEND_UNSIGNED
12548 || (mode == Pmode && mem_mode != VOIDmode)
12549 #endif
12550 )
12551 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12552 {
12553 mem_loc_result = mem_loc_descriptor (inner,
12554 GET_MODE (inner),
12555 mem_mode, initialized);
12556 break;
12557 }
12558 if (dwarf_strict)
12559 break;
12560 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12561 break;
12562 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12563 && (GET_MODE_CLASS (mode) != MODE_INT
12564 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12565 break;
12566 else
12567 {
12568 dw_die_ref type_die;
12569 dw_loc_descr_ref cvt;
12570
12571 mem_loc_result = mem_loc_descriptor (inner,
12572 GET_MODE (inner),
12573 mem_mode, initialized);
12574 if (mem_loc_result == NULL)
12575 break;
12576 type_die = base_type_for_mode (mode,
12577 GET_MODE_CLASS (mode) == MODE_INT);
12578 if (type_die == NULL)
12579 {
12580 mem_loc_result = NULL;
12581 break;
12582 }
12583 if (GET_MODE_SIZE (mode)
12584 != GET_MODE_SIZE (GET_MODE (inner)))
12585 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12586 else
12587 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12588 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12589 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12590 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12591 add_loc_descr (&mem_loc_result, cvt);
12592 }
12593 break;
12594
12595 case REG:
12596 if (GET_MODE_CLASS (mode) != MODE_INT
12597 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12598 && rtl != arg_pointer_rtx
12599 && rtl != frame_pointer_rtx
12600 #ifdef POINTERS_EXTEND_UNSIGNED
12601 && (mode != Pmode || mem_mode == VOIDmode)
12602 #endif
12603 ))
12604 {
12605 dw_die_ref type_die;
12606 unsigned int dbx_regnum;
12607
12608 if (dwarf_strict)
12609 break;
12610 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12611 break;
12612 type_die = base_type_for_mode (mode,
12613 GET_MODE_CLASS (mode) == MODE_INT);
12614 if (type_die == NULL)
12615 break;
12616
12617 dbx_regnum = dbx_reg_number (rtl);
12618 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12619 break;
12620 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12621 dbx_regnum, 0);
12622 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12623 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12624 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12625 break;
12626 }
12627 /* Whenever a register number forms a part of the description of the
12628 method for calculating the (dynamic) address of a memory resident
12629 object, DWARF rules require the register number be referred to as
12630 a "base register". This distinction is not based in any way upon
12631 what category of register the hardware believes the given register
12632 belongs to. This is strictly DWARF terminology we're dealing with
12633 here. Note that in cases where the location of a memory-resident
12634 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12635 OP_CONST (0)) the actual DWARF location descriptor that we generate
12636 may just be OP_BASEREG (basereg). This may look deceptively like
12637 the object in question was allocated to a register (rather than in
12638 memory) so DWARF consumers need to be aware of the subtle
12639 distinction between OP_REG and OP_BASEREG. */
12640 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12641 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12642 else if (stack_realign_drap
12643 && crtl->drap_reg
12644 && crtl->args.internal_arg_pointer == rtl
12645 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12646 {
12647 /* If RTL is internal_arg_pointer, which has been optimized
12648 out, use DRAP instead. */
12649 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12650 VAR_INIT_STATUS_INITIALIZED);
12651 }
12652 break;
12653
12654 case SIGN_EXTEND:
12655 case ZERO_EXTEND:
12656 if (GET_MODE_CLASS (mode) != MODE_INT)
12657 break;
12658 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12659 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12660 if (op0 == 0)
12661 break;
12662 else if (GET_CODE (rtl) == ZERO_EXTEND
12663 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12664 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12665 < HOST_BITS_PER_WIDE_INT
12666 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12667 to expand zero extend as two shifts instead of
12668 masking. */
12669 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12670 {
12671 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12672 mem_loc_result = op0;
12673 add_loc_descr (&mem_loc_result,
12674 int_loc_descriptor (GET_MODE_MASK (imode)));
12675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12676 }
12677 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12678 {
12679 int shift = DWARF2_ADDR_SIZE
12680 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12681 shift *= BITS_PER_UNIT;
12682 if (GET_CODE (rtl) == SIGN_EXTEND)
12683 op = DW_OP_shra;
12684 else
12685 op = DW_OP_shr;
12686 mem_loc_result = op0;
12687 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12688 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12689 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12690 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12691 }
12692 else if (!dwarf_strict)
12693 {
12694 dw_die_ref type_die1, type_die2;
12695 dw_loc_descr_ref cvt;
12696
12697 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12698 GET_CODE (rtl) == ZERO_EXTEND);
12699 if (type_die1 == NULL)
12700 break;
12701 type_die2 = base_type_for_mode (mode, 1);
12702 if (type_die2 == NULL)
12703 break;
12704 mem_loc_result = op0;
12705 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12706 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12707 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12708 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12709 add_loc_descr (&mem_loc_result, cvt);
12710 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12711 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12712 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12713 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12714 add_loc_descr (&mem_loc_result, cvt);
12715 }
12716 break;
12717
12718 case MEM:
12719 {
12720 rtx new_rtl = avoid_constant_pool_reference (rtl);
12721 if (new_rtl != rtl)
12722 {
12723 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12724 initialized);
12725 if (mem_loc_result != NULL)
12726 return mem_loc_result;
12727 }
12728 }
12729 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12730 get_address_mode (rtl), mode,
12731 VAR_INIT_STATUS_INITIALIZED);
12732 if (mem_loc_result == NULL)
12733 mem_loc_result = tls_mem_loc_descriptor (rtl);
12734 if (mem_loc_result != NULL)
12735 {
12736 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12737 || GET_MODE_CLASS (mode) != MODE_INT)
12738 {
12739 dw_die_ref type_die;
12740 dw_loc_descr_ref deref;
12741
12742 if (dwarf_strict)
12743 return NULL;
12744 type_die
12745 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12746 if (type_die == NULL)
12747 return NULL;
12748 deref = new_loc_descr (DW_OP_GNU_deref_type,
12749 GET_MODE_SIZE (mode), 0);
12750 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12751 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12752 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12753 add_loc_descr (&mem_loc_result, deref);
12754 }
12755 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12756 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12757 else
12758 add_loc_descr (&mem_loc_result,
12759 new_loc_descr (DW_OP_deref_size,
12760 GET_MODE_SIZE (mode), 0));
12761 }
12762 break;
12763
12764 case LO_SUM:
12765 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12766
12767 case LABEL_REF:
12768 /* Some ports can transform a symbol ref into a label ref, because
12769 the symbol ref is too far away and has to be dumped into a constant
12770 pool. */
12771 case CONST:
12772 case SYMBOL_REF:
12773 if ((GET_MODE_CLASS (mode) != MODE_INT
12774 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12775 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12776 #ifdef POINTERS_EXTEND_UNSIGNED
12777 && (mode != Pmode || mem_mode == VOIDmode)
12778 #endif
12779 ))
12780 break;
12781 if (GET_CODE (rtl) == SYMBOL_REF
12782 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12783 {
12784 dw_loc_descr_ref temp;
12785
12786 /* If this is not defined, we have no way to emit the data. */
12787 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12788 break;
12789
12790 temp = new_addr_loc_descr (rtl, dtprel_true);
12791
12792 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12793 add_loc_descr (&mem_loc_result, temp);
12794
12795 break;
12796 }
12797
12798 if (!const_ok_for_output (rtl))
12799 {
12800 if (GET_CODE (rtl) == CONST)
12801 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12802 initialized);
12803 break;
12804 }
12805
12806 symref:
12807 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12808 vec_safe_push (used_rtx_array, rtl);
12809 break;
12810
12811 case CONCAT:
12812 case CONCATN:
12813 case VAR_LOCATION:
12814 case DEBUG_IMPLICIT_PTR:
12815 expansion_failed (NULL_TREE, rtl,
12816 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12817 return 0;
12818
12819 case ENTRY_VALUE:
12820 if (dwarf_strict)
12821 return NULL;
12822 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12823 {
12824 if (GET_MODE_CLASS (mode) != MODE_INT
12825 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12826 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12827 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12828 else
12829 {
12830 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12831 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12832 return NULL;
12833 op0 = one_reg_loc_descriptor (dbx_regnum,
12834 VAR_INIT_STATUS_INITIALIZED);
12835 }
12836 }
12837 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12838 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12839 {
12840 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12841 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12842 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12843 return NULL;
12844 }
12845 else
12846 gcc_unreachable ();
12847 if (op0 == NULL)
12848 return NULL;
12849 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12850 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12851 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12852 break;
12853
12854 case DEBUG_PARAMETER_REF:
12855 mem_loc_result = parameter_ref_descriptor (rtl);
12856 break;
12857
12858 case PRE_MODIFY:
12859 /* Extract the PLUS expression nested inside and fall into
12860 PLUS code below. */
12861 rtl = XEXP (rtl, 1);
12862 goto plus;
12863
12864 case PRE_INC:
12865 case PRE_DEC:
12866 /* Turn these into a PLUS expression and fall into the PLUS code
12867 below. */
12868 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12869 gen_int_mode (GET_CODE (rtl) == PRE_INC
12870 ? GET_MODE_UNIT_SIZE (mem_mode)
12871 : -GET_MODE_UNIT_SIZE (mem_mode),
12872 mode));
12873
12874 /* ... fall through ... */
12875
12876 case PLUS:
12877 plus:
12878 if (is_based_loc (rtl)
12879 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12880 || XEXP (rtl, 0) == arg_pointer_rtx
12881 || XEXP (rtl, 0) == frame_pointer_rtx)
12882 && GET_MODE_CLASS (mode) == MODE_INT)
12883 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12884 INTVAL (XEXP (rtl, 1)),
12885 VAR_INIT_STATUS_INITIALIZED);
12886 else
12887 {
12888 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12889 VAR_INIT_STATUS_INITIALIZED);
12890 if (mem_loc_result == 0)
12891 break;
12892
12893 if (CONST_INT_P (XEXP (rtl, 1))
12894 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12895 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12896 else
12897 {
12898 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12899 VAR_INIT_STATUS_INITIALIZED);
12900 if (op1 == 0)
12901 return NULL;
12902 add_loc_descr (&mem_loc_result, op1);
12903 add_loc_descr (&mem_loc_result,
12904 new_loc_descr (DW_OP_plus, 0, 0));
12905 }
12906 }
12907 break;
12908
12909 /* If a pseudo-reg is optimized away, it is possible for it to
12910 be replaced with a MEM containing a multiply or shift. */
12911 case MINUS:
12912 op = DW_OP_minus;
12913 goto do_binop;
12914
12915 case MULT:
12916 op = DW_OP_mul;
12917 goto do_binop;
12918
12919 case DIV:
12920 if (!dwarf_strict
12921 && GET_MODE_CLASS (mode) == MODE_INT
12922 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12923 {
12924 mem_loc_result = typed_binop (DW_OP_div, rtl,
12925 base_type_for_mode (mode, 0),
12926 mode, mem_mode);
12927 break;
12928 }
12929 op = DW_OP_div;
12930 goto do_binop;
12931
12932 case UMOD:
12933 op = DW_OP_mod;
12934 goto do_binop;
12935
12936 case ASHIFT:
12937 op = DW_OP_shl;
12938 goto do_shift;
12939
12940 case ASHIFTRT:
12941 op = DW_OP_shra;
12942 goto do_shift;
12943
12944 case LSHIFTRT:
12945 op = DW_OP_shr;
12946 goto do_shift;
12947
12948 do_shift:
12949 if (GET_MODE_CLASS (mode) != MODE_INT)
12950 break;
12951 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12952 VAR_INIT_STATUS_INITIALIZED);
12953 {
12954 rtx rtlop1 = XEXP (rtl, 1);
12955 if (GET_MODE (rtlop1) != VOIDmode
12956 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12957 < GET_MODE_BITSIZE (mode))
12958 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12959 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12960 VAR_INIT_STATUS_INITIALIZED);
12961 }
12962
12963 if (op0 == 0 || op1 == 0)
12964 break;
12965
12966 mem_loc_result = op0;
12967 add_loc_descr (&mem_loc_result, op1);
12968 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12969 break;
12970
12971 case AND:
12972 op = DW_OP_and;
12973 goto do_binop;
12974
12975 case IOR:
12976 op = DW_OP_or;
12977 goto do_binop;
12978
12979 case XOR:
12980 op = DW_OP_xor;
12981 goto do_binop;
12982
12983 do_binop:
12984 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12985 VAR_INIT_STATUS_INITIALIZED);
12986 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12987 VAR_INIT_STATUS_INITIALIZED);
12988
12989 if (op0 == 0 || op1 == 0)
12990 break;
12991
12992 mem_loc_result = op0;
12993 add_loc_descr (&mem_loc_result, op1);
12994 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12995 break;
12996
12997 case MOD:
12998 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12999 {
13000 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13001 base_type_for_mode (mode, 0),
13002 mode, mem_mode);
13003 break;
13004 }
13005
13006 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13007 VAR_INIT_STATUS_INITIALIZED);
13008 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13009 VAR_INIT_STATUS_INITIALIZED);
13010
13011 if (op0 == 0 || op1 == 0)
13012 break;
13013
13014 mem_loc_result = op0;
13015 add_loc_descr (&mem_loc_result, op1);
13016 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13017 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13018 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13019 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13020 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13021 break;
13022
13023 case UDIV:
13024 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13025 {
13026 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13027 {
13028 op = DW_OP_div;
13029 goto do_binop;
13030 }
13031 mem_loc_result = typed_binop (DW_OP_div, rtl,
13032 base_type_for_mode (mode, 1),
13033 mode, mem_mode);
13034 }
13035 break;
13036
13037 case NOT:
13038 op = DW_OP_not;
13039 goto do_unop;
13040
13041 case ABS:
13042 op = DW_OP_abs;
13043 goto do_unop;
13044
13045 case NEG:
13046 op = DW_OP_neg;
13047 goto do_unop;
13048
13049 do_unop:
13050 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13051 VAR_INIT_STATUS_INITIALIZED);
13052
13053 if (op0 == 0)
13054 break;
13055
13056 mem_loc_result = op0;
13057 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13058 break;
13059
13060 case CONST_INT:
13061 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13062 #ifdef POINTERS_EXTEND_UNSIGNED
13063 || (mode == Pmode
13064 && mem_mode != VOIDmode
13065 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13066 #endif
13067 )
13068 {
13069 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13070 break;
13071 }
13072 if (!dwarf_strict
13073 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13074 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13075 {
13076 dw_die_ref type_die = base_type_for_mode (mode, 1);
13077 machine_mode amode;
13078 if (type_die == NULL)
13079 return NULL;
13080 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13081 MODE_INT, 0);
13082 if (INTVAL (rtl) >= 0
13083 && amode != BLKmode
13084 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13085 /* const DW_OP_GNU_convert <XXX> vs.
13086 DW_OP_GNU_const_type <XXX, 1, const>. */
13087 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13088 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13089 {
13090 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13091 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13092 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13093 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13094 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13095 add_loc_descr (&mem_loc_result, op0);
13096 return mem_loc_result;
13097 }
13098 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13099 INTVAL (rtl));
13100 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13101 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13102 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13103 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13104 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13105 else
13106 {
13107 mem_loc_result->dw_loc_oprnd2.val_class
13108 = dw_val_class_const_double;
13109 mem_loc_result->dw_loc_oprnd2.v.val_double
13110 = double_int::from_shwi (INTVAL (rtl));
13111 }
13112 }
13113 break;
13114
13115 case CONST_DOUBLE:
13116 if (!dwarf_strict)
13117 {
13118 dw_die_ref type_die;
13119
13120 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13121 CONST_DOUBLE rtx could represent either a large integer
13122 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13123 the value is always a floating point constant.
13124
13125 When it is an integer, a CONST_DOUBLE is used whenever
13126 the constant requires 2 HWIs to be adequately represented.
13127 We output CONST_DOUBLEs as blocks. */
13128 if (mode == VOIDmode
13129 || (GET_MODE (rtl) == VOIDmode
13130 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13131 break;
13132 type_die = base_type_for_mode (mode,
13133 GET_MODE_CLASS (mode) == MODE_INT);
13134 if (type_die == NULL)
13135 return NULL;
13136 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13137 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13138 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13139 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13140 #if TARGET_SUPPORTS_WIDE_INT == 0
13141 if (!SCALAR_FLOAT_MODE_P (mode))
13142 {
13143 mem_loc_result->dw_loc_oprnd2.val_class
13144 = dw_val_class_const_double;
13145 mem_loc_result->dw_loc_oprnd2.v.val_double
13146 = rtx_to_double_int (rtl);
13147 }
13148 else
13149 #endif
13150 {
13151 unsigned int length = GET_MODE_SIZE (mode);
13152 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13153
13154 insert_float (rtl, array);
13155 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13156 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13157 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13158 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13159 }
13160 }
13161 break;
13162
13163 case CONST_WIDE_INT:
13164 if (!dwarf_strict)
13165 {
13166 dw_die_ref type_die;
13167
13168 type_die = base_type_for_mode (mode,
13169 GET_MODE_CLASS (mode) == MODE_INT);
13170 if (type_die == NULL)
13171 return NULL;
13172 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13173 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13174 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13175 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13176 mem_loc_result->dw_loc_oprnd2.val_class
13177 = dw_val_class_wide_int;
13178 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13179 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13180 }
13181 break;
13182
13183 case EQ:
13184 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13185 break;
13186
13187 case GE:
13188 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13189 break;
13190
13191 case GT:
13192 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13193 break;
13194
13195 case LE:
13196 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13197 break;
13198
13199 case LT:
13200 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13201 break;
13202
13203 case NE:
13204 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13205 break;
13206
13207 case GEU:
13208 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13209 break;
13210
13211 case GTU:
13212 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13213 break;
13214
13215 case LEU:
13216 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13217 break;
13218
13219 case LTU:
13220 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13221 break;
13222
13223 case UMIN:
13224 case UMAX:
13225 if (GET_MODE_CLASS (mode) != MODE_INT)
13226 break;
13227 /* FALLTHRU */
13228 case SMIN:
13229 case SMAX:
13230 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13231 break;
13232
13233 case ZERO_EXTRACT:
13234 case SIGN_EXTRACT:
13235 if (CONST_INT_P (XEXP (rtl, 1))
13236 && CONST_INT_P (XEXP (rtl, 2))
13237 && ((unsigned) INTVAL (XEXP (rtl, 1))
13238 + (unsigned) INTVAL (XEXP (rtl, 2))
13239 <= GET_MODE_BITSIZE (mode))
13240 && GET_MODE_CLASS (mode) == MODE_INT
13241 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13242 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13243 {
13244 int shift, size;
13245 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13246 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13247 if (op0 == 0)
13248 break;
13249 if (GET_CODE (rtl) == SIGN_EXTRACT)
13250 op = DW_OP_shra;
13251 else
13252 op = DW_OP_shr;
13253 mem_loc_result = op0;
13254 size = INTVAL (XEXP (rtl, 1));
13255 shift = INTVAL (XEXP (rtl, 2));
13256 if (BITS_BIG_ENDIAN)
13257 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13258 - shift - size;
13259 if (shift + size != (int) DWARF2_ADDR_SIZE)
13260 {
13261 add_loc_descr (&mem_loc_result,
13262 int_loc_descriptor (DWARF2_ADDR_SIZE
13263 - shift - size));
13264 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13265 }
13266 if (size != (int) DWARF2_ADDR_SIZE)
13267 {
13268 add_loc_descr (&mem_loc_result,
13269 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13270 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13271 }
13272 }
13273 break;
13274
13275 case IF_THEN_ELSE:
13276 {
13277 dw_loc_descr_ref op2, bra_node, drop_node;
13278 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13279 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13280 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13281 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13282 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13283 VAR_INIT_STATUS_INITIALIZED);
13284 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13285 VAR_INIT_STATUS_INITIALIZED);
13286 if (op0 == NULL || op1 == NULL || op2 == NULL)
13287 break;
13288
13289 mem_loc_result = op1;
13290 add_loc_descr (&mem_loc_result, op2);
13291 add_loc_descr (&mem_loc_result, op0);
13292 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13293 add_loc_descr (&mem_loc_result, bra_node);
13294 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13295 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13296 add_loc_descr (&mem_loc_result, drop_node);
13297 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13298 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13299 }
13300 break;
13301
13302 case FLOAT_EXTEND:
13303 case FLOAT_TRUNCATE:
13304 case FLOAT:
13305 case UNSIGNED_FLOAT:
13306 case FIX:
13307 case UNSIGNED_FIX:
13308 if (!dwarf_strict)
13309 {
13310 dw_die_ref type_die;
13311 dw_loc_descr_ref cvt;
13312
13313 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13314 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13315 if (op0 == NULL)
13316 break;
13317 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13318 && (GET_CODE (rtl) == FLOAT
13319 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13320 <= DWARF2_ADDR_SIZE))
13321 {
13322 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13323 GET_CODE (rtl) == UNSIGNED_FLOAT);
13324 if (type_die == NULL)
13325 break;
13326 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13327 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13328 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13329 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13330 add_loc_descr (&op0, cvt);
13331 }
13332 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13333 if (type_die == NULL)
13334 break;
13335 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13336 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13337 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13338 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13339 add_loc_descr (&op0, cvt);
13340 if (GET_MODE_CLASS (mode) == MODE_INT
13341 && (GET_CODE (rtl) == FIX
13342 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13343 {
13344 op0 = convert_descriptor_to_mode (mode, op0);
13345 if (op0 == NULL)
13346 break;
13347 }
13348 mem_loc_result = op0;
13349 }
13350 break;
13351
13352 case CLZ:
13353 case CTZ:
13354 case FFS:
13355 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13356 break;
13357
13358 case POPCOUNT:
13359 case PARITY:
13360 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13361 break;
13362
13363 case BSWAP:
13364 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13365 break;
13366
13367 case ROTATE:
13368 case ROTATERT:
13369 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13370 break;
13371
13372 case COMPARE:
13373 /* In theory, we could implement the above. */
13374 /* DWARF cannot represent the unsigned compare operations
13375 natively. */
13376 case SS_MULT:
13377 case US_MULT:
13378 case SS_DIV:
13379 case US_DIV:
13380 case SS_PLUS:
13381 case US_PLUS:
13382 case SS_MINUS:
13383 case US_MINUS:
13384 case SS_NEG:
13385 case US_NEG:
13386 case SS_ABS:
13387 case SS_ASHIFT:
13388 case US_ASHIFT:
13389 case SS_TRUNCATE:
13390 case US_TRUNCATE:
13391 case UNORDERED:
13392 case ORDERED:
13393 case UNEQ:
13394 case UNGE:
13395 case UNGT:
13396 case UNLE:
13397 case UNLT:
13398 case LTGT:
13399 case FRACT_CONVERT:
13400 case UNSIGNED_FRACT_CONVERT:
13401 case SAT_FRACT:
13402 case UNSIGNED_SAT_FRACT:
13403 case SQRT:
13404 case ASM_OPERANDS:
13405 case VEC_MERGE:
13406 case VEC_SELECT:
13407 case VEC_CONCAT:
13408 case VEC_DUPLICATE:
13409 case UNSPEC:
13410 case HIGH:
13411 case FMA:
13412 case STRICT_LOW_PART:
13413 case CONST_VECTOR:
13414 case CONST_FIXED:
13415 case CLRSB:
13416 case CLOBBER:
13417 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13418 can't express it in the debug info. This can happen e.g. with some
13419 TLS UNSPECs. */
13420 break;
13421
13422 case CONST_STRING:
13423 resolve_one_addr (&rtl);
13424 goto symref;
13425
13426 default:
13427 #ifdef ENABLE_CHECKING
13428 print_rtl (stderr, rtl);
13429 gcc_unreachable ();
13430 #else
13431 break;
13432 #endif
13433 }
13434
13435 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13436 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13437
13438 return mem_loc_result;
13439 }
13440
13441 /* Return a descriptor that describes the concatenation of two locations.
13442 This is typically a complex variable. */
13443
13444 static dw_loc_descr_ref
13445 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13446 {
13447 dw_loc_descr_ref cc_loc_result = NULL;
13448 dw_loc_descr_ref x0_ref
13449 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13450 dw_loc_descr_ref x1_ref
13451 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13452
13453 if (x0_ref == 0 || x1_ref == 0)
13454 return 0;
13455
13456 cc_loc_result = x0_ref;
13457 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13458
13459 add_loc_descr (&cc_loc_result, x1_ref);
13460 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13461
13462 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13463 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13464
13465 return cc_loc_result;
13466 }
13467
13468 /* Return a descriptor that describes the concatenation of N
13469 locations. */
13470
13471 static dw_loc_descr_ref
13472 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13473 {
13474 unsigned int i;
13475 dw_loc_descr_ref cc_loc_result = NULL;
13476 unsigned int n = XVECLEN (concatn, 0);
13477
13478 for (i = 0; i < n; ++i)
13479 {
13480 dw_loc_descr_ref ref;
13481 rtx x = XVECEXP (concatn, 0, i);
13482
13483 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13484 if (ref == NULL)
13485 return NULL;
13486
13487 add_loc_descr (&cc_loc_result, ref);
13488 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13489 }
13490
13491 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13492 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13493
13494 return cc_loc_result;
13495 }
13496
13497 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13498 for DEBUG_IMPLICIT_PTR RTL. */
13499
13500 static dw_loc_descr_ref
13501 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13502 {
13503 dw_loc_descr_ref ret;
13504 dw_die_ref ref;
13505
13506 if (dwarf_strict)
13507 return NULL;
13508 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13509 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13510 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13511 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13512 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13513 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13514 if (ref)
13515 {
13516 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13517 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13518 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13519 }
13520 else
13521 {
13522 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13523 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13524 }
13525 return ret;
13526 }
13527
13528 /* Output a proper Dwarf location descriptor for a variable or parameter
13529 which is either allocated in a register or in a memory location. For a
13530 register, we just generate an OP_REG and the register number. For a
13531 memory location we provide a Dwarf postfix expression describing how to
13532 generate the (dynamic) address of the object onto the address stack.
13533
13534 MODE is mode of the decl if this loc_descriptor is going to be used in
13535 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13536 allowed, VOIDmode otherwise.
13537
13538 If we don't know how to describe it, return 0. */
13539
13540 static dw_loc_descr_ref
13541 loc_descriptor (rtx rtl, machine_mode mode,
13542 enum var_init_status initialized)
13543 {
13544 dw_loc_descr_ref loc_result = NULL;
13545
13546 switch (GET_CODE (rtl))
13547 {
13548 case SUBREG:
13549 /* The case of a subreg may arise when we have a local (register)
13550 variable or a formal (register) parameter which doesn't quite fill
13551 up an entire register. For now, just assume that it is
13552 legitimate to make the Dwarf info refer to the whole register which
13553 contains the given subreg. */
13554 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13555 loc_result = loc_descriptor (SUBREG_REG (rtl),
13556 GET_MODE (SUBREG_REG (rtl)), initialized);
13557 else
13558 goto do_default;
13559 break;
13560
13561 case REG:
13562 loc_result = reg_loc_descriptor (rtl, initialized);
13563 break;
13564
13565 case MEM:
13566 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13567 GET_MODE (rtl), initialized);
13568 if (loc_result == NULL)
13569 loc_result = tls_mem_loc_descriptor (rtl);
13570 if (loc_result == NULL)
13571 {
13572 rtx new_rtl = avoid_constant_pool_reference (rtl);
13573 if (new_rtl != rtl)
13574 loc_result = loc_descriptor (new_rtl, mode, initialized);
13575 }
13576 break;
13577
13578 case CONCAT:
13579 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13580 initialized);
13581 break;
13582
13583 case CONCATN:
13584 loc_result = concatn_loc_descriptor (rtl, initialized);
13585 break;
13586
13587 case VAR_LOCATION:
13588 /* Single part. */
13589 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13590 {
13591 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13592 if (GET_CODE (loc) == EXPR_LIST)
13593 loc = XEXP (loc, 0);
13594 loc_result = loc_descriptor (loc, mode, initialized);
13595 break;
13596 }
13597
13598 rtl = XEXP (rtl, 1);
13599 /* FALLTHRU */
13600
13601 case PARALLEL:
13602 {
13603 rtvec par_elems = XVEC (rtl, 0);
13604 int num_elem = GET_NUM_ELEM (par_elems);
13605 machine_mode mode;
13606 int i;
13607
13608 /* Create the first one, so we have something to add to. */
13609 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13610 VOIDmode, initialized);
13611 if (loc_result == NULL)
13612 return NULL;
13613 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13614 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13615 for (i = 1; i < num_elem; i++)
13616 {
13617 dw_loc_descr_ref temp;
13618
13619 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13620 VOIDmode, initialized);
13621 if (temp == NULL)
13622 return NULL;
13623 add_loc_descr (&loc_result, temp);
13624 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13625 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13626 }
13627 }
13628 break;
13629
13630 case CONST_INT:
13631 if (mode != VOIDmode && mode != BLKmode)
13632 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13633 INTVAL (rtl));
13634 break;
13635
13636 case CONST_DOUBLE:
13637 if (mode == VOIDmode)
13638 mode = GET_MODE (rtl);
13639
13640 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13641 {
13642 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13643
13644 /* Note that a CONST_DOUBLE rtx could represent either an integer
13645 or a floating-point constant. A CONST_DOUBLE is used whenever
13646 the constant requires more than one word in order to be
13647 adequately represented. We output CONST_DOUBLEs as blocks. */
13648 loc_result = new_loc_descr (DW_OP_implicit_value,
13649 GET_MODE_SIZE (mode), 0);
13650 #if TARGET_SUPPORTS_WIDE_INT == 0
13651 if (!SCALAR_FLOAT_MODE_P (mode))
13652 {
13653 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13654 loc_result->dw_loc_oprnd2.v.val_double
13655 = rtx_to_double_int (rtl);
13656 }
13657 else
13658 #endif
13659 {
13660 unsigned int length = GET_MODE_SIZE (mode);
13661 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13662
13663 insert_float (rtl, array);
13664 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13665 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13666 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13667 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13668 }
13669 }
13670 break;
13671
13672 case CONST_WIDE_INT:
13673 if (mode == VOIDmode)
13674 mode = GET_MODE (rtl);
13675
13676 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13677 {
13678 loc_result = new_loc_descr (DW_OP_implicit_value,
13679 GET_MODE_SIZE (mode), 0);
13680 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13681 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13682 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13683 }
13684 break;
13685
13686 case CONST_VECTOR:
13687 if (mode == VOIDmode)
13688 mode = GET_MODE (rtl);
13689
13690 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13691 {
13692 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13693 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13694 unsigned char *array
13695 = ggc_vec_alloc<unsigned char> (length * elt_size);
13696 unsigned int i;
13697 unsigned char *p;
13698 machine_mode imode = GET_MODE_INNER (mode);
13699
13700 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13701 switch (GET_MODE_CLASS (mode))
13702 {
13703 case MODE_VECTOR_INT:
13704 for (i = 0, p = array; i < length; i++, p += elt_size)
13705 {
13706 rtx elt = CONST_VECTOR_ELT (rtl, i);
13707 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13708 }
13709 break;
13710
13711 case MODE_VECTOR_FLOAT:
13712 for (i = 0, p = array; i < length; i++, p += elt_size)
13713 {
13714 rtx elt = CONST_VECTOR_ELT (rtl, i);
13715 insert_float (elt, p);
13716 }
13717 break;
13718
13719 default:
13720 gcc_unreachable ();
13721 }
13722
13723 loc_result = new_loc_descr (DW_OP_implicit_value,
13724 length * elt_size, 0);
13725 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13726 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13727 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13728 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13729 }
13730 break;
13731
13732 case CONST:
13733 if (mode == VOIDmode
13734 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13735 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13736 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13737 {
13738 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13739 break;
13740 }
13741 /* FALLTHROUGH */
13742 case SYMBOL_REF:
13743 if (!const_ok_for_output (rtl))
13744 break;
13745 case LABEL_REF:
13746 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13747 && (dwarf_version >= 4 || !dwarf_strict))
13748 {
13749 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13750 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13751 vec_safe_push (used_rtx_array, rtl);
13752 }
13753 break;
13754
13755 case DEBUG_IMPLICIT_PTR:
13756 loc_result = implicit_ptr_descriptor (rtl, 0);
13757 break;
13758
13759 case PLUS:
13760 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13761 && CONST_INT_P (XEXP (rtl, 1)))
13762 {
13763 loc_result
13764 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13765 break;
13766 }
13767 /* FALLTHRU */
13768 do_default:
13769 default:
13770 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13771 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13772 && dwarf_version >= 4)
13773 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13774 {
13775 /* Value expression. */
13776 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13777 if (loc_result)
13778 add_loc_descr (&loc_result,
13779 new_loc_descr (DW_OP_stack_value, 0, 0));
13780 }
13781 break;
13782 }
13783
13784 return loc_result;
13785 }
13786
13787 /* We need to figure out what section we should use as the base for the
13788 address ranges where a given location is valid.
13789 1. If this particular DECL has a section associated with it, use that.
13790 2. If this function has a section associated with it, use that.
13791 3. Otherwise, use the text section.
13792 XXX: If you split a variable across multiple sections, we won't notice. */
13793
13794 static const char *
13795 secname_for_decl (const_tree decl)
13796 {
13797 const char *secname;
13798
13799 if (VAR_OR_FUNCTION_DECL_P (decl)
13800 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13801 && DECL_SECTION_NAME (decl))
13802 secname = DECL_SECTION_NAME (decl);
13803 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13804 secname = DECL_SECTION_NAME (current_function_decl);
13805 else if (cfun && in_cold_section_p)
13806 secname = crtl->subsections.cold_section_label;
13807 else
13808 secname = text_section_label;
13809
13810 return secname;
13811 }
13812
13813 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13814
13815 static bool
13816 decl_by_reference_p (tree decl)
13817 {
13818 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13819 || TREE_CODE (decl) == VAR_DECL)
13820 && DECL_BY_REFERENCE (decl));
13821 }
13822
13823 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13824 for VARLOC. */
13825
13826 static dw_loc_descr_ref
13827 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13828 enum var_init_status initialized)
13829 {
13830 int have_address = 0;
13831 dw_loc_descr_ref descr;
13832 machine_mode mode;
13833
13834 if (want_address != 2)
13835 {
13836 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13837 /* Single part. */
13838 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13839 {
13840 varloc = PAT_VAR_LOCATION_LOC (varloc);
13841 if (GET_CODE (varloc) == EXPR_LIST)
13842 varloc = XEXP (varloc, 0);
13843 mode = GET_MODE (varloc);
13844 if (MEM_P (varloc))
13845 {
13846 rtx addr = XEXP (varloc, 0);
13847 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13848 mode, initialized);
13849 if (descr)
13850 have_address = 1;
13851 else
13852 {
13853 rtx x = avoid_constant_pool_reference (varloc);
13854 if (x != varloc)
13855 descr = mem_loc_descriptor (x, mode, VOIDmode,
13856 initialized);
13857 }
13858 }
13859 else
13860 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13861 }
13862 else
13863 return 0;
13864 }
13865 else
13866 {
13867 if (GET_CODE (varloc) == VAR_LOCATION)
13868 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13869 else
13870 mode = DECL_MODE (loc);
13871 descr = loc_descriptor (varloc, mode, initialized);
13872 have_address = 1;
13873 }
13874
13875 if (!descr)
13876 return 0;
13877
13878 if (want_address == 2 && !have_address
13879 && (dwarf_version >= 4 || !dwarf_strict))
13880 {
13881 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13882 {
13883 expansion_failed (loc, NULL_RTX,
13884 "DWARF address size mismatch");
13885 return 0;
13886 }
13887 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13888 have_address = 1;
13889 }
13890 /* Show if we can't fill the request for an address. */
13891 if (want_address && !have_address)
13892 {
13893 expansion_failed (loc, NULL_RTX,
13894 "Want address and only have value");
13895 return 0;
13896 }
13897
13898 /* If we've got an address and don't want one, dereference. */
13899 if (!want_address && have_address)
13900 {
13901 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13902 enum dwarf_location_atom op;
13903
13904 if (size > DWARF2_ADDR_SIZE || size == -1)
13905 {
13906 expansion_failed (loc, NULL_RTX,
13907 "DWARF address size mismatch");
13908 return 0;
13909 }
13910 else if (size == DWARF2_ADDR_SIZE)
13911 op = DW_OP_deref;
13912 else
13913 op = DW_OP_deref_size;
13914
13915 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13916 }
13917
13918 return descr;
13919 }
13920
13921 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13922 if it is not possible. */
13923
13924 static dw_loc_descr_ref
13925 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13926 {
13927 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13928 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13929 else if (dwarf_version >= 3 || !dwarf_strict)
13930 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13931 else
13932 return NULL;
13933 }
13934
13935 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13936 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13937
13938 static dw_loc_descr_ref
13939 dw_sra_loc_expr (tree decl, rtx loc)
13940 {
13941 rtx p;
13942 unsigned HOST_WIDE_INT padsize = 0;
13943 dw_loc_descr_ref descr, *descr_tail;
13944 unsigned HOST_WIDE_INT decl_size;
13945 rtx varloc;
13946 enum var_init_status initialized;
13947
13948 if (DECL_SIZE (decl) == NULL
13949 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13950 return NULL;
13951
13952 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13953 descr = NULL;
13954 descr_tail = &descr;
13955
13956 for (p = loc; p; p = XEXP (p, 1))
13957 {
13958 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13959 rtx loc_note = *decl_piece_varloc_ptr (p);
13960 dw_loc_descr_ref cur_descr;
13961 dw_loc_descr_ref *tail, last = NULL;
13962 unsigned HOST_WIDE_INT opsize = 0;
13963
13964 if (loc_note == NULL_RTX
13965 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13966 {
13967 padsize += bitsize;
13968 continue;
13969 }
13970 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13971 varloc = NOTE_VAR_LOCATION (loc_note);
13972 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13973 if (cur_descr == NULL)
13974 {
13975 padsize += bitsize;
13976 continue;
13977 }
13978
13979 /* Check that cur_descr either doesn't use
13980 DW_OP_*piece operations, or their sum is equal
13981 to bitsize. Otherwise we can't embed it. */
13982 for (tail = &cur_descr; *tail != NULL;
13983 tail = &(*tail)->dw_loc_next)
13984 if ((*tail)->dw_loc_opc == DW_OP_piece)
13985 {
13986 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13987 * BITS_PER_UNIT;
13988 last = *tail;
13989 }
13990 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13991 {
13992 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13993 last = *tail;
13994 }
13995
13996 if (last != NULL && opsize != bitsize)
13997 {
13998 padsize += bitsize;
13999 /* Discard the current piece of the descriptor and release any
14000 addr_table entries it uses. */
14001 remove_loc_list_addr_table_entries (cur_descr);
14002 continue;
14003 }
14004
14005 /* If there is a hole, add DW_OP_*piece after empty DWARF
14006 expression, which means that those bits are optimized out. */
14007 if (padsize)
14008 {
14009 if (padsize > decl_size)
14010 {
14011 remove_loc_list_addr_table_entries (cur_descr);
14012 goto discard_descr;
14013 }
14014 decl_size -= padsize;
14015 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14016 if (*descr_tail == NULL)
14017 {
14018 remove_loc_list_addr_table_entries (cur_descr);
14019 goto discard_descr;
14020 }
14021 descr_tail = &(*descr_tail)->dw_loc_next;
14022 padsize = 0;
14023 }
14024 *descr_tail = cur_descr;
14025 descr_tail = tail;
14026 if (bitsize > decl_size)
14027 goto discard_descr;
14028 decl_size -= bitsize;
14029 if (last == NULL)
14030 {
14031 HOST_WIDE_INT offset = 0;
14032 if (GET_CODE (varloc) == VAR_LOCATION
14033 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14034 {
14035 varloc = PAT_VAR_LOCATION_LOC (varloc);
14036 if (GET_CODE (varloc) == EXPR_LIST)
14037 varloc = XEXP (varloc, 0);
14038 }
14039 do
14040 {
14041 if (GET_CODE (varloc) == CONST
14042 || GET_CODE (varloc) == SIGN_EXTEND
14043 || GET_CODE (varloc) == ZERO_EXTEND)
14044 varloc = XEXP (varloc, 0);
14045 else if (GET_CODE (varloc) == SUBREG)
14046 varloc = SUBREG_REG (varloc);
14047 else
14048 break;
14049 }
14050 while (1);
14051 /* DW_OP_bit_size offset should be zero for register
14052 or implicit location descriptions and empty location
14053 descriptions, but for memory addresses needs big endian
14054 adjustment. */
14055 if (MEM_P (varloc))
14056 {
14057 unsigned HOST_WIDE_INT memsize
14058 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14059 if (memsize != bitsize)
14060 {
14061 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14062 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14063 goto discard_descr;
14064 if (memsize < bitsize)
14065 goto discard_descr;
14066 if (BITS_BIG_ENDIAN)
14067 offset = memsize - bitsize;
14068 }
14069 }
14070
14071 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14072 if (*descr_tail == NULL)
14073 goto discard_descr;
14074 descr_tail = &(*descr_tail)->dw_loc_next;
14075 }
14076 }
14077
14078 /* If there were any non-empty expressions, add padding till the end of
14079 the decl. */
14080 if (descr != NULL && decl_size != 0)
14081 {
14082 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14083 if (*descr_tail == NULL)
14084 goto discard_descr;
14085 }
14086 return descr;
14087
14088 discard_descr:
14089 /* Discard the descriptor and release any addr_table entries it uses. */
14090 remove_loc_list_addr_table_entries (descr);
14091 return NULL;
14092 }
14093
14094 /* Return the dwarf representation of the location list LOC_LIST of
14095 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14096 function. */
14097
14098 static dw_loc_list_ref
14099 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14100 {
14101 const char *endname, *secname;
14102 rtx varloc;
14103 enum var_init_status initialized;
14104 struct var_loc_node *node;
14105 dw_loc_descr_ref descr;
14106 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14107 dw_loc_list_ref list = NULL;
14108 dw_loc_list_ref *listp = &list;
14109
14110 /* Now that we know what section we are using for a base,
14111 actually construct the list of locations.
14112 The first location information is what is passed to the
14113 function that creates the location list, and the remaining
14114 locations just get added on to that list.
14115 Note that we only know the start address for a location
14116 (IE location changes), so to build the range, we use
14117 the range [current location start, next location start].
14118 This means we have to special case the last node, and generate
14119 a range of [last location start, end of function label]. */
14120
14121 secname = secname_for_decl (decl);
14122
14123 for (node = loc_list->first; node; node = node->next)
14124 if (GET_CODE (node->loc) == EXPR_LIST
14125 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14126 {
14127 if (GET_CODE (node->loc) == EXPR_LIST)
14128 {
14129 /* This requires DW_OP_{,bit_}piece, which is not usable
14130 inside DWARF expressions. */
14131 if (want_address != 2)
14132 continue;
14133 descr = dw_sra_loc_expr (decl, node->loc);
14134 if (descr == NULL)
14135 continue;
14136 }
14137 else
14138 {
14139 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14140 varloc = NOTE_VAR_LOCATION (node->loc);
14141 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14142 }
14143 if (descr)
14144 {
14145 bool range_across_switch = false;
14146 /* If section switch happens in between node->label
14147 and node->next->label (or end of function) and
14148 we can't emit it as a single entry list,
14149 emit two ranges, first one ending at the end
14150 of first partition and second one starting at the
14151 beginning of second partition. */
14152 if (node == loc_list->last_before_switch
14153 && (node != loc_list->first || loc_list->first->next)
14154 && current_function_decl)
14155 {
14156 endname = cfun->fde->dw_fde_end;
14157 range_across_switch = true;
14158 }
14159 /* The variable has a location between NODE->LABEL and
14160 NODE->NEXT->LABEL. */
14161 else if (node->next)
14162 endname = node->next->label;
14163 /* If the variable has a location at the last label
14164 it keeps its location until the end of function. */
14165 else if (!current_function_decl)
14166 endname = text_end_label;
14167 else
14168 {
14169 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14170 current_function_funcdef_no);
14171 endname = ggc_strdup (label_id);
14172 }
14173
14174 *listp = new_loc_list (descr, node->label, endname, secname);
14175 if (TREE_CODE (decl) == PARM_DECL
14176 && node == loc_list->first
14177 && NOTE_P (node->loc)
14178 && strcmp (node->label, endname) == 0)
14179 (*listp)->force = true;
14180 listp = &(*listp)->dw_loc_next;
14181
14182 if (range_across_switch)
14183 {
14184 if (GET_CODE (node->loc) == EXPR_LIST)
14185 descr = dw_sra_loc_expr (decl, node->loc);
14186 else
14187 {
14188 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14189 varloc = NOTE_VAR_LOCATION (node->loc);
14190 descr = dw_loc_list_1 (decl, varloc, want_address,
14191 initialized);
14192 }
14193 gcc_assert (descr);
14194 /* The variable has a location between NODE->LABEL and
14195 NODE->NEXT->LABEL. */
14196 if (node->next)
14197 endname = node->next->label;
14198 else
14199 endname = cfun->fde->dw_fde_second_end;
14200 *listp = new_loc_list (descr,
14201 cfun->fde->dw_fde_second_begin,
14202 endname, secname);
14203 listp = &(*listp)->dw_loc_next;
14204 }
14205 }
14206 }
14207
14208 /* Try to avoid the overhead of a location list emitting a location
14209 expression instead, but only if we didn't have more than one
14210 location entry in the first place. If some entries were not
14211 representable, we don't want to pretend a single entry that was
14212 applies to the entire scope in which the variable is
14213 available. */
14214 if (list && loc_list->first->next)
14215 gen_llsym (list);
14216
14217 return list;
14218 }
14219
14220 /* Return if the loc_list has only single element and thus can be represented
14221 as location description. */
14222
14223 static bool
14224 single_element_loc_list_p (dw_loc_list_ref list)
14225 {
14226 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14227 return !list->ll_symbol;
14228 }
14229
14230 /* To each location in list LIST add loc descr REF. */
14231
14232 static void
14233 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14234 {
14235 dw_loc_descr_ref copy;
14236 add_loc_descr (&list->expr, ref);
14237 list = list->dw_loc_next;
14238 while (list)
14239 {
14240 copy = ggc_alloc<dw_loc_descr_node> ();
14241 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14242 add_loc_descr (&list->expr, copy);
14243 while (copy->dw_loc_next)
14244 {
14245 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14246 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14247 copy->dw_loc_next = new_copy;
14248 copy = new_copy;
14249 }
14250 list = list->dw_loc_next;
14251 }
14252 }
14253
14254 /* Given two lists RET and LIST
14255 produce location list that is result of adding expression in LIST
14256 to expression in RET on each position in program.
14257 Might be destructive on both RET and LIST.
14258
14259 TODO: We handle only simple cases of RET or LIST having at most one
14260 element. General case would inolve sorting the lists in program order
14261 and merging them that will need some additional work.
14262 Adding that will improve quality of debug info especially for SRA-ed
14263 structures. */
14264
14265 static void
14266 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14267 {
14268 if (!list)
14269 return;
14270 if (!*ret)
14271 {
14272 *ret = list;
14273 return;
14274 }
14275 if (!list->dw_loc_next)
14276 {
14277 add_loc_descr_to_each (*ret, list->expr);
14278 return;
14279 }
14280 if (!(*ret)->dw_loc_next)
14281 {
14282 add_loc_descr_to_each (list, (*ret)->expr);
14283 *ret = list;
14284 return;
14285 }
14286 expansion_failed (NULL_TREE, NULL_RTX,
14287 "Don't know how to merge two non-trivial"
14288 " location lists.\n");
14289 *ret = NULL;
14290 return;
14291 }
14292
14293 /* LOC is constant expression. Try a luck, look it up in constant
14294 pool and return its loc_descr of its address. */
14295
14296 static dw_loc_descr_ref
14297 cst_pool_loc_descr (tree loc)
14298 {
14299 /* Get an RTL for this, if something has been emitted. */
14300 rtx rtl = lookup_constant_def (loc);
14301
14302 if (!rtl || !MEM_P (rtl))
14303 {
14304 gcc_assert (!rtl);
14305 return 0;
14306 }
14307 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14308
14309 /* TODO: We might get more coverage if we was actually delaying expansion
14310 of all expressions till end of compilation when constant pools are fully
14311 populated. */
14312 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14313 {
14314 expansion_failed (loc, NULL_RTX,
14315 "CST value in contant pool but not marked.");
14316 return 0;
14317 }
14318 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14319 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14320 }
14321
14322 /* Return dw_loc_list representing address of addr_expr LOC
14323 by looking for inner INDIRECT_REF expression and turning
14324 it into simple arithmetics.
14325
14326 See loc_list_from_tree for the meaning of CONTEXT. */
14327
14328 static dw_loc_list_ref
14329 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14330 const loc_descr_context *context)
14331 {
14332 tree obj, offset;
14333 HOST_WIDE_INT bitsize, bitpos, bytepos;
14334 machine_mode mode;
14335 int unsignedp, volatilep = 0;
14336 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14337
14338 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14339 &bitsize, &bitpos, &offset, &mode,
14340 &unsignedp, &volatilep, false);
14341 STRIP_NOPS (obj);
14342 if (bitpos % BITS_PER_UNIT)
14343 {
14344 expansion_failed (loc, NULL_RTX, "bitfield access");
14345 return 0;
14346 }
14347 if (!INDIRECT_REF_P (obj))
14348 {
14349 expansion_failed (obj,
14350 NULL_RTX, "no indirect ref in inner refrence");
14351 return 0;
14352 }
14353 if (!offset && !bitpos)
14354 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14355 context);
14356 else if (toplev
14357 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14358 && (dwarf_version >= 4 || !dwarf_strict))
14359 {
14360 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14361 if (!list_ret)
14362 return 0;
14363 if (offset)
14364 {
14365 /* Variable offset. */
14366 list_ret1 = loc_list_from_tree (offset, 0, context);
14367 if (list_ret1 == 0)
14368 return 0;
14369 add_loc_list (&list_ret, list_ret1);
14370 if (!list_ret)
14371 return 0;
14372 add_loc_descr_to_each (list_ret,
14373 new_loc_descr (DW_OP_plus, 0, 0));
14374 }
14375 bytepos = bitpos / BITS_PER_UNIT;
14376 if (bytepos > 0)
14377 add_loc_descr_to_each (list_ret,
14378 new_loc_descr (DW_OP_plus_uconst,
14379 bytepos, 0));
14380 else if (bytepos < 0)
14381 loc_list_plus_const (list_ret, bytepos);
14382 add_loc_descr_to_each (list_ret,
14383 new_loc_descr (DW_OP_stack_value, 0, 0));
14384 }
14385 return list_ret;
14386 }
14387
14388
14389 /* Helper structure for location descriptions generation. */
14390 struct loc_descr_context
14391 {
14392 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14393 NULL_TREE if DW_OP_push_object_address in invalid for this location
14394 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14395 tree context_type;
14396 /* The ..._DECL node that should be translated as a
14397 DW_OP_push_object_address operation. */
14398 tree base_decl;
14399 };
14400
14401 /* Generate Dwarf location list representing LOC.
14402 If WANT_ADDRESS is false, expression computing LOC will be computed
14403 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14404 if WANT_ADDRESS is 2, expression computing address useable in location
14405 will be returned (i.e. DW_OP_reg can be used
14406 to refer to register values).
14407
14408 CONTEXT provides information to customize the location descriptions
14409 generation. Its context_type field specifies what type is implicitly
14410 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14411 will not be generated.
14412
14413 If CONTEXT is NULL, the behavior is the same as if both context_type and
14414 base_decl fields were NULL_TREE. */
14415
14416 static dw_loc_list_ref
14417 loc_list_from_tree (tree loc, int want_address,
14418 const struct loc_descr_context *context)
14419 {
14420 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14421 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14422 int have_address = 0;
14423 enum dwarf_location_atom op;
14424
14425 /* ??? Most of the time we do not take proper care for sign/zero
14426 extending the values properly. Hopefully this won't be a real
14427 problem... */
14428
14429 if (context != NULL
14430 && context->base_decl == loc
14431 && want_address == 0)
14432 {
14433 if (dwarf_version >= 3 || !dwarf_strict)
14434 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14435 NULL, NULL, NULL);
14436 else
14437 return NULL;
14438 }
14439
14440 switch (TREE_CODE (loc))
14441 {
14442 case ERROR_MARK:
14443 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14444 return 0;
14445
14446 case PLACEHOLDER_EXPR:
14447 /* This case involves extracting fields from an object to determine the
14448 position of other fields. It is supposed to appear only as the first
14449 operand of COMPONENT_REF nodes and to reference precisely the type
14450 that the context allows. */
14451 if (context != NULL
14452 && TREE_TYPE (loc) == context->context_type
14453 && want_address >= 1)
14454 {
14455 if (dwarf_version >= 3 || !dwarf_strict)
14456 {
14457 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14458 have_address = 1;
14459 break;
14460 }
14461 else
14462 return NULL;
14463 }
14464 else
14465 expansion_failed (loc, NULL_RTX,
14466 "PLACEHOLDER_EXPR for an unexpected type");
14467 break;
14468
14469 case CALL_EXPR:
14470 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14471 /* There are no opcodes for these operations. */
14472 return 0;
14473
14474 case PREINCREMENT_EXPR:
14475 case PREDECREMENT_EXPR:
14476 case POSTINCREMENT_EXPR:
14477 case POSTDECREMENT_EXPR:
14478 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14479 /* There are no opcodes for these operations. */
14480 return 0;
14481
14482 case ADDR_EXPR:
14483 /* If we already want an address, see if there is INDIRECT_REF inside
14484 e.g. for &this->field. */
14485 if (want_address)
14486 {
14487 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14488 (loc, want_address == 2, context);
14489 if (list_ret)
14490 have_address = 1;
14491 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14492 && (ret = cst_pool_loc_descr (loc)))
14493 have_address = 1;
14494 }
14495 /* Otherwise, process the argument and look for the address. */
14496 if (!list_ret && !ret)
14497 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14498 else
14499 {
14500 if (want_address)
14501 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14502 return NULL;
14503 }
14504 break;
14505
14506 case VAR_DECL:
14507 if (DECL_THREAD_LOCAL_P (loc))
14508 {
14509 rtx rtl;
14510 enum dwarf_location_atom tls_op;
14511 enum dtprel_bool dtprel = dtprel_false;
14512
14513 if (targetm.have_tls)
14514 {
14515 /* If this is not defined, we have no way to emit the
14516 data. */
14517 if (!targetm.asm_out.output_dwarf_dtprel)
14518 return 0;
14519
14520 /* The way DW_OP_GNU_push_tls_address is specified, we
14521 can only look up addresses of objects in the current
14522 module. We used DW_OP_addr as first op, but that's
14523 wrong, because DW_OP_addr is relocated by the debug
14524 info consumer, while DW_OP_GNU_push_tls_address
14525 operand shouldn't be. */
14526 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14527 return 0;
14528 dtprel = dtprel_true;
14529 tls_op = DW_OP_GNU_push_tls_address;
14530 }
14531 else
14532 {
14533 if (!targetm.emutls.debug_form_tls_address
14534 || !(dwarf_version >= 3 || !dwarf_strict))
14535 return 0;
14536 /* We stuffed the control variable into the DECL_VALUE_EXPR
14537 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14538 no longer appear in gimple code. We used the control
14539 variable in specific so that we could pick it up here. */
14540 loc = DECL_VALUE_EXPR (loc);
14541 tls_op = DW_OP_form_tls_address;
14542 }
14543
14544 rtl = rtl_for_decl_location (loc);
14545 if (rtl == NULL_RTX)
14546 return 0;
14547
14548 if (!MEM_P (rtl))
14549 return 0;
14550 rtl = XEXP (rtl, 0);
14551 if (! CONSTANT_P (rtl))
14552 return 0;
14553
14554 ret = new_addr_loc_descr (rtl, dtprel);
14555 ret1 = new_loc_descr (tls_op, 0, 0);
14556 add_loc_descr (&ret, ret1);
14557
14558 have_address = 1;
14559 break;
14560 }
14561 /* FALLTHRU */
14562
14563 case PARM_DECL:
14564 case RESULT_DECL:
14565 if (DECL_HAS_VALUE_EXPR_P (loc))
14566 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14567 want_address, context);
14568 /* FALLTHRU */
14569
14570 case FUNCTION_DECL:
14571 {
14572 rtx rtl;
14573 var_loc_list *loc_list = lookup_decl_loc (loc);
14574
14575 if (loc_list && loc_list->first)
14576 {
14577 list_ret = dw_loc_list (loc_list, loc, want_address);
14578 have_address = want_address != 0;
14579 break;
14580 }
14581 rtl = rtl_for_decl_location (loc);
14582 if (rtl == NULL_RTX)
14583 {
14584 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14585 return 0;
14586 }
14587 else if (CONST_INT_P (rtl))
14588 {
14589 HOST_WIDE_INT val = INTVAL (rtl);
14590 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14591 val &= GET_MODE_MASK (DECL_MODE (loc));
14592 ret = int_loc_descriptor (val);
14593 }
14594 else if (GET_CODE (rtl) == CONST_STRING)
14595 {
14596 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14597 return 0;
14598 }
14599 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14600 ret = new_addr_loc_descr (rtl, dtprel_false);
14601 else
14602 {
14603 machine_mode mode, mem_mode;
14604
14605 /* Certain constructs can only be represented at top-level. */
14606 if (want_address == 2)
14607 {
14608 ret = loc_descriptor (rtl, VOIDmode,
14609 VAR_INIT_STATUS_INITIALIZED);
14610 have_address = 1;
14611 }
14612 else
14613 {
14614 mode = GET_MODE (rtl);
14615 mem_mode = VOIDmode;
14616 if (MEM_P (rtl))
14617 {
14618 mem_mode = mode;
14619 mode = get_address_mode (rtl);
14620 rtl = XEXP (rtl, 0);
14621 have_address = 1;
14622 }
14623 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14624 VAR_INIT_STATUS_INITIALIZED);
14625 }
14626 if (!ret)
14627 expansion_failed (loc, rtl,
14628 "failed to produce loc descriptor for rtl");
14629 }
14630 }
14631 break;
14632
14633 case MEM_REF:
14634 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14635 {
14636 have_address = 1;
14637 goto do_plus;
14638 }
14639 /* Fallthru. */
14640 case INDIRECT_REF:
14641 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14642 have_address = 1;
14643 break;
14644
14645 case TARGET_MEM_REF:
14646 case SSA_NAME:
14647 case DEBUG_EXPR_DECL:
14648 return NULL;
14649
14650 case COMPOUND_EXPR:
14651 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14652
14653 CASE_CONVERT:
14654 case VIEW_CONVERT_EXPR:
14655 case SAVE_EXPR:
14656 case MODIFY_EXPR:
14657 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14658
14659 case COMPONENT_REF:
14660 case BIT_FIELD_REF:
14661 case ARRAY_REF:
14662 case ARRAY_RANGE_REF:
14663 case REALPART_EXPR:
14664 case IMAGPART_EXPR:
14665 {
14666 tree obj, offset;
14667 HOST_WIDE_INT bitsize, bitpos, bytepos;
14668 machine_mode mode;
14669 int unsignedp, volatilep = 0;
14670
14671 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14672 &unsignedp, &volatilep, false);
14673
14674 gcc_assert (obj != loc);
14675
14676 list_ret = loc_list_from_tree (obj,
14677 want_address == 2
14678 && !bitpos && !offset ? 2 : 1,
14679 context);
14680 /* TODO: We can extract value of the small expression via shifting even
14681 for nonzero bitpos. */
14682 if (list_ret == 0)
14683 return 0;
14684 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14685 {
14686 expansion_failed (loc, NULL_RTX,
14687 "bitfield access");
14688 return 0;
14689 }
14690
14691 if (offset != NULL_TREE)
14692 {
14693 /* Variable offset. */
14694 list_ret1 = loc_list_from_tree (offset, 0, context);
14695 if (list_ret1 == 0)
14696 return 0;
14697 add_loc_list (&list_ret, list_ret1);
14698 if (!list_ret)
14699 return 0;
14700 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14701 }
14702
14703 bytepos = bitpos / BITS_PER_UNIT;
14704 if (bytepos > 0)
14705 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14706 else if (bytepos < 0)
14707 loc_list_plus_const (list_ret, bytepos);
14708
14709 have_address = 1;
14710 break;
14711 }
14712
14713 case INTEGER_CST:
14714 if ((want_address || !tree_fits_shwi_p (loc))
14715 && (ret = cst_pool_loc_descr (loc)))
14716 have_address = 1;
14717 else if (want_address == 2
14718 && tree_fits_shwi_p (loc)
14719 && (ret = address_of_int_loc_descriptor
14720 (int_size_in_bytes (TREE_TYPE (loc)),
14721 tree_to_shwi (loc))))
14722 have_address = 1;
14723 else if (tree_fits_shwi_p (loc))
14724 ret = int_loc_descriptor (tree_to_shwi (loc));
14725 else
14726 {
14727 expansion_failed (loc, NULL_RTX,
14728 "Integer operand is not host integer");
14729 return 0;
14730 }
14731 break;
14732
14733 case CONSTRUCTOR:
14734 case REAL_CST:
14735 case STRING_CST:
14736 case COMPLEX_CST:
14737 if ((ret = cst_pool_loc_descr (loc)))
14738 have_address = 1;
14739 else
14740 /* We can construct small constants here using int_loc_descriptor. */
14741 expansion_failed (loc, NULL_RTX,
14742 "constructor or constant not in constant pool");
14743 break;
14744
14745 case TRUTH_AND_EXPR:
14746 case TRUTH_ANDIF_EXPR:
14747 case BIT_AND_EXPR:
14748 op = DW_OP_and;
14749 goto do_binop;
14750
14751 case TRUTH_XOR_EXPR:
14752 case BIT_XOR_EXPR:
14753 op = DW_OP_xor;
14754 goto do_binop;
14755
14756 case TRUTH_OR_EXPR:
14757 case TRUTH_ORIF_EXPR:
14758 case BIT_IOR_EXPR:
14759 op = DW_OP_or;
14760 goto do_binop;
14761
14762 case FLOOR_DIV_EXPR:
14763 case CEIL_DIV_EXPR:
14764 case ROUND_DIV_EXPR:
14765 case TRUNC_DIV_EXPR:
14766 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14767 return 0;
14768 op = DW_OP_div;
14769 goto do_binop;
14770
14771 case MINUS_EXPR:
14772 op = DW_OP_minus;
14773 goto do_binop;
14774
14775 case FLOOR_MOD_EXPR:
14776 case CEIL_MOD_EXPR:
14777 case ROUND_MOD_EXPR:
14778 case TRUNC_MOD_EXPR:
14779 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14780 {
14781 op = DW_OP_mod;
14782 goto do_binop;
14783 }
14784 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14785 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14786 if (list_ret == 0 || list_ret1 == 0)
14787 return 0;
14788
14789 add_loc_list (&list_ret, list_ret1);
14790 if (list_ret == 0)
14791 return 0;
14792 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14793 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14794 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14795 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14796 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14797 break;
14798
14799 case MULT_EXPR:
14800 op = DW_OP_mul;
14801 goto do_binop;
14802
14803 case LSHIFT_EXPR:
14804 op = DW_OP_shl;
14805 goto do_binop;
14806
14807 case RSHIFT_EXPR:
14808 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14809 goto do_binop;
14810
14811 case POINTER_PLUS_EXPR:
14812 case PLUS_EXPR:
14813 do_plus:
14814 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14815 {
14816 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14817 if (list_ret == 0)
14818 return 0;
14819
14820 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14821 break;
14822 }
14823
14824 op = DW_OP_plus;
14825 goto do_binop;
14826
14827 case LE_EXPR:
14828 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14829 return 0;
14830
14831 op = DW_OP_le;
14832 goto do_binop;
14833
14834 case GE_EXPR:
14835 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14836 return 0;
14837
14838 op = DW_OP_ge;
14839 goto do_binop;
14840
14841 case LT_EXPR:
14842 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14843 return 0;
14844
14845 op = DW_OP_lt;
14846 goto do_binop;
14847
14848 case GT_EXPR:
14849 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14850 return 0;
14851
14852 op = DW_OP_gt;
14853 goto do_binop;
14854
14855 case EQ_EXPR:
14856 op = DW_OP_eq;
14857 goto do_binop;
14858
14859 case NE_EXPR:
14860 op = DW_OP_ne;
14861 goto do_binop;
14862
14863 do_binop:
14864 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14865 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14866 if (list_ret == 0 || list_ret1 == 0)
14867 return 0;
14868
14869 add_loc_list (&list_ret, list_ret1);
14870 if (list_ret == 0)
14871 return 0;
14872 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14873 break;
14874
14875 case TRUTH_NOT_EXPR:
14876 case BIT_NOT_EXPR:
14877 op = DW_OP_not;
14878 goto do_unop;
14879
14880 case ABS_EXPR:
14881 op = DW_OP_abs;
14882 goto do_unop;
14883
14884 case NEGATE_EXPR:
14885 op = DW_OP_neg;
14886 goto do_unop;
14887
14888 do_unop:
14889 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14890 if (list_ret == 0)
14891 return 0;
14892
14893 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14894 break;
14895
14896 case MIN_EXPR:
14897 case MAX_EXPR:
14898 {
14899 const enum tree_code code =
14900 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14901
14902 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14903 build2 (code, integer_type_node,
14904 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14905 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14906 }
14907
14908 /* ... fall through ... */
14909
14910 case COND_EXPR:
14911 {
14912 dw_loc_descr_ref lhs
14913 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14914 dw_loc_list_ref rhs
14915 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14916 dw_loc_descr_ref bra_node, jump_node, tmp;
14917
14918 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14919 if (list_ret == 0 || lhs == 0 || rhs == 0)
14920 return 0;
14921
14922 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14923 add_loc_descr_to_each (list_ret, bra_node);
14924
14925 add_loc_list (&list_ret, rhs);
14926 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14927 add_loc_descr_to_each (list_ret, jump_node);
14928
14929 add_loc_descr_to_each (list_ret, lhs);
14930 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14931 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14932
14933 /* ??? Need a node to point the skip at. Use a nop. */
14934 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14935 add_loc_descr_to_each (list_ret, tmp);
14936 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14937 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14938 }
14939 break;
14940
14941 case FIX_TRUNC_EXPR:
14942 return 0;
14943
14944 default:
14945 /* Leave front-end specific codes as simply unknown. This comes
14946 up, for instance, with the C STMT_EXPR. */
14947 if ((unsigned int) TREE_CODE (loc)
14948 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14949 {
14950 expansion_failed (loc, NULL_RTX,
14951 "language specific tree node");
14952 return 0;
14953 }
14954
14955 #ifdef ENABLE_CHECKING
14956 /* Otherwise this is a generic code; we should just lists all of
14957 these explicitly. We forgot one. */
14958 gcc_unreachable ();
14959 #else
14960 /* In a release build, we want to degrade gracefully: better to
14961 generate incomplete debugging information than to crash. */
14962 return NULL;
14963 #endif
14964 }
14965
14966 if (!ret && !list_ret)
14967 return 0;
14968
14969 if (want_address == 2 && !have_address
14970 && (dwarf_version >= 4 || !dwarf_strict))
14971 {
14972 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14973 {
14974 expansion_failed (loc, NULL_RTX,
14975 "DWARF address size mismatch");
14976 return 0;
14977 }
14978 if (ret)
14979 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14980 else
14981 add_loc_descr_to_each (list_ret,
14982 new_loc_descr (DW_OP_stack_value, 0, 0));
14983 have_address = 1;
14984 }
14985 /* Show if we can't fill the request for an address. */
14986 if (want_address && !have_address)
14987 {
14988 expansion_failed (loc, NULL_RTX,
14989 "Want address and only have value");
14990 return 0;
14991 }
14992
14993 gcc_assert (!ret || !list_ret);
14994
14995 /* If we've got an address and don't want one, dereference. */
14996 if (!want_address && have_address)
14997 {
14998 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14999
15000 if (size > DWARF2_ADDR_SIZE || size == -1)
15001 {
15002 expansion_failed (loc, NULL_RTX,
15003 "DWARF address size mismatch");
15004 return 0;
15005 }
15006 else if (size == DWARF2_ADDR_SIZE)
15007 op = DW_OP_deref;
15008 else
15009 op = DW_OP_deref_size;
15010
15011 if (ret)
15012 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15013 else
15014 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15015 }
15016 if (ret)
15017 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15018
15019 return list_ret;
15020 }
15021
15022 /* Same as above but return only single location expression. */
15023 static dw_loc_descr_ref
15024 loc_descriptor_from_tree (tree loc, int want_address,
15025 const struct loc_descr_context *context)
15026 {
15027 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15028 if (!ret)
15029 return NULL;
15030 if (ret->dw_loc_next)
15031 {
15032 expansion_failed (loc, NULL_RTX,
15033 "Location list where only loc descriptor needed");
15034 return NULL;
15035 }
15036 return ret->expr;
15037 }
15038
15039 /* Given a value, round it up to the lowest multiple of `boundary'
15040 which is not less than the value itself. */
15041
15042 static inline HOST_WIDE_INT
15043 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15044 {
15045 return (((value + boundary - 1) / boundary) * boundary);
15046 }
15047
15048 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15049 pointer to the declared type for the relevant field variable, or return
15050 `integer_type_node' if the given node turns out to be an
15051 ERROR_MARK node. */
15052
15053 static inline tree
15054 field_type (const_tree decl)
15055 {
15056 tree type;
15057
15058 if (TREE_CODE (decl) == ERROR_MARK)
15059 return integer_type_node;
15060
15061 type = DECL_BIT_FIELD_TYPE (decl);
15062 if (type == NULL_TREE)
15063 type = TREE_TYPE (decl);
15064
15065 return type;
15066 }
15067
15068 /* Given a pointer to a tree node, return the alignment in bits for
15069 it, or else return BITS_PER_WORD if the node actually turns out to
15070 be an ERROR_MARK node. */
15071
15072 static inline unsigned
15073 simple_type_align_in_bits (const_tree type)
15074 {
15075 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15076 }
15077
15078 static inline unsigned
15079 simple_decl_align_in_bits (const_tree decl)
15080 {
15081 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15082 }
15083
15084 /* Return the result of rounding T up to ALIGN. */
15085
15086 static inline offset_int
15087 round_up_to_align (const offset_int &t, unsigned int align)
15088 {
15089 return wi::udiv_trunc (t + align - 1, align) * align;
15090 }
15091
15092 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15093 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15094 or return 0 if we are unable to determine what that offset is, either
15095 because the argument turns out to be a pointer to an ERROR_MARK node, or
15096 because the offset is actually variable. (We can't handle the latter case
15097 just yet). */
15098
15099 static HOST_WIDE_INT
15100 field_byte_offset (const_tree decl)
15101 {
15102 offset_int object_offset_in_bits;
15103 offset_int object_offset_in_bytes;
15104 offset_int bitpos_int;
15105
15106 if (TREE_CODE (decl) == ERROR_MARK)
15107 return 0;
15108
15109 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15110
15111 /* We cannot yet cope with fields whose positions are variable, so
15112 for now, when we see such things, we simply return 0. Someday, we may
15113 be able to handle such cases, but it will be damn difficult. */
15114 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15115 return 0;
15116
15117 bitpos_int = wi::to_offset (bit_position (decl));
15118
15119 if (PCC_BITFIELD_TYPE_MATTERS)
15120 {
15121 tree type;
15122 tree field_size_tree;
15123 offset_int deepest_bitpos;
15124 offset_int field_size_in_bits;
15125 unsigned int type_align_in_bits;
15126 unsigned int decl_align_in_bits;
15127 offset_int type_size_in_bits;
15128
15129 type = field_type (decl);
15130 type_size_in_bits = offset_int_type_size_in_bits (type);
15131 type_align_in_bits = simple_type_align_in_bits (type);
15132
15133 field_size_tree = DECL_SIZE (decl);
15134
15135 /* The size could be unspecified if there was an error, or for
15136 a flexible array member. */
15137 if (!field_size_tree)
15138 field_size_tree = bitsize_zero_node;
15139
15140 /* If the size of the field is not constant, use the type size. */
15141 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15142 field_size_in_bits = wi::to_offset (field_size_tree);
15143 else
15144 field_size_in_bits = type_size_in_bits;
15145
15146 decl_align_in_bits = simple_decl_align_in_bits (decl);
15147
15148 /* The GCC front-end doesn't make any attempt to keep track of the
15149 starting bit offset (relative to the start of the containing
15150 structure type) of the hypothetical "containing object" for a
15151 bit-field. Thus, when computing the byte offset value for the
15152 start of the "containing object" of a bit-field, we must deduce
15153 this information on our own. This can be rather tricky to do in
15154 some cases. For example, handling the following structure type
15155 definition when compiling for an i386/i486 target (which only
15156 aligns long long's to 32-bit boundaries) can be very tricky:
15157
15158 struct S { int field1; long long field2:31; };
15159
15160 Fortunately, there is a simple rule-of-thumb which can be used
15161 in such cases. When compiling for an i386/i486, GCC will
15162 allocate 8 bytes for the structure shown above. It decides to
15163 do this based upon one simple rule for bit-field allocation.
15164 GCC allocates each "containing object" for each bit-field at
15165 the first (i.e. lowest addressed) legitimate alignment boundary
15166 (based upon the required minimum alignment for the declared
15167 type of the field) which it can possibly use, subject to the
15168 condition that there is still enough available space remaining
15169 in the containing object (when allocated at the selected point)
15170 to fully accommodate all of the bits of the bit-field itself.
15171
15172 This simple rule makes it obvious why GCC allocates 8 bytes for
15173 each object of the structure type shown above. When looking
15174 for a place to allocate the "containing object" for `field2',
15175 the compiler simply tries to allocate a 64-bit "containing
15176 object" at each successive 32-bit boundary (starting at zero)
15177 until it finds a place to allocate that 64- bit field such that
15178 at least 31 contiguous (and previously unallocated) bits remain
15179 within that selected 64 bit field. (As it turns out, for the
15180 example above, the compiler finds it is OK to allocate the
15181 "containing object" 64-bit field at bit-offset zero within the
15182 structure type.)
15183
15184 Here we attempt to work backwards from the limited set of facts
15185 we're given, and we try to deduce from those facts, where GCC
15186 must have believed that the containing object started (within
15187 the structure type). The value we deduce is then used (by the
15188 callers of this routine) to generate DW_AT_location and
15189 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15190 the case of DW_AT_location, regular fields as well). */
15191
15192 /* Figure out the bit-distance from the start of the structure to
15193 the "deepest" bit of the bit-field. */
15194 deepest_bitpos = bitpos_int + field_size_in_bits;
15195
15196 /* This is the tricky part. Use some fancy footwork to deduce
15197 where the lowest addressed bit of the containing object must
15198 be. */
15199 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15200
15201 /* Round up to type_align by default. This works best for
15202 bitfields. */
15203 object_offset_in_bits
15204 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15205
15206 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15207 {
15208 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15209
15210 /* Round up to decl_align instead. */
15211 object_offset_in_bits
15212 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15213 }
15214 }
15215 else
15216 object_offset_in_bits = bitpos_int;
15217
15218 object_offset_in_bytes
15219 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15220 return object_offset_in_bytes.to_shwi ();
15221 }
15222 \f
15223 /* The following routines define various Dwarf attributes and any data
15224 associated with them. */
15225
15226 /* Add a location description attribute value to a DIE.
15227
15228 This emits location attributes suitable for whole variables and
15229 whole parameters. Note that the location attributes for struct fields are
15230 generated by the routine `data_member_location_attribute' below. */
15231
15232 static inline void
15233 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15234 dw_loc_list_ref descr)
15235 {
15236 if (descr == 0)
15237 return;
15238 if (single_element_loc_list_p (descr))
15239 add_AT_loc (die, attr_kind, descr->expr);
15240 else
15241 add_AT_loc_list (die, attr_kind, descr);
15242 }
15243
15244 /* Add DW_AT_accessibility attribute to DIE if needed. */
15245
15246 static void
15247 add_accessibility_attribute (dw_die_ref die, tree decl)
15248 {
15249 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15250 children, otherwise the default is DW_ACCESS_public. In DWARF2
15251 the default has always been DW_ACCESS_public. */
15252 if (TREE_PROTECTED (decl))
15253 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15254 else if (TREE_PRIVATE (decl))
15255 {
15256 if (dwarf_version == 2
15257 || die->die_parent == NULL
15258 || die->die_parent->die_tag != DW_TAG_class_type)
15259 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15260 }
15261 else if (dwarf_version > 2
15262 && die->die_parent
15263 && die->die_parent->die_tag == DW_TAG_class_type)
15264 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15265 }
15266
15267 /* Attach the specialized form of location attribute used for data members of
15268 struct and union types. In the special case of a FIELD_DECL node which
15269 represents a bit-field, the "offset" part of this special location
15270 descriptor must indicate the distance in bytes from the lowest-addressed
15271 byte of the containing struct or union type to the lowest-addressed byte of
15272 the "containing object" for the bit-field. (See the `field_byte_offset'
15273 function above).
15274
15275 For any given bit-field, the "containing object" is a hypothetical object
15276 (of some integral or enum type) within which the given bit-field lives. The
15277 type of this hypothetical "containing object" is always the same as the
15278 declared type of the individual bit-field itself (for GCC anyway... the
15279 DWARF spec doesn't actually mandate this). Note that it is the size (in
15280 bytes) of the hypothetical "containing object" which will be given in the
15281 DW_AT_byte_size attribute for this bit-field. (See the
15282 `byte_size_attribute' function below.) It is also used when calculating the
15283 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15284 function below.) */
15285
15286 static void
15287 add_data_member_location_attribute (dw_die_ref die, tree decl)
15288 {
15289 HOST_WIDE_INT offset;
15290 dw_loc_descr_ref loc_descr = 0;
15291
15292 if (TREE_CODE (decl) == TREE_BINFO)
15293 {
15294 /* We're working on the TAG_inheritance for a base class. */
15295 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15296 {
15297 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15298 aren't at a fixed offset from all (sub)objects of the same
15299 type. We need to extract the appropriate offset from our
15300 vtable. The following dwarf expression means
15301
15302 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15303
15304 This is specific to the V3 ABI, of course. */
15305
15306 dw_loc_descr_ref tmp;
15307
15308 /* Make a copy of the object address. */
15309 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15310 add_loc_descr (&loc_descr, tmp);
15311
15312 /* Extract the vtable address. */
15313 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15314 add_loc_descr (&loc_descr, tmp);
15315
15316 /* Calculate the address of the offset. */
15317 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15318 gcc_assert (offset < 0);
15319
15320 tmp = int_loc_descriptor (-offset);
15321 add_loc_descr (&loc_descr, tmp);
15322 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15323 add_loc_descr (&loc_descr, tmp);
15324
15325 /* Extract the offset. */
15326 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15327 add_loc_descr (&loc_descr, tmp);
15328
15329 /* Add it to the object address. */
15330 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15331 add_loc_descr (&loc_descr, tmp);
15332 }
15333 else
15334 offset = tree_to_shwi (BINFO_OFFSET (decl));
15335 }
15336 else
15337 offset = field_byte_offset (decl);
15338
15339 if (! loc_descr)
15340 {
15341 if (dwarf_version > 2)
15342 {
15343 /* Don't need to output a location expression, just the constant. */
15344 if (offset < 0)
15345 add_AT_int (die, DW_AT_data_member_location, offset);
15346 else
15347 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15348 return;
15349 }
15350 else
15351 {
15352 enum dwarf_location_atom op;
15353
15354 /* The DWARF2 standard says that we should assume that the structure
15355 address is already on the stack, so we can specify a structure
15356 field address by using DW_OP_plus_uconst. */
15357 op = DW_OP_plus_uconst;
15358 loc_descr = new_loc_descr (op, offset, 0);
15359 }
15360 }
15361
15362 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15363 }
15364
15365 /* Writes integer values to dw_vec_const array. */
15366
15367 static void
15368 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15369 {
15370 while (size != 0)
15371 {
15372 *dest++ = val & 0xff;
15373 val >>= 8;
15374 --size;
15375 }
15376 }
15377
15378 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15379
15380 static HOST_WIDE_INT
15381 extract_int (const unsigned char *src, unsigned int size)
15382 {
15383 HOST_WIDE_INT val = 0;
15384
15385 src += size;
15386 while (size != 0)
15387 {
15388 val <<= 8;
15389 val |= *--src & 0xff;
15390 --size;
15391 }
15392 return val;
15393 }
15394
15395 /* Writes wide_int values to dw_vec_const array. */
15396
15397 static void
15398 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15399 {
15400 int i;
15401
15402 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15403 {
15404 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15405 return;
15406 }
15407
15408 /* We'd have to extend this code to support odd sizes. */
15409 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15410
15411 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15412
15413 if (WORDS_BIG_ENDIAN)
15414 for (i = n - 1; i >= 0; i--)
15415 {
15416 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15417 dest += sizeof (HOST_WIDE_INT);
15418 }
15419 else
15420 for (i = 0; i < n; i++)
15421 {
15422 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15423 dest += sizeof (HOST_WIDE_INT);
15424 }
15425 }
15426
15427 /* Writes floating point values to dw_vec_const array. */
15428
15429 static void
15430 insert_float (const_rtx rtl, unsigned char *array)
15431 {
15432 REAL_VALUE_TYPE rv;
15433 long val[4];
15434 int i;
15435
15436 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15437 real_to_target (val, &rv, GET_MODE (rtl));
15438
15439 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15440 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15441 {
15442 insert_int (val[i], 4, array);
15443 array += 4;
15444 }
15445 }
15446
15447 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15448 does not have a "location" either in memory or in a register. These
15449 things can arise in GNU C when a constant is passed as an actual parameter
15450 to an inlined function. They can also arise in C++ where declared
15451 constants do not necessarily get memory "homes". */
15452
15453 static bool
15454 add_const_value_attribute (dw_die_ref die, rtx rtl)
15455 {
15456 switch (GET_CODE (rtl))
15457 {
15458 case CONST_INT:
15459 {
15460 HOST_WIDE_INT val = INTVAL (rtl);
15461
15462 if (val < 0)
15463 add_AT_int (die, DW_AT_const_value, val);
15464 else
15465 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15466 }
15467 return true;
15468
15469 case CONST_WIDE_INT:
15470 add_AT_wide (die, DW_AT_const_value,
15471 std::make_pair (rtl, GET_MODE (rtl)));
15472 return true;
15473
15474 case CONST_DOUBLE:
15475 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15476 floating-point constant. A CONST_DOUBLE is used whenever the
15477 constant requires more than one word in order to be adequately
15478 represented. */
15479 {
15480 machine_mode mode = GET_MODE (rtl);
15481
15482 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15483 add_AT_double (die, DW_AT_const_value,
15484 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15485 else
15486 {
15487 unsigned int length = GET_MODE_SIZE (mode);
15488 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15489
15490 insert_float (rtl, array);
15491 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15492 }
15493 }
15494 return true;
15495
15496 case CONST_VECTOR:
15497 {
15498 machine_mode mode = GET_MODE (rtl);
15499 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15500 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15501 unsigned char *array
15502 = ggc_vec_alloc<unsigned char> (length * elt_size);
15503 unsigned int i;
15504 unsigned char *p;
15505 machine_mode imode = GET_MODE_INNER (mode);
15506
15507 switch (GET_MODE_CLASS (mode))
15508 {
15509 case MODE_VECTOR_INT:
15510 for (i = 0, p = array; i < length; i++, p += elt_size)
15511 {
15512 rtx elt = CONST_VECTOR_ELT (rtl, i);
15513 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15514 }
15515 break;
15516
15517 case MODE_VECTOR_FLOAT:
15518 for (i = 0, p = array; i < length; i++, p += elt_size)
15519 {
15520 rtx elt = CONST_VECTOR_ELT (rtl, i);
15521 insert_float (elt, p);
15522 }
15523 break;
15524
15525 default:
15526 gcc_unreachable ();
15527 }
15528
15529 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15530 }
15531 return true;
15532
15533 case CONST_STRING:
15534 if (dwarf_version >= 4 || !dwarf_strict)
15535 {
15536 dw_loc_descr_ref loc_result;
15537 resolve_one_addr (&rtl);
15538 rtl_addr:
15539 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15540 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15541 add_AT_loc (die, DW_AT_location, loc_result);
15542 vec_safe_push (used_rtx_array, rtl);
15543 return true;
15544 }
15545 return false;
15546
15547 case CONST:
15548 if (CONSTANT_P (XEXP (rtl, 0)))
15549 return add_const_value_attribute (die, XEXP (rtl, 0));
15550 /* FALLTHROUGH */
15551 case SYMBOL_REF:
15552 if (!const_ok_for_output (rtl))
15553 return false;
15554 case LABEL_REF:
15555 if (dwarf_version >= 4 || !dwarf_strict)
15556 goto rtl_addr;
15557 return false;
15558
15559 case PLUS:
15560 /* In cases where an inlined instance of an inline function is passed
15561 the address of an `auto' variable (which is local to the caller) we
15562 can get a situation where the DECL_RTL of the artificial local
15563 variable (for the inlining) which acts as a stand-in for the
15564 corresponding formal parameter (of the inline function) will look
15565 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15566 exactly a compile-time constant expression, but it isn't the address
15567 of the (artificial) local variable either. Rather, it represents the
15568 *value* which the artificial local variable always has during its
15569 lifetime. We currently have no way to represent such quasi-constant
15570 values in Dwarf, so for now we just punt and generate nothing. */
15571 return false;
15572
15573 case HIGH:
15574 case CONST_FIXED:
15575 return false;
15576
15577 case MEM:
15578 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15579 && MEM_READONLY_P (rtl)
15580 && GET_MODE (rtl) == BLKmode)
15581 {
15582 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15583 return true;
15584 }
15585 return false;
15586
15587 default:
15588 /* No other kinds of rtx should be possible here. */
15589 gcc_unreachable ();
15590 }
15591 return false;
15592 }
15593
15594 /* Determine whether the evaluation of EXPR references any variables
15595 or functions which aren't otherwise used (and therefore may not be
15596 output). */
15597 static tree
15598 reference_to_unused (tree * tp, int * walk_subtrees,
15599 void * data ATTRIBUTE_UNUSED)
15600 {
15601 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15602 *walk_subtrees = 0;
15603
15604 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15605 && ! TREE_ASM_WRITTEN (*tp))
15606 return *tp;
15607 /* ??? The C++ FE emits debug information for using decls, so
15608 putting gcc_unreachable here falls over. See PR31899. For now
15609 be conservative. */
15610 else if (!symtab->global_info_ready
15611 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15612 return *tp;
15613 else if (TREE_CODE (*tp) == VAR_DECL)
15614 {
15615 varpool_node *node = varpool_node::get (*tp);
15616 if (!node || !node->definition)
15617 return *tp;
15618 }
15619 else if (TREE_CODE (*tp) == FUNCTION_DECL
15620 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15621 {
15622 /* The call graph machinery must have finished analyzing,
15623 optimizing and gimplifying the CU by now.
15624 So if *TP has no call graph node associated
15625 to it, it means *TP will not be emitted. */
15626 if (!cgraph_node::get (*tp))
15627 return *tp;
15628 }
15629 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15630 return *tp;
15631
15632 return NULL_TREE;
15633 }
15634
15635 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15636 for use in a later add_const_value_attribute call. */
15637
15638 static rtx
15639 rtl_for_decl_init (tree init, tree type)
15640 {
15641 rtx rtl = NULL_RTX;
15642
15643 STRIP_NOPS (init);
15644
15645 /* If a variable is initialized with a string constant without embedded
15646 zeros, build CONST_STRING. */
15647 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15648 {
15649 tree enttype = TREE_TYPE (type);
15650 tree domain = TYPE_DOMAIN (type);
15651 machine_mode mode = TYPE_MODE (enttype);
15652
15653 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15654 && domain
15655 && integer_zerop (TYPE_MIN_VALUE (domain))
15656 && compare_tree_int (TYPE_MAX_VALUE (domain),
15657 TREE_STRING_LENGTH (init) - 1) == 0
15658 && ((size_t) TREE_STRING_LENGTH (init)
15659 == strlen (TREE_STRING_POINTER (init)) + 1))
15660 {
15661 rtl = gen_rtx_CONST_STRING (VOIDmode,
15662 ggc_strdup (TREE_STRING_POINTER (init)));
15663 rtl = gen_rtx_MEM (BLKmode, rtl);
15664 MEM_READONLY_P (rtl) = 1;
15665 }
15666 }
15667 /* Other aggregates, and complex values, could be represented using
15668 CONCAT: FIXME! */
15669 else if (AGGREGATE_TYPE_P (type)
15670 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15671 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15672 || TREE_CODE (type) == COMPLEX_TYPE)
15673 ;
15674 /* Vectors only work if their mode is supported by the target.
15675 FIXME: generic vectors ought to work too. */
15676 else if (TREE_CODE (type) == VECTOR_TYPE
15677 && !VECTOR_MODE_P (TYPE_MODE (type)))
15678 ;
15679 /* If the initializer is something that we know will expand into an
15680 immediate RTL constant, expand it now. We must be careful not to
15681 reference variables which won't be output. */
15682 else if (initializer_constant_valid_p (init, type)
15683 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15684 {
15685 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15686 possible. */
15687 if (TREE_CODE (type) == VECTOR_TYPE)
15688 switch (TREE_CODE (init))
15689 {
15690 case VECTOR_CST:
15691 break;
15692 case CONSTRUCTOR:
15693 if (TREE_CONSTANT (init))
15694 {
15695 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15696 bool constant_p = true;
15697 tree value;
15698 unsigned HOST_WIDE_INT ix;
15699
15700 /* Even when ctor is constant, it might contain non-*_CST
15701 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15702 belong into VECTOR_CST nodes. */
15703 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15704 if (!CONSTANT_CLASS_P (value))
15705 {
15706 constant_p = false;
15707 break;
15708 }
15709
15710 if (constant_p)
15711 {
15712 init = build_vector_from_ctor (type, elts);
15713 break;
15714 }
15715 }
15716 /* FALLTHRU */
15717
15718 default:
15719 return NULL;
15720 }
15721
15722 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15723
15724 /* If expand_expr returns a MEM, it wasn't immediate. */
15725 gcc_assert (!rtl || !MEM_P (rtl));
15726 }
15727
15728 return rtl;
15729 }
15730
15731 /* Generate RTL for the variable DECL to represent its location. */
15732
15733 static rtx
15734 rtl_for_decl_location (tree decl)
15735 {
15736 rtx rtl;
15737
15738 /* Here we have to decide where we are going to say the parameter "lives"
15739 (as far as the debugger is concerned). We only have a couple of
15740 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15741
15742 DECL_RTL normally indicates where the parameter lives during most of the
15743 activation of the function. If optimization is enabled however, this
15744 could be either NULL or else a pseudo-reg. Both of those cases indicate
15745 that the parameter doesn't really live anywhere (as far as the code
15746 generation parts of GCC are concerned) during most of the function's
15747 activation. That will happen (for example) if the parameter is never
15748 referenced within the function.
15749
15750 We could just generate a location descriptor here for all non-NULL
15751 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15752 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15753 where DECL_RTL is NULL or is a pseudo-reg.
15754
15755 Note however that we can only get away with using DECL_INCOMING_RTL as
15756 a backup substitute for DECL_RTL in certain limited cases. In cases
15757 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15758 we can be sure that the parameter was passed using the same type as it is
15759 declared to have within the function, and that its DECL_INCOMING_RTL
15760 points us to a place where a value of that type is passed.
15761
15762 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15763 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15764 because in these cases DECL_INCOMING_RTL points us to a value of some
15765 type which is *different* from the type of the parameter itself. Thus,
15766 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15767 such cases, the debugger would end up (for example) trying to fetch a
15768 `float' from a place which actually contains the first part of a
15769 `double'. That would lead to really incorrect and confusing
15770 output at debug-time.
15771
15772 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15773 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15774 are a couple of exceptions however. On little-endian machines we can
15775 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15776 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15777 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15778 when (on a little-endian machine) a non-prototyped function has a
15779 parameter declared to be of type `short' or `char'. In such cases,
15780 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15781 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15782 passed `int' value. If the debugger then uses that address to fetch
15783 a `short' or a `char' (on a little-endian machine) the result will be
15784 the correct data, so we allow for such exceptional cases below.
15785
15786 Note that our goal here is to describe the place where the given formal
15787 parameter lives during most of the function's activation (i.e. between the
15788 end of the prologue and the start of the epilogue). We'll do that as best
15789 as we can. Note however that if the given formal parameter is modified
15790 sometime during the execution of the function, then a stack backtrace (at
15791 debug-time) will show the function as having been called with the *new*
15792 value rather than the value which was originally passed in. This happens
15793 rarely enough that it is not a major problem, but it *is* a problem, and
15794 I'd like to fix it.
15795
15796 A future version of dwarf2out.c may generate two additional attributes for
15797 any given DW_TAG_formal_parameter DIE which will describe the "passed
15798 type" and the "passed location" for the given formal parameter in addition
15799 to the attributes we now generate to indicate the "declared type" and the
15800 "active location" for each parameter. This additional set of attributes
15801 could be used by debuggers for stack backtraces. Separately, note that
15802 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15803 This happens (for example) for inlined-instances of inline function formal
15804 parameters which are never referenced. This really shouldn't be
15805 happening. All PARM_DECL nodes should get valid non-NULL
15806 DECL_INCOMING_RTL values. FIXME. */
15807
15808 /* Use DECL_RTL as the "location" unless we find something better. */
15809 rtl = DECL_RTL_IF_SET (decl);
15810
15811 /* When generating abstract instances, ignore everything except
15812 constants, symbols living in memory, and symbols living in
15813 fixed registers. */
15814 if (! reload_completed)
15815 {
15816 if (rtl
15817 && (CONSTANT_P (rtl)
15818 || (MEM_P (rtl)
15819 && CONSTANT_P (XEXP (rtl, 0)))
15820 || (REG_P (rtl)
15821 && TREE_CODE (decl) == VAR_DECL
15822 && TREE_STATIC (decl))))
15823 {
15824 rtl = targetm.delegitimize_address (rtl);
15825 return rtl;
15826 }
15827 rtl = NULL_RTX;
15828 }
15829 else if (TREE_CODE (decl) == PARM_DECL)
15830 {
15831 if (rtl == NULL_RTX
15832 || is_pseudo_reg (rtl)
15833 || (MEM_P (rtl)
15834 && is_pseudo_reg (XEXP (rtl, 0))
15835 && DECL_INCOMING_RTL (decl)
15836 && MEM_P (DECL_INCOMING_RTL (decl))
15837 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15838 {
15839 tree declared_type = TREE_TYPE (decl);
15840 tree passed_type = DECL_ARG_TYPE (decl);
15841 machine_mode dmode = TYPE_MODE (declared_type);
15842 machine_mode pmode = TYPE_MODE (passed_type);
15843
15844 /* This decl represents a formal parameter which was optimized out.
15845 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15846 all cases where (rtl == NULL_RTX) just below. */
15847 if (dmode == pmode)
15848 rtl = DECL_INCOMING_RTL (decl);
15849 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15850 && SCALAR_INT_MODE_P (dmode)
15851 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15852 && DECL_INCOMING_RTL (decl))
15853 {
15854 rtx inc = DECL_INCOMING_RTL (decl);
15855 if (REG_P (inc))
15856 rtl = inc;
15857 else if (MEM_P (inc))
15858 {
15859 if (BYTES_BIG_ENDIAN)
15860 rtl = adjust_address_nv (inc, dmode,
15861 GET_MODE_SIZE (pmode)
15862 - GET_MODE_SIZE (dmode));
15863 else
15864 rtl = inc;
15865 }
15866 }
15867 }
15868
15869 /* If the parm was passed in registers, but lives on the stack, then
15870 make a big endian correction if the mode of the type of the
15871 parameter is not the same as the mode of the rtl. */
15872 /* ??? This is the same series of checks that are made in dbxout.c before
15873 we reach the big endian correction code there. It isn't clear if all
15874 of these checks are necessary here, but keeping them all is the safe
15875 thing to do. */
15876 else if (MEM_P (rtl)
15877 && XEXP (rtl, 0) != const0_rtx
15878 && ! CONSTANT_P (XEXP (rtl, 0))
15879 /* Not passed in memory. */
15880 && !MEM_P (DECL_INCOMING_RTL (decl))
15881 /* Not passed by invisible reference. */
15882 && (!REG_P (XEXP (rtl, 0))
15883 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15884 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15885 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15886 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15887 #endif
15888 )
15889 /* Big endian correction check. */
15890 && BYTES_BIG_ENDIAN
15891 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15892 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15893 < UNITS_PER_WORD))
15894 {
15895 machine_mode addr_mode = get_address_mode (rtl);
15896 int offset = (UNITS_PER_WORD
15897 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15898
15899 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15900 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15901 }
15902 }
15903 else if (TREE_CODE (decl) == VAR_DECL
15904 && rtl
15905 && MEM_P (rtl)
15906 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15907 && BYTES_BIG_ENDIAN)
15908 {
15909 machine_mode addr_mode = get_address_mode (rtl);
15910 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15911 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15912
15913 /* If a variable is declared "register" yet is smaller than
15914 a register, then if we store the variable to memory, it
15915 looks like we're storing a register-sized value, when in
15916 fact we are not. We need to adjust the offset of the
15917 storage location to reflect the actual value's bytes,
15918 else gdb will not be able to display it. */
15919 if (rsize > dsize)
15920 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15921 plus_constant (addr_mode, XEXP (rtl, 0),
15922 rsize - dsize));
15923 }
15924
15925 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15926 and will have been substituted directly into all expressions that use it.
15927 C does not have such a concept, but C++ and other languages do. */
15928 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15929 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15930
15931 if (rtl)
15932 rtl = targetm.delegitimize_address (rtl);
15933
15934 /* If we don't look past the constant pool, we risk emitting a
15935 reference to a constant pool entry that isn't referenced from
15936 code, and thus is not emitted. */
15937 if (rtl)
15938 rtl = avoid_constant_pool_reference (rtl);
15939
15940 /* Try harder to get a rtl. If this symbol ends up not being emitted
15941 in the current CU, resolve_addr will remove the expression referencing
15942 it. */
15943 if (rtl == NULL_RTX
15944 && TREE_CODE (decl) == VAR_DECL
15945 && !DECL_EXTERNAL (decl)
15946 && TREE_STATIC (decl)
15947 && DECL_NAME (decl)
15948 && !DECL_HARD_REGISTER (decl)
15949 && DECL_MODE (decl) != VOIDmode)
15950 {
15951 rtl = make_decl_rtl_for_debug (decl);
15952 if (!MEM_P (rtl)
15953 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15954 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15955 rtl = NULL_RTX;
15956 }
15957
15958 return rtl;
15959 }
15960
15961 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15962 returned. If so, the decl for the COMMON block is returned, and the
15963 value is the offset into the common block for the symbol. */
15964
15965 static tree
15966 fortran_common (tree decl, HOST_WIDE_INT *value)
15967 {
15968 tree val_expr, cvar;
15969 machine_mode mode;
15970 HOST_WIDE_INT bitsize, bitpos;
15971 tree offset;
15972 int unsignedp, volatilep = 0;
15973
15974 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15975 it does not have a value (the offset into the common area), or if it
15976 is thread local (as opposed to global) then it isn't common, and shouldn't
15977 be handled as such. */
15978 if (TREE_CODE (decl) != VAR_DECL
15979 || !TREE_STATIC (decl)
15980 || !DECL_HAS_VALUE_EXPR_P (decl)
15981 || !is_fortran ())
15982 return NULL_TREE;
15983
15984 val_expr = DECL_VALUE_EXPR (decl);
15985 if (TREE_CODE (val_expr) != COMPONENT_REF)
15986 return NULL_TREE;
15987
15988 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15989 &mode, &unsignedp, &volatilep, true);
15990
15991 if (cvar == NULL_TREE
15992 || TREE_CODE (cvar) != VAR_DECL
15993 || DECL_ARTIFICIAL (cvar)
15994 || !TREE_PUBLIC (cvar))
15995 return NULL_TREE;
15996
15997 *value = 0;
15998 if (offset != NULL)
15999 {
16000 if (!tree_fits_shwi_p (offset))
16001 return NULL_TREE;
16002 *value = tree_to_shwi (offset);
16003 }
16004 if (bitpos != 0)
16005 *value += bitpos / BITS_PER_UNIT;
16006
16007 return cvar;
16008 }
16009
16010 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16011 data attribute for a variable or a parameter. We generate the
16012 DW_AT_const_value attribute only in those cases where the given variable
16013 or parameter does not have a true "location" either in memory or in a
16014 register. This can happen (for example) when a constant is passed as an
16015 actual argument in a call to an inline function. (It's possible that
16016 these things can crop up in other ways also.) Note that one type of
16017 constant value which can be passed into an inlined function is a constant
16018 pointer. This can happen for example if an actual argument in an inlined
16019 function call evaluates to a compile-time constant address.
16020
16021 CACHE_P is true if it is worth caching the location list for DECL,
16022 so that future calls can reuse it rather than regenerate it from scratch.
16023 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16024 since we will need to refer to them each time the function is inlined. */
16025
16026 static bool
16027 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16028 enum dwarf_attribute attr)
16029 {
16030 rtx rtl;
16031 dw_loc_list_ref list;
16032 var_loc_list *loc_list;
16033 cached_dw_loc_list *cache;
16034
16035 if (TREE_CODE (decl) == ERROR_MARK)
16036 return false;
16037
16038 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16039 || TREE_CODE (decl) == RESULT_DECL);
16040
16041 /* Try to get some constant RTL for this decl, and use that as the value of
16042 the location. */
16043
16044 rtl = rtl_for_decl_location (decl);
16045 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16046 && add_const_value_attribute (die, rtl))
16047 return true;
16048
16049 /* See if we have single element location list that is equivalent to
16050 a constant value. That way we are better to use add_const_value_attribute
16051 rather than expanding constant value equivalent. */
16052 loc_list = lookup_decl_loc (decl);
16053 if (loc_list
16054 && loc_list->first
16055 && loc_list->first->next == NULL
16056 && NOTE_P (loc_list->first->loc)
16057 && NOTE_VAR_LOCATION (loc_list->first->loc)
16058 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16059 {
16060 struct var_loc_node *node;
16061
16062 node = loc_list->first;
16063 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16064 if (GET_CODE (rtl) == EXPR_LIST)
16065 rtl = XEXP (rtl, 0);
16066 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16067 && add_const_value_attribute (die, rtl))
16068 return true;
16069 }
16070 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16071 list several times. See if we've already cached the contents. */
16072 list = NULL;
16073 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16074 cache_p = false;
16075 if (cache_p)
16076 {
16077 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16078 if (cache)
16079 list = cache->loc_list;
16080 }
16081 if (list == NULL)
16082 {
16083 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16084 NULL);
16085 /* It is usually worth caching this result if the decl is from
16086 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16087 if (cache_p && list && list->dw_loc_next)
16088 {
16089 cached_dw_loc_list **slot
16090 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16091 DECL_UID (decl),
16092 INSERT);
16093 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16094 cache->decl_id = DECL_UID (decl);
16095 cache->loc_list = list;
16096 *slot = cache;
16097 }
16098 }
16099 if (list)
16100 {
16101 add_AT_location_description (die, attr, list);
16102 return true;
16103 }
16104 /* None of that worked, so it must not really have a location;
16105 try adding a constant value attribute from the DECL_INITIAL. */
16106 return tree_add_const_value_attribute_for_decl (die, decl);
16107 }
16108
16109 /* Add VARIABLE and DIE into deferred locations list. */
16110
16111 static void
16112 defer_location (tree variable, dw_die_ref die)
16113 {
16114 deferred_locations entry;
16115 entry.variable = variable;
16116 entry.die = die;
16117 vec_safe_push (deferred_locations_list, entry);
16118 }
16119
16120 /* Helper function for tree_add_const_value_attribute. Natively encode
16121 initializer INIT into an array. Return true if successful. */
16122
16123 static bool
16124 native_encode_initializer (tree init, unsigned char *array, int size)
16125 {
16126 tree type;
16127
16128 if (init == NULL_TREE)
16129 return false;
16130
16131 STRIP_NOPS (init);
16132 switch (TREE_CODE (init))
16133 {
16134 case STRING_CST:
16135 type = TREE_TYPE (init);
16136 if (TREE_CODE (type) == ARRAY_TYPE)
16137 {
16138 tree enttype = TREE_TYPE (type);
16139 machine_mode mode = TYPE_MODE (enttype);
16140
16141 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16142 return false;
16143 if (int_size_in_bytes (type) != size)
16144 return false;
16145 if (size > TREE_STRING_LENGTH (init))
16146 {
16147 memcpy (array, TREE_STRING_POINTER (init),
16148 TREE_STRING_LENGTH (init));
16149 memset (array + TREE_STRING_LENGTH (init),
16150 '\0', size - TREE_STRING_LENGTH (init));
16151 }
16152 else
16153 memcpy (array, TREE_STRING_POINTER (init), size);
16154 return true;
16155 }
16156 return false;
16157 case CONSTRUCTOR:
16158 type = TREE_TYPE (init);
16159 if (int_size_in_bytes (type) != size)
16160 return false;
16161 if (TREE_CODE (type) == ARRAY_TYPE)
16162 {
16163 HOST_WIDE_INT min_index;
16164 unsigned HOST_WIDE_INT cnt;
16165 int curpos = 0, fieldsize;
16166 constructor_elt *ce;
16167
16168 if (TYPE_DOMAIN (type) == NULL_TREE
16169 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16170 return false;
16171
16172 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16173 if (fieldsize <= 0)
16174 return false;
16175
16176 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16177 memset (array, '\0', size);
16178 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16179 {
16180 tree val = ce->value;
16181 tree index = ce->index;
16182 int pos = curpos;
16183 if (index && TREE_CODE (index) == RANGE_EXPR)
16184 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16185 * fieldsize;
16186 else if (index)
16187 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16188
16189 if (val)
16190 {
16191 STRIP_NOPS (val);
16192 if (!native_encode_initializer (val, array + pos, fieldsize))
16193 return false;
16194 }
16195 curpos = pos + fieldsize;
16196 if (index && TREE_CODE (index) == RANGE_EXPR)
16197 {
16198 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16199 - tree_to_shwi (TREE_OPERAND (index, 0));
16200 while (count-- > 0)
16201 {
16202 if (val)
16203 memcpy (array + curpos, array + pos, fieldsize);
16204 curpos += fieldsize;
16205 }
16206 }
16207 gcc_assert (curpos <= size);
16208 }
16209 return true;
16210 }
16211 else if (TREE_CODE (type) == RECORD_TYPE
16212 || TREE_CODE (type) == UNION_TYPE)
16213 {
16214 tree field = NULL_TREE;
16215 unsigned HOST_WIDE_INT cnt;
16216 constructor_elt *ce;
16217
16218 if (int_size_in_bytes (type) != size)
16219 return false;
16220
16221 if (TREE_CODE (type) == RECORD_TYPE)
16222 field = TYPE_FIELDS (type);
16223
16224 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16225 {
16226 tree val = ce->value;
16227 int pos, fieldsize;
16228
16229 if (ce->index != 0)
16230 field = ce->index;
16231
16232 if (val)
16233 STRIP_NOPS (val);
16234
16235 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16236 return false;
16237
16238 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16239 && TYPE_DOMAIN (TREE_TYPE (field))
16240 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16241 return false;
16242 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16243 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16244 return false;
16245 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16246 pos = int_byte_position (field);
16247 gcc_assert (pos + fieldsize <= size);
16248 if (val
16249 && !native_encode_initializer (val, array + pos, fieldsize))
16250 return false;
16251 }
16252 return true;
16253 }
16254 return false;
16255 case VIEW_CONVERT_EXPR:
16256 case NON_LVALUE_EXPR:
16257 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16258 default:
16259 return native_encode_expr (init, array, size) == size;
16260 }
16261 }
16262
16263 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16264 attribute is the const value T. */
16265
16266 static bool
16267 tree_add_const_value_attribute (dw_die_ref die, tree t)
16268 {
16269 tree init;
16270 tree type = TREE_TYPE (t);
16271 rtx rtl;
16272
16273 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16274 return false;
16275
16276 init = t;
16277 gcc_assert (!DECL_P (init));
16278
16279 rtl = rtl_for_decl_init (init, type);
16280 if (rtl)
16281 return add_const_value_attribute (die, rtl);
16282 /* If the host and target are sane, try harder. */
16283 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16284 && initializer_constant_valid_p (init, type))
16285 {
16286 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16287 if (size > 0 && (int) size == size)
16288 {
16289 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16290
16291 if (native_encode_initializer (init, array, size))
16292 {
16293 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16294 return true;
16295 }
16296 ggc_free (array);
16297 }
16298 }
16299 return false;
16300 }
16301
16302 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16303 attribute is the const value of T, where T is an integral constant
16304 variable with static storage duration
16305 (so it can't be a PARM_DECL or a RESULT_DECL). */
16306
16307 static bool
16308 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16309 {
16310
16311 if (!decl
16312 || (TREE_CODE (decl) != VAR_DECL
16313 && TREE_CODE (decl) != CONST_DECL)
16314 || (TREE_CODE (decl) == VAR_DECL
16315 && !TREE_STATIC (decl)))
16316 return false;
16317
16318 if (TREE_READONLY (decl)
16319 && ! TREE_THIS_VOLATILE (decl)
16320 && DECL_INITIAL (decl))
16321 /* OK */;
16322 else
16323 return false;
16324
16325 /* Don't add DW_AT_const_value if abstract origin already has one. */
16326 if (get_AT (var_die, DW_AT_const_value))
16327 return false;
16328
16329 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16330 }
16331
16332 /* Convert the CFI instructions for the current function into a
16333 location list. This is used for DW_AT_frame_base when we targeting
16334 a dwarf2 consumer that does not support the dwarf3
16335 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16336 expressions. */
16337
16338 static dw_loc_list_ref
16339 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16340 {
16341 int ix;
16342 dw_fde_ref fde;
16343 dw_loc_list_ref list, *list_tail;
16344 dw_cfi_ref cfi;
16345 dw_cfa_location last_cfa, next_cfa;
16346 const char *start_label, *last_label, *section;
16347 dw_cfa_location remember;
16348
16349 fde = cfun->fde;
16350 gcc_assert (fde != NULL);
16351
16352 section = secname_for_decl (current_function_decl);
16353 list_tail = &list;
16354 list = NULL;
16355
16356 memset (&next_cfa, 0, sizeof (next_cfa));
16357 next_cfa.reg = INVALID_REGNUM;
16358 remember = next_cfa;
16359
16360 start_label = fde->dw_fde_begin;
16361
16362 /* ??? Bald assumption that the CIE opcode list does not contain
16363 advance opcodes. */
16364 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16365 lookup_cfa_1 (cfi, &next_cfa, &remember);
16366
16367 last_cfa = next_cfa;
16368 last_label = start_label;
16369
16370 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16371 {
16372 /* If the first partition contained no CFI adjustments, the
16373 CIE opcodes apply to the whole first partition. */
16374 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16375 fde->dw_fde_begin, fde->dw_fde_end, section);
16376 list_tail =&(*list_tail)->dw_loc_next;
16377 start_label = last_label = fde->dw_fde_second_begin;
16378 }
16379
16380 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16381 {
16382 switch (cfi->dw_cfi_opc)
16383 {
16384 case DW_CFA_set_loc:
16385 case DW_CFA_advance_loc1:
16386 case DW_CFA_advance_loc2:
16387 case DW_CFA_advance_loc4:
16388 if (!cfa_equal_p (&last_cfa, &next_cfa))
16389 {
16390 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16391 start_label, last_label, section);
16392
16393 list_tail = &(*list_tail)->dw_loc_next;
16394 last_cfa = next_cfa;
16395 start_label = last_label;
16396 }
16397 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16398 break;
16399
16400 case DW_CFA_advance_loc:
16401 /* The encoding is complex enough that we should never emit this. */
16402 gcc_unreachable ();
16403
16404 default:
16405 lookup_cfa_1 (cfi, &next_cfa, &remember);
16406 break;
16407 }
16408 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16409 {
16410 if (!cfa_equal_p (&last_cfa, &next_cfa))
16411 {
16412 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16413 start_label, last_label, section);
16414
16415 list_tail = &(*list_tail)->dw_loc_next;
16416 last_cfa = next_cfa;
16417 start_label = last_label;
16418 }
16419 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16420 start_label, fde->dw_fde_end, section);
16421 list_tail = &(*list_tail)->dw_loc_next;
16422 start_label = last_label = fde->dw_fde_second_begin;
16423 }
16424 }
16425
16426 if (!cfa_equal_p (&last_cfa, &next_cfa))
16427 {
16428 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16429 start_label, last_label, section);
16430 list_tail = &(*list_tail)->dw_loc_next;
16431 start_label = last_label;
16432 }
16433
16434 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16435 start_label,
16436 fde->dw_fde_second_begin
16437 ? fde->dw_fde_second_end : fde->dw_fde_end,
16438 section);
16439
16440 if (list && list->dw_loc_next)
16441 gen_llsym (list);
16442
16443 return list;
16444 }
16445
16446 /* Compute a displacement from the "steady-state frame pointer" to the
16447 frame base (often the same as the CFA), and store it in
16448 frame_pointer_fb_offset. OFFSET is added to the displacement
16449 before the latter is negated. */
16450
16451 static void
16452 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16453 {
16454 rtx reg, elim;
16455
16456 #ifdef FRAME_POINTER_CFA_OFFSET
16457 reg = frame_pointer_rtx;
16458 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16459 #else
16460 reg = arg_pointer_rtx;
16461 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16462 #endif
16463
16464 elim = (ira_use_lra_p
16465 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16466 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16467 if (GET_CODE (elim) == PLUS)
16468 {
16469 offset += INTVAL (XEXP (elim, 1));
16470 elim = XEXP (elim, 0);
16471 }
16472
16473 frame_pointer_fb_offset = -offset;
16474
16475 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16476 in which to eliminate. This is because it's stack pointer isn't
16477 directly accessible as a register within the ISA. To work around
16478 this, assume that while we cannot provide a proper value for
16479 frame_pointer_fb_offset, we won't need one either. */
16480 frame_pointer_fb_offset_valid
16481 = ((SUPPORTS_STACK_ALIGNMENT
16482 && (elim == hard_frame_pointer_rtx
16483 || elim == stack_pointer_rtx))
16484 || elim == (frame_pointer_needed
16485 ? hard_frame_pointer_rtx
16486 : stack_pointer_rtx));
16487 }
16488
16489 /* Generate a DW_AT_name attribute given some string value to be included as
16490 the value of the attribute. */
16491
16492 static void
16493 add_name_attribute (dw_die_ref die, const char *name_string)
16494 {
16495 if (name_string != NULL && *name_string != 0)
16496 {
16497 if (demangle_name_func)
16498 name_string = (*demangle_name_func) (name_string);
16499
16500 add_AT_string (die, DW_AT_name, name_string);
16501 }
16502 }
16503
16504 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16505 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16506 of TYPE accordingly.
16507
16508 ??? This is a temporary measure until after we're able to generate
16509 regular DWARF for the complex Ada type system. */
16510
16511 static void
16512 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16513 dw_die_ref context_die)
16514 {
16515 tree dtype;
16516 dw_die_ref dtype_die;
16517
16518 if (!lang_hooks.types.descriptive_type)
16519 return;
16520
16521 dtype = lang_hooks.types.descriptive_type (type);
16522 if (!dtype)
16523 return;
16524
16525 dtype_die = lookup_type_die (dtype);
16526 if (!dtype_die)
16527 {
16528 gen_type_die (dtype, context_die);
16529 dtype_die = lookup_type_die (dtype);
16530 gcc_assert (dtype_die);
16531 }
16532
16533 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16534 }
16535
16536 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16537
16538 static const char *
16539 comp_dir_string (void)
16540 {
16541 const char *wd;
16542 char *wd1;
16543 static const char *cached_wd = NULL;
16544
16545 if (cached_wd != NULL)
16546 return cached_wd;
16547
16548 wd = get_src_pwd ();
16549 if (wd == NULL)
16550 return NULL;
16551
16552 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16553 {
16554 int wdlen;
16555
16556 wdlen = strlen (wd);
16557 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16558 strcpy (wd1, wd);
16559 wd1 [wdlen] = DIR_SEPARATOR;
16560 wd1 [wdlen + 1] = 0;
16561 wd = wd1;
16562 }
16563
16564 cached_wd = remap_debug_filename (wd);
16565 return cached_wd;
16566 }
16567
16568 /* Generate a DW_AT_comp_dir attribute for DIE. */
16569
16570 static void
16571 add_comp_dir_attribute (dw_die_ref die)
16572 {
16573 const char * wd = comp_dir_string ();
16574 if (wd != NULL)
16575 add_AT_string (die, DW_AT_comp_dir, wd);
16576 }
16577
16578 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16579 pointer computation, ...), output a representation for that bound according
16580 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16581 loc_list_from_tree for the meaning of CONTEXT. */
16582
16583 static void
16584 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16585 int forms, const struct loc_descr_context *context)
16586 {
16587 dw_die_ref ctx, decl_die;
16588 dw_loc_list_ref list;
16589
16590 bool strip_conversions = true;
16591
16592 while (strip_conversions)
16593 switch (TREE_CODE (value))
16594 {
16595 case ERROR_MARK:
16596 case SAVE_EXPR:
16597 return;
16598
16599 CASE_CONVERT:
16600 case VIEW_CONVERT_EXPR:
16601 value = TREE_OPERAND (value, 0);
16602 break;
16603
16604 default:
16605 strip_conversions = false;
16606 break;
16607 }
16608
16609 /* If possible and permitted, output the attribute as a constant. */
16610 if ((forms & dw_scalar_form_constant) != 0
16611 && TREE_CODE (value) == INTEGER_CST)
16612 {
16613 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16614
16615 /* If HOST_WIDE_INT is big enough then represent the bound as
16616 a constant value. We need to choose a form based on
16617 whether the type is signed or unsigned. We cannot just
16618 call add_AT_unsigned if the value itself is positive
16619 (add_AT_unsigned might add the unsigned value encoded as
16620 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16621 bounds type and then sign extend any unsigned values found
16622 for signed types. This is needed only for
16623 DW_AT_{lower,upper}_bound, since for most other attributes,
16624 consumers will treat DW_FORM_data[1248] as unsigned values,
16625 regardless of the underlying type. */
16626 if (prec <= HOST_BITS_PER_WIDE_INT
16627 || tree_fits_uhwi_p (value))
16628 {
16629 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16630 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16631 else
16632 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16633 }
16634 else
16635 /* Otherwise represent the bound as an unsigned value with
16636 the precision of its type. The precision and signedness
16637 of the type will be necessary to re-interpret it
16638 unambiguously. */
16639 add_AT_wide (die, attr, value);
16640 return;
16641 }
16642
16643 /* Otherwise, if it's possible and permitted too, output a reference to
16644 another DIE. */
16645 if ((forms & dw_scalar_form_reference) != 0)
16646 {
16647 tree decl = NULL_TREE;
16648
16649 /* Some type attributes reference an outer type. For instance, the upper
16650 bound of an array may reference an embedding record (this happens in
16651 Ada). */
16652 if (TREE_CODE (value) == COMPONENT_REF
16653 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16654 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16655 decl = TREE_OPERAND (value, 1);
16656
16657 else if (TREE_CODE (value) == VAR_DECL
16658 || TREE_CODE (value) == PARM_DECL
16659 || TREE_CODE (value) == RESULT_DECL)
16660 decl = value;
16661
16662 if (decl != NULL_TREE)
16663 {
16664 dw_die_ref decl_die = lookup_decl_die (decl);
16665
16666 /* ??? Can this happen, or should the variable have been bound
16667 first? Probably it can, since I imagine that we try to create
16668 the types of parameters in the order in which they exist in
16669 the list, and won't have created a forward reference to a
16670 later parameter. */
16671 if (decl_die != NULL)
16672 {
16673 add_AT_die_ref (die, attr, decl_die);
16674 return;
16675 }
16676 }
16677 }
16678
16679 /* Last chance: try to create a stack operation procedure to evaluate the
16680 value. Do nothing if even that is not possible or permitted. */
16681 if ((forms & dw_scalar_form_exprloc) == 0)
16682 return;
16683
16684 list = loc_list_from_tree (value, 2, context);
16685 if (list == NULL || single_element_loc_list_p (list))
16686 {
16687 /* If this attribute is not a reference nor constant, it is
16688 a DWARF expression rather than location description. For that
16689 loc_list_from_tree (value, 0, &context) is needed. */
16690 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16691 if (list2 && single_element_loc_list_p (list2))
16692 {
16693 add_AT_loc (die, attr, list2->expr);
16694 return;
16695 }
16696 }
16697
16698 /* If that failed to give a single element location list, fall back to
16699 outputting this as a reference... still if permitted. */
16700 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16701 return;
16702
16703 if (current_function_decl == 0)
16704 ctx = comp_unit_die ();
16705 else
16706 ctx = lookup_decl_die (current_function_decl);
16707
16708 decl_die = new_die (DW_TAG_variable, ctx, value);
16709 add_AT_flag (decl_die, DW_AT_artificial, 1);
16710 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16711 add_AT_location_description (decl_die, DW_AT_location, list);
16712 add_AT_die_ref (die, attr, decl_die);
16713 }
16714
16715 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16716 default. */
16717
16718 static int
16719 lower_bound_default (void)
16720 {
16721 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16722 {
16723 case DW_LANG_C:
16724 case DW_LANG_C89:
16725 case DW_LANG_C99:
16726 case DW_LANG_C11:
16727 case DW_LANG_C_plus_plus:
16728 case DW_LANG_C_plus_plus_11:
16729 case DW_LANG_C_plus_plus_14:
16730 case DW_LANG_ObjC:
16731 case DW_LANG_ObjC_plus_plus:
16732 case DW_LANG_Java:
16733 return 0;
16734 case DW_LANG_Fortran77:
16735 case DW_LANG_Fortran90:
16736 case DW_LANG_Fortran95:
16737 case DW_LANG_Fortran03:
16738 case DW_LANG_Fortran08:
16739 return 1;
16740 case DW_LANG_UPC:
16741 case DW_LANG_D:
16742 case DW_LANG_Python:
16743 return dwarf_version >= 4 ? 0 : -1;
16744 case DW_LANG_Ada95:
16745 case DW_LANG_Ada83:
16746 case DW_LANG_Cobol74:
16747 case DW_LANG_Cobol85:
16748 case DW_LANG_Pascal83:
16749 case DW_LANG_Modula2:
16750 case DW_LANG_PLI:
16751 return dwarf_version >= 4 ? 1 : -1;
16752 default:
16753 return -1;
16754 }
16755 }
16756
16757 /* Given a tree node describing an array bound (either lower or upper) output
16758 a representation for that bound. */
16759
16760 static void
16761 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16762 tree bound, const struct loc_descr_context *context)
16763 {
16764 int dflt;
16765
16766 while (1)
16767 switch (TREE_CODE (bound))
16768 {
16769 /* Strip all conversions. */
16770 CASE_CONVERT:
16771 case VIEW_CONVERT_EXPR:
16772 bound = TREE_OPERAND (bound, 0);
16773 break;
16774
16775 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16776 are even omitted when they are the default. */
16777 case INTEGER_CST:
16778 /* If the value for this bound is the default one, we can even omit the
16779 attribute. */
16780 if (bound_attr == DW_AT_lower_bound
16781 && tree_fits_shwi_p (bound)
16782 && (dflt = lower_bound_default ()) != -1
16783 && tree_to_shwi (bound) == dflt)
16784 return;
16785
16786 /* FALLTHRU */
16787
16788 default:
16789 add_scalar_info (subrange_die, bound_attr, bound,
16790 dw_scalar_form_constant
16791 | dw_scalar_form_exprloc
16792 | dw_scalar_form_reference,
16793 context);
16794 return;
16795 }
16796 }
16797
16798 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16799 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16800 Note that the block of subscript information for an array type also
16801 includes information about the element type of the given array type. */
16802
16803 static void
16804 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16805 {
16806 unsigned dimension_number;
16807 tree lower, upper;
16808 dw_die_ref subrange_die;
16809
16810 for (dimension_number = 0;
16811 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16812 type = TREE_TYPE (type), dimension_number++)
16813 {
16814 tree domain = TYPE_DOMAIN (type);
16815
16816 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16817 break;
16818
16819 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16820 and (in GNU C only) variable bounds. Handle all three forms
16821 here. */
16822 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16823 if (domain)
16824 {
16825 /* We have an array type with specified bounds. */
16826 lower = TYPE_MIN_VALUE (domain);
16827 upper = TYPE_MAX_VALUE (domain);
16828
16829 /* Define the index type. */
16830 if (TREE_TYPE (domain))
16831 {
16832 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16833 TREE_TYPE field. We can't emit debug info for this
16834 because it is an unnamed integral type. */
16835 if (TREE_CODE (domain) == INTEGER_TYPE
16836 && TYPE_NAME (domain) == NULL_TREE
16837 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16838 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16839 ;
16840 else
16841 add_type_attribute (subrange_die, TREE_TYPE (domain),
16842 TYPE_UNQUALIFIED, type_die);
16843 }
16844
16845 /* ??? If upper is NULL, the array has unspecified length,
16846 but it does have a lower bound. This happens with Fortran
16847 dimension arr(N:*)
16848 Since the debugger is definitely going to need to know N
16849 to produce useful results, go ahead and output the lower
16850 bound solo, and hope the debugger can cope. */
16851
16852 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16853 if (upper)
16854 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16855 }
16856
16857 /* Otherwise we have an array type with an unspecified length. The
16858 DWARF-2 spec does not say how to handle this; let's just leave out the
16859 bounds. */
16860 }
16861 }
16862
16863 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16864
16865 static void
16866 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16867 {
16868 dw_die_ref decl_die;
16869 HOST_WIDE_INT size;
16870
16871 switch (TREE_CODE (tree_node))
16872 {
16873 case ERROR_MARK:
16874 size = 0;
16875 break;
16876 case ENUMERAL_TYPE:
16877 case RECORD_TYPE:
16878 case UNION_TYPE:
16879 case QUAL_UNION_TYPE:
16880 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16881 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16882 {
16883 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16884 return;
16885 }
16886 size = int_size_in_bytes (tree_node);
16887 break;
16888 case FIELD_DECL:
16889 /* For a data member of a struct or union, the DW_AT_byte_size is
16890 generally given as the number of bytes normally allocated for an
16891 object of the *declared* type of the member itself. This is true
16892 even for bit-fields. */
16893 size = int_size_in_bytes (field_type (tree_node));
16894 break;
16895 default:
16896 gcc_unreachable ();
16897 }
16898
16899 /* Note that `size' might be -1 when we get to this point. If it is, that
16900 indicates that the byte size of the entity in question is variable. We
16901 have no good way of expressing this fact in Dwarf at the present time,
16902 when location description was not used by the caller code instead. */
16903 if (size >= 0)
16904 add_AT_unsigned (die, DW_AT_byte_size, size);
16905 }
16906
16907 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16908 which specifies the distance in bits from the highest order bit of the
16909 "containing object" for the bit-field to the highest order bit of the
16910 bit-field itself.
16911
16912 For any given bit-field, the "containing object" is a hypothetical object
16913 (of some integral or enum type) within which the given bit-field lives. The
16914 type of this hypothetical "containing object" is always the same as the
16915 declared type of the individual bit-field itself. The determination of the
16916 exact location of the "containing object" for a bit-field is rather
16917 complicated. It's handled by the `field_byte_offset' function (above).
16918
16919 Note that it is the size (in bytes) of the hypothetical "containing object"
16920 which will be given in the DW_AT_byte_size attribute for this bit-field.
16921 (See `byte_size_attribute' above). */
16922
16923 static inline void
16924 add_bit_offset_attribute (dw_die_ref die, tree decl)
16925 {
16926 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16927 tree type = DECL_BIT_FIELD_TYPE (decl);
16928 HOST_WIDE_INT bitpos_int;
16929 HOST_WIDE_INT highest_order_object_bit_offset;
16930 HOST_WIDE_INT highest_order_field_bit_offset;
16931 HOST_WIDE_INT bit_offset;
16932
16933 /* Must be a field and a bit field. */
16934 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16935
16936 /* We can't yet handle bit-fields whose offsets are variable, so if we
16937 encounter such things, just return without generating any attribute
16938 whatsoever. Likewise for variable or too large size. */
16939 if (! tree_fits_shwi_p (bit_position (decl))
16940 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16941 return;
16942
16943 bitpos_int = int_bit_position (decl);
16944
16945 /* Note that the bit offset is always the distance (in bits) from the
16946 highest-order bit of the "containing object" to the highest-order bit of
16947 the bit-field itself. Since the "high-order end" of any object or field
16948 is different on big-endian and little-endian machines, the computation
16949 below must take account of these differences. */
16950 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16951 highest_order_field_bit_offset = bitpos_int;
16952
16953 if (! BYTES_BIG_ENDIAN)
16954 {
16955 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16956 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16957 }
16958
16959 bit_offset
16960 = (! BYTES_BIG_ENDIAN
16961 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16962 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16963
16964 if (bit_offset < 0)
16965 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16966 else
16967 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16968 }
16969
16970 /* For a FIELD_DECL node which represents a bit field, output an attribute
16971 which specifies the length in bits of the given field. */
16972
16973 static inline void
16974 add_bit_size_attribute (dw_die_ref die, tree decl)
16975 {
16976 /* Must be a field and a bit field. */
16977 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16978 && DECL_BIT_FIELD_TYPE (decl));
16979
16980 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16981 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16982 }
16983
16984 /* If the compiled language is ANSI C, then add a 'prototyped'
16985 attribute, if arg types are given for the parameters of a function. */
16986
16987 static inline void
16988 add_prototyped_attribute (dw_die_ref die, tree func_type)
16989 {
16990 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16991 {
16992 case DW_LANG_C:
16993 case DW_LANG_C89:
16994 case DW_LANG_C99:
16995 case DW_LANG_C11:
16996 case DW_LANG_ObjC:
16997 if (prototype_p (func_type))
16998 add_AT_flag (die, DW_AT_prototyped, 1);
16999 break;
17000 default:
17001 break;
17002 }
17003 }
17004
17005 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17006 by looking in either the type declaration or object declaration
17007 equate table. */
17008
17009 static inline dw_die_ref
17010 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17011 {
17012 dw_die_ref origin_die = NULL;
17013
17014 if (TREE_CODE (origin) != FUNCTION_DECL)
17015 {
17016 /* We may have gotten separated from the block for the inlined
17017 function, if we're in an exception handler or some such; make
17018 sure that the abstract function has been written out.
17019
17020 Doing this for nested functions is wrong, however; functions are
17021 distinct units, and our context might not even be inline. */
17022 tree fn = origin;
17023
17024 if (TYPE_P (fn))
17025 fn = TYPE_STUB_DECL (fn);
17026
17027 fn = decl_function_context (fn);
17028 if (fn)
17029 dwarf2out_abstract_function (fn);
17030 }
17031
17032 if (DECL_P (origin))
17033 origin_die = lookup_decl_die (origin);
17034 else if (TYPE_P (origin))
17035 origin_die = lookup_type_die (origin);
17036
17037 /* XXX: Functions that are never lowered don't always have correct block
17038 trees (in the case of java, they simply have no block tree, in some other
17039 languages). For these functions, there is nothing we can really do to
17040 output correct debug info for inlined functions in all cases. Rather
17041 than die, we'll just produce deficient debug info now, in that we will
17042 have variables without a proper abstract origin. In the future, when all
17043 functions are lowered, we should re-add a gcc_assert (origin_die)
17044 here. */
17045
17046 if (origin_die)
17047 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17048 return origin_die;
17049 }
17050
17051 /* We do not currently support the pure_virtual attribute. */
17052
17053 static inline void
17054 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17055 {
17056 if (DECL_VINDEX (func_decl))
17057 {
17058 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17059
17060 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17061 add_AT_loc (die, DW_AT_vtable_elem_location,
17062 new_loc_descr (DW_OP_constu,
17063 tree_to_shwi (DECL_VINDEX (func_decl)),
17064 0));
17065
17066 /* GNU extension: Record what type this method came from originally. */
17067 if (debug_info_level > DINFO_LEVEL_TERSE
17068 && DECL_CONTEXT (func_decl))
17069 add_AT_die_ref (die, DW_AT_containing_type,
17070 lookup_type_die (DECL_CONTEXT (func_decl)));
17071 }
17072 }
17073 \f
17074 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17075 given decl. This used to be a vendor extension until after DWARF 4
17076 standardized it. */
17077
17078 static void
17079 add_linkage_attr (dw_die_ref die, tree decl)
17080 {
17081 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17082
17083 /* Mimic what assemble_name_raw does with a leading '*'. */
17084 if (name[0] == '*')
17085 name = &name[1];
17086
17087 if (dwarf_version >= 4)
17088 add_AT_string (die, DW_AT_linkage_name, name);
17089 else
17090 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17091 }
17092
17093 /* Add source coordinate attributes for the given decl. */
17094
17095 static void
17096 add_src_coords_attributes (dw_die_ref die, tree decl)
17097 {
17098 expanded_location s;
17099
17100 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17101 return;
17102 s = expand_location (DECL_SOURCE_LOCATION (decl));
17103 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17104 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17105 }
17106
17107 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17108
17109 static void
17110 add_linkage_name (dw_die_ref die, tree decl)
17111 {
17112 if (debug_info_level > DINFO_LEVEL_NONE
17113 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17114 && TREE_PUBLIC (decl)
17115 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17116 && die->die_tag != DW_TAG_member)
17117 {
17118 /* Defer until we have an assembler name set. */
17119 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17120 {
17121 limbo_die_node *asm_name;
17122
17123 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17124 asm_name->die = die;
17125 asm_name->created_for = decl;
17126 asm_name->next = deferred_asm_name;
17127 deferred_asm_name = asm_name;
17128 }
17129 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17130 add_linkage_attr (die, decl);
17131 }
17132 }
17133
17134 /* Add a DW_AT_name attribute and source coordinate attribute for the
17135 given decl, but only if it actually has a name. */
17136
17137 static void
17138 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17139 {
17140 tree decl_name;
17141
17142 decl_name = DECL_NAME (decl);
17143 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17144 {
17145 const char *name = dwarf2_name (decl, 0);
17146 if (name)
17147 add_name_attribute (die, name);
17148 if (! DECL_ARTIFICIAL (decl))
17149 add_src_coords_attributes (die, decl);
17150
17151 add_linkage_name (die, decl);
17152 }
17153
17154 #ifdef VMS_DEBUGGING_INFO
17155 /* Get the function's name, as described by its RTL. This may be different
17156 from the DECL_NAME name used in the source file. */
17157 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17158 {
17159 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17160 XEXP (DECL_RTL (decl), 0), false);
17161 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17162 }
17163 #endif /* VMS_DEBUGGING_INFO */
17164 }
17165
17166 #ifdef VMS_DEBUGGING_INFO
17167 /* Output the debug main pointer die for VMS */
17168
17169 void
17170 dwarf2out_vms_debug_main_pointer (void)
17171 {
17172 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17173 dw_die_ref die;
17174
17175 /* Allocate the VMS debug main subprogram die. */
17176 die = ggc_cleared_alloc<die_node> ();
17177 die->die_tag = DW_TAG_subprogram;
17178 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17179 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17180 current_function_funcdef_no);
17181 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17182
17183 /* Make it the first child of comp_unit_die (). */
17184 die->die_parent = comp_unit_die ();
17185 if (comp_unit_die ()->die_child)
17186 {
17187 die->die_sib = comp_unit_die ()->die_child->die_sib;
17188 comp_unit_die ()->die_child->die_sib = die;
17189 }
17190 else
17191 {
17192 die->die_sib = die;
17193 comp_unit_die ()->die_child = die;
17194 }
17195 }
17196 #endif /* VMS_DEBUGGING_INFO */
17197
17198 /* Push a new declaration scope. */
17199
17200 static void
17201 push_decl_scope (tree scope)
17202 {
17203 vec_safe_push (decl_scope_table, scope);
17204 }
17205
17206 /* Pop a declaration scope. */
17207
17208 static inline void
17209 pop_decl_scope (void)
17210 {
17211 decl_scope_table->pop ();
17212 }
17213
17214 /* walk_tree helper function for uses_local_type, below. */
17215
17216 static tree
17217 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17218 {
17219 if (!TYPE_P (*tp))
17220 *walk_subtrees = 0;
17221 else
17222 {
17223 tree name = TYPE_NAME (*tp);
17224 if (name && DECL_P (name) && decl_function_context (name))
17225 return *tp;
17226 }
17227 return NULL_TREE;
17228 }
17229
17230 /* If TYPE involves a function-local type (including a local typedef to a
17231 non-local type), returns that type; otherwise returns NULL_TREE. */
17232
17233 static tree
17234 uses_local_type (tree type)
17235 {
17236 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17237 return used;
17238 }
17239
17240 /* Return the DIE for the scope that immediately contains this type.
17241 Non-named types that do not involve a function-local type get global
17242 scope. Named types nested in namespaces or other types get their
17243 containing scope. All other types (i.e. function-local named types) get
17244 the current active scope. */
17245
17246 static dw_die_ref
17247 scope_die_for (tree t, dw_die_ref context_die)
17248 {
17249 dw_die_ref scope_die = NULL;
17250 tree containing_scope;
17251
17252 /* Non-types always go in the current scope. */
17253 gcc_assert (TYPE_P (t));
17254
17255 /* Use the scope of the typedef, rather than the scope of the type
17256 it refers to. */
17257 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17258 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17259 else
17260 containing_scope = TYPE_CONTEXT (t);
17261
17262 /* Use the containing namespace if there is one. */
17263 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17264 {
17265 if (context_die == lookup_decl_die (containing_scope))
17266 /* OK */;
17267 else if (debug_info_level > DINFO_LEVEL_TERSE)
17268 context_die = get_context_die (containing_scope);
17269 else
17270 containing_scope = NULL_TREE;
17271 }
17272
17273 /* Ignore function type "scopes" from the C frontend. They mean that
17274 a tagged type is local to a parmlist of a function declarator, but
17275 that isn't useful to DWARF. */
17276 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17277 containing_scope = NULL_TREE;
17278
17279 if (SCOPE_FILE_SCOPE_P (containing_scope))
17280 {
17281 /* If T uses a local type keep it local as well, to avoid references
17282 to function-local DIEs from outside the function. */
17283 if (current_function_decl && uses_local_type (t))
17284 scope_die = context_die;
17285 else
17286 scope_die = comp_unit_die ();
17287 }
17288 else if (TYPE_P (containing_scope))
17289 {
17290 /* For types, we can just look up the appropriate DIE. */
17291 if (debug_info_level > DINFO_LEVEL_TERSE)
17292 scope_die = get_context_die (containing_scope);
17293 else
17294 {
17295 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17296 if (scope_die == NULL)
17297 scope_die = comp_unit_die ();
17298 }
17299 }
17300 else
17301 scope_die = context_die;
17302
17303 return scope_die;
17304 }
17305
17306 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17307
17308 static inline int
17309 local_scope_p (dw_die_ref context_die)
17310 {
17311 for (; context_die; context_die = context_die->die_parent)
17312 if (context_die->die_tag == DW_TAG_inlined_subroutine
17313 || context_die->die_tag == DW_TAG_subprogram)
17314 return 1;
17315
17316 return 0;
17317 }
17318
17319 /* Returns nonzero if CONTEXT_DIE is a class. */
17320
17321 static inline int
17322 class_scope_p (dw_die_ref context_die)
17323 {
17324 return (context_die
17325 && (context_die->die_tag == DW_TAG_structure_type
17326 || context_die->die_tag == DW_TAG_class_type
17327 || context_die->die_tag == DW_TAG_interface_type
17328 || context_die->die_tag == DW_TAG_union_type));
17329 }
17330
17331 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17332 whether or not to treat a DIE in this context as a declaration. */
17333
17334 static inline int
17335 class_or_namespace_scope_p (dw_die_ref context_die)
17336 {
17337 return (class_scope_p (context_die)
17338 || (context_die && context_die->die_tag == DW_TAG_namespace));
17339 }
17340
17341 /* Many forms of DIEs require a "type description" attribute. This
17342 routine locates the proper "type descriptor" die for the type given
17343 by 'type' plus any additional qualifiers given by 'cv_quals', and
17344 adds a DW_AT_type attribute below the given die. */
17345
17346 static void
17347 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17348 dw_die_ref context_die)
17349 {
17350 enum tree_code code = TREE_CODE (type);
17351 dw_die_ref type_die = NULL;
17352
17353 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17354 or fixed-point type, use the inner type. This is because we have no
17355 support for unnamed types in base_type_die. This can happen if this is
17356 an Ada subrange type. Correct solution is emit a subrange type die. */
17357 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17358 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17359 type = TREE_TYPE (type), code = TREE_CODE (type);
17360
17361 if (code == ERROR_MARK
17362 /* Handle a special case. For functions whose return type is void, we
17363 generate *no* type attribute. (Note that no object may have type
17364 `void', so this only applies to function return types). */
17365 || code == VOID_TYPE)
17366 return;
17367
17368 type_die = modified_type_die (type,
17369 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17370 context_die);
17371
17372 if (type_die != NULL)
17373 add_AT_die_ref (object_die, DW_AT_type, type_die);
17374 }
17375
17376 /* Given an object die, add the calling convention attribute for the
17377 function call type. */
17378 static void
17379 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17380 {
17381 enum dwarf_calling_convention value = DW_CC_normal;
17382
17383 value = ((enum dwarf_calling_convention)
17384 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17385
17386 if (is_fortran ()
17387 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17388 {
17389 /* DWARF 2 doesn't provide a way to identify a program's source-level
17390 entry point. DW_AT_calling_convention attributes are only meant
17391 to describe functions' calling conventions. However, lacking a
17392 better way to signal the Fortran main program, we used this for
17393 a long time, following existing custom. Now, DWARF 4 has
17394 DW_AT_main_subprogram, which we add below, but some tools still
17395 rely on the old way, which we thus keep. */
17396 value = DW_CC_program;
17397
17398 if (dwarf_version >= 4 || !dwarf_strict)
17399 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17400 }
17401
17402 /* Only add the attribute if the backend requests it, and
17403 is not DW_CC_normal. */
17404 if (value && (value != DW_CC_normal))
17405 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17406 }
17407
17408 /* Given a tree pointer to a struct, class, union, or enum type node, return
17409 a pointer to the (string) tag name for the given type, or zero if the type
17410 was declared without a tag. */
17411
17412 static const char *
17413 type_tag (const_tree type)
17414 {
17415 const char *name = 0;
17416
17417 if (TYPE_NAME (type) != 0)
17418 {
17419 tree t = 0;
17420
17421 /* Find the IDENTIFIER_NODE for the type name. */
17422 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17423 && !TYPE_NAMELESS (type))
17424 t = TYPE_NAME (type);
17425
17426 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17427 a TYPE_DECL node, regardless of whether or not a `typedef' was
17428 involved. */
17429 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17430 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17431 {
17432 /* We want to be extra verbose. Don't call dwarf_name if
17433 DECL_NAME isn't set. The default hook for decl_printable_name
17434 doesn't like that, and in this context it's correct to return
17435 0, instead of "<anonymous>" or the like. */
17436 if (DECL_NAME (TYPE_NAME (type))
17437 && !DECL_NAMELESS (TYPE_NAME (type)))
17438 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17439 }
17440
17441 /* Now get the name as a string, or invent one. */
17442 if (!name && t != 0)
17443 name = IDENTIFIER_POINTER (t);
17444 }
17445
17446 return (name == 0 || *name == '\0') ? 0 : name;
17447 }
17448
17449 /* Return the type associated with a data member, make a special check
17450 for bit field types. */
17451
17452 static inline tree
17453 member_declared_type (const_tree member)
17454 {
17455 return (DECL_BIT_FIELD_TYPE (member)
17456 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17457 }
17458
17459 /* Get the decl's label, as described by its RTL. This may be different
17460 from the DECL_NAME name used in the source file. */
17461
17462 #if 0
17463 static const char *
17464 decl_start_label (tree decl)
17465 {
17466 rtx x;
17467 const char *fnname;
17468
17469 x = DECL_RTL (decl);
17470 gcc_assert (MEM_P (x));
17471
17472 x = XEXP (x, 0);
17473 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17474
17475 fnname = XSTR (x, 0);
17476 return fnname;
17477 }
17478 #endif
17479 \f
17480 /* These routines generate the internal representation of the DIE's for
17481 the compilation unit. Debugging information is collected by walking
17482 the declaration trees passed in from dwarf2out_decl(). */
17483
17484 static void
17485 gen_array_type_die (tree type, dw_die_ref context_die)
17486 {
17487 dw_die_ref scope_die = scope_die_for (type, context_die);
17488 dw_die_ref array_die;
17489
17490 /* GNU compilers represent multidimensional array types as sequences of one
17491 dimensional array types whose element types are themselves array types.
17492 We sometimes squish that down to a single array_type DIE with multiple
17493 subscripts in the Dwarf debugging info. The draft Dwarf specification
17494 say that we are allowed to do this kind of compression in C, because
17495 there is no difference between an array of arrays and a multidimensional
17496 array. We don't do this for Ada to remain as close as possible to the
17497 actual representation, which is especially important against the language
17498 flexibilty wrt arrays of variable size. */
17499
17500 bool collapse_nested_arrays = !is_ada ();
17501 tree element_type;
17502
17503 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17504 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17505 if (TYPE_STRING_FLAG (type)
17506 && TREE_CODE (type) == ARRAY_TYPE
17507 && is_fortran ()
17508 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17509 {
17510 HOST_WIDE_INT size;
17511
17512 array_die = new_die (DW_TAG_string_type, scope_die, type);
17513 add_name_attribute (array_die, type_tag (type));
17514 equate_type_number_to_die (type, array_die);
17515 size = int_size_in_bytes (type);
17516 if (size >= 0)
17517 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17518 else if (TYPE_DOMAIN (type) != NULL_TREE
17519 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17520 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17521 {
17522 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17523 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17524
17525 size = int_size_in_bytes (TREE_TYPE (szdecl));
17526 if (loc && size > 0)
17527 {
17528 add_AT_location_description (array_die, DW_AT_string_length, loc);
17529 if (size != DWARF2_ADDR_SIZE)
17530 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17531 }
17532 }
17533 return;
17534 }
17535
17536 array_die = new_die (DW_TAG_array_type, scope_die, type);
17537 add_name_attribute (array_die, type_tag (type));
17538 equate_type_number_to_die (type, array_die);
17539
17540 if (TREE_CODE (type) == VECTOR_TYPE)
17541 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17542
17543 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17544 if (is_fortran ()
17545 && TREE_CODE (type) == ARRAY_TYPE
17546 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17547 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17548 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17549
17550 #if 0
17551 /* We default the array ordering. SDB will probably do
17552 the right things even if DW_AT_ordering is not present. It's not even
17553 an issue until we start to get into multidimensional arrays anyway. If
17554 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17555 then we'll have to put the DW_AT_ordering attribute back in. (But if
17556 and when we find out that we need to put these in, we will only do so
17557 for multidimensional arrays. */
17558 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17559 #endif
17560
17561 if (TREE_CODE (type) == VECTOR_TYPE)
17562 {
17563 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17564 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17565 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17566 add_bound_info (subrange_die, DW_AT_upper_bound,
17567 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17568 }
17569 else
17570 add_subscript_info (array_die, type, collapse_nested_arrays);
17571
17572 /* Add representation of the type of the elements of this array type and
17573 emit the corresponding DIE if we haven't done it already. */
17574 element_type = TREE_TYPE (type);
17575 if (collapse_nested_arrays)
17576 while (TREE_CODE (element_type) == ARRAY_TYPE)
17577 {
17578 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17579 break;
17580 element_type = TREE_TYPE (element_type);
17581 }
17582
17583 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17584
17585 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17586 if (TYPE_ARTIFICIAL (type))
17587 add_AT_flag (array_die, DW_AT_artificial, 1);
17588
17589 if (get_AT (array_die, DW_AT_name))
17590 add_pubtype (type, array_die);
17591 }
17592
17593 /* This routine generates DIE for array with hidden descriptor, details
17594 are filled into *info by a langhook. */
17595
17596 static void
17597 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17598 dw_die_ref context_die)
17599 {
17600 const dw_die_ref scope_die = scope_die_for (type, context_die);
17601 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17602 const struct loc_descr_context context = { type, info->base_decl };
17603 int dim;
17604
17605 add_name_attribute (array_die, type_tag (type));
17606 equate_type_number_to_die (type, array_die);
17607
17608 if (info->ndimensions > 1)
17609 switch (info->ordering)
17610 {
17611 case array_descr_ordering_row_major:
17612 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17613 break;
17614 case array_descr_ordering_column_major:
17615 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17616 break;
17617 default:
17618 break;
17619 }
17620
17621 if (dwarf_version >= 3 || !dwarf_strict)
17622 {
17623 if (info->data_location)
17624 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17625 dw_scalar_form_exprloc, &context);
17626 if (info->associated)
17627 add_scalar_info (array_die, DW_AT_associated, info->associated,
17628 dw_scalar_form_constant
17629 | dw_scalar_form_exprloc
17630 | dw_scalar_form_reference, &context);
17631 if (info->allocated)
17632 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17633 dw_scalar_form_constant
17634 | dw_scalar_form_exprloc
17635 | dw_scalar_form_reference, &context);
17636 }
17637
17638 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17639
17640 for (dim = 0; dim < info->ndimensions; dim++)
17641 {
17642 dw_die_ref subrange_die
17643 = new_die (DW_TAG_subrange_type, array_die, NULL);
17644
17645 if (info->dimen[dim].bounds_type)
17646 add_type_attribute (subrange_die,
17647 info->dimen[dim].bounds_type, 0,
17648 context_die);
17649 if (info->dimen[dim].lower_bound)
17650 add_bound_info (subrange_die, DW_AT_lower_bound,
17651 info->dimen[dim].lower_bound, &context);
17652 if (info->dimen[dim].upper_bound)
17653 add_bound_info (subrange_die, DW_AT_upper_bound,
17654 info->dimen[dim].upper_bound, &context);
17655 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17656 add_scalar_info (subrange_die, DW_AT_byte_stride,
17657 info->dimen[dim].stride,
17658 dw_scalar_form_constant
17659 | dw_scalar_form_exprloc
17660 | dw_scalar_form_reference,
17661 &context);
17662 }
17663
17664 gen_type_die (info->element_type, context_die);
17665 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17666 context_die);
17667
17668 if (get_AT (array_die, DW_AT_name))
17669 add_pubtype (type, array_die);
17670 }
17671
17672 #if 0
17673 static void
17674 gen_entry_point_die (tree decl, dw_die_ref context_die)
17675 {
17676 tree origin = decl_ultimate_origin (decl);
17677 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17678
17679 if (origin != NULL)
17680 add_abstract_origin_attribute (decl_die, origin);
17681 else
17682 {
17683 add_name_and_src_coords_attributes (decl_die, decl);
17684 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17685 TYPE_UNQUALIFIED, context_die);
17686 }
17687
17688 if (DECL_ABSTRACT_P (decl))
17689 equate_decl_number_to_die (decl, decl_die);
17690 else
17691 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17692 }
17693 #endif
17694
17695 /* Walk through the list of incomplete types again, trying once more to
17696 emit full debugging info for them. */
17697
17698 static void
17699 retry_incomplete_types (void)
17700 {
17701 int i;
17702
17703 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17704 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17705 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17706 }
17707
17708 /* Determine what tag to use for a record type. */
17709
17710 static enum dwarf_tag
17711 record_type_tag (tree type)
17712 {
17713 if (! lang_hooks.types.classify_record)
17714 return DW_TAG_structure_type;
17715
17716 switch (lang_hooks.types.classify_record (type))
17717 {
17718 case RECORD_IS_STRUCT:
17719 return DW_TAG_structure_type;
17720
17721 case RECORD_IS_CLASS:
17722 return DW_TAG_class_type;
17723
17724 case RECORD_IS_INTERFACE:
17725 if (dwarf_version >= 3 || !dwarf_strict)
17726 return DW_TAG_interface_type;
17727 return DW_TAG_structure_type;
17728
17729 default:
17730 gcc_unreachable ();
17731 }
17732 }
17733
17734 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17735 include all of the information about the enumeration values also. Each
17736 enumerated type name/value is listed as a child of the enumerated type
17737 DIE. */
17738
17739 static dw_die_ref
17740 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17741 {
17742 dw_die_ref type_die = lookup_type_die (type);
17743
17744 if (type_die == NULL)
17745 {
17746 type_die = new_die (DW_TAG_enumeration_type,
17747 scope_die_for (type, context_die), type);
17748 equate_type_number_to_die (type, type_die);
17749 add_name_attribute (type_die, type_tag (type));
17750 if (dwarf_version >= 4 || !dwarf_strict)
17751 {
17752 if (ENUM_IS_SCOPED (type))
17753 add_AT_flag (type_die, DW_AT_enum_class, 1);
17754 if (ENUM_IS_OPAQUE (type))
17755 add_AT_flag (type_die, DW_AT_declaration, 1);
17756 }
17757 }
17758 else if (! TYPE_SIZE (type))
17759 return type_die;
17760 else
17761 remove_AT (type_die, DW_AT_declaration);
17762
17763 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17764 given enum type is incomplete, do not generate the DW_AT_byte_size
17765 attribute or the DW_AT_element_list attribute. */
17766 if (TYPE_SIZE (type))
17767 {
17768 tree link;
17769
17770 TREE_ASM_WRITTEN (type) = 1;
17771 add_byte_size_attribute (type_die, type);
17772 if (dwarf_version >= 3 || !dwarf_strict)
17773 {
17774 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17775 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17776 context_die);
17777 }
17778 if (TYPE_STUB_DECL (type) != NULL_TREE)
17779 {
17780 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17781 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17782 }
17783
17784 /* If the first reference to this type was as the return type of an
17785 inline function, then it may not have a parent. Fix this now. */
17786 if (type_die->die_parent == NULL)
17787 add_child_die (scope_die_for (type, context_die), type_die);
17788
17789 for (link = TYPE_VALUES (type);
17790 link != NULL; link = TREE_CHAIN (link))
17791 {
17792 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17793 tree value = TREE_VALUE (link);
17794
17795 add_name_attribute (enum_die,
17796 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17797
17798 if (TREE_CODE (value) == CONST_DECL)
17799 value = DECL_INITIAL (value);
17800
17801 if (simple_type_size_in_bits (TREE_TYPE (value))
17802 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17803 {
17804 /* For constant forms created by add_AT_unsigned DWARF
17805 consumers (GDB, elfutils, etc.) always zero extend
17806 the value. Only when the actual value is negative
17807 do we need to use add_AT_int to generate a constant
17808 form that can represent negative values. */
17809 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17810 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17811 add_AT_unsigned (enum_die, DW_AT_const_value,
17812 (unsigned HOST_WIDE_INT) val);
17813 else
17814 add_AT_int (enum_die, DW_AT_const_value, val);
17815 }
17816 else
17817 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17818 that here. TODO: This should be re-worked to use correct
17819 signed/unsigned double tags for all cases. */
17820 add_AT_wide (enum_die, DW_AT_const_value, value);
17821 }
17822
17823 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17824 if (TYPE_ARTIFICIAL (type))
17825 add_AT_flag (type_die, DW_AT_artificial, 1);
17826 }
17827 else
17828 add_AT_flag (type_die, DW_AT_declaration, 1);
17829
17830 add_pubtype (type, type_die);
17831
17832 return type_die;
17833 }
17834
17835 /* Generate a DIE to represent either a real live formal parameter decl or to
17836 represent just the type of some formal parameter position in some function
17837 type.
17838
17839 Note that this routine is a bit unusual because its argument may be a
17840 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17841 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17842 node. If it's the former then this function is being called to output a
17843 DIE to represent a formal parameter object (or some inlining thereof). If
17844 it's the latter, then this function is only being called to output a
17845 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17846 argument type of some subprogram type.
17847 If EMIT_NAME_P is true, name and source coordinate attributes
17848 are emitted. */
17849
17850 static dw_die_ref
17851 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17852 dw_die_ref context_die)
17853 {
17854 tree node_or_origin = node ? node : origin;
17855 tree ultimate_origin;
17856 dw_die_ref parm_die
17857 = new_die (DW_TAG_formal_parameter, context_die, node);
17858
17859 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17860 {
17861 case tcc_declaration:
17862 ultimate_origin = decl_ultimate_origin (node_or_origin);
17863 if (node || ultimate_origin)
17864 origin = ultimate_origin;
17865 if (origin != NULL)
17866 add_abstract_origin_attribute (parm_die, origin);
17867 else if (emit_name_p)
17868 add_name_and_src_coords_attributes (parm_die, node);
17869 if (origin == NULL
17870 || (! DECL_ABSTRACT_P (node_or_origin)
17871 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17872 decl_function_context
17873 (node_or_origin))))
17874 {
17875 tree type = TREE_TYPE (node_or_origin);
17876 if (decl_by_reference_p (node_or_origin))
17877 add_type_attribute (parm_die, TREE_TYPE (type),
17878 TYPE_UNQUALIFIED, context_die);
17879 else
17880 add_type_attribute (parm_die, type,
17881 decl_quals (node_or_origin),
17882 context_die);
17883 }
17884 if (origin == NULL && DECL_ARTIFICIAL (node))
17885 add_AT_flag (parm_die, DW_AT_artificial, 1);
17886
17887 if (node && node != origin)
17888 equate_decl_number_to_die (node, parm_die);
17889 if (! DECL_ABSTRACT_P (node_or_origin))
17890 add_location_or_const_value_attribute (parm_die, node_or_origin,
17891 node == NULL, DW_AT_location);
17892
17893 break;
17894
17895 case tcc_type:
17896 /* We were called with some kind of a ..._TYPE node. */
17897 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17898 context_die);
17899 break;
17900
17901 default:
17902 gcc_unreachable ();
17903 }
17904
17905 return parm_die;
17906 }
17907
17908 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17909 children DW_TAG_formal_parameter DIEs representing the arguments of the
17910 parameter pack.
17911
17912 PARM_PACK must be a function parameter pack.
17913 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17914 must point to the subsequent arguments of the function PACK_ARG belongs to.
17915 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17916 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17917 following the last one for which a DIE was generated. */
17918
17919 static dw_die_ref
17920 gen_formal_parameter_pack_die (tree parm_pack,
17921 tree pack_arg,
17922 dw_die_ref subr_die,
17923 tree *next_arg)
17924 {
17925 tree arg;
17926 dw_die_ref parm_pack_die;
17927
17928 gcc_assert (parm_pack
17929 && lang_hooks.function_parameter_pack_p (parm_pack)
17930 && subr_die);
17931
17932 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17933 add_src_coords_attributes (parm_pack_die, parm_pack);
17934
17935 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17936 {
17937 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17938 parm_pack))
17939 break;
17940 gen_formal_parameter_die (arg, NULL,
17941 false /* Don't emit name attribute. */,
17942 parm_pack_die);
17943 }
17944 if (next_arg)
17945 *next_arg = arg;
17946 return parm_pack_die;
17947 }
17948
17949 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17950 at the end of an (ANSI prototyped) formal parameters list. */
17951
17952 static void
17953 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17954 {
17955 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17956 }
17957
17958 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17959 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17960 parameters as specified in some function type specification (except for
17961 those which appear as part of a function *definition*). */
17962
17963 static void
17964 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17965 {
17966 tree link;
17967 tree formal_type = NULL;
17968 tree first_parm_type;
17969 tree arg;
17970
17971 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17972 {
17973 arg = DECL_ARGUMENTS (function_or_method_type);
17974 function_or_method_type = TREE_TYPE (function_or_method_type);
17975 }
17976 else
17977 arg = NULL_TREE;
17978
17979 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17980
17981 /* Make our first pass over the list of formal parameter types and output a
17982 DW_TAG_formal_parameter DIE for each one. */
17983 for (link = first_parm_type; link; )
17984 {
17985 dw_die_ref parm_die;
17986
17987 formal_type = TREE_VALUE (link);
17988 if (formal_type == void_type_node)
17989 break;
17990
17991 /* Output a (nameless) DIE to represent the formal parameter itself. */
17992 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17993 {
17994 parm_die = gen_formal_parameter_die (formal_type, NULL,
17995 true /* Emit name attribute. */,
17996 context_die);
17997 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17998 && link == first_parm_type)
17999 {
18000 add_AT_flag (parm_die, DW_AT_artificial, 1);
18001 if (dwarf_version >= 3 || !dwarf_strict)
18002 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18003 }
18004 else if (arg && DECL_ARTIFICIAL (arg))
18005 add_AT_flag (parm_die, DW_AT_artificial, 1);
18006 }
18007
18008 link = TREE_CHAIN (link);
18009 if (arg)
18010 arg = DECL_CHAIN (arg);
18011 }
18012
18013 /* If this function type has an ellipsis, add a
18014 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18015 if (formal_type != void_type_node)
18016 gen_unspecified_parameters_die (function_or_method_type, context_die);
18017
18018 /* Make our second (and final) pass over the list of formal parameter types
18019 and output DIEs to represent those types (as necessary). */
18020 for (link = TYPE_ARG_TYPES (function_or_method_type);
18021 link && TREE_VALUE (link);
18022 link = TREE_CHAIN (link))
18023 gen_type_die (TREE_VALUE (link), context_die);
18024 }
18025
18026 /* We want to generate the DIE for TYPE so that we can generate the
18027 die for MEMBER, which has been defined; we will need to refer back
18028 to the member declaration nested within TYPE. If we're trying to
18029 generate minimal debug info for TYPE, processing TYPE won't do the
18030 trick; we need to attach the member declaration by hand. */
18031
18032 static void
18033 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18034 {
18035 gen_type_die (type, context_die);
18036
18037 /* If we're trying to avoid duplicate debug info, we may not have
18038 emitted the member decl for this function. Emit it now. */
18039 if (TYPE_STUB_DECL (type)
18040 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18041 && ! lookup_decl_die (member))
18042 {
18043 dw_die_ref type_die;
18044 gcc_assert (!decl_ultimate_origin (member));
18045
18046 push_decl_scope (type);
18047 type_die = lookup_type_die_strip_naming_typedef (type);
18048 if (TREE_CODE (member) == FUNCTION_DECL)
18049 gen_subprogram_die (member, type_die);
18050 else if (TREE_CODE (member) == FIELD_DECL)
18051 {
18052 /* Ignore the nameless fields that are used to skip bits but handle
18053 C++ anonymous unions and structs. */
18054 if (DECL_NAME (member) != NULL_TREE
18055 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18056 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18057 {
18058 gen_type_die (member_declared_type (member), type_die);
18059 gen_field_die (member, type_die);
18060 }
18061 }
18062 else
18063 gen_variable_die (member, NULL_TREE, type_die);
18064
18065 pop_decl_scope ();
18066 }
18067 }
18068 \f
18069 /* Forward declare these functions, because they are mutually recursive
18070 with their set_block_* pairing functions. */
18071 static void set_decl_origin_self (tree);
18072 static void set_decl_abstract_flags (tree, vec<tree> &);
18073
18074 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18075 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18076 that it points to the node itself, thus indicating that the node is its
18077 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18078 the given node is NULL, recursively descend the decl/block tree which
18079 it is the root of, and for each other ..._DECL or BLOCK node contained
18080 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18081 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18082 values to point to themselves. */
18083
18084 static void
18085 set_block_origin_self (tree stmt)
18086 {
18087 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18088 {
18089 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18090
18091 {
18092 tree local_decl;
18093
18094 for (local_decl = BLOCK_VARS (stmt);
18095 local_decl != NULL_TREE;
18096 local_decl = DECL_CHAIN (local_decl))
18097 /* Do not recurse on nested functions since the inlining status
18098 of parent and child can be different as per the DWARF spec. */
18099 if (TREE_CODE (local_decl) != FUNCTION_DECL
18100 && !DECL_EXTERNAL (local_decl))
18101 set_decl_origin_self (local_decl);
18102 }
18103
18104 {
18105 tree subblock;
18106
18107 for (subblock = BLOCK_SUBBLOCKS (stmt);
18108 subblock != NULL_TREE;
18109 subblock = BLOCK_CHAIN (subblock))
18110 set_block_origin_self (subblock); /* Recurse. */
18111 }
18112 }
18113 }
18114
18115 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18116 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18117 node to so that it points to the node itself, thus indicating that the
18118 node represents its own (abstract) origin. Additionally, if the
18119 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18120 the decl/block tree of which the given node is the root of, and for
18121 each other ..._DECL or BLOCK node contained therein whose
18122 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18123 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18124 point to themselves. */
18125
18126 static void
18127 set_decl_origin_self (tree decl)
18128 {
18129 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18130 {
18131 DECL_ABSTRACT_ORIGIN (decl) = decl;
18132 if (TREE_CODE (decl) == FUNCTION_DECL)
18133 {
18134 tree arg;
18135
18136 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18137 DECL_ABSTRACT_ORIGIN (arg) = arg;
18138 if (DECL_INITIAL (decl) != NULL_TREE
18139 && DECL_INITIAL (decl) != error_mark_node)
18140 set_block_origin_self (DECL_INITIAL (decl));
18141 }
18142 }
18143 }
18144 \f
18145 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18146 and if it wasn't 1 before, push it to abstract_vec vector.
18147 For all local decls and all local sub-blocks (recursively) do it
18148 too. */
18149
18150 static void
18151 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18152 {
18153 tree local_decl;
18154 tree subblock;
18155 unsigned int i;
18156
18157 if (!BLOCK_ABSTRACT (stmt))
18158 {
18159 abstract_vec.safe_push (stmt);
18160 BLOCK_ABSTRACT (stmt) = 1;
18161 }
18162
18163 for (local_decl = BLOCK_VARS (stmt);
18164 local_decl != NULL_TREE;
18165 local_decl = DECL_CHAIN (local_decl))
18166 if (! DECL_EXTERNAL (local_decl))
18167 set_decl_abstract_flags (local_decl, abstract_vec);
18168
18169 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18170 {
18171 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18172 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18173 || TREE_CODE (local_decl) == PARM_DECL)
18174 set_decl_abstract_flags (local_decl, abstract_vec);
18175 }
18176
18177 for (subblock = BLOCK_SUBBLOCKS (stmt);
18178 subblock != NULL_TREE;
18179 subblock = BLOCK_CHAIN (subblock))
18180 set_block_abstract_flags (subblock, abstract_vec);
18181 }
18182
18183 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18184 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18185 In the case where the decl is a FUNCTION_DECL also set the abstract
18186 flags for all of the parameters, local vars, local
18187 blocks and sub-blocks (recursively). */
18188
18189 static void
18190 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18191 {
18192 if (!DECL_ABSTRACT_P (decl))
18193 {
18194 abstract_vec.safe_push (decl);
18195 DECL_ABSTRACT_P (decl) = 1;
18196 }
18197
18198 if (TREE_CODE (decl) == FUNCTION_DECL)
18199 {
18200 tree arg;
18201
18202 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18203 if (!DECL_ABSTRACT_P (arg))
18204 {
18205 abstract_vec.safe_push (arg);
18206 DECL_ABSTRACT_P (arg) = 1;
18207 }
18208 if (DECL_INITIAL (decl) != NULL_TREE
18209 && DECL_INITIAL (decl) != error_mark_node)
18210 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18211 }
18212 }
18213
18214 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18215 may later generate inlined and/or out-of-line instances of. */
18216
18217 static void
18218 dwarf2out_abstract_function (tree decl)
18219 {
18220 dw_die_ref old_die;
18221 tree save_fn;
18222 tree context;
18223 hash_table<decl_loc_hasher> *old_decl_loc_table;
18224 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18225 int old_call_site_count, old_tail_call_site_count;
18226 struct call_arg_loc_node *old_call_arg_locations;
18227
18228 /* Make sure we have the actual abstract inline, not a clone. */
18229 decl = DECL_ORIGIN (decl);
18230
18231 old_die = lookup_decl_die (decl);
18232 if (old_die && get_AT (old_die, DW_AT_inline))
18233 /* We've already generated the abstract instance. */
18234 return;
18235
18236 /* We can be called while recursively when seeing block defining inlined subroutine
18237 DIE. Be sure to not clobber the outer location table nor use it or we would
18238 get locations in abstract instantces. */
18239 old_decl_loc_table = decl_loc_table;
18240 decl_loc_table = NULL;
18241 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18242 cached_dw_loc_list_table = NULL;
18243 old_call_arg_locations = call_arg_locations;
18244 call_arg_locations = NULL;
18245 old_call_site_count = call_site_count;
18246 call_site_count = -1;
18247 old_tail_call_site_count = tail_call_site_count;
18248 tail_call_site_count = -1;
18249
18250 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18251 we don't get confused by DECL_ABSTRACT_P. */
18252 if (debug_info_level > DINFO_LEVEL_TERSE)
18253 {
18254 context = decl_class_context (decl);
18255 if (context)
18256 gen_type_die_for_member
18257 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18258 }
18259
18260 /* Pretend we've just finished compiling this function. */
18261 save_fn = current_function_decl;
18262 current_function_decl = decl;
18263
18264 auto_vec<tree, 64> abstract_vec;
18265 set_decl_abstract_flags (decl, abstract_vec);
18266 dwarf2out_decl (decl);
18267 unsigned int i;
18268 tree t;
18269 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18270 if (TREE_CODE (t) == BLOCK)
18271 BLOCK_ABSTRACT (t) = 0;
18272 else
18273 DECL_ABSTRACT_P (t) = 0;
18274
18275 current_function_decl = save_fn;
18276 decl_loc_table = old_decl_loc_table;
18277 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18278 call_arg_locations = old_call_arg_locations;
18279 call_site_count = old_call_site_count;
18280 tail_call_site_count = old_tail_call_site_count;
18281 }
18282
18283 /* Helper function of premark_used_types() which gets called through
18284 htab_traverse.
18285
18286 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18287 marked as unused by prune_unused_types. */
18288
18289 bool
18290 premark_used_types_helper (tree const &type, void *)
18291 {
18292 dw_die_ref die;
18293
18294 die = lookup_type_die (type);
18295 if (die != NULL)
18296 die->die_perennial_p = 1;
18297 return true;
18298 }
18299
18300 /* Helper function of premark_types_used_by_global_vars which gets called
18301 through htab_traverse.
18302
18303 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18304 marked as unused by prune_unused_types. The DIE of the type is marked
18305 only if the global variable using the type will actually be emitted. */
18306
18307 int
18308 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18309 void *)
18310 {
18311 struct types_used_by_vars_entry *entry;
18312 dw_die_ref die;
18313
18314 entry = (struct types_used_by_vars_entry *) *slot;
18315 gcc_assert (entry->type != NULL
18316 && entry->var_decl != NULL);
18317 die = lookup_type_die (entry->type);
18318 if (die)
18319 {
18320 /* Ask cgraph if the global variable really is to be emitted.
18321 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18322 varpool_node *node = varpool_node::get (entry->var_decl);
18323 if (node && node->definition)
18324 {
18325 die->die_perennial_p = 1;
18326 /* Keep the parent DIEs as well. */
18327 while ((die = die->die_parent) && die->die_perennial_p == 0)
18328 die->die_perennial_p = 1;
18329 }
18330 }
18331 return 1;
18332 }
18333
18334 /* Mark all members of used_types_hash as perennial. */
18335
18336 static void
18337 premark_used_types (struct function *fun)
18338 {
18339 if (fun && fun->used_types_hash)
18340 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18341 }
18342
18343 /* Mark all members of types_used_by_vars_entry as perennial. */
18344
18345 static void
18346 premark_types_used_by_global_vars (void)
18347 {
18348 if (types_used_by_vars_hash)
18349 types_used_by_vars_hash
18350 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18351 }
18352
18353 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18354 for CA_LOC call arg loc node. */
18355
18356 static dw_die_ref
18357 gen_call_site_die (tree decl, dw_die_ref subr_die,
18358 struct call_arg_loc_node *ca_loc)
18359 {
18360 dw_die_ref stmt_die = NULL, die;
18361 tree block = ca_loc->block;
18362
18363 while (block
18364 && block != DECL_INITIAL (decl)
18365 && TREE_CODE (block) == BLOCK)
18366 {
18367 stmt_die = BLOCK_DIE (block);
18368 if (stmt_die)
18369 break;
18370 block = BLOCK_SUPERCONTEXT (block);
18371 }
18372 if (stmt_die == NULL)
18373 stmt_die = subr_die;
18374 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18375 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18376 if (ca_loc->tail_call_p)
18377 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18378 if (ca_loc->symbol_ref)
18379 {
18380 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18381 if (tdie)
18382 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18383 else
18384 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18385 }
18386 return die;
18387 }
18388
18389 /* Generate a DIE to represent a declared function (either file-scope or
18390 block-local). */
18391
18392 static void
18393 gen_subprogram_die (tree decl, dw_die_ref context_die)
18394 {
18395 tree origin = decl_ultimate_origin (decl);
18396 dw_die_ref subr_die;
18397 tree outer_scope;
18398 dw_die_ref old_die = lookup_decl_die (decl);
18399 int declaration = (current_function_decl != decl
18400 || class_or_namespace_scope_p (context_die));
18401
18402 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18403
18404 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18405 started to generate the abstract instance of an inline, decided to output
18406 its containing class, and proceeded to emit the declaration of the inline
18407 from the member list for the class. If so, DECLARATION takes priority;
18408 we'll get back to the abstract instance when done with the class. */
18409
18410 /* The class-scope declaration DIE must be the primary DIE. */
18411 if (origin && declaration && class_or_namespace_scope_p (context_die))
18412 {
18413 origin = NULL;
18414 gcc_assert (!old_die);
18415 }
18416
18417 /* Now that the C++ front end lazily declares artificial member fns, we
18418 might need to retrofit the declaration into its class. */
18419 if (!declaration && !origin && !old_die
18420 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18421 && !class_or_namespace_scope_p (context_die)
18422 && debug_info_level > DINFO_LEVEL_TERSE)
18423 old_die = force_decl_die (decl);
18424
18425 if (origin != NULL)
18426 {
18427 gcc_assert (!declaration || local_scope_p (context_die));
18428
18429 /* Fixup die_parent for the abstract instance of a nested
18430 inline function. */
18431 if (old_die && old_die->die_parent == NULL)
18432 add_child_die (context_die, old_die);
18433
18434 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18435 add_abstract_origin_attribute (subr_die, origin);
18436 /* This is where the actual code for a cloned function is.
18437 Let's emit linkage name attribute for it. This helps
18438 debuggers to e.g, set breakpoints into
18439 constructors/destructors when the user asks "break
18440 K::K". */
18441 add_linkage_name (subr_die, decl);
18442 }
18443 else if (old_die)
18444 {
18445 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18446 struct dwarf_file_data * file_index = lookup_filename (s.file);
18447
18448 if (!get_AT_flag (old_die, DW_AT_declaration)
18449 /* We can have a normal definition following an inline one in the
18450 case of redefinition of GNU C extern inlines.
18451 It seems reasonable to use AT_specification in this case. */
18452 && !get_AT (old_die, DW_AT_inline))
18453 {
18454 /* Detect and ignore this case, where we are trying to output
18455 something we have already output. */
18456 return;
18457 }
18458
18459 /* If the definition comes from the same place as the declaration,
18460 maybe use the old DIE. We always want the DIE for this function
18461 that has the *_pc attributes to be under comp_unit_die so the
18462 debugger can find it. We also need to do this for abstract
18463 instances of inlines, since the spec requires the out-of-line copy
18464 to have the same parent. For local class methods, this doesn't
18465 apply; we just use the old DIE. */
18466 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18467 && (DECL_ARTIFICIAL (decl)
18468 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18469 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18470 == (unsigned) s.line))))
18471 {
18472 subr_die = old_die;
18473
18474 /* Clear out the declaration attribute and the formal parameters.
18475 Do not remove all children, because it is possible that this
18476 declaration die was forced using force_decl_die(). In such
18477 cases die that forced declaration die (e.g. TAG_imported_module)
18478 is one of the children that we do not want to remove. */
18479 remove_AT (subr_die, DW_AT_declaration);
18480 remove_AT (subr_die, DW_AT_object_pointer);
18481 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18482 }
18483 else
18484 {
18485 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18486 add_AT_specification (subr_die, old_die);
18487 add_pubname (decl, subr_die);
18488 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18489 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18490 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18491 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18492
18493 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18494 emit the real type on the definition die. */
18495 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18496 {
18497 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18498 if (die == auto_die || die == decltype_auto_die)
18499 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18500 TYPE_UNQUALIFIED, context_die);
18501 }
18502 }
18503 }
18504 else
18505 {
18506 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18507
18508 if (TREE_PUBLIC (decl))
18509 add_AT_flag (subr_die, DW_AT_external, 1);
18510
18511 add_name_and_src_coords_attributes (subr_die, decl);
18512 add_pubname (decl, subr_die);
18513 if (debug_info_level > DINFO_LEVEL_TERSE)
18514 {
18515 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18516 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18517 TYPE_UNQUALIFIED, context_die);
18518 }
18519
18520 add_pure_or_virtual_attribute (subr_die, decl);
18521 if (DECL_ARTIFICIAL (decl))
18522 add_AT_flag (subr_die, DW_AT_artificial, 1);
18523
18524 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18525 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18526
18527 add_accessibility_attribute (subr_die, decl);
18528 }
18529
18530 if (declaration)
18531 {
18532 if (!old_die || !get_AT (old_die, DW_AT_inline))
18533 {
18534 add_AT_flag (subr_die, DW_AT_declaration, 1);
18535
18536 /* If this is an explicit function declaration then generate
18537 a DW_AT_explicit attribute. */
18538 if (lang_hooks.decls.function_decl_explicit_p (decl)
18539 && (dwarf_version >= 3 || !dwarf_strict))
18540 add_AT_flag (subr_die, DW_AT_explicit, 1);
18541
18542 /* If this is a C++11 deleted special function member then generate
18543 a DW_AT_GNU_deleted attribute. */
18544 if (lang_hooks.decls.function_decl_deleted_p (decl)
18545 && (! dwarf_strict))
18546 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18547
18548 /* The first time we see a member function, it is in the context of
18549 the class to which it belongs. We make sure of this by emitting
18550 the class first. The next time is the definition, which is
18551 handled above. The two may come from the same source text.
18552
18553 Note that force_decl_die() forces function declaration die. It is
18554 later reused to represent definition. */
18555 equate_decl_number_to_die (decl, subr_die);
18556 }
18557 }
18558 else if (DECL_ABSTRACT_P (decl))
18559 {
18560 if (DECL_DECLARED_INLINE_P (decl))
18561 {
18562 if (cgraph_function_possibly_inlined_p (decl))
18563 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18564 else
18565 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18566 }
18567 else
18568 {
18569 if (cgraph_function_possibly_inlined_p (decl))
18570 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18571 else
18572 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18573 }
18574
18575 if (DECL_DECLARED_INLINE_P (decl)
18576 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18577 add_AT_flag (subr_die, DW_AT_artificial, 1);
18578
18579 equate_decl_number_to_die (decl, subr_die);
18580 }
18581 else if (!DECL_EXTERNAL (decl))
18582 {
18583 HOST_WIDE_INT cfa_fb_offset;
18584 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18585
18586 if (!old_die || !get_AT (old_die, DW_AT_inline))
18587 equate_decl_number_to_die (decl, subr_die);
18588
18589 gcc_checking_assert (fun);
18590 if (!flag_reorder_blocks_and_partition)
18591 {
18592 dw_fde_ref fde = fun->fde;
18593 if (fde->dw_fde_begin)
18594 {
18595 /* We have already generated the labels. */
18596 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18597 fde->dw_fde_end, false);
18598 }
18599 else
18600 {
18601 /* Create start/end labels and add the range. */
18602 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18603 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18604 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18605 current_function_funcdef_no);
18606 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18607 current_function_funcdef_no);
18608 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18609 false);
18610 }
18611
18612 #if VMS_DEBUGGING_INFO
18613 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18614 Section 2.3 Prologue and Epilogue Attributes:
18615 When a breakpoint is set on entry to a function, it is generally
18616 desirable for execution to be suspended, not on the very first
18617 instruction of the function, but rather at a point after the
18618 function's frame has been set up, after any language defined local
18619 declaration processing has been completed, and before execution of
18620 the first statement of the function begins. Debuggers generally
18621 cannot properly determine where this point is. Similarly for a
18622 breakpoint set on exit from a function. The prologue and epilogue
18623 attributes allow a compiler to communicate the location(s) to use. */
18624
18625 {
18626 if (fde->dw_fde_vms_end_prologue)
18627 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18628 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18629
18630 if (fde->dw_fde_vms_begin_epilogue)
18631 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18632 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18633 }
18634 #endif
18635
18636 }
18637 else
18638 {
18639 /* Generate pubnames entries for the split function code ranges. */
18640 dw_fde_ref fde = fun->fde;
18641
18642 if (fde->dw_fde_second_begin)
18643 {
18644 if (dwarf_version >= 3 || !dwarf_strict)
18645 {
18646 /* We should use ranges for non-contiguous code section
18647 addresses. Use the actual code range for the initial
18648 section, since the HOT/COLD labels might precede an
18649 alignment offset. */
18650 bool range_list_added = false;
18651 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18652 fde->dw_fde_end, &range_list_added,
18653 false);
18654 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18655 fde->dw_fde_second_end,
18656 &range_list_added, false);
18657 if (range_list_added)
18658 add_ranges (NULL);
18659 }
18660 else
18661 {
18662 /* There is no real support in DW2 for this .. so we make
18663 a work-around. First, emit the pub name for the segment
18664 containing the function label. Then make and emit a
18665 simplified subprogram DIE for the second segment with the
18666 name pre-fixed by __hot/cold_sect_of_. We use the same
18667 linkage name for the second die so that gdb will find both
18668 sections when given "b foo". */
18669 const char *name = NULL;
18670 tree decl_name = DECL_NAME (decl);
18671 dw_die_ref seg_die;
18672
18673 /* Do the 'primary' section. */
18674 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18675 fde->dw_fde_end, false);
18676
18677 /* Build a minimal DIE for the secondary section. */
18678 seg_die = new_die (DW_TAG_subprogram,
18679 subr_die->die_parent, decl);
18680
18681 if (TREE_PUBLIC (decl))
18682 add_AT_flag (seg_die, DW_AT_external, 1);
18683
18684 if (decl_name != NULL
18685 && IDENTIFIER_POINTER (decl_name) != NULL)
18686 {
18687 name = dwarf2_name (decl, 1);
18688 if (! DECL_ARTIFICIAL (decl))
18689 add_src_coords_attributes (seg_die, decl);
18690
18691 add_linkage_name (seg_die, decl);
18692 }
18693 gcc_assert (name != NULL);
18694 add_pure_or_virtual_attribute (seg_die, decl);
18695 if (DECL_ARTIFICIAL (decl))
18696 add_AT_flag (seg_die, DW_AT_artificial, 1);
18697
18698 name = concat ("__second_sect_of_", name, NULL);
18699 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18700 fde->dw_fde_second_end, false);
18701 add_name_attribute (seg_die, name);
18702 if (want_pubnames ())
18703 add_pubname_string (name, seg_die);
18704 }
18705 }
18706 else
18707 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18708 false);
18709 }
18710
18711 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18712
18713 /* We define the "frame base" as the function's CFA. This is more
18714 convenient for several reasons: (1) It's stable across the prologue
18715 and epilogue, which makes it better than just a frame pointer,
18716 (2) With dwarf3, there exists a one-byte encoding that allows us
18717 to reference the .debug_frame data by proxy, but failing that,
18718 (3) We can at least reuse the code inspection and interpretation
18719 code that determines the CFA position at various points in the
18720 function. */
18721 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18722 {
18723 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18724 add_AT_loc (subr_die, DW_AT_frame_base, op);
18725 }
18726 else
18727 {
18728 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18729 if (list->dw_loc_next)
18730 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18731 else
18732 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18733 }
18734
18735 /* Compute a displacement from the "steady-state frame pointer" to
18736 the CFA. The former is what all stack slots and argument slots
18737 will reference in the rtl; the latter is what we've told the
18738 debugger about. We'll need to adjust all frame_base references
18739 by this displacement. */
18740 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18741
18742 if (fun->static_chain_decl)
18743 add_AT_location_description (subr_die, DW_AT_static_link,
18744 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18745 }
18746
18747 /* Generate child dies for template paramaters. */
18748 if (debug_info_level > DINFO_LEVEL_TERSE)
18749 gen_generic_params_dies (decl);
18750
18751 /* Now output descriptions of the arguments for this function. This gets
18752 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18753 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18754 `...' at the end of the formal parameter list. In order to find out if
18755 there was a trailing ellipsis or not, we must instead look at the type
18756 associated with the FUNCTION_DECL. This will be a node of type
18757 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18758 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18759 an ellipsis at the end. */
18760
18761 /* In the case where we are describing a mere function declaration, all we
18762 need to do here (and all we *can* do here) is to describe the *types* of
18763 its formal parameters. */
18764 if (debug_info_level <= DINFO_LEVEL_TERSE)
18765 ;
18766 else if (declaration)
18767 gen_formal_types_die (decl, subr_die);
18768 else
18769 {
18770 /* Generate DIEs to represent all known formal parameters. */
18771 tree parm = DECL_ARGUMENTS (decl);
18772 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18773 tree generic_decl_parm = generic_decl
18774 ? DECL_ARGUMENTS (generic_decl)
18775 : NULL;
18776
18777 /* Now we want to walk the list of parameters of the function and
18778 emit their relevant DIEs.
18779
18780 We consider the case of DECL being an instance of a generic function
18781 as well as it being a normal function.
18782
18783 If DECL is an instance of a generic function we walk the
18784 parameters of the generic function declaration _and_ the parameters of
18785 DECL itself. This is useful because we want to emit specific DIEs for
18786 function parameter packs and those are declared as part of the
18787 generic function declaration. In that particular case,
18788 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18789 That DIE has children DIEs representing the set of arguments
18790 of the pack. Note that the set of pack arguments can be empty.
18791 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18792 children DIE.
18793
18794 Otherwise, we just consider the parameters of DECL. */
18795 while (generic_decl_parm || parm)
18796 {
18797 if (generic_decl_parm
18798 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18799 gen_formal_parameter_pack_die (generic_decl_parm,
18800 parm, subr_die,
18801 &parm);
18802 else if (parm && !POINTER_BOUNDS_P (parm))
18803 {
18804 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18805
18806 if (parm == DECL_ARGUMENTS (decl)
18807 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18808 && parm_die
18809 && (dwarf_version >= 3 || !dwarf_strict))
18810 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18811
18812 parm = DECL_CHAIN (parm);
18813 }
18814 else if (parm)
18815 parm = DECL_CHAIN (parm);
18816
18817 if (generic_decl_parm)
18818 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18819 }
18820
18821 /* Decide whether we need an unspecified_parameters DIE at the end.
18822 There are 2 more cases to do this for: 1) the ansi ... declaration -
18823 this is detectable when the end of the arg list is not a
18824 void_type_node 2) an unprototyped function declaration (not a
18825 definition). This just means that we have no info about the
18826 parameters at all. */
18827 if (prototype_p (TREE_TYPE (decl)))
18828 {
18829 /* This is the prototyped case, check for.... */
18830 if (stdarg_p (TREE_TYPE (decl)))
18831 gen_unspecified_parameters_die (decl, subr_die);
18832 }
18833 else if (DECL_INITIAL (decl) == NULL_TREE)
18834 gen_unspecified_parameters_die (decl, subr_die);
18835 }
18836
18837 /* Output Dwarf info for all of the stuff within the body of the function
18838 (if it has one - it may be just a declaration). */
18839 outer_scope = DECL_INITIAL (decl);
18840
18841 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18842 a function. This BLOCK actually represents the outermost binding contour
18843 for the function, i.e. the contour in which the function's formal
18844 parameters and labels get declared. Curiously, it appears that the front
18845 end doesn't actually put the PARM_DECL nodes for the current function onto
18846 the BLOCK_VARS list for this outer scope, but are strung off of the
18847 DECL_ARGUMENTS list for the function instead.
18848
18849 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18850 the LABEL_DECL nodes for the function however, and we output DWARF info
18851 for those in decls_for_scope. Just within the `outer_scope' there will be
18852 a BLOCK node representing the function's outermost pair of curly braces,
18853 and any blocks used for the base and member initializers of a C++
18854 constructor function. */
18855 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18856 {
18857 int call_site_note_count = 0;
18858 int tail_call_site_note_count = 0;
18859
18860 /* Emit a DW_TAG_variable DIE for a named return value. */
18861 if (DECL_NAME (DECL_RESULT (decl)))
18862 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18863
18864 decls_for_scope (outer_scope, subr_die);
18865
18866 if (call_arg_locations && !dwarf_strict)
18867 {
18868 struct call_arg_loc_node *ca_loc;
18869 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18870 {
18871 dw_die_ref die = NULL;
18872 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18873 rtx arg, next_arg;
18874
18875 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18876 arg; arg = next_arg)
18877 {
18878 dw_loc_descr_ref reg, val;
18879 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18880 dw_die_ref cdie, tdie = NULL;
18881
18882 next_arg = XEXP (arg, 1);
18883 if (REG_P (XEXP (XEXP (arg, 0), 0))
18884 && next_arg
18885 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18886 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18887 && REGNO (XEXP (XEXP (arg, 0), 0))
18888 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18889 next_arg = XEXP (next_arg, 1);
18890 if (mode == VOIDmode)
18891 {
18892 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18893 if (mode == VOIDmode)
18894 mode = GET_MODE (XEXP (arg, 0));
18895 }
18896 if (mode == VOIDmode || mode == BLKmode)
18897 continue;
18898 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18899 {
18900 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18901 tloc = XEXP (XEXP (arg, 0), 1);
18902 continue;
18903 }
18904 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18905 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18906 {
18907 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18908 tlocc = XEXP (XEXP (arg, 0), 1);
18909 continue;
18910 }
18911 reg = NULL;
18912 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18913 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18914 VAR_INIT_STATUS_INITIALIZED);
18915 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18916 {
18917 rtx mem = XEXP (XEXP (arg, 0), 0);
18918 reg = mem_loc_descriptor (XEXP (mem, 0),
18919 get_address_mode (mem),
18920 GET_MODE (mem),
18921 VAR_INIT_STATUS_INITIALIZED);
18922 }
18923 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18924 == DEBUG_PARAMETER_REF)
18925 {
18926 tree tdecl
18927 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18928 tdie = lookup_decl_die (tdecl);
18929 if (tdie == NULL)
18930 continue;
18931 }
18932 else
18933 continue;
18934 if (reg == NULL
18935 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18936 != DEBUG_PARAMETER_REF)
18937 continue;
18938 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18939 VOIDmode,
18940 VAR_INIT_STATUS_INITIALIZED);
18941 if (val == NULL)
18942 continue;
18943 if (die == NULL)
18944 die = gen_call_site_die (decl, subr_die, ca_loc);
18945 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18946 NULL_TREE);
18947 if (reg != NULL)
18948 add_AT_loc (cdie, DW_AT_location, reg);
18949 else if (tdie != NULL)
18950 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18951 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18952 if (next_arg != XEXP (arg, 1))
18953 {
18954 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18955 if (mode == VOIDmode)
18956 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18957 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18958 0), 1),
18959 mode, VOIDmode,
18960 VAR_INIT_STATUS_INITIALIZED);
18961 if (val != NULL)
18962 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18963 }
18964 }
18965 if (die == NULL
18966 && (ca_loc->symbol_ref || tloc))
18967 die = gen_call_site_die (decl, subr_die, ca_loc);
18968 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18969 {
18970 dw_loc_descr_ref tval = NULL;
18971
18972 if (tloc != NULL_RTX)
18973 tval = mem_loc_descriptor (tloc,
18974 GET_MODE (tloc) == VOIDmode
18975 ? Pmode : GET_MODE (tloc),
18976 VOIDmode,
18977 VAR_INIT_STATUS_INITIALIZED);
18978 if (tval)
18979 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18980 else if (tlocc != NULL_RTX)
18981 {
18982 tval = mem_loc_descriptor (tlocc,
18983 GET_MODE (tlocc) == VOIDmode
18984 ? Pmode : GET_MODE (tlocc),
18985 VOIDmode,
18986 VAR_INIT_STATUS_INITIALIZED);
18987 if (tval)
18988 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18989 tval);
18990 }
18991 }
18992 if (die != NULL)
18993 {
18994 call_site_note_count++;
18995 if (ca_loc->tail_call_p)
18996 tail_call_site_note_count++;
18997 }
18998 }
18999 }
19000 call_arg_locations = NULL;
19001 call_arg_loc_last = NULL;
19002 if (tail_call_site_count >= 0
19003 && tail_call_site_count == tail_call_site_note_count
19004 && !dwarf_strict)
19005 {
19006 if (call_site_count >= 0
19007 && call_site_count == call_site_note_count)
19008 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19009 else
19010 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19011 }
19012 call_site_count = -1;
19013 tail_call_site_count = -1;
19014 }
19015
19016 if (subr_die != old_die)
19017 /* Add the calling convention attribute if requested. */
19018 add_calling_convention_attribute (subr_die, decl);
19019 }
19020
19021 /* Returns a hash value for X (which really is a die_struct). */
19022
19023 hashval_t
19024 block_die_hasher::hash (die_struct *d)
19025 {
19026 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19027 }
19028
19029 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19030 as decl_id and die_parent of die_struct Y. */
19031
19032 bool
19033 block_die_hasher::equal (die_struct *x, die_struct *y)
19034 {
19035 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19036 }
19037
19038 /* Generate a DIE to represent a declared data object.
19039 Either DECL or ORIGIN must be non-null. */
19040
19041 static void
19042 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19043 {
19044 HOST_WIDE_INT off = 0;
19045 tree com_decl;
19046 tree decl_or_origin = decl ? decl : origin;
19047 tree ultimate_origin;
19048 dw_die_ref var_die;
19049 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19050 dw_die_ref origin_die;
19051 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19052 || class_or_namespace_scope_p (context_die));
19053 bool specialization_p = false;
19054
19055 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19056 if (decl || ultimate_origin)
19057 origin = ultimate_origin;
19058 com_decl = fortran_common (decl_or_origin, &off);
19059
19060 /* Symbol in common gets emitted as a child of the common block, in the form
19061 of a data member. */
19062 if (com_decl)
19063 {
19064 dw_die_ref com_die;
19065 dw_loc_list_ref loc;
19066 die_node com_die_arg;
19067
19068 var_die = lookup_decl_die (decl_or_origin);
19069 if (var_die)
19070 {
19071 if (get_AT (var_die, DW_AT_location) == NULL)
19072 {
19073 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19074 if (loc)
19075 {
19076 if (off)
19077 {
19078 /* Optimize the common case. */
19079 if (single_element_loc_list_p (loc)
19080 && loc->expr->dw_loc_opc == DW_OP_addr
19081 && loc->expr->dw_loc_next == NULL
19082 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19083 == SYMBOL_REF)
19084 {
19085 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19086 loc->expr->dw_loc_oprnd1.v.val_addr
19087 = plus_constant (GET_MODE (x), x , off);
19088 }
19089 else
19090 loc_list_plus_const (loc, off);
19091 }
19092 add_AT_location_description (var_die, DW_AT_location, loc);
19093 remove_AT (var_die, DW_AT_declaration);
19094 }
19095 }
19096 return;
19097 }
19098
19099 if (common_block_die_table == NULL)
19100 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19101
19102 com_die_arg.decl_id = DECL_UID (com_decl);
19103 com_die_arg.die_parent = context_die;
19104 com_die = common_block_die_table->find (&com_die_arg);
19105 loc = loc_list_from_tree (com_decl, 2, NULL);
19106 if (com_die == NULL)
19107 {
19108 const char *cnam
19109 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19110 die_node **slot;
19111
19112 com_die = new_die (DW_TAG_common_block, context_die, decl);
19113 add_name_and_src_coords_attributes (com_die, com_decl);
19114 if (loc)
19115 {
19116 add_AT_location_description (com_die, DW_AT_location, loc);
19117 /* Avoid sharing the same loc descriptor between
19118 DW_TAG_common_block and DW_TAG_variable. */
19119 loc = loc_list_from_tree (com_decl, 2, NULL);
19120 }
19121 else if (DECL_EXTERNAL (decl))
19122 add_AT_flag (com_die, DW_AT_declaration, 1);
19123 if (want_pubnames ())
19124 add_pubname_string (cnam, com_die); /* ??? needed? */
19125 com_die->decl_id = DECL_UID (com_decl);
19126 slot = common_block_die_table->find_slot (com_die, INSERT);
19127 *slot = com_die;
19128 }
19129 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19130 {
19131 add_AT_location_description (com_die, DW_AT_location, loc);
19132 loc = loc_list_from_tree (com_decl, 2, NULL);
19133 remove_AT (com_die, DW_AT_declaration);
19134 }
19135 var_die = new_die (DW_TAG_variable, com_die, decl);
19136 add_name_and_src_coords_attributes (var_die, decl);
19137 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19138 context_die);
19139 add_AT_flag (var_die, DW_AT_external, 1);
19140 if (loc)
19141 {
19142 if (off)
19143 {
19144 /* Optimize the common case. */
19145 if (single_element_loc_list_p (loc)
19146 && loc->expr->dw_loc_opc == DW_OP_addr
19147 && loc->expr->dw_loc_next == NULL
19148 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19149 {
19150 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19151 loc->expr->dw_loc_oprnd1.v.val_addr
19152 = plus_constant (GET_MODE (x), x, off);
19153 }
19154 else
19155 loc_list_plus_const (loc, off);
19156 }
19157 add_AT_location_description (var_die, DW_AT_location, loc);
19158 }
19159 else if (DECL_EXTERNAL (decl))
19160 add_AT_flag (var_die, DW_AT_declaration, 1);
19161 equate_decl_number_to_die (decl, var_die);
19162 return;
19163 }
19164
19165 /* If the compiler emitted a definition for the DECL declaration
19166 and if we already emitted a DIE for it, don't emit a second
19167 DIE for it again. Allow re-declarations of DECLs that are
19168 inside functions, though. */
19169 if (old_die && declaration && !local_scope_p (context_die))
19170 return;
19171
19172 /* For static data members, the declaration in the class is supposed
19173 to have DW_TAG_member tag; the specification should still be
19174 DW_TAG_variable referencing the DW_TAG_member DIE. */
19175 if (declaration && class_scope_p (context_die))
19176 var_die = new_die (DW_TAG_member, context_die, decl);
19177 else
19178 var_die = new_die (DW_TAG_variable, context_die, decl);
19179
19180 origin_die = NULL;
19181 if (origin != NULL)
19182 origin_die = add_abstract_origin_attribute (var_die, origin);
19183
19184 /* Loop unrolling can create multiple blocks that refer to the same
19185 static variable, so we must test for the DW_AT_declaration flag.
19186
19187 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19188 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19189 sharing them.
19190
19191 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19192
19193 ??? The declare_in_namespace support causes us to get two DIEs for one
19194 variable, both of which are declarations. We want to avoid considering
19195 one to be a specification, so we must test that this DIE is not a
19196 declaration. */
19197 else if (old_die && TREE_STATIC (decl) && ! declaration
19198 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19199 {
19200 /* This is a definition of a C++ class level static. */
19201 add_AT_specification (var_die, old_die);
19202 specialization_p = true;
19203 if (DECL_NAME (decl))
19204 {
19205 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19206 struct dwarf_file_data * file_index = lookup_filename (s.file);
19207
19208 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19209 add_AT_file (var_die, DW_AT_decl_file, file_index);
19210
19211 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19212 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19213
19214 if (old_die->die_tag == DW_TAG_member)
19215 add_linkage_name (var_die, decl);
19216 }
19217 }
19218 else
19219 add_name_and_src_coords_attributes (var_die, decl);
19220
19221 if ((origin == NULL && !specialization_p)
19222 || (origin != NULL
19223 && !DECL_ABSTRACT_P (decl_or_origin)
19224 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19225 decl_function_context
19226 (decl_or_origin))))
19227 {
19228 tree type = TREE_TYPE (decl_or_origin);
19229
19230 if (decl_by_reference_p (decl_or_origin))
19231 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19232 context_die);
19233 else
19234 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19235 context_die);
19236 }
19237
19238 if (origin == NULL && !specialization_p)
19239 {
19240 if (TREE_PUBLIC (decl))
19241 add_AT_flag (var_die, DW_AT_external, 1);
19242
19243 if (DECL_ARTIFICIAL (decl))
19244 add_AT_flag (var_die, DW_AT_artificial, 1);
19245
19246 add_accessibility_attribute (var_die, decl);
19247 }
19248
19249 if (declaration)
19250 add_AT_flag (var_die, DW_AT_declaration, 1);
19251
19252 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19253 equate_decl_number_to_die (decl, var_die);
19254
19255 if (! declaration
19256 && (! DECL_ABSTRACT_P (decl_or_origin)
19257 /* Local static vars are shared between all clones/inlines,
19258 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19259 already set. */
19260 || (TREE_CODE (decl_or_origin) == VAR_DECL
19261 && TREE_STATIC (decl_or_origin)
19262 && DECL_RTL_SET_P (decl_or_origin)))
19263 /* When abstract origin already has DW_AT_location attribute, no need
19264 to add it again. */
19265 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19266 {
19267 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19268 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19269 defer_location (decl_or_origin, var_die);
19270 else
19271 add_location_or_const_value_attribute (var_die, decl_or_origin,
19272 decl == NULL, DW_AT_location);
19273 add_pubname (decl_or_origin, var_die);
19274 }
19275 else
19276 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19277 }
19278
19279 /* Generate a DIE to represent a named constant. */
19280
19281 static void
19282 gen_const_die (tree decl, dw_die_ref context_die)
19283 {
19284 dw_die_ref const_die;
19285 tree type = TREE_TYPE (decl);
19286
19287 const_die = new_die (DW_TAG_constant, context_die, decl);
19288 add_name_and_src_coords_attributes (const_die, decl);
19289 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19290 if (TREE_PUBLIC (decl))
19291 add_AT_flag (const_die, DW_AT_external, 1);
19292 if (DECL_ARTIFICIAL (decl))
19293 add_AT_flag (const_die, DW_AT_artificial, 1);
19294 tree_add_const_value_attribute_for_decl (const_die, decl);
19295 }
19296
19297 /* Generate a DIE to represent a label identifier. */
19298
19299 static void
19300 gen_label_die (tree decl, dw_die_ref context_die)
19301 {
19302 tree origin = decl_ultimate_origin (decl);
19303 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19304 rtx insn;
19305 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19306
19307 if (origin != NULL)
19308 add_abstract_origin_attribute (lbl_die, origin);
19309 else
19310 add_name_and_src_coords_attributes (lbl_die, decl);
19311
19312 if (DECL_ABSTRACT_P (decl))
19313 equate_decl_number_to_die (decl, lbl_die);
19314 else
19315 {
19316 insn = DECL_RTL_IF_SET (decl);
19317
19318 /* Deleted labels are programmer specified labels which have been
19319 eliminated because of various optimizations. We still emit them
19320 here so that it is possible to put breakpoints on them. */
19321 if (insn
19322 && (LABEL_P (insn)
19323 || ((NOTE_P (insn)
19324 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19325 {
19326 /* When optimization is enabled (via -O) some parts of the compiler
19327 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19328 represent source-level labels which were explicitly declared by
19329 the user. This really shouldn't be happening though, so catch
19330 it if it ever does happen. */
19331 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19332
19333 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19334 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19335 }
19336 else if (insn
19337 && NOTE_P (insn)
19338 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19339 && CODE_LABEL_NUMBER (insn) != -1)
19340 {
19341 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19342 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19343 }
19344 }
19345 }
19346
19347 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19348 attributes to the DIE for a block STMT, to describe where the inlined
19349 function was called from. This is similar to add_src_coords_attributes. */
19350
19351 static inline void
19352 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19353 {
19354 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19355
19356 if (dwarf_version >= 3 || !dwarf_strict)
19357 {
19358 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19359 add_AT_unsigned (die, DW_AT_call_line, s.line);
19360 }
19361 }
19362
19363
19364 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19365 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19366
19367 static inline void
19368 add_high_low_attributes (tree stmt, dw_die_ref die)
19369 {
19370 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19371
19372 if (BLOCK_FRAGMENT_CHAIN (stmt)
19373 && (dwarf_version >= 3 || !dwarf_strict))
19374 {
19375 tree chain, superblock = NULL_TREE;
19376 dw_die_ref pdie;
19377 dw_attr_ref attr = NULL;
19378
19379 if (inlined_function_outer_scope_p (stmt))
19380 {
19381 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19382 BLOCK_NUMBER (stmt));
19383 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19384 }
19385
19386 /* Optimize duplicate .debug_ranges lists or even tails of
19387 lists. If this BLOCK has same ranges as its supercontext,
19388 lookup DW_AT_ranges attribute in the supercontext (and
19389 recursively so), verify that the ranges_table contains the
19390 right values and use it instead of adding a new .debug_range. */
19391 for (chain = stmt, pdie = die;
19392 BLOCK_SAME_RANGE (chain);
19393 chain = BLOCK_SUPERCONTEXT (chain))
19394 {
19395 dw_attr_ref new_attr;
19396
19397 pdie = pdie->die_parent;
19398 if (pdie == NULL)
19399 break;
19400 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19401 break;
19402 new_attr = get_AT (pdie, DW_AT_ranges);
19403 if (new_attr == NULL
19404 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19405 break;
19406 attr = new_attr;
19407 superblock = BLOCK_SUPERCONTEXT (chain);
19408 }
19409 if (attr != NULL
19410 && (ranges_table[attr->dw_attr_val.v.val_offset
19411 / 2 / DWARF2_ADDR_SIZE].num
19412 == BLOCK_NUMBER (superblock))
19413 && BLOCK_FRAGMENT_CHAIN (superblock))
19414 {
19415 unsigned long off = attr->dw_attr_val.v.val_offset
19416 / 2 / DWARF2_ADDR_SIZE;
19417 unsigned long supercnt = 0, thiscnt = 0;
19418 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19419 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19420 {
19421 ++supercnt;
19422 gcc_checking_assert (ranges_table[off + supercnt].num
19423 == BLOCK_NUMBER (chain));
19424 }
19425 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19426 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19427 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19428 ++thiscnt;
19429 gcc_assert (supercnt >= thiscnt);
19430 add_AT_range_list (die, DW_AT_ranges,
19431 ((off + supercnt - thiscnt)
19432 * 2 * DWARF2_ADDR_SIZE),
19433 false);
19434 return;
19435 }
19436
19437 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19438
19439 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19440 do
19441 {
19442 add_ranges (chain);
19443 chain = BLOCK_FRAGMENT_CHAIN (chain);
19444 }
19445 while (chain);
19446 add_ranges (NULL);
19447 }
19448 else
19449 {
19450 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19451 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19452 BLOCK_NUMBER (stmt));
19453 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19454 BLOCK_NUMBER (stmt));
19455 add_AT_low_high_pc (die, label, label_high, false);
19456 }
19457 }
19458
19459 /* Generate a DIE for a lexical block. */
19460
19461 static void
19462 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19463 {
19464 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19465
19466 if (call_arg_locations)
19467 BLOCK_DIE (stmt) = stmt_die;
19468
19469 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19470 add_high_low_attributes (stmt, stmt_die);
19471
19472 decls_for_scope (stmt, stmt_die);
19473 }
19474
19475 /* Generate a DIE for an inlined subprogram. */
19476
19477 static void
19478 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19479 {
19480 tree decl;
19481
19482 /* The instance of function that is effectively being inlined shall not
19483 be abstract. */
19484 gcc_assert (! BLOCK_ABSTRACT (stmt));
19485
19486 decl = block_ultimate_origin (stmt);
19487
19488 /* Emit info for the abstract instance first, if we haven't yet. We
19489 must emit this even if the block is abstract, otherwise when we
19490 emit the block below (or elsewhere), we may end up trying to emit
19491 a die whose origin die hasn't been emitted, and crashing. */
19492 dwarf2out_abstract_function (decl);
19493
19494 if (! BLOCK_ABSTRACT (stmt))
19495 {
19496 dw_die_ref subr_die
19497 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19498
19499 if (call_arg_locations)
19500 BLOCK_DIE (stmt) = subr_die;
19501 add_abstract_origin_attribute (subr_die, decl);
19502 if (TREE_ASM_WRITTEN (stmt))
19503 add_high_low_attributes (stmt, subr_die);
19504 add_call_src_coords_attributes (stmt, subr_die);
19505
19506 decls_for_scope (stmt, subr_die);
19507 }
19508 }
19509
19510 /* Generate a DIE for a field in a record, or structure. */
19511
19512 static void
19513 gen_field_die (tree decl, dw_die_ref context_die)
19514 {
19515 dw_die_ref decl_die;
19516
19517 if (TREE_TYPE (decl) == error_mark_node)
19518 return;
19519
19520 decl_die = new_die (DW_TAG_member, context_die, decl);
19521 add_name_and_src_coords_attributes (decl_die, decl);
19522 add_type_attribute (decl_die, member_declared_type (decl),
19523 decl_quals (decl), context_die);
19524
19525 if (DECL_BIT_FIELD_TYPE (decl))
19526 {
19527 add_byte_size_attribute (decl_die, decl);
19528 add_bit_size_attribute (decl_die, decl);
19529 add_bit_offset_attribute (decl_die, decl);
19530 }
19531
19532 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19533 add_data_member_location_attribute (decl_die, decl);
19534
19535 if (DECL_ARTIFICIAL (decl))
19536 add_AT_flag (decl_die, DW_AT_artificial, 1);
19537
19538 add_accessibility_attribute (decl_die, decl);
19539
19540 /* Equate decl number to die, so that we can look up this decl later on. */
19541 equate_decl_number_to_die (decl, decl_die);
19542 }
19543
19544 #if 0
19545 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19546 Use modified_type_die instead.
19547 We keep this code here just in case these types of DIEs may be needed to
19548 represent certain things in other languages (e.g. Pascal) someday. */
19549
19550 static void
19551 gen_pointer_type_die (tree type, dw_die_ref context_die)
19552 {
19553 dw_die_ref ptr_die
19554 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19555
19556 equate_type_number_to_die (type, ptr_die);
19557 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19558 context_die);
19559 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19560 }
19561
19562 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19563 Use modified_type_die instead.
19564 We keep this code here just in case these types of DIEs may be needed to
19565 represent certain things in other languages (e.g. Pascal) someday. */
19566
19567 static void
19568 gen_reference_type_die (tree type, dw_die_ref context_die)
19569 {
19570 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19571
19572 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19573 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19574 else
19575 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19576
19577 equate_type_number_to_die (type, ref_die);
19578 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19579 context_die);
19580 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19581 }
19582 #endif
19583
19584 /* Generate a DIE for a pointer to a member type. */
19585
19586 static void
19587 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19588 {
19589 dw_die_ref ptr_die
19590 = new_die (DW_TAG_ptr_to_member_type,
19591 scope_die_for (type, context_die), type);
19592
19593 equate_type_number_to_die (type, ptr_die);
19594 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19595 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19596 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19597 context_die);
19598 }
19599
19600 typedef const char *dchar_p; /* For DEF_VEC_P. */
19601
19602 static char *producer_string;
19603
19604 /* Return a heap allocated producer string including command line options
19605 if -grecord-gcc-switches. */
19606
19607 static char *
19608 gen_producer_string (void)
19609 {
19610 size_t j;
19611 auto_vec<dchar_p> switches;
19612 const char *language_string = lang_hooks.name;
19613 char *producer, *tail;
19614 const char *p;
19615 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19616 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19617
19618 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19619 switch (save_decoded_options[j].opt_index)
19620 {
19621 case OPT_o:
19622 case OPT_d:
19623 case OPT_dumpbase:
19624 case OPT_dumpdir:
19625 case OPT_auxbase:
19626 case OPT_auxbase_strip:
19627 case OPT_quiet:
19628 case OPT_version:
19629 case OPT_v:
19630 case OPT_w:
19631 case OPT_L:
19632 case OPT_D:
19633 case OPT_I:
19634 case OPT_U:
19635 case OPT_SPECIAL_unknown:
19636 case OPT_SPECIAL_ignore:
19637 case OPT_SPECIAL_program_name:
19638 case OPT_SPECIAL_input_file:
19639 case OPT_grecord_gcc_switches:
19640 case OPT_gno_record_gcc_switches:
19641 case OPT__output_pch_:
19642 case OPT_fdiagnostics_show_location_:
19643 case OPT_fdiagnostics_show_option:
19644 case OPT_fdiagnostics_show_caret:
19645 case OPT_fdiagnostics_color_:
19646 case OPT_fverbose_asm:
19647 case OPT____:
19648 case OPT__sysroot_:
19649 case OPT_nostdinc:
19650 case OPT_nostdinc__:
19651 case OPT_fpreprocessed:
19652 case OPT_fltrans_output_list_:
19653 case OPT_fresolution_:
19654 /* Ignore these. */
19655 continue;
19656 default:
19657 if (cl_options[save_decoded_options[j].opt_index].flags
19658 & CL_NO_DWARF_RECORD)
19659 continue;
19660 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19661 == '-');
19662 switch (save_decoded_options[j].canonical_option[0][1])
19663 {
19664 case 'M':
19665 case 'i':
19666 case 'W':
19667 continue;
19668 case 'f':
19669 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19670 "dump", 4) == 0)
19671 continue;
19672 break;
19673 default:
19674 break;
19675 }
19676 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19677 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19678 break;
19679 }
19680
19681 producer = XNEWVEC (char, plen + 1 + len + 1);
19682 tail = producer;
19683 sprintf (tail, "%s %s", language_string, version_string);
19684 tail += plen;
19685
19686 FOR_EACH_VEC_ELT (switches, j, p)
19687 {
19688 len = strlen (p);
19689 *tail = ' ';
19690 memcpy (tail + 1, p, len);
19691 tail += len + 1;
19692 }
19693
19694 *tail = '\0';
19695 return producer;
19696 }
19697
19698 /* Given a C and/or C++ language/version string return the "highest".
19699 C++ is assumed to be "higher" than C in this case. Used for merging
19700 LTO translation unit languages. */
19701 static const char *
19702 highest_c_language (const char *lang1, const char *lang2)
19703 {
19704 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19705 return "GNU C++14";
19706 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19707 return "GNU C++11";
19708 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19709 return "GNU C++98";
19710
19711 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19712 return "GNU C11";
19713 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19714 return "GNU C99";
19715 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19716 return "GNU C89";
19717
19718 gcc_unreachable ();
19719 }
19720
19721
19722 /* Generate the DIE for the compilation unit. */
19723
19724 static dw_die_ref
19725 gen_compile_unit_die (const char *filename)
19726 {
19727 dw_die_ref die;
19728 const char *language_string = lang_hooks.name;
19729 int language;
19730
19731 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19732
19733 if (filename)
19734 {
19735 add_name_attribute (die, filename);
19736 /* Don't add cwd for <built-in>. */
19737 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19738 add_comp_dir_attribute (die);
19739 }
19740
19741 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19742
19743 /* If our producer is LTO try to figure out a common language to use
19744 from the global list of translation units. */
19745 if (strcmp (language_string, "GNU GIMPLE") == 0)
19746 {
19747 unsigned i;
19748 tree t;
19749 const char *common_lang = NULL;
19750
19751 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19752 {
19753 if (!TRANSLATION_UNIT_LANGUAGE (t))
19754 continue;
19755 if (!common_lang)
19756 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19757 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19758 ;
19759 else if (strncmp (common_lang, "GNU C", 5) == 0
19760 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19761 /* Mixing C and C++ is ok, use C++ in that case. */
19762 common_lang = highest_c_language (common_lang,
19763 TRANSLATION_UNIT_LANGUAGE (t));
19764 else
19765 {
19766 /* Fall back to C. */
19767 common_lang = NULL;
19768 break;
19769 }
19770 }
19771
19772 if (common_lang)
19773 language_string = common_lang;
19774 }
19775
19776 language = DW_LANG_C;
19777 if (strncmp (language_string, "GNU C", 5) == 0
19778 && ISDIGIT (language_string[5]))
19779 {
19780 language = DW_LANG_C89;
19781 if (dwarf_version >= 3 || !dwarf_strict)
19782 {
19783 if (strcmp (language_string, "GNU C89") != 0)
19784 language = DW_LANG_C99;
19785
19786 if (dwarf_version >= 5 /* || !dwarf_strict */)
19787 if (strcmp (language_string, "GNU C11") == 0)
19788 language = DW_LANG_C11;
19789 }
19790 }
19791 else if (strncmp (language_string, "GNU C++", 7) == 0)
19792 {
19793 language = DW_LANG_C_plus_plus;
19794 if (dwarf_version >= 5 /* || !dwarf_strict */)
19795 {
19796 if (strcmp (language_string, "GNU C++11") == 0)
19797 language = DW_LANG_C_plus_plus_11;
19798 else if (strcmp (language_string, "GNU C++14") == 0)
19799 language = DW_LANG_C_plus_plus_14;
19800 }
19801 }
19802 else if (strcmp (language_string, "GNU F77") == 0)
19803 language = DW_LANG_Fortran77;
19804 else if (strcmp (language_string, "GNU Pascal") == 0)
19805 language = DW_LANG_Pascal83;
19806 else if (dwarf_version >= 3 || !dwarf_strict)
19807 {
19808 if (strcmp (language_string, "GNU Ada") == 0)
19809 language = DW_LANG_Ada95;
19810 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19811 {
19812 language = DW_LANG_Fortran95;
19813 if (dwarf_version >= 5 /* || !dwarf_strict */)
19814 {
19815 if (strcmp (language_string, "GNU Fortran2003") == 0)
19816 language = DW_LANG_Fortran03;
19817 else if (strcmp (language_string, "GNU Fortran2008") == 0)
19818 language = DW_LANG_Fortran08;
19819 }
19820 }
19821 else if (strcmp (language_string, "GNU Java") == 0)
19822 language = DW_LANG_Java;
19823 else if (strcmp (language_string, "GNU Objective-C") == 0)
19824 language = DW_LANG_ObjC;
19825 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19826 language = DW_LANG_ObjC_plus_plus;
19827 else if (dwarf_version >= 5 || !dwarf_strict)
19828 {
19829 if (strcmp (language_string, "GNU Go") == 0)
19830 language = DW_LANG_Go;
19831 }
19832 }
19833 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19834 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19835 language = DW_LANG_Fortran90;
19836
19837 add_AT_unsigned (die, DW_AT_language, language);
19838
19839 switch (language)
19840 {
19841 case DW_LANG_Fortran77:
19842 case DW_LANG_Fortran90:
19843 case DW_LANG_Fortran95:
19844 case DW_LANG_Fortran03:
19845 case DW_LANG_Fortran08:
19846 /* Fortran has case insensitive identifiers and the front-end
19847 lowercases everything. */
19848 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19849 break;
19850 default:
19851 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19852 break;
19853 }
19854 return die;
19855 }
19856
19857 /* Generate the DIE for a base class. */
19858
19859 static void
19860 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19861 {
19862 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19863
19864 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19865 add_data_member_location_attribute (die, binfo);
19866
19867 if (BINFO_VIRTUAL_P (binfo))
19868 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19869
19870 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19871 children, otherwise the default is DW_ACCESS_public. In DWARF2
19872 the default has always been DW_ACCESS_private. */
19873 if (access == access_public_node)
19874 {
19875 if (dwarf_version == 2
19876 || context_die->die_tag == DW_TAG_class_type)
19877 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19878 }
19879 else if (access == access_protected_node)
19880 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19881 else if (dwarf_version > 2
19882 && context_die->die_tag != DW_TAG_class_type)
19883 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19884 }
19885
19886 /* Generate a DIE for a class member. */
19887
19888 static void
19889 gen_member_die (tree type, dw_die_ref context_die)
19890 {
19891 tree member;
19892 tree binfo = TYPE_BINFO (type);
19893 dw_die_ref child;
19894
19895 /* If this is not an incomplete type, output descriptions of each of its
19896 members. Note that as we output the DIEs necessary to represent the
19897 members of this record or union type, we will also be trying to output
19898 DIEs to represent the *types* of those members. However the `type'
19899 function (above) will specifically avoid generating type DIEs for member
19900 types *within* the list of member DIEs for this (containing) type except
19901 for those types (of members) which are explicitly marked as also being
19902 members of this (containing) type themselves. The g++ front- end can
19903 force any given type to be treated as a member of some other (containing)
19904 type by setting the TYPE_CONTEXT of the given (member) type to point to
19905 the TREE node representing the appropriate (containing) type. */
19906
19907 /* First output info about the base classes. */
19908 if (binfo)
19909 {
19910 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19911 int i;
19912 tree base;
19913
19914 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19915 gen_inheritance_die (base,
19916 (accesses ? (*accesses)[i] : access_public_node),
19917 context_die);
19918 }
19919
19920 /* Now output info about the data members and type members. */
19921 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19922 {
19923 /* If we thought we were generating minimal debug info for TYPE
19924 and then changed our minds, some of the member declarations
19925 may have already been defined. Don't define them again, but
19926 do put them in the right order. */
19927
19928 child = lookup_decl_die (member);
19929 if (child)
19930 splice_child_die (context_die, child);
19931 else
19932 gen_decl_die (member, NULL, context_die);
19933 }
19934
19935 /* We do not keep type methods in type variants. */
19936 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
19937 /* Now output info about the function members (if any). */
19938 if (TYPE_METHODS (type) != error_mark_node)
19939 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19940 {
19941 /* Don't include clones in the member list. */
19942 if (DECL_ABSTRACT_ORIGIN (member))
19943 continue;
19944 /* Nor constructors for anonymous classes. */
19945 if (DECL_ARTIFICIAL (member)
19946 && dwarf2_name (member, 0) == NULL)
19947 continue;
19948
19949 child = lookup_decl_die (member);
19950 if (child)
19951 splice_child_die (context_die, child);
19952 else
19953 gen_decl_die (member, NULL, context_die);
19954 }
19955 }
19956
19957 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19958 is set, we pretend that the type was never defined, so we only get the
19959 member DIEs needed by later specification DIEs. */
19960
19961 static void
19962 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19963 enum debug_info_usage usage)
19964 {
19965 dw_die_ref type_die = lookup_type_die (type);
19966 dw_die_ref scope_die = 0;
19967 int nested = 0;
19968 int complete = (TYPE_SIZE (type)
19969 && (! TYPE_STUB_DECL (type)
19970 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19971 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19972 complete = complete && should_emit_struct_debug (type, usage);
19973
19974 if (type_die && ! complete)
19975 return;
19976
19977 if (TYPE_CONTEXT (type) != NULL_TREE
19978 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19979 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19980 nested = 1;
19981
19982 scope_die = scope_die_for (type, context_die);
19983
19984 /* Generate child dies for template paramaters. */
19985 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19986 schedule_generic_params_dies_gen (type);
19987
19988 if (! type_die || (nested && is_cu_die (scope_die)))
19989 /* First occurrence of type or toplevel definition of nested class. */
19990 {
19991 dw_die_ref old_die = type_die;
19992
19993 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19994 ? record_type_tag (type) : DW_TAG_union_type,
19995 scope_die, type);
19996 equate_type_number_to_die (type, type_die);
19997 if (old_die)
19998 add_AT_specification (type_die, old_die);
19999 else
20000 add_name_attribute (type_die, type_tag (type));
20001 }
20002 else
20003 remove_AT (type_die, DW_AT_declaration);
20004
20005 /* If this type has been completed, then give it a byte_size attribute and
20006 then give a list of members. */
20007 if (complete && !ns_decl)
20008 {
20009 /* Prevent infinite recursion in cases where the type of some member of
20010 this type is expressed in terms of this type itself. */
20011 TREE_ASM_WRITTEN (type) = 1;
20012 add_byte_size_attribute (type_die, type);
20013 if (TYPE_STUB_DECL (type) != NULL_TREE)
20014 {
20015 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20016 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20017 }
20018
20019 /* If the first reference to this type was as the return type of an
20020 inline function, then it may not have a parent. Fix this now. */
20021 if (type_die->die_parent == NULL)
20022 add_child_die (scope_die, type_die);
20023
20024 push_decl_scope (type);
20025 gen_member_die (type, type_die);
20026 pop_decl_scope ();
20027
20028 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20029 if (TYPE_ARTIFICIAL (type))
20030 add_AT_flag (type_die, DW_AT_artificial, 1);
20031
20032 /* GNU extension: Record what type our vtable lives in. */
20033 if (TYPE_VFIELD (type))
20034 {
20035 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20036
20037 gen_type_die (vtype, context_die);
20038 add_AT_die_ref (type_die, DW_AT_containing_type,
20039 lookup_type_die (vtype));
20040 }
20041 }
20042 else
20043 {
20044 add_AT_flag (type_die, DW_AT_declaration, 1);
20045
20046 /* We don't need to do this for function-local types. */
20047 if (TYPE_STUB_DECL (type)
20048 && ! decl_function_context (TYPE_STUB_DECL (type)))
20049 vec_safe_push (incomplete_types, type);
20050 }
20051
20052 if (get_AT (type_die, DW_AT_name))
20053 add_pubtype (type, type_die);
20054 }
20055
20056 /* Generate a DIE for a subroutine _type_. */
20057
20058 static void
20059 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20060 {
20061 tree return_type = TREE_TYPE (type);
20062 dw_die_ref subr_die
20063 = new_die (DW_TAG_subroutine_type,
20064 scope_die_for (type, context_die), type);
20065
20066 equate_type_number_to_die (type, subr_die);
20067 add_prototyped_attribute (subr_die, type);
20068 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20069 gen_formal_types_die (type, subr_die);
20070
20071 if (get_AT (subr_die, DW_AT_name))
20072 add_pubtype (type, subr_die);
20073 }
20074
20075 /* Generate a DIE for a type definition. */
20076
20077 static void
20078 gen_typedef_die (tree decl, dw_die_ref context_die)
20079 {
20080 dw_die_ref type_die;
20081 tree origin;
20082
20083 if (TREE_ASM_WRITTEN (decl))
20084 return;
20085
20086 TREE_ASM_WRITTEN (decl) = 1;
20087 type_die = new_die (DW_TAG_typedef, context_die, decl);
20088 origin = decl_ultimate_origin (decl);
20089 if (origin != NULL)
20090 add_abstract_origin_attribute (type_die, origin);
20091 else
20092 {
20093 tree type;
20094
20095 add_name_and_src_coords_attributes (type_die, decl);
20096 if (DECL_ORIGINAL_TYPE (decl))
20097 {
20098 type = DECL_ORIGINAL_TYPE (decl);
20099
20100 gcc_assert (type != TREE_TYPE (decl));
20101 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20102 }
20103 else
20104 {
20105 type = TREE_TYPE (decl);
20106
20107 if (is_naming_typedef_decl (TYPE_NAME (type)))
20108 {
20109 /* Here, we are in the case of decl being a typedef naming
20110 an anonymous type, e.g:
20111 typedef struct {...} foo;
20112 In that case TREE_TYPE (decl) is not a typedef variant
20113 type and TYPE_NAME of the anonymous type is set to the
20114 TYPE_DECL of the typedef. This construct is emitted by
20115 the C++ FE.
20116
20117 TYPE is the anonymous struct named by the typedef
20118 DECL. As we need the DW_AT_type attribute of the
20119 DW_TAG_typedef to point to the DIE of TYPE, let's
20120 generate that DIE right away. add_type_attribute
20121 called below will then pick (via lookup_type_die) that
20122 anonymous struct DIE. */
20123 if (!TREE_ASM_WRITTEN (type))
20124 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20125
20126 /* This is a GNU Extension. We are adding a
20127 DW_AT_linkage_name attribute to the DIE of the
20128 anonymous struct TYPE. The value of that attribute
20129 is the name of the typedef decl naming the anonymous
20130 struct. This greatly eases the work of consumers of
20131 this debug info. */
20132 add_linkage_attr (lookup_type_die (type), decl);
20133 }
20134 }
20135
20136 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20137
20138 if (is_naming_typedef_decl (decl))
20139 /* We want that all subsequent calls to lookup_type_die with
20140 TYPE in argument yield the DW_TAG_typedef we have just
20141 created. */
20142 equate_type_number_to_die (type, type_die);
20143
20144 add_accessibility_attribute (type_die, decl);
20145 }
20146
20147 if (DECL_ABSTRACT_P (decl))
20148 equate_decl_number_to_die (decl, type_die);
20149
20150 if (get_AT (type_die, DW_AT_name))
20151 add_pubtype (decl, type_die);
20152 }
20153
20154 /* Generate a DIE for a struct, class, enum or union type. */
20155
20156 static void
20157 gen_tagged_type_die (tree type,
20158 dw_die_ref context_die,
20159 enum debug_info_usage usage)
20160 {
20161 int need_pop;
20162
20163 if (type == NULL_TREE
20164 || !is_tagged_type (type))
20165 return;
20166
20167 /* If this is a nested type whose containing class hasn't been written
20168 out yet, writing it out will cover this one, too. This does not apply
20169 to instantiations of member class templates; they need to be added to
20170 the containing class as they are generated. FIXME: This hurts the
20171 idea of combining type decls from multiple TUs, since we can't predict
20172 what set of template instantiations we'll get. */
20173 if (TYPE_CONTEXT (type)
20174 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20175 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20176 {
20177 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20178
20179 if (TREE_ASM_WRITTEN (type))
20180 return;
20181
20182 /* If that failed, attach ourselves to the stub. */
20183 push_decl_scope (TYPE_CONTEXT (type));
20184 context_die = lookup_type_die (TYPE_CONTEXT (type));
20185 need_pop = 1;
20186 }
20187 else if (TYPE_CONTEXT (type) != NULL_TREE
20188 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20189 {
20190 /* If this type is local to a function that hasn't been written
20191 out yet, use a NULL context for now; it will be fixed up in
20192 decls_for_scope. */
20193 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20194 /* A declaration DIE doesn't count; nested types need to go in the
20195 specification. */
20196 if (context_die && is_declaration_die (context_die))
20197 context_die = NULL;
20198 need_pop = 0;
20199 }
20200 else
20201 {
20202 context_die = declare_in_namespace (type, context_die);
20203 need_pop = 0;
20204 }
20205
20206 if (TREE_CODE (type) == ENUMERAL_TYPE)
20207 {
20208 /* This might have been written out by the call to
20209 declare_in_namespace. */
20210 if (!TREE_ASM_WRITTEN (type))
20211 gen_enumeration_type_die (type, context_die);
20212 }
20213 else
20214 gen_struct_or_union_type_die (type, context_die, usage);
20215
20216 if (need_pop)
20217 pop_decl_scope ();
20218
20219 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20220 it up if it is ever completed. gen_*_type_die will set it for us
20221 when appropriate. */
20222 }
20223
20224 /* Generate a type description DIE. */
20225
20226 static void
20227 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20228 enum debug_info_usage usage)
20229 {
20230 struct array_descr_info info;
20231
20232 if (type == NULL_TREE || type == error_mark_node)
20233 return;
20234
20235 #ifdef ENABLE_CHECKING
20236 if (type)
20237 verify_type (type);
20238 #endif
20239
20240 if (TYPE_NAME (type) != NULL_TREE
20241 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20242 && is_redundant_typedef (TYPE_NAME (type))
20243 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20244 /* The DECL of this type is a typedef we don't want to emit debug
20245 info for but we want debug info for its underlying typedef.
20246 This can happen for e.g, the injected-class-name of a C++
20247 type. */
20248 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20249
20250 /* If TYPE is a typedef type variant, let's generate debug info
20251 for the parent typedef which TYPE is a type of. */
20252 if (typedef_variant_p (type))
20253 {
20254 if (TREE_ASM_WRITTEN (type))
20255 return;
20256
20257 /* Prevent broken recursion; we can't hand off to the same type. */
20258 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20259
20260 /* Give typedefs the right scope. */
20261 context_die = scope_die_for (type, context_die);
20262
20263 TREE_ASM_WRITTEN (type) = 1;
20264
20265 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20266 return;
20267 }
20268
20269 /* If type is an anonymous tagged type named by a typedef, let's
20270 generate debug info for the typedef. */
20271 if (is_naming_typedef_decl (TYPE_NAME (type)))
20272 {
20273 /* Use the DIE of the containing namespace as the parent DIE of
20274 the type description DIE we want to generate. */
20275 if (DECL_CONTEXT (TYPE_NAME (type))
20276 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20277 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20278
20279 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20280 return;
20281 }
20282
20283 /* We are going to output a DIE to represent the unqualified version
20284 of this type (i.e. without any const or volatile qualifiers) so
20285 get the main variant (i.e. the unqualified version) of this type
20286 now. (Vectors are special because the debugging info is in the
20287 cloned type itself). */
20288 if (TREE_CODE (type) != VECTOR_TYPE)
20289 type = type_main_variant (type);
20290
20291 /* If this is an array type with hidden descriptor, handle it first. */
20292 if (!TREE_ASM_WRITTEN (type)
20293 && lang_hooks.types.get_array_descr_info)
20294 {
20295 memset (&info, 0, sizeof (info));
20296 if (lang_hooks.types.get_array_descr_info (type, &info))
20297 {
20298 gen_descr_array_type_die (type, &info, context_die);
20299 TREE_ASM_WRITTEN (type) = 1;
20300 return;
20301 }
20302 }
20303
20304 if (TREE_ASM_WRITTEN (type))
20305 return;
20306
20307 switch (TREE_CODE (type))
20308 {
20309 case ERROR_MARK:
20310 break;
20311
20312 case POINTER_TYPE:
20313 case REFERENCE_TYPE:
20314 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20315 ensures that the gen_type_die recursion will terminate even if the
20316 type is recursive. Recursive types are possible in Ada. */
20317 /* ??? We could perhaps do this for all types before the switch
20318 statement. */
20319 TREE_ASM_WRITTEN (type) = 1;
20320
20321 /* For these types, all that is required is that we output a DIE (or a
20322 set of DIEs) to represent the "basis" type. */
20323 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20324 DINFO_USAGE_IND_USE);
20325 break;
20326
20327 case OFFSET_TYPE:
20328 /* This code is used for C++ pointer-to-data-member types.
20329 Output a description of the relevant class type. */
20330 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20331 DINFO_USAGE_IND_USE);
20332
20333 /* Output a description of the type of the object pointed to. */
20334 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20335 DINFO_USAGE_IND_USE);
20336
20337 /* Now output a DIE to represent this pointer-to-data-member type
20338 itself. */
20339 gen_ptr_to_mbr_type_die (type, context_die);
20340 break;
20341
20342 case FUNCTION_TYPE:
20343 /* Force out return type (in case it wasn't forced out already). */
20344 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20345 DINFO_USAGE_DIR_USE);
20346 gen_subroutine_type_die (type, context_die);
20347 break;
20348
20349 case METHOD_TYPE:
20350 /* Force out return type (in case it wasn't forced out already). */
20351 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20352 DINFO_USAGE_DIR_USE);
20353 gen_subroutine_type_die (type, context_die);
20354 break;
20355
20356 case ARRAY_TYPE:
20357 gen_array_type_die (type, context_die);
20358 break;
20359
20360 case VECTOR_TYPE:
20361 gen_array_type_die (type, context_die);
20362 break;
20363
20364 case ENUMERAL_TYPE:
20365 case RECORD_TYPE:
20366 case UNION_TYPE:
20367 case QUAL_UNION_TYPE:
20368 gen_tagged_type_die (type, context_die, usage);
20369 return;
20370
20371 case VOID_TYPE:
20372 case INTEGER_TYPE:
20373 case REAL_TYPE:
20374 case FIXED_POINT_TYPE:
20375 case COMPLEX_TYPE:
20376 case BOOLEAN_TYPE:
20377 case POINTER_BOUNDS_TYPE:
20378 /* No DIEs needed for fundamental types. */
20379 break;
20380
20381 case NULLPTR_TYPE:
20382 case LANG_TYPE:
20383 /* Just use DW_TAG_unspecified_type. */
20384 {
20385 dw_die_ref type_die = lookup_type_die (type);
20386 if (type_die == NULL)
20387 {
20388 tree name = TYPE_IDENTIFIER (type);
20389 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20390 type);
20391 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20392 equate_type_number_to_die (type, type_die);
20393 }
20394 }
20395 break;
20396
20397 default:
20398 if (is_cxx_auto (type))
20399 {
20400 tree name = TYPE_IDENTIFIER (type);
20401 dw_die_ref *die = (name == get_identifier ("auto")
20402 ? &auto_die : &decltype_auto_die);
20403 if (!*die)
20404 {
20405 *die = new_die (DW_TAG_unspecified_type,
20406 comp_unit_die (), NULL_TREE);
20407 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20408 }
20409 equate_type_number_to_die (type, *die);
20410 break;
20411 }
20412 gcc_unreachable ();
20413 }
20414
20415 TREE_ASM_WRITTEN (type) = 1;
20416 }
20417
20418 static void
20419 gen_type_die (tree type, dw_die_ref context_die)
20420 {
20421 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20422 }
20423
20424 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20425 things which are local to the given block. */
20426
20427 static void
20428 gen_block_die (tree stmt, dw_die_ref context_die)
20429 {
20430 int must_output_die = 0;
20431 bool inlined_func;
20432
20433 /* Ignore blocks that are NULL. */
20434 if (stmt == NULL_TREE)
20435 return;
20436
20437 inlined_func = inlined_function_outer_scope_p (stmt);
20438
20439 /* If the block is one fragment of a non-contiguous block, do not
20440 process the variables, since they will have been done by the
20441 origin block. Do process subblocks. */
20442 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20443 {
20444 tree sub;
20445
20446 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20447 gen_block_die (sub, context_die);
20448
20449 return;
20450 }
20451
20452 /* Determine if we need to output any Dwarf DIEs at all to represent this
20453 block. */
20454 if (inlined_func)
20455 /* The outer scopes for inlinings *must* always be represented. We
20456 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20457 must_output_die = 1;
20458 else
20459 {
20460 /* Determine if this block directly contains any "significant"
20461 local declarations which we will need to output DIEs for. */
20462 if (debug_info_level > DINFO_LEVEL_TERSE)
20463 /* We are not in terse mode so *any* local declaration counts
20464 as being a "significant" one. */
20465 must_output_die = ((BLOCK_VARS (stmt) != NULL
20466 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20467 && (TREE_USED (stmt)
20468 || TREE_ASM_WRITTEN (stmt)
20469 || BLOCK_ABSTRACT (stmt)));
20470 else if ((TREE_USED (stmt)
20471 || TREE_ASM_WRITTEN (stmt)
20472 || BLOCK_ABSTRACT (stmt))
20473 && !dwarf2out_ignore_block (stmt))
20474 must_output_die = 1;
20475 }
20476
20477 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20478 DIE for any block which contains no significant local declarations at
20479 all. Rather, in such cases we just call `decls_for_scope' so that any
20480 needed Dwarf info for any sub-blocks will get properly generated. Note
20481 that in terse mode, our definition of what constitutes a "significant"
20482 local declaration gets restricted to include only inlined function
20483 instances and local (nested) function definitions. */
20484 if (must_output_die)
20485 {
20486 if (inlined_func)
20487 {
20488 /* If STMT block is abstract, that means we have been called
20489 indirectly from dwarf2out_abstract_function.
20490 That function rightfully marks the descendent blocks (of
20491 the abstract function it is dealing with) as being abstract,
20492 precisely to prevent us from emitting any
20493 DW_TAG_inlined_subroutine DIE as a descendent
20494 of an abstract function instance. So in that case, we should
20495 not call gen_inlined_subroutine_die.
20496
20497 Later though, when cgraph asks dwarf2out to emit info
20498 for the concrete instance of the function decl into which
20499 the concrete instance of STMT got inlined, the later will lead
20500 to the generation of a DW_TAG_inlined_subroutine DIE. */
20501 if (! BLOCK_ABSTRACT (stmt))
20502 gen_inlined_subroutine_die (stmt, context_die);
20503 }
20504 else
20505 gen_lexical_block_die (stmt, context_die);
20506 }
20507 else
20508 decls_for_scope (stmt, context_die);
20509 }
20510
20511 /* Process variable DECL (or variable with origin ORIGIN) within
20512 block STMT and add it to CONTEXT_DIE. */
20513 static void
20514 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20515 {
20516 dw_die_ref die;
20517 tree decl_or_origin = decl ? decl : origin;
20518
20519 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20520 die = lookup_decl_die (decl_or_origin);
20521 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20522 && TYPE_DECL_IS_STUB (decl_or_origin))
20523 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20524 else
20525 die = NULL;
20526
20527 if (die != NULL && die->die_parent == NULL)
20528 add_child_die (context_die, die);
20529 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20530 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20531 stmt, context_die);
20532 else
20533 gen_decl_die (decl, origin, context_die);
20534 }
20535
20536 /* Generate all of the decls declared within a given scope and (recursively)
20537 all of its sub-blocks. */
20538
20539 static void
20540 decls_for_scope (tree stmt, dw_die_ref context_die)
20541 {
20542 tree decl;
20543 unsigned int i;
20544 tree subblocks;
20545
20546 /* Ignore NULL blocks. */
20547 if (stmt == NULL_TREE)
20548 return;
20549
20550 /* Output the DIEs to represent all of the data objects and typedefs
20551 declared directly within this block but not within any nested
20552 sub-blocks. Also, nested function and tag DIEs have been
20553 generated with a parent of NULL; fix that up now. We don't
20554 have to do this if we're at -g1. */
20555 if (debug_info_level > DINFO_LEVEL_TERSE)
20556 {
20557 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20558 process_scope_var (stmt, decl, NULL_TREE, context_die);
20559 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20560 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20561 context_die);
20562 }
20563
20564 /* Even if we're at -g1, we need to process the subblocks in order to get
20565 inlined call information. */
20566
20567 /* Output the DIEs to represent all sub-blocks (and the items declared
20568 therein) of this block. */
20569 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20570 subblocks != NULL;
20571 subblocks = BLOCK_CHAIN (subblocks))
20572 gen_block_die (subblocks, context_die);
20573 }
20574
20575 /* Is this a typedef we can avoid emitting? */
20576
20577 static inline int
20578 is_redundant_typedef (const_tree decl)
20579 {
20580 if (TYPE_DECL_IS_STUB (decl))
20581 return 1;
20582
20583 if (DECL_ARTIFICIAL (decl)
20584 && DECL_CONTEXT (decl)
20585 && is_tagged_type (DECL_CONTEXT (decl))
20586 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20587 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20588 /* Also ignore the artificial member typedef for the class name. */
20589 return 1;
20590
20591 return 0;
20592 }
20593
20594 /* Return TRUE if TYPE is a typedef that names a type for linkage
20595 purposes. This kind of typedefs is produced by the C++ FE for
20596 constructs like:
20597
20598 typedef struct {...} foo;
20599
20600 In that case, there is no typedef variant type produced for foo.
20601 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20602 struct type. */
20603
20604 static bool
20605 is_naming_typedef_decl (const_tree decl)
20606 {
20607 if (decl == NULL_TREE
20608 || TREE_CODE (decl) != TYPE_DECL
20609 || !is_tagged_type (TREE_TYPE (decl))
20610 || DECL_IS_BUILTIN (decl)
20611 || is_redundant_typedef (decl)
20612 /* It looks like Ada produces TYPE_DECLs that are very similar
20613 to C++ naming typedefs but that have different
20614 semantics. Let's be specific to c++ for now. */
20615 || !is_cxx ())
20616 return FALSE;
20617
20618 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20619 && TYPE_NAME (TREE_TYPE (decl)) == decl
20620 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20621 != TYPE_NAME (TREE_TYPE (decl))));
20622 }
20623
20624 /* Looks up the DIE for a context. */
20625
20626 static inline dw_die_ref
20627 lookup_context_die (tree context)
20628 {
20629 if (context)
20630 {
20631 /* Find die that represents this context. */
20632 if (TYPE_P (context))
20633 {
20634 context = TYPE_MAIN_VARIANT (context);
20635 dw_die_ref ctx = lookup_type_die (context);
20636 if (!ctx)
20637 return NULL;
20638 return strip_naming_typedef (context, ctx);
20639 }
20640 else
20641 return lookup_decl_die (context);
20642 }
20643 return comp_unit_die ();
20644 }
20645
20646 /* Returns the DIE for a context. */
20647
20648 static inline dw_die_ref
20649 get_context_die (tree context)
20650 {
20651 if (context)
20652 {
20653 /* Find die that represents this context. */
20654 if (TYPE_P (context))
20655 {
20656 context = TYPE_MAIN_VARIANT (context);
20657 return strip_naming_typedef (context, force_type_die (context));
20658 }
20659 else
20660 return force_decl_die (context);
20661 }
20662 return comp_unit_die ();
20663 }
20664
20665 /* Returns the DIE for decl. A DIE will always be returned. */
20666
20667 static dw_die_ref
20668 force_decl_die (tree decl)
20669 {
20670 dw_die_ref decl_die;
20671 unsigned saved_external_flag;
20672 tree save_fn = NULL_TREE;
20673 decl_die = lookup_decl_die (decl);
20674 if (!decl_die)
20675 {
20676 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20677
20678 decl_die = lookup_decl_die (decl);
20679 if (decl_die)
20680 return decl_die;
20681
20682 switch (TREE_CODE (decl))
20683 {
20684 case FUNCTION_DECL:
20685 /* Clear current_function_decl, so that gen_subprogram_die thinks
20686 that this is a declaration. At this point, we just want to force
20687 declaration die. */
20688 save_fn = current_function_decl;
20689 current_function_decl = NULL_TREE;
20690 gen_subprogram_die (decl, context_die);
20691 current_function_decl = save_fn;
20692 break;
20693
20694 case VAR_DECL:
20695 /* Set external flag to force declaration die. Restore it after
20696 gen_decl_die() call. */
20697 saved_external_flag = DECL_EXTERNAL (decl);
20698 DECL_EXTERNAL (decl) = 1;
20699 gen_decl_die (decl, NULL, context_die);
20700 DECL_EXTERNAL (decl) = saved_external_flag;
20701 break;
20702
20703 case NAMESPACE_DECL:
20704 if (dwarf_version >= 3 || !dwarf_strict)
20705 dwarf2out_decl (decl);
20706 else
20707 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20708 decl_die = comp_unit_die ();
20709 break;
20710
20711 case TRANSLATION_UNIT_DECL:
20712 decl_die = comp_unit_die ();
20713 break;
20714
20715 default:
20716 gcc_unreachable ();
20717 }
20718
20719 /* We should be able to find the DIE now. */
20720 if (!decl_die)
20721 decl_die = lookup_decl_die (decl);
20722 gcc_assert (decl_die);
20723 }
20724
20725 return decl_die;
20726 }
20727
20728 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20729 always returned. */
20730
20731 static dw_die_ref
20732 force_type_die (tree type)
20733 {
20734 dw_die_ref type_die;
20735
20736 type_die = lookup_type_die (type);
20737 if (!type_die)
20738 {
20739 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20740
20741 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20742 context_die);
20743 gcc_assert (type_die);
20744 }
20745 return type_die;
20746 }
20747
20748 /* Force out any required namespaces to be able to output DECL,
20749 and return the new context_die for it, if it's changed. */
20750
20751 static dw_die_ref
20752 setup_namespace_context (tree thing, dw_die_ref context_die)
20753 {
20754 tree context = (DECL_P (thing)
20755 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20756 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20757 /* Force out the namespace. */
20758 context_die = force_decl_die (context);
20759
20760 return context_die;
20761 }
20762
20763 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20764 type) within its namespace, if appropriate.
20765
20766 For compatibility with older debuggers, namespace DIEs only contain
20767 declarations; all definitions are emitted at CU scope. */
20768
20769 static dw_die_ref
20770 declare_in_namespace (tree thing, dw_die_ref context_die)
20771 {
20772 dw_die_ref ns_context;
20773
20774 if (debug_info_level <= DINFO_LEVEL_TERSE)
20775 return context_die;
20776
20777 /* External declarations in the local scope only need to be emitted
20778 once, not once in the namespace and once in the scope.
20779
20780 This avoids declaring the `extern' below in the
20781 namespace DIE as well as in the innermost scope:
20782
20783 namespace S
20784 {
20785 int i=5;
20786 int foo()
20787 {
20788 int i=8;
20789 extern int i;
20790 return i;
20791 }
20792 }
20793 */
20794 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20795 return context_die;
20796
20797 /* If this decl is from an inlined function, then don't try to emit it in its
20798 namespace, as we will get confused. It would have already been emitted
20799 when the abstract instance of the inline function was emitted anyways. */
20800 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20801 return context_die;
20802
20803 ns_context = setup_namespace_context (thing, context_die);
20804
20805 if (ns_context != context_die)
20806 {
20807 if (is_fortran ())
20808 return ns_context;
20809 if (DECL_P (thing))
20810 gen_decl_die (thing, NULL, ns_context);
20811 else
20812 gen_type_die (thing, ns_context);
20813 }
20814 return context_die;
20815 }
20816
20817 /* Generate a DIE for a namespace or namespace alias. */
20818
20819 static void
20820 gen_namespace_die (tree decl, dw_die_ref context_die)
20821 {
20822 dw_die_ref namespace_die;
20823
20824 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20825 they are an alias of. */
20826 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20827 {
20828 /* Output a real namespace or module. */
20829 context_die = setup_namespace_context (decl, comp_unit_die ());
20830 namespace_die = new_die (is_fortran ()
20831 ? DW_TAG_module : DW_TAG_namespace,
20832 context_die, decl);
20833 /* For Fortran modules defined in different CU don't add src coords. */
20834 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20835 {
20836 const char *name = dwarf2_name (decl, 0);
20837 if (name)
20838 add_name_attribute (namespace_die, name);
20839 }
20840 else
20841 add_name_and_src_coords_attributes (namespace_die, decl);
20842 if (DECL_EXTERNAL (decl))
20843 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20844 equate_decl_number_to_die (decl, namespace_die);
20845 }
20846 else
20847 {
20848 /* Output a namespace alias. */
20849
20850 /* Force out the namespace we are an alias of, if necessary. */
20851 dw_die_ref origin_die
20852 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20853
20854 if (DECL_FILE_SCOPE_P (decl)
20855 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20856 context_die = setup_namespace_context (decl, comp_unit_die ());
20857 /* Now create the namespace alias DIE. */
20858 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20859 add_name_and_src_coords_attributes (namespace_die, decl);
20860 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20861 equate_decl_number_to_die (decl, namespace_die);
20862 }
20863 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20864 if (want_pubnames ())
20865 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20866 }
20867
20868 /* Generate Dwarf debug information for a decl described by DECL.
20869 The return value is currently only meaningful for PARM_DECLs,
20870 for all other decls it returns NULL. */
20871
20872 static dw_die_ref
20873 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20874 {
20875 tree decl_or_origin = decl ? decl : origin;
20876 tree class_origin = NULL, ultimate_origin;
20877
20878 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20879 return NULL;
20880
20881 /* Ignore pointer bounds decls. */
20882 if (DECL_P (decl_or_origin)
20883 && TREE_TYPE (decl_or_origin)
20884 && POINTER_BOUNDS_P (decl_or_origin))
20885 return NULL;
20886
20887 switch (TREE_CODE (decl_or_origin))
20888 {
20889 case ERROR_MARK:
20890 break;
20891
20892 case CONST_DECL:
20893 if (!is_fortran () && !is_ada ())
20894 {
20895 /* The individual enumerators of an enum type get output when we output
20896 the Dwarf representation of the relevant enum type itself. */
20897 break;
20898 }
20899
20900 /* Emit its type. */
20901 gen_type_die (TREE_TYPE (decl), context_die);
20902
20903 /* And its containing namespace. */
20904 context_die = declare_in_namespace (decl, context_die);
20905
20906 gen_const_die (decl, context_die);
20907 break;
20908
20909 case FUNCTION_DECL:
20910 /* Don't output any DIEs to represent mere function declarations,
20911 unless they are class members or explicit block externs. */
20912 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20913 && DECL_FILE_SCOPE_P (decl_or_origin)
20914 && (current_function_decl == NULL_TREE
20915 || DECL_ARTIFICIAL (decl_or_origin)))
20916 break;
20917
20918 #if 0
20919 /* FIXME */
20920 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20921 on local redeclarations of global functions. That seems broken. */
20922 if (current_function_decl != decl)
20923 /* This is only a declaration. */;
20924 #endif
20925
20926 /* If we're emitting a clone, emit info for the abstract instance. */
20927 if (origin || DECL_ORIGIN (decl) != decl)
20928 dwarf2out_abstract_function (origin
20929 ? DECL_ORIGIN (origin)
20930 : DECL_ABSTRACT_ORIGIN (decl));
20931
20932 /* If we're emitting an out-of-line copy of an inline function,
20933 emit info for the abstract instance and set up to refer to it. */
20934 else if (cgraph_function_possibly_inlined_p (decl)
20935 && ! DECL_ABSTRACT_P (decl)
20936 && ! class_or_namespace_scope_p (context_die)
20937 /* dwarf2out_abstract_function won't emit a die if this is just
20938 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20939 that case, because that works only if we have a die. */
20940 && DECL_INITIAL (decl) != NULL_TREE)
20941 {
20942 dwarf2out_abstract_function (decl);
20943 set_decl_origin_self (decl);
20944 }
20945
20946 /* Otherwise we're emitting the primary DIE for this decl. */
20947 else if (debug_info_level > DINFO_LEVEL_TERSE)
20948 {
20949 /* Before we describe the FUNCTION_DECL itself, make sure that we
20950 have its containing type. */
20951 if (!origin)
20952 origin = decl_class_context (decl);
20953 if (origin != NULL_TREE)
20954 gen_type_die (origin, context_die);
20955
20956 /* And its return type. */
20957 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20958
20959 /* And its virtual context. */
20960 if (DECL_VINDEX (decl) != NULL_TREE)
20961 gen_type_die (DECL_CONTEXT (decl), context_die);
20962
20963 /* Make sure we have a member DIE for decl. */
20964 if (origin != NULL_TREE)
20965 gen_type_die_for_member (origin, decl, context_die);
20966
20967 /* And its containing namespace. */
20968 context_die = declare_in_namespace (decl, context_die);
20969 }
20970
20971 /* Now output a DIE to represent the function itself. */
20972 if (decl)
20973 gen_subprogram_die (decl, context_die);
20974 break;
20975
20976 case TYPE_DECL:
20977 /* If we are in terse mode, don't generate any DIEs to represent any
20978 actual typedefs. */
20979 if (debug_info_level <= DINFO_LEVEL_TERSE)
20980 break;
20981
20982 /* In the special case of a TYPE_DECL node representing the declaration
20983 of some type tag, if the given TYPE_DECL is marked as having been
20984 instantiated from some other (original) TYPE_DECL node (e.g. one which
20985 was generated within the original definition of an inline function) we
20986 used to generate a special (abbreviated) DW_TAG_structure_type,
20987 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20988 should be actually referencing those DIEs, as variable DIEs with that
20989 type would be emitted already in the abstract origin, so it was always
20990 removed during unused type prunning. Don't add anything in this
20991 case. */
20992 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20993 break;
20994
20995 if (is_redundant_typedef (decl))
20996 gen_type_die (TREE_TYPE (decl), context_die);
20997 else
20998 /* Output a DIE to represent the typedef itself. */
20999 gen_typedef_die (decl, context_die);
21000 break;
21001
21002 case LABEL_DECL:
21003 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21004 gen_label_die (decl, context_die);
21005 break;
21006
21007 case VAR_DECL:
21008 case RESULT_DECL:
21009 /* If we are in terse mode, don't generate any DIEs to represent any
21010 variable declarations or definitions. */
21011 if (debug_info_level <= DINFO_LEVEL_TERSE)
21012 break;
21013
21014 /* Output any DIEs that are needed to specify the type of this data
21015 object. */
21016 if (decl_by_reference_p (decl_or_origin))
21017 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21018 else
21019 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21020
21021 /* And its containing type. */
21022 class_origin = decl_class_context (decl_or_origin);
21023 if (class_origin != NULL_TREE)
21024 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21025
21026 /* And its containing namespace. */
21027 context_die = declare_in_namespace (decl_or_origin, context_die);
21028
21029 /* Now output the DIE to represent the data object itself. This gets
21030 complicated because of the possibility that the VAR_DECL really
21031 represents an inlined instance of a formal parameter for an inline
21032 function. */
21033 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21034 if (ultimate_origin != NULL_TREE
21035 && TREE_CODE (ultimate_origin) == PARM_DECL)
21036 gen_formal_parameter_die (decl, origin,
21037 true /* Emit name attribute. */,
21038 context_die);
21039 else
21040 gen_variable_die (decl, origin, context_die);
21041 break;
21042
21043 case FIELD_DECL:
21044 /* Ignore the nameless fields that are used to skip bits but handle C++
21045 anonymous unions and structs. */
21046 if (DECL_NAME (decl) != NULL_TREE
21047 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21048 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21049 {
21050 gen_type_die (member_declared_type (decl), context_die);
21051 gen_field_die (decl, context_die);
21052 }
21053 break;
21054
21055 case PARM_DECL:
21056 if (DECL_BY_REFERENCE (decl_or_origin))
21057 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21058 else
21059 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21060 return gen_formal_parameter_die (decl, origin,
21061 true /* Emit name attribute. */,
21062 context_die);
21063
21064 case NAMESPACE_DECL:
21065 case IMPORTED_DECL:
21066 if (dwarf_version >= 3 || !dwarf_strict)
21067 gen_namespace_die (decl, context_die);
21068 break;
21069
21070 case NAMELIST_DECL:
21071 gen_namelist_decl (DECL_NAME (decl), context_die,
21072 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21073 break;
21074
21075 default:
21076 /* Probably some frontend-internal decl. Assume we don't care. */
21077 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21078 break;
21079 }
21080
21081 return NULL;
21082 }
21083 \f
21084 /* Output debug information for global decl DECL. Called from toplev.c after
21085 compilation proper has finished. */
21086
21087 static void
21088 dwarf2out_global_decl (tree decl)
21089 {
21090 /* Output DWARF2 information for file-scope tentative data object
21091 declarations, file-scope (extern) function declarations (which
21092 had no corresponding body) and file-scope tagged type declarations
21093 and definitions which have not yet been forced out. */
21094 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21095 && !POINTER_BOUNDS_P (decl))
21096 dwarf2out_decl (decl);
21097 }
21098
21099 /* Output debug information for type decl DECL. Called from toplev.c
21100 and from language front ends (to record built-in types). */
21101 static void
21102 dwarf2out_type_decl (tree decl, int local)
21103 {
21104 if (!local)
21105 dwarf2out_decl (decl);
21106 }
21107
21108 /* Output debug information for imported module or decl DECL.
21109 NAME is non-NULL name in the lexical block if the decl has been renamed.
21110 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21111 that DECL belongs to.
21112 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21113 static void
21114 dwarf2out_imported_module_or_decl_1 (tree decl,
21115 tree name,
21116 tree lexical_block,
21117 dw_die_ref lexical_block_die)
21118 {
21119 expanded_location xloc;
21120 dw_die_ref imported_die = NULL;
21121 dw_die_ref at_import_die;
21122
21123 if (TREE_CODE (decl) == IMPORTED_DECL)
21124 {
21125 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21126 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21127 gcc_assert (decl);
21128 }
21129 else
21130 xloc = expand_location (input_location);
21131
21132 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21133 {
21134 at_import_die = force_type_die (TREE_TYPE (decl));
21135 /* For namespace N { typedef void T; } using N::T; base_type_die
21136 returns NULL, but DW_TAG_imported_declaration requires
21137 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21138 if (!at_import_die)
21139 {
21140 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21141 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21142 at_import_die = lookup_type_die (TREE_TYPE (decl));
21143 gcc_assert (at_import_die);
21144 }
21145 }
21146 else
21147 {
21148 at_import_die = lookup_decl_die (decl);
21149 if (!at_import_die)
21150 {
21151 /* If we're trying to avoid duplicate debug info, we may not have
21152 emitted the member decl for this field. Emit it now. */
21153 if (TREE_CODE (decl) == FIELD_DECL)
21154 {
21155 tree type = DECL_CONTEXT (decl);
21156
21157 if (TYPE_CONTEXT (type)
21158 && TYPE_P (TYPE_CONTEXT (type))
21159 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21160 DINFO_USAGE_DIR_USE))
21161 return;
21162 gen_type_die_for_member (type, decl,
21163 get_context_die (TYPE_CONTEXT (type)));
21164 }
21165 if (TREE_CODE (decl) == NAMELIST_DECL)
21166 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21167 get_context_die (DECL_CONTEXT (decl)),
21168 NULL_TREE);
21169 else
21170 at_import_die = force_decl_die (decl);
21171 }
21172 }
21173
21174 if (TREE_CODE (decl) == NAMESPACE_DECL)
21175 {
21176 if (dwarf_version >= 3 || !dwarf_strict)
21177 imported_die = new_die (DW_TAG_imported_module,
21178 lexical_block_die,
21179 lexical_block);
21180 else
21181 return;
21182 }
21183 else
21184 imported_die = new_die (DW_TAG_imported_declaration,
21185 lexical_block_die,
21186 lexical_block);
21187
21188 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21189 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21190 if (name)
21191 add_AT_string (imported_die, DW_AT_name,
21192 IDENTIFIER_POINTER (name));
21193 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21194 }
21195
21196 /* Output debug information for imported module or decl DECL.
21197 NAME is non-NULL name in context if the decl has been renamed.
21198 CHILD is true if decl is one of the renamed decls as part of
21199 importing whole module. */
21200
21201 static void
21202 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21203 bool child)
21204 {
21205 /* dw_die_ref at_import_die; */
21206 dw_die_ref scope_die;
21207
21208 if (debug_info_level <= DINFO_LEVEL_TERSE)
21209 return;
21210
21211 gcc_assert (decl);
21212
21213 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21214 We need decl DIE for reference and scope die. First, get DIE for the decl
21215 itself. */
21216
21217 /* Get the scope die for decl context. Use comp_unit_die for global module
21218 or decl. If die is not found for non globals, force new die. */
21219 if (context
21220 && TYPE_P (context)
21221 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21222 return;
21223
21224 if (!(dwarf_version >= 3 || !dwarf_strict))
21225 return;
21226
21227 scope_die = get_context_die (context);
21228
21229 if (child)
21230 {
21231 gcc_assert (scope_die->die_child);
21232 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21233 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21234 scope_die = scope_die->die_child;
21235 }
21236
21237 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21238 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21239
21240 }
21241
21242 /* Output debug information for namelists. */
21243
21244 static dw_die_ref
21245 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21246 {
21247 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21248 tree value;
21249 unsigned i;
21250
21251 if (debug_info_level <= DINFO_LEVEL_TERSE)
21252 return NULL;
21253
21254 gcc_assert (scope_die != NULL);
21255 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21256 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21257
21258 /* If there are no item_decls, we have a nondefining namelist, e.g.
21259 with USE association; hence, set DW_AT_declaration. */
21260 if (item_decls == NULL_TREE)
21261 {
21262 add_AT_flag (nml_die, DW_AT_declaration, 1);
21263 return nml_die;
21264 }
21265
21266 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21267 {
21268 nml_item_ref_die = lookup_decl_die (value);
21269 if (!nml_item_ref_die)
21270 nml_item_ref_die = force_decl_die (value);
21271
21272 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21273 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21274 }
21275 return nml_die;
21276 }
21277
21278
21279 /* Write the debugging output for DECL. */
21280
21281 static void
21282 dwarf2out_decl (tree decl)
21283 {
21284 dw_die_ref context_die = comp_unit_die ();
21285
21286 switch (TREE_CODE (decl))
21287 {
21288 case ERROR_MARK:
21289 return;
21290
21291 case FUNCTION_DECL:
21292 /* What we would really like to do here is to filter out all mere
21293 file-scope declarations of file-scope functions which are never
21294 referenced later within this translation unit (and keep all of ones
21295 that *are* referenced later on) but we aren't clairvoyant, so we have
21296 no idea which functions will be referenced in the future (i.e. later
21297 on within the current translation unit). So here we just ignore all
21298 file-scope function declarations which are not also definitions. If
21299 and when the debugger needs to know something about these functions,
21300 it will have to hunt around and find the DWARF information associated
21301 with the definition of the function.
21302
21303 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21304 nodes represent definitions and which ones represent mere
21305 declarations. We have to check DECL_INITIAL instead. That's because
21306 the C front-end supports some weird semantics for "extern inline"
21307 function definitions. These can get inlined within the current
21308 translation unit (and thus, we need to generate Dwarf info for their
21309 abstract instances so that the Dwarf info for the concrete inlined
21310 instances can have something to refer to) but the compiler never
21311 generates any out-of-lines instances of such things (despite the fact
21312 that they *are* definitions).
21313
21314 The important point is that the C front-end marks these "extern
21315 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21316 them anyway. Note that the C++ front-end also plays some similar games
21317 for inline function definitions appearing within include files which
21318 also contain `#pragma interface' pragmas.
21319
21320 If we are called from dwarf2out_abstract_function output a DIE
21321 anyway. We can end up here this way with early inlining and LTO
21322 where the inlined function is output in a different LTRANS unit
21323 or not at all. */
21324 if (DECL_INITIAL (decl) == NULL_TREE
21325 && ! DECL_ABSTRACT_P (decl))
21326 return;
21327
21328 /* If we're a nested function, initially use a parent of NULL; if we're
21329 a plain function, this will be fixed up in decls_for_scope. If
21330 we're a method, it will be ignored, since we already have a DIE. */
21331 if (decl_function_context (decl)
21332 /* But if we're in terse mode, we don't care about scope. */
21333 && debug_info_level > DINFO_LEVEL_TERSE)
21334 context_die = NULL;
21335 break;
21336
21337 case VAR_DECL:
21338 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21339 declaration and if the declaration was never even referenced from
21340 within this entire compilation unit. We suppress these DIEs in
21341 order to save space in the .debug section (by eliminating entries
21342 which are probably useless). Note that we must not suppress
21343 block-local extern declarations (whether used or not) because that
21344 would screw-up the debugger's name lookup mechanism and cause it to
21345 miss things which really ought to be in scope at a given point. */
21346 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21347 return;
21348
21349 /* For local statics lookup proper context die. */
21350 if (TREE_STATIC (decl)
21351 && DECL_CONTEXT (decl)
21352 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21353 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21354
21355 /* If we are in terse mode, don't generate any DIEs to represent any
21356 variable declarations or definitions. */
21357 if (debug_info_level <= DINFO_LEVEL_TERSE)
21358 return;
21359 break;
21360
21361 case CONST_DECL:
21362 if (debug_info_level <= DINFO_LEVEL_TERSE)
21363 return;
21364 if (!is_fortran () && !is_ada ())
21365 return;
21366 if (TREE_STATIC (decl) && decl_function_context (decl))
21367 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21368 break;
21369
21370 case NAMESPACE_DECL:
21371 case IMPORTED_DECL:
21372 if (debug_info_level <= DINFO_LEVEL_TERSE)
21373 return;
21374 if (lookup_decl_die (decl) != NULL)
21375 return;
21376 break;
21377
21378 case TYPE_DECL:
21379 /* Don't emit stubs for types unless they are needed by other DIEs. */
21380 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21381 return;
21382
21383 /* Don't bother trying to generate any DIEs to represent any of the
21384 normal built-in types for the language we are compiling. */
21385 if (DECL_IS_BUILTIN (decl))
21386 return;
21387
21388 /* If we are in terse mode, don't generate any DIEs for types. */
21389 if (debug_info_level <= DINFO_LEVEL_TERSE)
21390 return;
21391
21392 /* If we're a function-scope tag, initially use a parent of NULL;
21393 this will be fixed up in decls_for_scope. */
21394 if (decl_function_context (decl))
21395 context_die = NULL;
21396
21397 break;
21398
21399 case NAMELIST_DECL:
21400 break;
21401
21402 default:
21403 return;
21404 }
21405
21406 gen_decl_die (decl, NULL, context_die);
21407 }
21408
21409 /* Write the debugging output for DECL. */
21410
21411 static void
21412 dwarf2out_function_decl (tree decl)
21413 {
21414 dwarf2out_decl (decl);
21415 call_arg_locations = NULL;
21416 call_arg_loc_last = NULL;
21417 call_site_count = -1;
21418 tail_call_site_count = -1;
21419 decl_loc_table->empty ();
21420 cached_dw_loc_list_table->empty ();
21421 }
21422
21423 /* Output a marker (i.e. a label) for the beginning of the generated code for
21424 a lexical block. */
21425
21426 static void
21427 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21428 unsigned int blocknum)
21429 {
21430 switch_to_section (current_function_section ());
21431 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21432 }
21433
21434 /* Output a marker (i.e. a label) for the end of the generated code for a
21435 lexical block. */
21436
21437 static void
21438 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21439 {
21440 switch_to_section (current_function_section ());
21441 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21442 }
21443
21444 /* Returns nonzero if it is appropriate not to emit any debugging
21445 information for BLOCK, because it doesn't contain any instructions.
21446
21447 Don't allow this for blocks with nested functions or local classes
21448 as we would end up with orphans, and in the presence of scheduling
21449 we may end up calling them anyway. */
21450
21451 static bool
21452 dwarf2out_ignore_block (const_tree block)
21453 {
21454 tree decl;
21455 unsigned int i;
21456
21457 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21458 if (TREE_CODE (decl) == FUNCTION_DECL
21459 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21460 return 0;
21461 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21462 {
21463 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21464 if (TREE_CODE (decl) == FUNCTION_DECL
21465 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21466 return 0;
21467 }
21468
21469 return 1;
21470 }
21471
21472 /* Hash table routines for file_hash. */
21473
21474 bool
21475 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21476 {
21477 return filename_cmp (p1->filename, p2) == 0;
21478 }
21479
21480 hashval_t
21481 dwarf_file_hasher::hash (dwarf_file_data *p)
21482 {
21483 return htab_hash_string (p->filename);
21484 }
21485
21486 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21487 dwarf2out.c) and return its "index". The index of each (known) filename is
21488 just a unique number which is associated with only that one filename. We
21489 need such numbers for the sake of generating labels (in the .debug_sfnames
21490 section) and references to those files numbers (in the .debug_srcinfo
21491 and.debug_macinfo sections). If the filename given as an argument is not
21492 found in our current list, add it to the list and assign it the next
21493 available unique index number. In order to speed up searches, we remember
21494 the index of the filename was looked up last. This handles the majority of
21495 all searches. */
21496
21497 static struct dwarf_file_data *
21498 lookup_filename (const char *file_name)
21499 {
21500 struct dwarf_file_data * created;
21501
21502 /* Check to see if the file name that was searched on the previous
21503 call matches this file name. If so, return the index. */
21504 if (file_table_last_lookup
21505 && (file_name == file_table_last_lookup->filename
21506 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21507 return file_table_last_lookup;
21508
21509 /* Didn't match the previous lookup, search the table. */
21510 dwarf_file_data **slot
21511 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21512 INSERT);
21513 if (*slot)
21514 return *slot;
21515
21516 created = ggc_alloc<dwarf_file_data> ();
21517 created->filename = file_name;
21518 created->emitted_number = 0;
21519 *slot = created;
21520 return created;
21521 }
21522
21523 /* If the assembler will construct the file table, then translate the compiler
21524 internal file table number into the assembler file table number, and emit
21525 a .file directive if we haven't already emitted one yet. The file table
21526 numbers are different because we prune debug info for unused variables and
21527 types, which may include filenames. */
21528
21529 static int
21530 maybe_emit_file (struct dwarf_file_data * fd)
21531 {
21532 if (! fd->emitted_number)
21533 {
21534 if (last_emitted_file)
21535 fd->emitted_number = last_emitted_file->emitted_number + 1;
21536 else
21537 fd->emitted_number = 1;
21538 last_emitted_file = fd;
21539
21540 if (DWARF2_ASM_LINE_DEBUG_INFO)
21541 {
21542 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21543 output_quoted_string (asm_out_file,
21544 remap_debug_filename (fd->filename));
21545 fputc ('\n', asm_out_file);
21546 }
21547 }
21548
21549 return fd->emitted_number;
21550 }
21551
21552 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21553 That generation should happen after function debug info has been
21554 generated. The value of the attribute is the constant value of ARG. */
21555
21556 static void
21557 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21558 {
21559 die_arg_entry entry;
21560
21561 if (!die || !arg)
21562 return;
21563
21564 if (!tmpl_value_parm_die_table)
21565 vec_alloc (tmpl_value_parm_die_table, 32);
21566
21567 entry.die = die;
21568 entry.arg = arg;
21569 vec_safe_push (tmpl_value_parm_die_table, entry);
21570 }
21571
21572 /* Return TRUE if T is an instance of generic type, FALSE
21573 otherwise. */
21574
21575 static bool
21576 generic_type_p (tree t)
21577 {
21578 if (t == NULL_TREE || !TYPE_P (t))
21579 return false;
21580 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21581 }
21582
21583 /* Schedule the generation of the generic parameter dies for the
21584 instance of generic type T. The proper generation itself is later
21585 done by gen_scheduled_generic_parms_dies. */
21586
21587 static void
21588 schedule_generic_params_dies_gen (tree t)
21589 {
21590 if (!generic_type_p (t))
21591 return;
21592
21593 if (!generic_type_instances)
21594 vec_alloc (generic_type_instances, 256);
21595
21596 vec_safe_push (generic_type_instances, t);
21597 }
21598
21599 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21600 by append_entry_to_tmpl_value_parm_die_table. This function must
21601 be called after function DIEs have been generated. */
21602
21603 static void
21604 gen_remaining_tmpl_value_param_die_attribute (void)
21605 {
21606 if (tmpl_value_parm_die_table)
21607 {
21608 unsigned i;
21609 die_arg_entry *e;
21610
21611 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21612 tree_add_const_value_attribute (e->die, e->arg);
21613 }
21614 }
21615
21616 /* Generate generic parameters DIEs for instances of generic types
21617 that have been previously scheduled by
21618 schedule_generic_params_dies_gen. This function must be called
21619 after all the types of the CU have been laid out. */
21620
21621 static void
21622 gen_scheduled_generic_parms_dies (void)
21623 {
21624 unsigned i;
21625 tree t;
21626
21627 if (!generic_type_instances)
21628 return;
21629
21630 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21631 if (COMPLETE_TYPE_P (t))
21632 gen_generic_params_dies (t);
21633 }
21634
21635
21636 /* Replace DW_AT_name for the decl with name. */
21637
21638 static void
21639 dwarf2out_set_name (tree decl, tree name)
21640 {
21641 dw_die_ref die;
21642 dw_attr_ref attr;
21643 const char *dname;
21644
21645 die = TYPE_SYMTAB_DIE (decl);
21646 if (!die)
21647 return;
21648
21649 dname = dwarf2_name (name, 0);
21650 if (!dname)
21651 return;
21652
21653 attr = get_AT (die, DW_AT_name);
21654 if (attr)
21655 {
21656 struct indirect_string_node *node;
21657
21658 node = find_AT_string (dname);
21659 /* replace the string. */
21660 attr->dw_attr_val.v.val_str = node;
21661 }
21662
21663 else
21664 add_name_attribute (die, dname);
21665 }
21666
21667 /* True if before or during processing of the first function being emitted. */
21668 static bool in_first_function_p = true;
21669 /* True if loc_note during dwarf2out_var_location call might still be
21670 before first real instruction at address equal to .Ltext0. */
21671 static bool maybe_at_text_label_p = true;
21672 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21673 static unsigned int first_loclabel_num_not_at_text_label;
21674
21675 /* Called by the final INSN scan whenever we see a var location. We
21676 use it to drop labels in the right places, and throw the location in
21677 our lookup table. */
21678
21679 static void
21680 dwarf2out_var_location (rtx_insn *loc_note)
21681 {
21682 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21683 struct var_loc_node *newloc;
21684 rtx_insn *next_real, *next_note;
21685 static const char *last_label;
21686 static const char *last_postcall_label;
21687 static bool last_in_cold_section_p;
21688 static rtx_insn *expected_next_loc_note;
21689 tree decl;
21690 bool var_loc_p;
21691
21692 if (!NOTE_P (loc_note))
21693 {
21694 if (CALL_P (loc_note))
21695 {
21696 call_site_count++;
21697 if (SIBLING_CALL_P (loc_note))
21698 tail_call_site_count++;
21699 }
21700 return;
21701 }
21702
21703 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21704 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21705 return;
21706
21707 /* Optimize processing a large consecutive sequence of location
21708 notes so we don't spend too much time in next_real_insn. If the
21709 next insn is another location note, remember the next_real_insn
21710 calculation for next time. */
21711 next_real = cached_next_real_insn;
21712 if (next_real)
21713 {
21714 if (expected_next_loc_note != loc_note)
21715 next_real = NULL;
21716 }
21717
21718 next_note = NEXT_INSN (loc_note);
21719 if (! next_note
21720 || next_note->deleted ()
21721 || ! NOTE_P (next_note)
21722 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21723 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21724 next_note = NULL;
21725
21726 if (! next_real)
21727 next_real = next_real_insn (loc_note);
21728
21729 if (next_note)
21730 {
21731 expected_next_loc_note = next_note;
21732 cached_next_real_insn = next_real;
21733 }
21734 else
21735 cached_next_real_insn = NULL;
21736
21737 /* If there are no instructions which would be affected by this note,
21738 don't do anything. */
21739 if (var_loc_p
21740 && next_real == NULL_RTX
21741 && !NOTE_DURING_CALL_P (loc_note))
21742 return;
21743
21744 if (next_real == NULL_RTX)
21745 next_real = get_last_insn ();
21746
21747 /* If there were any real insns between note we processed last time
21748 and this note (or if it is the first note), clear
21749 last_{,postcall_}label so that they are not reused this time. */
21750 if (last_var_location_insn == NULL_RTX
21751 || last_var_location_insn != next_real
21752 || last_in_cold_section_p != in_cold_section_p)
21753 {
21754 last_label = NULL;
21755 last_postcall_label = NULL;
21756 }
21757
21758 if (var_loc_p)
21759 {
21760 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21761 newloc = add_var_loc_to_decl (decl, loc_note,
21762 NOTE_DURING_CALL_P (loc_note)
21763 ? last_postcall_label : last_label);
21764 if (newloc == NULL)
21765 return;
21766 }
21767 else
21768 {
21769 decl = NULL_TREE;
21770 newloc = NULL;
21771 }
21772
21773 /* If there were no real insns between note we processed last time
21774 and this note, use the label we emitted last time. Otherwise
21775 create a new label and emit it. */
21776 if (last_label == NULL)
21777 {
21778 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21779 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21780 loclabel_num++;
21781 last_label = ggc_strdup (loclabel);
21782 /* See if loclabel might be equal to .Ltext0. If yes,
21783 bump first_loclabel_num_not_at_text_label. */
21784 if (!have_multiple_function_sections
21785 && in_first_function_p
21786 && maybe_at_text_label_p)
21787 {
21788 static rtx_insn *last_start;
21789 rtx_insn *insn;
21790 for (insn = loc_note; insn; insn = previous_insn (insn))
21791 if (insn == last_start)
21792 break;
21793 else if (!NONDEBUG_INSN_P (insn))
21794 continue;
21795 else
21796 {
21797 rtx body = PATTERN (insn);
21798 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21799 continue;
21800 /* Inline asm could occupy zero bytes. */
21801 else if (GET_CODE (body) == ASM_INPUT
21802 || asm_noperands (body) >= 0)
21803 continue;
21804 #ifdef HAVE_attr_length
21805 else if (get_attr_min_length (insn) == 0)
21806 continue;
21807 #endif
21808 else
21809 {
21810 /* Assume insn has non-zero length. */
21811 maybe_at_text_label_p = false;
21812 break;
21813 }
21814 }
21815 if (maybe_at_text_label_p)
21816 {
21817 last_start = loc_note;
21818 first_loclabel_num_not_at_text_label = loclabel_num;
21819 }
21820 }
21821 }
21822
21823 if (!var_loc_p)
21824 {
21825 struct call_arg_loc_node *ca_loc
21826 = ggc_cleared_alloc<call_arg_loc_node> ();
21827 rtx_insn *prev = prev_real_insn (loc_note);
21828 rtx x;
21829 ca_loc->call_arg_loc_note = loc_note;
21830 ca_loc->next = NULL;
21831 ca_loc->label = last_label;
21832 gcc_assert (prev
21833 && (CALL_P (prev)
21834 || (NONJUMP_INSN_P (prev)
21835 && GET_CODE (PATTERN (prev)) == SEQUENCE
21836 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21837 if (!CALL_P (prev))
21838 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21839 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21840 x = get_call_rtx_from (PATTERN (prev));
21841 if (x)
21842 {
21843 x = XEXP (XEXP (x, 0), 0);
21844 if (GET_CODE (x) == SYMBOL_REF
21845 && SYMBOL_REF_DECL (x)
21846 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21847 ca_loc->symbol_ref = x;
21848 }
21849 ca_loc->block = insn_scope (prev);
21850 if (call_arg_locations)
21851 call_arg_loc_last->next = ca_loc;
21852 else
21853 call_arg_locations = ca_loc;
21854 call_arg_loc_last = ca_loc;
21855 }
21856 else if (!NOTE_DURING_CALL_P (loc_note))
21857 newloc->label = last_label;
21858 else
21859 {
21860 if (!last_postcall_label)
21861 {
21862 sprintf (loclabel, "%s-1", last_label);
21863 last_postcall_label = ggc_strdup (loclabel);
21864 }
21865 newloc->label = last_postcall_label;
21866 }
21867
21868 last_var_location_insn = next_real;
21869 last_in_cold_section_p = in_cold_section_p;
21870 }
21871
21872 /* Note in one location list that text section has changed. */
21873
21874 int
21875 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21876 {
21877 var_loc_list *list = *slot;
21878 if (list->first)
21879 list->last_before_switch
21880 = list->last->next ? list->last->next : list->last;
21881 return 1;
21882 }
21883
21884 /* Note in all location lists that text section has changed. */
21885
21886 static void
21887 var_location_switch_text_section (void)
21888 {
21889 if (decl_loc_table == NULL)
21890 return;
21891
21892 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21893 }
21894
21895 /* Create a new line number table. */
21896
21897 static dw_line_info_table *
21898 new_line_info_table (void)
21899 {
21900 dw_line_info_table *table;
21901
21902 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21903 table->file_num = 1;
21904 table->line_num = 1;
21905 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21906
21907 return table;
21908 }
21909
21910 /* Lookup the "current" table into which we emit line info, so
21911 that we don't have to do it for every source line. */
21912
21913 static void
21914 set_cur_line_info_table (section *sec)
21915 {
21916 dw_line_info_table *table;
21917
21918 if (sec == text_section)
21919 table = text_section_line_info;
21920 else if (sec == cold_text_section)
21921 {
21922 table = cold_text_section_line_info;
21923 if (!table)
21924 {
21925 cold_text_section_line_info = table = new_line_info_table ();
21926 table->end_label = cold_end_label;
21927 }
21928 }
21929 else
21930 {
21931 const char *end_label;
21932
21933 if (flag_reorder_blocks_and_partition)
21934 {
21935 if (in_cold_section_p)
21936 end_label = crtl->subsections.cold_section_end_label;
21937 else
21938 end_label = crtl->subsections.hot_section_end_label;
21939 }
21940 else
21941 {
21942 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21943 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21944 current_function_funcdef_no);
21945 end_label = ggc_strdup (label);
21946 }
21947
21948 table = new_line_info_table ();
21949 table->end_label = end_label;
21950
21951 vec_safe_push (separate_line_info, table);
21952 }
21953
21954 if (DWARF2_ASM_LINE_DEBUG_INFO)
21955 table->is_stmt = (cur_line_info_table
21956 ? cur_line_info_table->is_stmt
21957 : DWARF_LINE_DEFAULT_IS_STMT_START);
21958 cur_line_info_table = table;
21959 }
21960
21961
21962 /* We need to reset the locations at the beginning of each
21963 function. We can't do this in the end_function hook, because the
21964 declarations that use the locations won't have been output when
21965 that hook is called. Also compute have_multiple_function_sections here. */
21966
21967 static void
21968 dwarf2out_begin_function (tree fun)
21969 {
21970 section *sec = function_section (fun);
21971
21972 if (sec != text_section)
21973 have_multiple_function_sections = true;
21974
21975 if (flag_reorder_blocks_and_partition && !cold_text_section)
21976 {
21977 gcc_assert (current_function_decl == fun);
21978 cold_text_section = unlikely_text_section ();
21979 switch_to_section (cold_text_section);
21980 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21981 switch_to_section (sec);
21982 }
21983
21984 dwarf2out_note_section_used ();
21985 call_site_count = 0;
21986 tail_call_site_count = 0;
21987
21988 set_cur_line_info_table (sec);
21989 }
21990
21991 /* Helper function of dwarf2out_end_function, called only after emitting
21992 the very first function into assembly. Check if some .debug_loc range
21993 might end with a .LVL* label that could be equal to .Ltext0.
21994 In that case we must force using absolute addresses in .debug_loc ranges,
21995 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21996 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21997 list terminator.
21998 Set have_multiple_function_sections to true in that case and
21999 terminate htab traversal. */
22000
22001 int
22002 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22003 {
22004 var_loc_list *entry = *slot;
22005 struct var_loc_node *node;
22006
22007 node = entry->first;
22008 if (node && node->next && node->next->label)
22009 {
22010 unsigned int i;
22011 const char *label = node->next->label;
22012 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22013
22014 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22015 {
22016 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22017 if (strcmp (label, loclabel) == 0)
22018 {
22019 have_multiple_function_sections = true;
22020 return 0;
22021 }
22022 }
22023 }
22024 return 1;
22025 }
22026
22027 /* Hook called after emitting a function into assembly.
22028 This does something only for the very first function emitted. */
22029
22030 static void
22031 dwarf2out_end_function (unsigned int)
22032 {
22033 if (in_first_function_p
22034 && !have_multiple_function_sections
22035 && first_loclabel_num_not_at_text_label
22036 && decl_loc_table)
22037 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22038 in_first_function_p = false;
22039 maybe_at_text_label_p = false;
22040 }
22041
22042 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22043
22044 static void
22045 push_dw_line_info_entry (dw_line_info_table *table,
22046 enum dw_line_info_opcode opcode, unsigned int val)
22047 {
22048 dw_line_info_entry e;
22049 e.opcode = opcode;
22050 e.val = val;
22051 vec_safe_push (table->entries, e);
22052 }
22053
22054 /* Output a label to mark the beginning of a source code line entry
22055 and record information relating to this source line, in
22056 'line_info_table' for later output of the .debug_line section. */
22057 /* ??? The discriminator parameter ought to be unsigned. */
22058
22059 static void
22060 dwarf2out_source_line (unsigned int line, const char *filename,
22061 int discriminator, bool is_stmt)
22062 {
22063 unsigned int file_num;
22064 dw_line_info_table *table;
22065
22066 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22067 return;
22068
22069 /* The discriminator column was added in dwarf4. Simplify the below
22070 by simply removing it if we're not supposed to output it. */
22071 if (dwarf_version < 4 && dwarf_strict)
22072 discriminator = 0;
22073
22074 table = cur_line_info_table;
22075 file_num = maybe_emit_file (lookup_filename (filename));
22076
22077 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22078 the debugger has used the second (possibly duplicate) line number
22079 at the beginning of the function to mark the end of the prologue.
22080 We could eliminate any other duplicates within the function. For
22081 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22082 that second line number entry. */
22083 /* Recall that this end-of-prologue indication is *not* the same thing
22084 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22085 to which the hook corresponds, follows the last insn that was
22086 emitted by gen_prologue. What we need is to precede the first insn
22087 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22088 insn that corresponds to something the user wrote. These may be
22089 very different locations once scheduling is enabled. */
22090
22091 if (0 && file_num == table->file_num
22092 && line == table->line_num
22093 && discriminator == table->discrim_num
22094 && is_stmt == table->is_stmt)
22095 return;
22096
22097 switch_to_section (current_function_section ());
22098
22099 /* If requested, emit something human-readable. */
22100 if (flag_debug_asm)
22101 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22102
22103 if (DWARF2_ASM_LINE_DEBUG_INFO)
22104 {
22105 /* Emit the .loc directive understood by GNU as. */
22106 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22107 file_num, line, is_stmt, discriminator */
22108 fputs ("\t.loc ", asm_out_file);
22109 fprint_ul (asm_out_file, file_num);
22110 putc (' ', asm_out_file);
22111 fprint_ul (asm_out_file, line);
22112 putc (' ', asm_out_file);
22113 putc ('0', asm_out_file);
22114
22115 if (is_stmt != table->is_stmt)
22116 {
22117 fputs (" is_stmt ", asm_out_file);
22118 putc (is_stmt ? '1' : '0', asm_out_file);
22119 }
22120 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22121 {
22122 gcc_assert (discriminator > 0);
22123 fputs (" discriminator ", asm_out_file);
22124 fprint_ul (asm_out_file, (unsigned long) discriminator);
22125 }
22126 putc ('\n', asm_out_file);
22127 }
22128 else
22129 {
22130 unsigned int label_num = ++line_info_label_num;
22131
22132 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22133
22134 push_dw_line_info_entry (table, LI_set_address, label_num);
22135 if (file_num != table->file_num)
22136 push_dw_line_info_entry (table, LI_set_file, file_num);
22137 if (discriminator != table->discrim_num)
22138 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22139 if (is_stmt != table->is_stmt)
22140 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22141 push_dw_line_info_entry (table, LI_set_line, line);
22142 }
22143
22144 table->file_num = file_num;
22145 table->line_num = line;
22146 table->discrim_num = discriminator;
22147 table->is_stmt = is_stmt;
22148 table->in_use = true;
22149 }
22150
22151 /* Record the beginning of a new source file. */
22152
22153 static void
22154 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22155 {
22156 if (flag_eliminate_dwarf2_dups)
22157 {
22158 /* Record the beginning of the file for break_out_includes. */
22159 dw_die_ref bincl_die;
22160
22161 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22162 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22163 }
22164
22165 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22166 {
22167 macinfo_entry e;
22168 e.code = DW_MACINFO_start_file;
22169 e.lineno = lineno;
22170 e.info = ggc_strdup (filename);
22171 vec_safe_push (macinfo_table, e);
22172 }
22173 }
22174
22175 /* Record the end of a source file. */
22176
22177 static void
22178 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22179 {
22180 if (flag_eliminate_dwarf2_dups)
22181 /* Record the end of the file for break_out_includes. */
22182 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22183
22184 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22185 {
22186 macinfo_entry e;
22187 e.code = DW_MACINFO_end_file;
22188 e.lineno = lineno;
22189 e.info = NULL;
22190 vec_safe_push (macinfo_table, e);
22191 }
22192 }
22193
22194 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22195 the tail part of the directive line, i.e. the part which is past the
22196 initial whitespace, #, whitespace, directive-name, whitespace part. */
22197
22198 static void
22199 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22200 const char *buffer ATTRIBUTE_UNUSED)
22201 {
22202 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22203 {
22204 macinfo_entry e;
22205 /* Insert a dummy first entry to be able to optimize the whole
22206 predefined macro block using DW_MACRO_GNU_transparent_include. */
22207 if (macinfo_table->is_empty () && lineno <= 1)
22208 {
22209 e.code = 0;
22210 e.lineno = 0;
22211 e.info = NULL;
22212 vec_safe_push (macinfo_table, e);
22213 }
22214 e.code = DW_MACINFO_define;
22215 e.lineno = lineno;
22216 e.info = ggc_strdup (buffer);
22217 vec_safe_push (macinfo_table, e);
22218 }
22219 }
22220
22221 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22222 the tail part of the directive line, i.e. the part which is past the
22223 initial whitespace, #, whitespace, directive-name, whitespace part. */
22224
22225 static void
22226 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22227 const char *buffer ATTRIBUTE_UNUSED)
22228 {
22229 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22230 {
22231 macinfo_entry e;
22232 /* Insert a dummy first entry to be able to optimize the whole
22233 predefined macro block using DW_MACRO_GNU_transparent_include. */
22234 if (macinfo_table->is_empty () && lineno <= 1)
22235 {
22236 e.code = 0;
22237 e.lineno = 0;
22238 e.info = NULL;
22239 vec_safe_push (macinfo_table, e);
22240 }
22241 e.code = DW_MACINFO_undef;
22242 e.lineno = lineno;
22243 e.info = ggc_strdup (buffer);
22244 vec_safe_push (macinfo_table, e);
22245 }
22246 }
22247
22248 /* Helpers to manipulate hash table of CUs. */
22249
22250 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22251 {
22252 typedef macinfo_entry *value_type;
22253 typedef macinfo_entry *compare_type;
22254 static inline hashval_t hash (const macinfo_entry *);
22255 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22256 };
22257
22258 inline hashval_t
22259 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22260 {
22261 return htab_hash_string (entry->info);
22262 }
22263
22264 inline bool
22265 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22266 const macinfo_entry *entry2)
22267 {
22268 return !strcmp (entry1->info, entry2->info);
22269 }
22270
22271 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22272
22273 /* Output a single .debug_macinfo entry. */
22274
22275 static void
22276 output_macinfo_op (macinfo_entry *ref)
22277 {
22278 int file_num;
22279 size_t len;
22280 struct indirect_string_node *node;
22281 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22282 struct dwarf_file_data *fd;
22283
22284 switch (ref->code)
22285 {
22286 case DW_MACINFO_start_file:
22287 fd = lookup_filename (ref->info);
22288 file_num = maybe_emit_file (fd);
22289 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22290 dw2_asm_output_data_uleb128 (ref->lineno,
22291 "Included from line number %lu",
22292 (unsigned long) ref->lineno);
22293 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22294 break;
22295 case DW_MACINFO_end_file:
22296 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22297 break;
22298 case DW_MACINFO_define:
22299 case DW_MACINFO_undef:
22300 len = strlen (ref->info) + 1;
22301 if (!dwarf_strict
22302 && len > DWARF_OFFSET_SIZE
22303 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22304 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22305 {
22306 ref->code = ref->code == DW_MACINFO_define
22307 ? DW_MACRO_GNU_define_indirect
22308 : DW_MACRO_GNU_undef_indirect;
22309 output_macinfo_op (ref);
22310 return;
22311 }
22312 dw2_asm_output_data (1, ref->code,
22313 ref->code == DW_MACINFO_define
22314 ? "Define macro" : "Undefine macro");
22315 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22316 (unsigned long) ref->lineno);
22317 dw2_asm_output_nstring (ref->info, -1, "The macro");
22318 break;
22319 case DW_MACRO_GNU_define_indirect:
22320 case DW_MACRO_GNU_undef_indirect:
22321 node = find_AT_string (ref->info);
22322 gcc_assert (node
22323 && ((node->form == DW_FORM_strp)
22324 || (node->form == DW_FORM_GNU_str_index)));
22325 dw2_asm_output_data (1, ref->code,
22326 ref->code == DW_MACRO_GNU_define_indirect
22327 ? "Define macro indirect"
22328 : "Undefine macro indirect");
22329 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22330 (unsigned long) ref->lineno);
22331 if (node->form == DW_FORM_strp)
22332 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22333 debug_str_section, "The macro: \"%s\"",
22334 ref->info);
22335 else
22336 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22337 ref->info);
22338 break;
22339 case DW_MACRO_GNU_transparent_include:
22340 dw2_asm_output_data (1, ref->code, "Transparent include");
22341 ASM_GENERATE_INTERNAL_LABEL (label,
22342 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22343 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22344 break;
22345 default:
22346 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22347 ASM_COMMENT_START, (unsigned long) ref->code);
22348 break;
22349 }
22350 }
22351
22352 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22353 other compilation unit .debug_macinfo sections. IDX is the first
22354 index of a define/undef, return the number of ops that should be
22355 emitted in a comdat .debug_macinfo section and emit
22356 a DW_MACRO_GNU_transparent_include entry referencing it.
22357 If the define/undef entry should be emitted normally, return 0. */
22358
22359 static unsigned
22360 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22361 macinfo_hash_type **macinfo_htab)
22362 {
22363 macinfo_entry *first, *second, *cur, *inc;
22364 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22365 unsigned char checksum[16];
22366 struct md5_ctx ctx;
22367 char *grp_name, *tail;
22368 const char *base;
22369 unsigned int i, count, encoded_filename_len, linebuf_len;
22370 macinfo_entry **slot;
22371
22372 first = &(*macinfo_table)[idx];
22373 second = &(*macinfo_table)[idx + 1];
22374
22375 /* Optimize only if there are at least two consecutive define/undef ops,
22376 and either all of them are before first DW_MACINFO_start_file
22377 with lineno {0,1} (i.e. predefined macro block), or all of them are
22378 in some included header file. */
22379 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22380 return 0;
22381 if (vec_safe_is_empty (files))
22382 {
22383 if (first->lineno > 1 || second->lineno > 1)
22384 return 0;
22385 }
22386 else if (first->lineno == 0)
22387 return 0;
22388
22389 /* Find the last define/undef entry that can be grouped together
22390 with first and at the same time compute md5 checksum of their
22391 codes, linenumbers and strings. */
22392 md5_init_ctx (&ctx);
22393 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22394 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22395 break;
22396 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22397 break;
22398 else
22399 {
22400 unsigned char code = cur->code;
22401 md5_process_bytes (&code, 1, &ctx);
22402 checksum_uleb128 (cur->lineno, &ctx);
22403 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22404 }
22405 md5_finish_ctx (&ctx, checksum);
22406 count = i - idx;
22407
22408 /* From the containing include filename (if any) pick up just
22409 usable characters from its basename. */
22410 if (vec_safe_is_empty (files))
22411 base = "";
22412 else
22413 base = lbasename (files->last ().info);
22414 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22415 if (ISIDNUM (base[i]) || base[i] == '.')
22416 encoded_filename_len++;
22417 /* Count . at the end. */
22418 if (encoded_filename_len)
22419 encoded_filename_len++;
22420
22421 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22422 linebuf_len = strlen (linebuf);
22423
22424 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22425 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22426 + 16 * 2 + 1);
22427 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22428 tail = grp_name + 4;
22429 if (encoded_filename_len)
22430 {
22431 for (i = 0; base[i]; i++)
22432 if (ISIDNUM (base[i]) || base[i] == '.')
22433 *tail++ = base[i];
22434 *tail++ = '.';
22435 }
22436 memcpy (tail, linebuf, linebuf_len);
22437 tail += linebuf_len;
22438 *tail++ = '.';
22439 for (i = 0; i < 16; i++)
22440 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22441
22442 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22443 in the empty vector entry before the first define/undef. */
22444 inc = &(*macinfo_table)[idx - 1];
22445 inc->code = DW_MACRO_GNU_transparent_include;
22446 inc->lineno = 0;
22447 inc->info = ggc_strdup (grp_name);
22448 if (!*macinfo_htab)
22449 *macinfo_htab = new macinfo_hash_type (10);
22450 /* Avoid emitting duplicates. */
22451 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22452 if (*slot != NULL)
22453 {
22454 inc->code = 0;
22455 inc->info = NULL;
22456 /* If such an entry has been used before, just emit
22457 a DW_MACRO_GNU_transparent_include op. */
22458 inc = *slot;
22459 output_macinfo_op (inc);
22460 /* And clear all macinfo_entry in the range to avoid emitting them
22461 in the second pass. */
22462 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22463 {
22464 cur->code = 0;
22465 cur->info = NULL;
22466 }
22467 }
22468 else
22469 {
22470 *slot = inc;
22471 inc->lineno = (*macinfo_htab)->elements ();
22472 output_macinfo_op (inc);
22473 }
22474 return count;
22475 }
22476
22477 /* Save any strings needed by the macinfo table in the debug str
22478 table. All strings must be collected into the table by the time
22479 index_string is called. */
22480
22481 static void
22482 save_macinfo_strings (void)
22483 {
22484 unsigned len;
22485 unsigned i;
22486 macinfo_entry *ref;
22487
22488 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22489 {
22490 switch (ref->code)
22491 {
22492 /* Match the logic in output_macinfo_op to decide on
22493 indirect strings. */
22494 case DW_MACINFO_define:
22495 case DW_MACINFO_undef:
22496 len = strlen (ref->info) + 1;
22497 if (!dwarf_strict
22498 && len > DWARF_OFFSET_SIZE
22499 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22500 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22501 set_indirect_string (find_AT_string (ref->info));
22502 break;
22503 case DW_MACRO_GNU_define_indirect:
22504 case DW_MACRO_GNU_undef_indirect:
22505 set_indirect_string (find_AT_string (ref->info));
22506 break;
22507 default:
22508 break;
22509 }
22510 }
22511 }
22512
22513 /* Output macinfo section(s). */
22514
22515 static void
22516 output_macinfo (void)
22517 {
22518 unsigned i;
22519 unsigned long length = vec_safe_length (macinfo_table);
22520 macinfo_entry *ref;
22521 vec<macinfo_entry, va_gc> *files = NULL;
22522 macinfo_hash_type *macinfo_htab = NULL;
22523
22524 if (! length)
22525 return;
22526
22527 /* output_macinfo* uses these interchangeably. */
22528 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22529 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22530 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22531 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22532
22533 /* For .debug_macro emit the section header. */
22534 if (!dwarf_strict)
22535 {
22536 dw2_asm_output_data (2, 4, "DWARF macro version number");
22537 if (DWARF_OFFSET_SIZE == 8)
22538 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22539 else
22540 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22541 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22542 (!dwarf_split_debug_info ? debug_line_section_label
22543 : debug_skeleton_line_section_label),
22544 debug_line_section, NULL);
22545 }
22546
22547 /* In the first loop, it emits the primary .debug_macinfo section
22548 and after each emitted op the macinfo_entry is cleared.
22549 If a longer range of define/undef ops can be optimized using
22550 DW_MACRO_GNU_transparent_include, the
22551 DW_MACRO_GNU_transparent_include op is emitted and kept in
22552 the vector before the first define/undef in the range and the
22553 whole range of define/undef ops is not emitted and kept. */
22554 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22555 {
22556 switch (ref->code)
22557 {
22558 case DW_MACINFO_start_file:
22559 vec_safe_push (files, *ref);
22560 break;
22561 case DW_MACINFO_end_file:
22562 if (!vec_safe_is_empty (files))
22563 files->pop ();
22564 break;
22565 case DW_MACINFO_define:
22566 case DW_MACINFO_undef:
22567 if (!dwarf_strict
22568 && HAVE_COMDAT_GROUP
22569 && vec_safe_length (files) != 1
22570 && i > 0
22571 && i + 1 < length
22572 && (*macinfo_table)[i - 1].code == 0)
22573 {
22574 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22575 if (count)
22576 {
22577 i += count - 1;
22578 continue;
22579 }
22580 }
22581 break;
22582 case 0:
22583 /* A dummy entry may be inserted at the beginning to be able
22584 to optimize the whole block of predefined macros. */
22585 if (i == 0)
22586 continue;
22587 default:
22588 break;
22589 }
22590 output_macinfo_op (ref);
22591 ref->info = NULL;
22592 ref->code = 0;
22593 }
22594
22595 if (!macinfo_htab)
22596 return;
22597
22598 delete macinfo_htab;
22599 macinfo_htab = NULL;
22600
22601 /* If any DW_MACRO_GNU_transparent_include were used, on those
22602 DW_MACRO_GNU_transparent_include entries terminate the
22603 current chain and switch to a new comdat .debug_macinfo
22604 section and emit the define/undef entries within it. */
22605 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22606 switch (ref->code)
22607 {
22608 case 0:
22609 continue;
22610 case DW_MACRO_GNU_transparent_include:
22611 {
22612 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22613 tree comdat_key = get_identifier (ref->info);
22614 /* Terminate the previous .debug_macinfo section. */
22615 dw2_asm_output_data (1, 0, "End compilation unit");
22616 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22617 SECTION_DEBUG
22618 | SECTION_LINKONCE,
22619 comdat_key);
22620 ASM_GENERATE_INTERNAL_LABEL (label,
22621 DEBUG_MACRO_SECTION_LABEL,
22622 ref->lineno);
22623 ASM_OUTPUT_LABEL (asm_out_file, label);
22624 ref->code = 0;
22625 ref->info = NULL;
22626 dw2_asm_output_data (2, 4, "DWARF macro version number");
22627 if (DWARF_OFFSET_SIZE == 8)
22628 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22629 else
22630 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22631 }
22632 break;
22633 case DW_MACINFO_define:
22634 case DW_MACINFO_undef:
22635 output_macinfo_op (ref);
22636 ref->code = 0;
22637 ref->info = NULL;
22638 break;
22639 default:
22640 gcc_unreachable ();
22641 }
22642 }
22643
22644 /* Set up for Dwarf output at the start of compilation. */
22645
22646 static void
22647 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22648 {
22649 /* This option is currently broken, see (PR53118 and PR46102). */
22650 if (flag_eliminate_dwarf2_dups
22651 && strstr (lang_hooks.name, "C++"))
22652 {
22653 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22654 flag_eliminate_dwarf2_dups = 0;
22655 }
22656
22657 /* Allocate the file_table. */
22658 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22659
22660 /* Allocate the decl_die_table. */
22661 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22662
22663 /* Allocate the decl_loc_table. */
22664 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22665
22666 /* Allocate the cached_dw_loc_list_table. */
22667 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22668
22669 /* Allocate the initial hunk of the decl_scope_table. */
22670 vec_alloc (decl_scope_table, 256);
22671
22672 /* Allocate the initial hunk of the abbrev_die_table. */
22673 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22674 (ABBREV_DIE_TABLE_INCREMENT);
22675 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22676 /* Zero-th entry is allocated, but unused. */
22677 abbrev_die_table_in_use = 1;
22678
22679 /* Allocate the pubtypes and pubnames vectors. */
22680 vec_alloc (pubname_table, 32);
22681 vec_alloc (pubtype_table, 32);
22682
22683 vec_alloc (incomplete_types, 64);
22684
22685 vec_alloc (used_rtx_array, 32);
22686
22687 if (!dwarf_split_debug_info)
22688 {
22689 debug_info_section = get_section (DEBUG_INFO_SECTION,
22690 SECTION_DEBUG, NULL);
22691 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22692 SECTION_DEBUG, NULL);
22693 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22694 SECTION_DEBUG, NULL);
22695 }
22696 else
22697 {
22698 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22699 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22700 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22701 SECTION_DEBUG | SECTION_EXCLUDE,
22702 NULL);
22703 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22704 SECTION_DEBUG, NULL);
22705 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22706 SECTION_DEBUG, NULL);
22707 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22708 SECTION_DEBUG, NULL);
22709 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22710 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22711
22712 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22713 the main .o, but the skeleton_line goes into the split off dwo. */
22714 debug_skeleton_line_section
22715 = get_section (DEBUG_DWO_LINE_SECTION,
22716 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22717 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22718 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22719 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22720 SECTION_DEBUG | SECTION_EXCLUDE,
22721 NULL);
22722 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22723 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22724 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22725 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22726 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22727 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22728 }
22729 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22730 SECTION_DEBUG, NULL);
22731 debug_macinfo_section = get_section (dwarf_strict
22732 ? DEBUG_MACINFO_SECTION
22733 : DEBUG_MACRO_SECTION,
22734 DEBUG_MACRO_SECTION_FLAGS, NULL);
22735 debug_line_section = get_section (DEBUG_LINE_SECTION,
22736 SECTION_DEBUG, NULL);
22737 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22738 SECTION_DEBUG, NULL);
22739 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22740 SECTION_DEBUG, NULL);
22741 debug_str_section = get_section (DEBUG_STR_SECTION,
22742 DEBUG_STR_SECTION_FLAGS, NULL);
22743 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22744 SECTION_DEBUG, NULL);
22745 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22746 SECTION_DEBUG, NULL);
22747
22748 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22749 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22750 DEBUG_ABBREV_SECTION_LABEL, 0);
22751 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22752 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22753 COLD_TEXT_SECTION_LABEL, 0);
22754 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22755
22756 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22757 DEBUG_INFO_SECTION_LABEL, 0);
22758 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22759 DEBUG_LINE_SECTION_LABEL, 0);
22760 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22761 DEBUG_RANGES_SECTION_LABEL, 0);
22762 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22763 DEBUG_ADDR_SECTION_LABEL, 0);
22764 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22765 dwarf_strict
22766 ? DEBUG_MACINFO_SECTION_LABEL
22767 : DEBUG_MACRO_SECTION_LABEL, 0);
22768 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22769
22770 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22771 vec_alloc (macinfo_table, 64);
22772
22773 switch_to_section (text_section);
22774 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22775
22776 /* Make sure the line number table for .text always exists. */
22777 text_section_line_info = new_line_info_table ();
22778 text_section_line_info->end_label = text_end_label;
22779 }
22780
22781 /* Called before compile () starts outputtting functions, variables
22782 and toplevel asms into assembly. */
22783
22784 static void
22785 dwarf2out_assembly_start (void)
22786 {
22787 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22788 && dwarf2out_do_cfi_asm ()
22789 && (!(flag_unwind_tables || flag_exceptions)
22790 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22791 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22792 }
22793
22794 /* A helper function for dwarf2out_finish called through
22795 htab_traverse. Assign a string its index. All strings must be
22796 collected into the table by the time index_string is called,
22797 because the indexing code relies on htab_traverse to traverse nodes
22798 in the same order for each run. */
22799
22800 int
22801 index_string (indirect_string_node **h, unsigned int *index)
22802 {
22803 indirect_string_node *node = *h;
22804
22805 find_string_form (node);
22806 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22807 {
22808 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22809 node->index = *index;
22810 *index += 1;
22811 }
22812 return 1;
22813 }
22814
22815 /* A helper function for output_indirect_strings called through
22816 htab_traverse. Output the offset to a string and update the
22817 current offset. */
22818
22819 int
22820 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22821 {
22822 indirect_string_node *node = *h;
22823
22824 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22825 {
22826 /* Assert that this node has been assigned an index. */
22827 gcc_assert (node->index != NO_INDEX_ASSIGNED
22828 && node->index != NOT_INDEXED);
22829 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22830 "indexed string 0x%x: %s", node->index, node->str);
22831 *offset += strlen (node->str) + 1;
22832 }
22833 return 1;
22834 }
22835
22836 /* A helper function for dwarf2out_finish called through
22837 htab_traverse. Output the indexed string. */
22838
22839 int
22840 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22841 {
22842 struct indirect_string_node *node = *h;
22843
22844 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22845 {
22846 /* Assert that the strings are output in the same order as their
22847 indexes were assigned. */
22848 gcc_assert (*cur_idx == node->index);
22849 assemble_string (node->str, strlen (node->str) + 1);
22850 *cur_idx += 1;
22851 }
22852 return 1;
22853 }
22854
22855 /* A helper function for dwarf2out_finish called through
22856 htab_traverse. Emit one queued .debug_str string. */
22857
22858 int
22859 output_indirect_string (indirect_string_node **h, void *)
22860 {
22861 struct indirect_string_node *node = *h;
22862
22863 node->form = find_string_form (node);
22864 if (node->form == DW_FORM_strp && node->refcount > 0)
22865 {
22866 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22867 assemble_string (node->str, strlen (node->str) + 1);
22868 }
22869
22870 return 1;
22871 }
22872
22873 /* Output the indexed string table. */
22874
22875 static void
22876 output_indirect_strings (void)
22877 {
22878 switch_to_section (debug_str_section);
22879 if (!dwarf_split_debug_info)
22880 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22881 else
22882 {
22883 unsigned int offset = 0;
22884 unsigned int cur_idx = 0;
22885
22886 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22887
22888 switch_to_section (debug_str_offsets_section);
22889 debug_str_hash->traverse_noresize
22890 <unsigned int *, output_index_string_offset> (&offset);
22891 switch_to_section (debug_str_dwo_section);
22892 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22893 (&cur_idx);
22894 }
22895 }
22896
22897 /* Callback for htab_traverse to assign an index to an entry in the
22898 table, and to write that entry to the .debug_addr section. */
22899
22900 int
22901 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22902 {
22903 addr_table_entry *entry = *slot;
22904
22905 if (entry->refcount == 0)
22906 {
22907 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22908 || entry->index == NOT_INDEXED);
22909 return 1;
22910 }
22911
22912 gcc_assert (entry->index == *cur_index);
22913 (*cur_index)++;
22914
22915 switch (entry->kind)
22916 {
22917 case ate_kind_rtx:
22918 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22919 "0x%x", entry->index);
22920 break;
22921 case ate_kind_rtx_dtprel:
22922 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22923 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22924 DWARF2_ADDR_SIZE,
22925 entry->addr.rtl);
22926 fputc ('\n', asm_out_file);
22927 break;
22928 case ate_kind_label:
22929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22930 "0x%x", entry->index);
22931 break;
22932 default:
22933 gcc_unreachable ();
22934 }
22935 return 1;
22936 }
22937
22938 /* Produce the .debug_addr section. */
22939
22940 static void
22941 output_addr_table (void)
22942 {
22943 unsigned int index = 0;
22944 if (addr_index_table == NULL || addr_index_table->size () == 0)
22945 return;
22946
22947 switch_to_section (debug_addr_section);
22948 addr_index_table
22949 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22950 }
22951
22952 #if ENABLE_ASSERT_CHECKING
22953 /* Verify that all marks are clear. */
22954
22955 static void
22956 verify_marks_clear (dw_die_ref die)
22957 {
22958 dw_die_ref c;
22959
22960 gcc_assert (! die->die_mark);
22961 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22962 }
22963 #endif /* ENABLE_ASSERT_CHECKING */
22964
22965 /* Clear the marks for a die and its children.
22966 Be cool if the mark isn't set. */
22967
22968 static void
22969 prune_unmark_dies (dw_die_ref die)
22970 {
22971 dw_die_ref c;
22972
22973 if (die->die_mark)
22974 die->die_mark = 0;
22975 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22976 }
22977
22978 /* Given DIE that we're marking as used, find any other dies
22979 it references as attributes and mark them as used. */
22980
22981 static void
22982 prune_unused_types_walk_attribs (dw_die_ref die)
22983 {
22984 dw_attr_ref a;
22985 unsigned ix;
22986
22987 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22988 {
22989 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22990 {
22991 /* A reference to another DIE.
22992 Make sure that it will get emitted.
22993 If it was broken out into a comdat group, don't follow it. */
22994 if (! AT_ref (a)->comdat_type_p
22995 || a->dw_attr == DW_AT_specification)
22996 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22997 }
22998 /* Set the string's refcount to 0 so that prune_unused_types_mark
22999 accounts properly for it. */
23000 if (AT_class (a) == dw_val_class_str)
23001 a->dw_attr_val.v.val_str->refcount = 0;
23002 }
23003 }
23004
23005 /* Mark the generic parameters and arguments children DIEs of DIE. */
23006
23007 static void
23008 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23009 {
23010 dw_die_ref c;
23011
23012 if (die == NULL || die->die_child == NULL)
23013 return;
23014 c = die->die_child;
23015 do
23016 {
23017 if (is_template_parameter (c))
23018 prune_unused_types_mark (c, 1);
23019 c = c->die_sib;
23020 } while (c && c != die->die_child);
23021 }
23022
23023 /* Mark DIE as being used. If DOKIDS is true, then walk down
23024 to DIE's children. */
23025
23026 static void
23027 prune_unused_types_mark (dw_die_ref die, int dokids)
23028 {
23029 dw_die_ref c;
23030
23031 if (die->die_mark == 0)
23032 {
23033 /* We haven't done this node yet. Mark it as used. */
23034 die->die_mark = 1;
23035 /* If this is the DIE of a generic type instantiation,
23036 mark the children DIEs that describe its generic parms and
23037 args. */
23038 prune_unused_types_mark_generic_parms_dies (die);
23039
23040 /* We also have to mark its parents as used.
23041 (But we don't want to mark our parent's kids due to this,
23042 unless it is a class.) */
23043 if (die->die_parent)
23044 prune_unused_types_mark (die->die_parent,
23045 class_scope_p (die->die_parent));
23046
23047 /* Mark any referenced nodes. */
23048 prune_unused_types_walk_attribs (die);
23049
23050 /* If this node is a specification,
23051 also mark the definition, if it exists. */
23052 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23053 prune_unused_types_mark (die->die_definition, 1);
23054 }
23055
23056 if (dokids && die->die_mark != 2)
23057 {
23058 /* We need to walk the children, but haven't done so yet.
23059 Remember that we've walked the kids. */
23060 die->die_mark = 2;
23061
23062 /* If this is an array type, we need to make sure our
23063 kids get marked, even if they're types. If we're
23064 breaking out types into comdat sections, do this
23065 for all type definitions. */
23066 if (die->die_tag == DW_TAG_array_type
23067 || (use_debug_types
23068 && is_type_die (die) && ! is_declaration_die (die)))
23069 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23070 else
23071 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23072 }
23073 }
23074
23075 /* For local classes, look if any static member functions were emitted
23076 and if so, mark them. */
23077
23078 static void
23079 prune_unused_types_walk_local_classes (dw_die_ref die)
23080 {
23081 dw_die_ref c;
23082
23083 if (die->die_mark == 2)
23084 return;
23085
23086 switch (die->die_tag)
23087 {
23088 case DW_TAG_structure_type:
23089 case DW_TAG_union_type:
23090 case DW_TAG_class_type:
23091 break;
23092
23093 case DW_TAG_subprogram:
23094 if (!get_AT_flag (die, DW_AT_declaration)
23095 || die->die_definition != NULL)
23096 prune_unused_types_mark (die, 1);
23097 return;
23098
23099 default:
23100 return;
23101 }
23102
23103 /* Mark children. */
23104 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23105 }
23106
23107 /* Walk the tree DIE and mark types that we actually use. */
23108
23109 static void
23110 prune_unused_types_walk (dw_die_ref die)
23111 {
23112 dw_die_ref c;
23113
23114 /* Don't do anything if this node is already marked and
23115 children have been marked as well. */
23116 if (die->die_mark == 2)
23117 return;
23118
23119 switch (die->die_tag)
23120 {
23121 case DW_TAG_structure_type:
23122 case DW_TAG_union_type:
23123 case DW_TAG_class_type:
23124 if (die->die_perennial_p)
23125 break;
23126
23127 for (c = die->die_parent; c; c = c->die_parent)
23128 if (c->die_tag == DW_TAG_subprogram)
23129 break;
23130
23131 /* Finding used static member functions inside of classes
23132 is needed just for local classes, because for other classes
23133 static member function DIEs with DW_AT_specification
23134 are emitted outside of the DW_TAG_*_type. If we ever change
23135 it, we'd need to call this even for non-local classes. */
23136 if (c)
23137 prune_unused_types_walk_local_classes (die);
23138
23139 /* It's a type node --- don't mark it. */
23140 return;
23141
23142 case DW_TAG_const_type:
23143 case DW_TAG_packed_type:
23144 case DW_TAG_pointer_type:
23145 case DW_TAG_reference_type:
23146 case DW_TAG_rvalue_reference_type:
23147 case DW_TAG_volatile_type:
23148 case DW_TAG_typedef:
23149 case DW_TAG_array_type:
23150 case DW_TAG_interface_type:
23151 case DW_TAG_friend:
23152 case DW_TAG_variant_part:
23153 case DW_TAG_enumeration_type:
23154 case DW_TAG_subroutine_type:
23155 case DW_TAG_string_type:
23156 case DW_TAG_set_type:
23157 case DW_TAG_subrange_type:
23158 case DW_TAG_ptr_to_member_type:
23159 case DW_TAG_file_type:
23160 if (die->die_perennial_p)
23161 break;
23162
23163 /* It's a type node --- don't mark it. */
23164 return;
23165
23166 default:
23167 /* Mark everything else. */
23168 break;
23169 }
23170
23171 if (die->die_mark == 0)
23172 {
23173 die->die_mark = 1;
23174
23175 /* Now, mark any dies referenced from here. */
23176 prune_unused_types_walk_attribs (die);
23177 }
23178
23179 die->die_mark = 2;
23180
23181 /* Mark children. */
23182 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23183 }
23184
23185 /* Increment the string counts on strings referred to from DIE's
23186 attributes. */
23187
23188 static void
23189 prune_unused_types_update_strings (dw_die_ref die)
23190 {
23191 dw_attr_ref a;
23192 unsigned ix;
23193
23194 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23195 if (AT_class (a) == dw_val_class_str)
23196 {
23197 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23198 s->refcount++;
23199 /* Avoid unnecessarily putting strings that are used less than
23200 twice in the hash table. */
23201 if (s->refcount
23202 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23203 {
23204 indirect_string_node **slot
23205 = debug_str_hash->find_slot_with_hash (s->str,
23206 htab_hash_string (s->str),
23207 INSERT);
23208 gcc_assert (*slot == NULL);
23209 *slot = s;
23210 }
23211 }
23212 }
23213
23214 /* Remove from the tree DIE any dies that aren't marked. */
23215
23216 static void
23217 prune_unused_types_prune (dw_die_ref die)
23218 {
23219 dw_die_ref c;
23220
23221 gcc_assert (die->die_mark);
23222 prune_unused_types_update_strings (die);
23223
23224 if (! die->die_child)
23225 return;
23226
23227 c = die->die_child;
23228 do {
23229 dw_die_ref prev = c;
23230 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23231 if (c == die->die_child)
23232 {
23233 /* No marked children between 'prev' and the end of the list. */
23234 if (prev == c)
23235 /* No marked children at all. */
23236 die->die_child = NULL;
23237 else
23238 {
23239 prev->die_sib = c->die_sib;
23240 die->die_child = prev;
23241 }
23242 return;
23243 }
23244
23245 if (c != prev->die_sib)
23246 prev->die_sib = c;
23247 prune_unused_types_prune (c);
23248 } while (c != die->die_child);
23249 }
23250
23251 /* Remove dies representing declarations that we never use. */
23252
23253 static void
23254 prune_unused_types (void)
23255 {
23256 unsigned int i;
23257 limbo_die_node *node;
23258 comdat_type_node *ctnode;
23259 pubname_ref pub;
23260 dw_die_ref base_type;
23261
23262 #if ENABLE_ASSERT_CHECKING
23263 /* All the marks should already be clear. */
23264 verify_marks_clear (comp_unit_die ());
23265 for (node = limbo_die_list; node; node = node->next)
23266 verify_marks_clear (node->die);
23267 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23268 verify_marks_clear (ctnode->root_die);
23269 #endif /* ENABLE_ASSERT_CHECKING */
23270
23271 /* Mark types that are used in global variables. */
23272 premark_types_used_by_global_vars ();
23273
23274 /* Set the mark on nodes that are actually used. */
23275 prune_unused_types_walk (comp_unit_die ());
23276 for (node = limbo_die_list; node; node = node->next)
23277 prune_unused_types_walk (node->die);
23278 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23279 {
23280 prune_unused_types_walk (ctnode->root_die);
23281 prune_unused_types_mark (ctnode->type_die, 1);
23282 }
23283
23284 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23285 are unusual in that they are pubnames that are the children of pubtypes.
23286 They should only be marked via their parent DW_TAG_enumeration_type die,
23287 not as roots in themselves. */
23288 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23289 if (pub->die->die_tag != DW_TAG_enumerator)
23290 prune_unused_types_mark (pub->die, 1);
23291 for (i = 0; base_types.iterate (i, &base_type); i++)
23292 prune_unused_types_mark (base_type, 1);
23293
23294 if (debug_str_hash)
23295 debug_str_hash->empty ();
23296 if (skeleton_debug_str_hash)
23297 skeleton_debug_str_hash->empty ();
23298 prune_unused_types_prune (comp_unit_die ());
23299 for (node = limbo_die_list; node; node = node->next)
23300 prune_unused_types_prune (node->die);
23301 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23302 prune_unused_types_prune (ctnode->root_die);
23303
23304 /* Leave the marks clear. */
23305 prune_unmark_dies (comp_unit_die ());
23306 for (node = limbo_die_list; node; node = node->next)
23307 prune_unmark_dies (node->die);
23308 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23309 prune_unmark_dies (ctnode->root_die);
23310 }
23311
23312 /* Set the parameter to true if there are any relative pathnames in
23313 the file table. */
23314 int
23315 file_table_relative_p (dwarf_file_data **slot, bool *p)
23316 {
23317 struct dwarf_file_data *d = *slot;
23318 if (!IS_ABSOLUTE_PATH (d->filename))
23319 {
23320 *p = true;
23321 return 0;
23322 }
23323 return 1;
23324 }
23325
23326 /* Helpers to manipulate hash table of comdat type units. */
23327
23328 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23329 {
23330 typedef comdat_type_node *value_type;
23331 typedef comdat_type_node *compare_type;
23332 static inline hashval_t hash (const comdat_type_node *);
23333 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23334 };
23335
23336 inline hashval_t
23337 comdat_type_hasher::hash (const comdat_type_node *type_node)
23338 {
23339 hashval_t h;
23340 memcpy (&h, type_node->signature, sizeof (h));
23341 return h;
23342 }
23343
23344 inline bool
23345 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23346 const comdat_type_node *type_node_2)
23347 {
23348 return (! memcmp (type_node_1->signature, type_node_2->signature,
23349 DWARF_TYPE_SIGNATURE_SIZE));
23350 }
23351
23352 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23353 to the location it would have been added, should we know its
23354 DECL_ASSEMBLER_NAME when we added other attributes. This will
23355 probably improve compactness of debug info, removing equivalent
23356 abbrevs, and hide any differences caused by deferring the
23357 computation of the assembler name, triggered by e.g. PCH. */
23358
23359 static inline void
23360 move_linkage_attr (dw_die_ref die)
23361 {
23362 unsigned ix = vec_safe_length (die->die_attr);
23363 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23364
23365 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23366 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23367
23368 while (--ix > 0)
23369 {
23370 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23371
23372 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23373 break;
23374 }
23375
23376 if (ix != vec_safe_length (die->die_attr) - 1)
23377 {
23378 die->die_attr->pop ();
23379 die->die_attr->quick_insert (ix, linkage);
23380 }
23381 }
23382
23383 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23384 referenced from typed stack ops and count how often they are used. */
23385
23386 static void
23387 mark_base_types (dw_loc_descr_ref loc)
23388 {
23389 dw_die_ref base_type = NULL;
23390
23391 for (; loc; loc = loc->dw_loc_next)
23392 {
23393 switch (loc->dw_loc_opc)
23394 {
23395 case DW_OP_GNU_regval_type:
23396 case DW_OP_GNU_deref_type:
23397 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23398 break;
23399 case DW_OP_GNU_convert:
23400 case DW_OP_GNU_reinterpret:
23401 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23402 continue;
23403 /* FALLTHRU */
23404 case DW_OP_GNU_const_type:
23405 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23406 break;
23407 case DW_OP_GNU_entry_value:
23408 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23409 continue;
23410 default:
23411 continue;
23412 }
23413 gcc_assert (base_type->die_parent == comp_unit_die ());
23414 if (base_type->die_mark)
23415 base_type->die_mark++;
23416 else
23417 {
23418 base_types.safe_push (base_type);
23419 base_type->die_mark = 1;
23420 }
23421 }
23422 }
23423
23424 /* Comparison function for sorting marked base types. */
23425
23426 static int
23427 base_type_cmp (const void *x, const void *y)
23428 {
23429 dw_die_ref dx = *(const dw_die_ref *) x;
23430 dw_die_ref dy = *(const dw_die_ref *) y;
23431 unsigned int byte_size1, byte_size2;
23432 unsigned int encoding1, encoding2;
23433 if (dx->die_mark > dy->die_mark)
23434 return -1;
23435 if (dx->die_mark < dy->die_mark)
23436 return 1;
23437 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23438 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23439 if (byte_size1 < byte_size2)
23440 return 1;
23441 if (byte_size1 > byte_size2)
23442 return -1;
23443 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23444 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23445 if (encoding1 < encoding2)
23446 return 1;
23447 if (encoding1 > encoding2)
23448 return -1;
23449 return 0;
23450 }
23451
23452 /* Move base types marked by mark_base_types as early as possible
23453 in the CU, sorted by decreasing usage count both to make the
23454 uleb128 references as small as possible and to make sure they
23455 will have die_offset already computed by calc_die_sizes when
23456 sizes of typed stack loc ops is computed. */
23457
23458 static void
23459 move_marked_base_types (void)
23460 {
23461 unsigned int i;
23462 dw_die_ref base_type, die, c;
23463
23464 if (base_types.is_empty ())
23465 return;
23466
23467 /* Sort by decreasing usage count, they will be added again in that
23468 order later on. */
23469 base_types.qsort (base_type_cmp);
23470 die = comp_unit_die ();
23471 c = die->die_child;
23472 do
23473 {
23474 dw_die_ref prev = c;
23475 c = c->die_sib;
23476 while (c->die_mark)
23477 {
23478 remove_child_with_prev (c, prev);
23479 /* As base types got marked, there must be at least
23480 one node other than DW_TAG_base_type. */
23481 gcc_assert (c != c->die_sib);
23482 c = c->die_sib;
23483 }
23484 }
23485 while (c != die->die_child);
23486 gcc_assert (die->die_child);
23487 c = die->die_child;
23488 for (i = 0; base_types.iterate (i, &base_type); i++)
23489 {
23490 base_type->die_mark = 0;
23491 base_type->die_sib = c->die_sib;
23492 c->die_sib = base_type;
23493 c = base_type;
23494 }
23495 }
23496
23497 /* Helper function for resolve_addr, attempt to resolve
23498 one CONST_STRING, return true if successful. Similarly verify that
23499 SYMBOL_REFs refer to variables emitted in the current CU. */
23500
23501 static bool
23502 resolve_one_addr (rtx *addr)
23503 {
23504 rtx rtl = *addr;
23505
23506 if (GET_CODE (rtl) == CONST_STRING)
23507 {
23508 size_t len = strlen (XSTR (rtl, 0)) + 1;
23509 tree t = build_string (len, XSTR (rtl, 0));
23510 tree tlen = size_int (len - 1);
23511 TREE_TYPE (t)
23512 = build_array_type (char_type_node, build_index_type (tlen));
23513 rtl = lookup_constant_def (t);
23514 if (!rtl || !MEM_P (rtl))
23515 return false;
23516 rtl = XEXP (rtl, 0);
23517 if (GET_CODE (rtl) == SYMBOL_REF
23518 && SYMBOL_REF_DECL (rtl)
23519 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23520 return false;
23521 vec_safe_push (used_rtx_array, rtl);
23522 *addr = rtl;
23523 return true;
23524 }
23525
23526 if (GET_CODE (rtl) == SYMBOL_REF
23527 && SYMBOL_REF_DECL (rtl))
23528 {
23529 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23530 {
23531 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23532 return false;
23533 }
23534 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23535 return false;
23536 }
23537
23538 if (GET_CODE (rtl) == CONST)
23539 {
23540 subrtx_ptr_iterator::array_type array;
23541 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23542 if (!resolve_one_addr (*iter))
23543 return false;
23544 }
23545
23546 return true;
23547 }
23548
23549 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23550 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23551 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23552
23553 static rtx
23554 string_cst_pool_decl (tree t)
23555 {
23556 rtx rtl = output_constant_def (t, 1);
23557 unsigned char *array;
23558 dw_loc_descr_ref l;
23559 tree decl;
23560 size_t len;
23561 dw_die_ref ref;
23562
23563 if (!rtl || !MEM_P (rtl))
23564 return NULL_RTX;
23565 rtl = XEXP (rtl, 0);
23566 if (GET_CODE (rtl) != SYMBOL_REF
23567 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23568 return NULL_RTX;
23569
23570 decl = SYMBOL_REF_DECL (rtl);
23571 if (!lookup_decl_die (decl))
23572 {
23573 len = TREE_STRING_LENGTH (t);
23574 vec_safe_push (used_rtx_array, rtl);
23575 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23576 array = ggc_vec_alloc<unsigned char> (len);
23577 memcpy (array, TREE_STRING_POINTER (t), len);
23578 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23579 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23580 l->dw_loc_oprnd2.v.val_vec.length = len;
23581 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23582 l->dw_loc_oprnd2.v.val_vec.array = array;
23583 add_AT_loc (ref, DW_AT_location, l);
23584 equate_decl_number_to_die (decl, ref);
23585 }
23586 return rtl;
23587 }
23588
23589 /* Helper function of resolve_addr_in_expr. LOC is
23590 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23591 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23592 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23593 with DW_OP_GNU_implicit_pointer if possible
23594 and return true, if unsuccessful, return false. */
23595
23596 static bool
23597 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23598 {
23599 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23600 HOST_WIDE_INT offset = 0;
23601 dw_die_ref ref = NULL;
23602 tree decl;
23603
23604 if (GET_CODE (rtl) == CONST
23605 && GET_CODE (XEXP (rtl, 0)) == PLUS
23606 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23607 {
23608 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23609 rtl = XEXP (XEXP (rtl, 0), 0);
23610 }
23611 if (GET_CODE (rtl) == CONST_STRING)
23612 {
23613 size_t len = strlen (XSTR (rtl, 0)) + 1;
23614 tree t = build_string (len, XSTR (rtl, 0));
23615 tree tlen = size_int (len - 1);
23616
23617 TREE_TYPE (t)
23618 = build_array_type (char_type_node, build_index_type (tlen));
23619 rtl = string_cst_pool_decl (t);
23620 if (!rtl)
23621 return false;
23622 }
23623 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23624 {
23625 decl = SYMBOL_REF_DECL (rtl);
23626 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23627 {
23628 ref = lookup_decl_die (decl);
23629 if (ref && (get_AT (ref, DW_AT_location)
23630 || get_AT (ref, DW_AT_const_value)))
23631 {
23632 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23633 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23634 loc->dw_loc_oprnd1.val_entry = NULL;
23635 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23636 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23637 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23638 loc->dw_loc_oprnd2.v.val_int = offset;
23639 return true;
23640 }
23641 }
23642 }
23643 return false;
23644 }
23645
23646 /* Helper function for resolve_addr, handle one location
23647 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23648 the location list couldn't be resolved. */
23649
23650 static bool
23651 resolve_addr_in_expr (dw_loc_descr_ref loc)
23652 {
23653 dw_loc_descr_ref keep = NULL;
23654 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23655 switch (loc->dw_loc_opc)
23656 {
23657 case DW_OP_addr:
23658 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23659 {
23660 if ((prev == NULL
23661 || prev->dw_loc_opc == DW_OP_piece
23662 || prev->dw_loc_opc == DW_OP_bit_piece)
23663 && loc->dw_loc_next
23664 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23665 && !dwarf_strict
23666 && optimize_one_addr_into_implicit_ptr (loc))
23667 break;
23668 return false;
23669 }
23670 break;
23671 case DW_OP_GNU_addr_index:
23672 case DW_OP_GNU_const_index:
23673 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23674 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23675 {
23676 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23677 if (!resolve_one_addr (&rtl))
23678 return false;
23679 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23680 loc->dw_loc_oprnd1.val_entry =
23681 add_addr_table_entry (rtl, ate_kind_rtx);
23682 }
23683 break;
23684 case DW_OP_const4u:
23685 case DW_OP_const8u:
23686 if (loc->dtprel
23687 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23688 return false;
23689 break;
23690 case DW_OP_plus_uconst:
23691 if (size_of_loc_descr (loc)
23692 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23693 + 1
23694 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23695 {
23696 dw_loc_descr_ref repl
23697 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23698 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23699 add_loc_descr (&repl, loc->dw_loc_next);
23700 *loc = *repl;
23701 }
23702 break;
23703 case DW_OP_implicit_value:
23704 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23705 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23706 return false;
23707 break;
23708 case DW_OP_GNU_implicit_pointer:
23709 case DW_OP_GNU_parameter_ref:
23710 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23711 {
23712 dw_die_ref ref
23713 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23714 if (ref == NULL)
23715 return false;
23716 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23717 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23718 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23719 }
23720 break;
23721 case DW_OP_GNU_const_type:
23722 case DW_OP_GNU_regval_type:
23723 case DW_OP_GNU_deref_type:
23724 case DW_OP_GNU_convert:
23725 case DW_OP_GNU_reinterpret:
23726 while (loc->dw_loc_next
23727 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23728 {
23729 dw_die_ref base1, base2;
23730 unsigned enc1, enc2, size1, size2;
23731 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23732 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23733 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23734 else if (loc->dw_loc_oprnd1.val_class
23735 == dw_val_class_unsigned_const)
23736 break;
23737 else
23738 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23739 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23740 == dw_val_class_unsigned_const)
23741 break;
23742 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23743 gcc_assert (base1->die_tag == DW_TAG_base_type
23744 && base2->die_tag == DW_TAG_base_type);
23745 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23746 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23747 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23748 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23749 if (size1 == size2
23750 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23751 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23752 && loc != keep)
23753 || enc1 == enc2))
23754 {
23755 /* Optimize away next DW_OP_GNU_convert after
23756 adjusting LOC's base type die reference. */
23757 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23758 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23759 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23760 else
23761 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23762 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23763 continue;
23764 }
23765 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23766 point typed stack entry. */
23767 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23768 keep = loc->dw_loc_next;
23769 break;
23770 }
23771 break;
23772 default:
23773 break;
23774 }
23775 return true;
23776 }
23777
23778 /* Helper function of resolve_addr. DIE had DW_AT_location of
23779 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23780 and DW_OP_addr couldn't be resolved. resolve_addr has already
23781 removed the DW_AT_location attribute. This function attempts to
23782 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23783 to it or DW_AT_const_value attribute, if possible. */
23784
23785 static void
23786 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23787 {
23788 if (TREE_CODE (decl) != VAR_DECL
23789 || lookup_decl_die (decl) != die
23790 || DECL_EXTERNAL (decl)
23791 || !TREE_STATIC (decl)
23792 || DECL_INITIAL (decl) == NULL_TREE
23793 || DECL_P (DECL_INITIAL (decl))
23794 || get_AT (die, DW_AT_const_value))
23795 return;
23796
23797 tree init = DECL_INITIAL (decl);
23798 HOST_WIDE_INT offset = 0;
23799 /* For variables that have been optimized away and thus
23800 don't have a memory location, see if we can emit
23801 DW_AT_const_value instead. */
23802 if (tree_add_const_value_attribute (die, init))
23803 return;
23804 if (dwarf_strict)
23805 return;
23806 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23807 and ADDR_EXPR refers to a decl that has DW_AT_location or
23808 DW_AT_const_value (but isn't addressable, otherwise
23809 resolving the original DW_OP_addr wouldn't fail), see if
23810 we can add DW_OP_GNU_implicit_pointer. */
23811 STRIP_NOPS (init);
23812 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23813 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23814 {
23815 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23816 init = TREE_OPERAND (init, 0);
23817 STRIP_NOPS (init);
23818 }
23819 if (TREE_CODE (init) != ADDR_EXPR)
23820 return;
23821 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23822 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23823 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23824 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23825 && TREE_OPERAND (init, 0) != decl))
23826 {
23827 dw_die_ref ref;
23828 dw_loc_descr_ref l;
23829
23830 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23831 {
23832 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23833 if (!rtl)
23834 return;
23835 decl = SYMBOL_REF_DECL (rtl);
23836 }
23837 else
23838 decl = TREE_OPERAND (init, 0);
23839 ref = lookup_decl_die (decl);
23840 if (ref == NULL
23841 || (!get_AT (ref, DW_AT_location)
23842 && !get_AT (ref, DW_AT_const_value)))
23843 return;
23844 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23845 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23846 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23847 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23848 add_AT_loc (die, DW_AT_location, l);
23849 }
23850 }
23851
23852 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23853 an address in .rodata section if the string literal is emitted there,
23854 or remove the containing location list or replace DW_AT_const_value
23855 with DW_AT_location and empty location expression, if it isn't found
23856 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23857 to something that has been emitted in the current CU. */
23858
23859 static void
23860 resolve_addr (dw_die_ref die)
23861 {
23862 dw_die_ref c;
23863 dw_attr_ref a;
23864 dw_loc_list_ref *curr, *start, loc;
23865 unsigned ix;
23866
23867 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23868 switch (AT_class (a))
23869 {
23870 case dw_val_class_loc_list:
23871 start = curr = AT_loc_list_ptr (a);
23872 loc = *curr;
23873 gcc_assert (loc);
23874 /* The same list can be referenced more than once. See if we have
23875 already recorded the result from a previous pass. */
23876 if (loc->replaced)
23877 *curr = loc->dw_loc_next;
23878 else if (!loc->resolved_addr)
23879 {
23880 /* As things stand, we do not expect or allow one die to
23881 reference a suffix of another die's location list chain.
23882 References must be identical or completely separate.
23883 There is therefore no need to cache the result of this
23884 pass on any list other than the first; doing so
23885 would lead to unnecessary writes. */
23886 while (*curr)
23887 {
23888 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23889 if (!resolve_addr_in_expr ((*curr)->expr))
23890 {
23891 dw_loc_list_ref next = (*curr)->dw_loc_next;
23892 dw_loc_descr_ref l = (*curr)->expr;
23893
23894 if (next && (*curr)->ll_symbol)
23895 {
23896 gcc_assert (!next->ll_symbol);
23897 next->ll_symbol = (*curr)->ll_symbol;
23898 }
23899 if (dwarf_split_debug_info)
23900 remove_loc_list_addr_table_entries (l);
23901 *curr = next;
23902 }
23903 else
23904 {
23905 mark_base_types ((*curr)->expr);
23906 curr = &(*curr)->dw_loc_next;
23907 }
23908 }
23909 if (loc == *start)
23910 loc->resolved_addr = 1;
23911 else
23912 {
23913 loc->replaced = 1;
23914 loc->dw_loc_next = *start;
23915 }
23916 }
23917 if (!*start)
23918 {
23919 remove_AT (die, a->dw_attr);
23920 ix--;
23921 }
23922 break;
23923 case dw_val_class_loc:
23924 {
23925 dw_loc_descr_ref l = AT_loc (a);
23926 /* For -gdwarf-2 don't attempt to optimize
23927 DW_AT_data_member_location containing
23928 DW_OP_plus_uconst - older consumers might
23929 rely on it being that op instead of a more complex,
23930 but shorter, location description. */
23931 if ((dwarf_version > 2
23932 || a->dw_attr != DW_AT_data_member_location
23933 || l == NULL
23934 || l->dw_loc_opc != DW_OP_plus_uconst
23935 || l->dw_loc_next != NULL)
23936 && !resolve_addr_in_expr (l))
23937 {
23938 if (dwarf_split_debug_info)
23939 remove_loc_list_addr_table_entries (l);
23940 if (l != NULL
23941 && l->dw_loc_next == NULL
23942 && l->dw_loc_opc == DW_OP_addr
23943 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23944 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23945 && a->dw_attr == DW_AT_location)
23946 {
23947 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23948 remove_AT (die, a->dw_attr);
23949 ix--;
23950 optimize_location_into_implicit_ptr (die, decl);
23951 break;
23952 }
23953 remove_AT (die, a->dw_attr);
23954 ix--;
23955 }
23956 else
23957 mark_base_types (l);
23958 }
23959 break;
23960 case dw_val_class_addr:
23961 if (a->dw_attr == DW_AT_const_value
23962 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23963 {
23964 if (AT_index (a) != NOT_INDEXED)
23965 remove_addr_table_entry (a->dw_attr_val.val_entry);
23966 remove_AT (die, a->dw_attr);
23967 ix--;
23968 }
23969 if (die->die_tag == DW_TAG_GNU_call_site
23970 && a->dw_attr == DW_AT_abstract_origin)
23971 {
23972 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23973 dw_die_ref tdie = lookup_decl_die (tdecl);
23974 dw_die_ref cdie;
23975 if (tdie == NULL
23976 && DECL_EXTERNAL (tdecl)
23977 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
23978 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
23979 {
23980 /* Creating a full DIE for tdecl is overly expensive and
23981 at this point even wrong when in the LTO phase
23982 as it can end up generating new type DIEs we didn't
23983 output and thus optimize_external_refs will crash. */
23984 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
23985 add_AT_flag (tdie, DW_AT_external, 1);
23986 add_AT_flag (tdie, DW_AT_declaration, 1);
23987 add_linkage_attr (tdie, tdecl);
23988 add_name_and_src_coords_attributes (tdie, tdecl);
23989 equate_decl_number_to_die (tdecl, tdie);
23990 }
23991 if (tdie)
23992 {
23993 a->dw_attr_val.val_class = dw_val_class_die_ref;
23994 a->dw_attr_val.v.val_die_ref.die = tdie;
23995 a->dw_attr_val.v.val_die_ref.external = 0;
23996 }
23997 else
23998 {
23999 if (AT_index (a) != NOT_INDEXED)
24000 remove_addr_table_entry (a->dw_attr_val.val_entry);
24001 remove_AT (die, a->dw_attr);
24002 ix--;
24003 }
24004 }
24005 break;
24006 default:
24007 break;
24008 }
24009
24010 FOR_EACH_CHILD (die, c, resolve_addr (c));
24011 }
24012 \f
24013 /* Helper routines for optimize_location_lists.
24014 This pass tries to share identical local lists in .debug_loc
24015 section. */
24016
24017 /* Iteratively hash operands of LOC opcode into HSTATE. */
24018
24019 static void
24020 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24021 {
24022 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24023 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24024
24025 switch (loc->dw_loc_opc)
24026 {
24027 case DW_OP_const4u:
24028 case DW_OP_const8u:
24029 if (loc->dtprel)
24030 goto hash_addr;
24031 /* FALLTHRU */
24032 case DW_OP_const1u:
24033 case DW_OP_const1s:
24034 case DW_OP_const2u:
24035 case DW_OP_const2s:
24036 case DW_OP_const4s:
24037 case DW_OP_const8s:
24038 case DW_OP_constu:
24039 case DW_OP_consts:
24040 case DW_OP_pick:
24041 case DW_OP_plus_uconst:
24042 case DW_OP_breg0:
24043 case DW_OP_breg1:
24044 case DW_OP_breg2:
24045 case DW_OP_breg3:
24046 case DW_OP_breg4:
24047 case DW_OP_breg5:
24048 case DW_OP_breg6:
24049 case DW_OP_breg7:
24050 case DW_OP_breg8:
24051 case DW_OP_breg9:
24052 case DW_OP_breg10:
24053 case DW_OP_breg11:
24054 case DW_OP_breg12:
24055 case DW_OP_breg13:
24056 case DW_OP_breg14:
24057 case DW_OP_breg15:
24058 case DW_OP_breg16:
24059 case DW_OP_breg17:
24060 case DW_OP_breg18:
24061 case DW_OP_breg19:
24062 case DW_OP_breg20:
24063 case DW_OP_breg21:
24064 case DW_OP_breg22:
24065 case DW_OP_breg23:
24066 case DW_OP_breg24:
24067 case DW_OP_breg25:
24068 case DW_OP_breg26:
24069 case DW_OP_breg27:
24070 case DW_OP_breg28:
24071 case DW_OP_breg29:
24072 case DW_OP_breg30:
24073 case DW_OP_breg31:
24074 case DW_OP_regx:
24075 case DW_OP_fbreg:
24076 case DW_OP_piece:
24077 case DW_OP_deref_size:
24078 case DW_OP_xderef_size:
24079 hstate.add_object (val1->v.val_int);
24080 break;
24081 case DW_OP_skip:
24082 case DW_OP_bra:
24083 {
24084 int offset;
24085
24086 gcc_assert (val1->val_class == dw_val_class_loc);
24087 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24088 hstate.add_object (offset);
24089 }
24090 break;
24091 case DW_OP_implicit_value:
24092 hstate.add_object (val1->v.val_unsigned);
24093 switch (val2->val_class)
24094 {
24095 case dw_val_class_const:
24096 hstate.add_object (val2->v.val_int);
24097 break;
24098 case dw_val_class_vec:
24099 {
24100 unsigned int elt_size = val2->v.val_vec.elt_size;
24101 unsigned int len = val2->v.val_vec.length;
24102
24103 hstate.add_int (elt_size);
24104 hstate.add_int (len);
24105 hstate.add (val2->v.val_vec.array, len * elt_size);
24106 }
24107 break;
24108 case dw_val_class_const_double:
24109 hstate.add_object (val2->v.val_double.low);
24110 hstate.add_object (val2->v.val_double.high);
24111 break;
24112 case dw_val_class_wide_int:
24113 hstate.add (val2->v.val_wide->get_val (),
24114 get_full_len (*val2->v.val_wide)
24115 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24116 break;
24117 case dw_val_class_addr:
24118 inchash::add_rtx (val2->v.val_addr, hstate);
24119 break;
24120 default:
24121 gcc_unreachable ();
24122 }
24123 break;
24124 case DW_OP_bregx:
24125 case DW_OP_bit_piece:
24126 hstate.add_object (val1->v.val_int);
24127 hstate.add_object (val2->v.val_int);
24128 break;
24129 case DW_OP_addr:
24130 hash_addr:
24131 if (loc->dtprel)
24132 {
24133 unsigned char dtprel = 0xd1;
24134 hstate.add_object (dtprel);
24135 }
24136 inchash::add_rtx (val1->v.val_addr, hstate);
24137 break;
24138 case DW_OP_GNU_addr_index:
24139 case DW_OP_GNU_const_index:
24140 {
24141 if (loc->dtprel)
24142 {
24143 unsigned char dtprel = 0xd1;
24144 hstate.add_object (dtprel);
24145 }
24146 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24147 }
24148 break;
24149 case DW_OP_GNU_implicit_pointer:
24150 hstate.add_int (val2->v.val_int);
24151 break;
24152 case DW_OP_GNU_entry_value:
24153 hstate.add_object (val1->v.val_loc);
24154 break;
24155 case DW_OP_GNU_regval_type:
24156 case DW_OP_GNU_deref_type:
24157 {
24158 unsigned int byte_size
24159 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24160 unsigned int encoding
24161 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24162 hstate.add_object (val1->v.val_int);
24163 hstate.add_object (byte_size);
24164 hstate.add_object (encoding);
24165 }
24166 break;
24167 case DW_OP_GNU_convert:
24168 case DW_OP_GNU_reinterpret:
24169 if (val1->val_class == dw_val_class_unsigned_const)
24170 {
24171 hstate.add_object (val1->v.val_unsigned);
24172 break;
24173 }
24174 /* FALLTHRU */
24175 case DW_OP_GNU_const_type:
24176 {
24177 unsigned int byte_size
24178 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24179 unsigned int encoding
24180 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24181 hstate.add_object (byte_size);
24182 hstate.add_object (encoding);
24183 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24184 break;
24185 hstate.add_object (val2->val_class);
24186 switch (val2->val_class)
24187 {
24188 case dw_val_class_const:
24189 hstate.add_object (val2->v.val_int);
24190 break;
24191 case dw_val_class_vec:
24192 {
24193 unsigned int elt_size = val2->v.val_vec.elt_size;
24194 unsigned int len = val2->v.val_vec.length;
24195
24196 hstate.add_object (elt_size);
24197 hstate.add_object (len);
24198 hstate.add (val2->v.val_vec.array, len * elt_size);
24199 }
24200 break;
24201 case dw_val_class_const_double:
24202 hstate.add_object (val2->v.val_double.low);
24203 hstate.add_object (val2->v.val_double.high);
24204 break;
24205 case dw_val_class_wide_int:
24206 hstate.add (val2->v.val_wide->get_val (),
24207 get_full_len (*val2->v.val_wide)
24208 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24209 break;
24210 default:
24211 gcc_unreachable ();
24212 }
24213 }
24214 break;
24215
24216 default:
24217 /* Other codes have no operands. */
24218 break;
24219 }
24220 }
24221
24222 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24223
24224 static inline void
24225 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24226 {
24227 dw_loc_descr_ref l;
24228 bool sizes_computed = false;
24229 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24230 size_of_locs (loc);
24231
24232 for (l = loc; l != NULL; l = l->dw_loc_next)
24233 {
24234 enum dwarf_location_atom opc = l->dw_loc_opc;
24235 hstate.add_object (opc);
24236 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24237 {
24238 size_of_locs (loc);
24239 sizes_computed = true;
24240 }
24241 hash_loc_operands (l, hstate);
24242 }
24243 }
24244
24245 /* Compute hash of the whole location list LIST_HEAD. */
24246
24247 static inline void
24248 hash_loc_list (dw_loc_list_ref list_head)
24249 {
24250 dw_loc_list_ref curr = list_head;
24251 inchash::hash hstate;
24252
24253 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24254 {
24255 hstate.add (curr->begin, strlen (curr->begin) + 1);
24256 hstate.add (curr->end, strlen (curr->end) + 1);
24257 if (curr->section)
24258 hstate.add (curr->section, strlen (curr->section) + 1);
24259 hash_locs (curr->expr, hstate);
24260 }
24261 list_head->hash = hstate.end ();
24262 }
24263
24264 /* Return true if X and Y opcodes have the same operands. */
24265
24266 static inline bool
24267 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24268 {
24269 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24270 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24271 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24272 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24273
24274 switch (x->dw_loc_opc)
24275 {
24276 case DW_OP_const4u:
24277 case DW_OP_const8u:
24278 if (x->dtprel)
24279 goto hash_addr;
24280 /* FALLTHRU */
24281 case DW_OP_const1u:
24282 case DW_OP_const1s:
24283 case DW_OP_const2u:
24284 case DW_OP_const2s:
24285 case DW_OP_const4s:
24286 case DW_OP_const8s:
24287 case DW_OP_constu:
24288 case DW_OP_consts:
24289 case DW_OP_pick:
24290 case DW_OP_plus_uconst:
24291 case DW_OP_breg0:
24292 case DW_OP_breg1:
24293 case DW_OP_breg2:
24294 case DW_OP_breg3:
24295 case DW_OP_breg4:
24296 case DW_OP_breg5:
24297 case DW_OP_breg6:
24298 case DW_OP_breg7:
24299 case DW_OP_breg8:
24300 case DW_OP_breg9:
24301 case DW_OP_breg10:
24302 case DW_OP_breg11:
24303 case DW_OP_breg12:
24304 case DW_OP_breg13:
24305 case DW_OP_breg14:
24306 case DW_OP_breg15:
24307 case DW_OP_breg16:
24308 case DW_OP_breg17:
24309 case DW_OP_breg18:
24310 case DW_OP_breg19:
24311 case DW_OP_breg20:
24312 case DW_OP_breg21:
24313 case DW_OP_breg22:
24314 case DW_OP_breg23:
24315 case DW_OP_breg24:
24316 case DW_OP_breg25:
24317 case DW_OP_breg26:
24318 case DW_OP_breg27:
24319 case DW_OP_breg28:
24320 case DW_OP_breg29:
24321 case DW_OP_breg30:
24322 case DW_OP_breg31:
24323 case DW_OP_regx:
24324 case DW_OP_fbreg:
24325 case DW_OP_piece:
24326 case DW_OP_deref_size:
24327 case DW_OP_xderef_size:
24328 return valx1->v.val_int == valy1->v.val_int;
24329 case DW_OP_skip:
24330 case DW_OP_bra:
24331 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24332 can cause irrelevant differences in dw_loc_addr. */
24333 gcc_assert (valx1->val_class == dw_val_class_loc
24334 && valy1->val_class == dw_val_class_loc
24335 && (dwarf_split_debug_info
24336 || x->dw_loc_addr == y->dw_loc_addr));
24337 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24338 case DW_OP_implicit_value:
24339 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24340 || valx2->val_class != valy2->val_class)
24341 return false;
24342 switch (valx2->val_class)
24343 {
24344 case dw_val_class_const:
24345 return valx2->v.val_int == valy2->v.val_int;
24346 case dw_val_class_vec:
24347 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24348 && valx2->v.val_vec.length == valy2->v.val_vec.length
24349 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24350 valx2->v.val_vec.elt_size
24351 * valx2->v.val_vec.length) == 0;
24352 case dw_val_class_const_double:
24353 return valx2->v.val_double.low == valy2->v.val_double.low
24354 && valx2->v.val_double.high == valy2->v.val_double.high;
24355 case dw_val_class_wide_int:
24356 return *valx2->v.val_wide == *valy2->v.val_wide;
24357 case dw_val_class_addr:
24358 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24359 default:
24360 gcc_unreachable ();
24361 }
24362 case DW_OP_bregx:
24363 case DW_OP_bit_piece:
24364 return valx1->v.val_int == valy1->v.val_int
24365 && valx2->v.val_int == valy2->v.val_int;
24366 case DW_OP_addr:
24367 hash_addr:
24368 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24369 case DW_OP_GNU_addr_index:
24370 case DW_OP_GNU_const_index:
24371 {
24372 rtx ax1 = valx1->val_entry->addr.rtl;
24373 rtx ay1 = valy1->val_entry->addr.rtl;
24374 return rtx_equal_p (ax1, ay1);
24375 }
24376 case DW_OP_GNU_implicit_pointer:
24377 return valx1->val_class == dw_val_class_die_ref
24378 && valx1->val_class == valy1->val_class
24379 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24380 && valx2->v.val_int == valy2->v.val_int;
24381 case DW_OP_GNU_entry_value:
24382 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24383 case DW_OP_GNU_const_type:
24384 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24385 || valx2->val_class != valy2->val_class)
24386 return false;
24387 switch (valx2->val_class)
24388 {
24389 case dw_val_class_const:
24390 return valx2->v.val_int == valy2->v.val_int;
24391 case dw_val_class_vec:
24392 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24393 && valx2->v.val_vec.length == valy2->v.val_vec.length
24394 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24395 valx2->v.val_vec.elt_size
24396 * valx2->v.val_vec.length) == 0;
24397 case dw_val_class_const_double:
24398 return valx2->v.val_double.low == valy2->v.val_double.low
24399 && valx2->v.val_double.high == valy2->v.val_double.high;
24400 case dw_val_class_wide_int:
24401 return *valx2->v.val_wide == *valy2->v.val_wide;
24402 default:
24403 gcc_unreachable ();
24404 }
24405 case DW_OP_GNU_regval_type:
24406 case DW_OP_GNU_deref_type:
24407 return valx1->v.val_int == valy1->v.val_int
24408 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24409 case DW_OP_GNU_convert:
24410 case DW_OP_GNU_reinterpret:
24411 if (valx1->val_class != valy1->val_class)
24412 return false;
24413 if (valx1->val_class == dw_val_class_unsigned_const)
24414 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24415 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24416 case DW_OP_GNU_parameter_ref:
24417 return valx1->val_class == dw_val_class_die_ref
24418 && valx1->val_class == valy1->val_class
24419 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24420 default:
24421 /* Other codes have no operands. */
24422 return true;
24423 }
24424 }
24425
24426 /* Return true if DWARF location expressions X and Y are the same. */
24427
24428 static inline bool
24429 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24430 {
24431 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24432 if (x->dw_loc_opc != y->dw_loc_opc
24433 || x->dtprel != y->dtprel
24434 || !compare_loc_operands (x, y))
24435 break;
24436 return x == NULL && y == NULL;
24437 }
24438
24439 /* Hashtable helpers. */
24440
24441 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24442 {
24443 typedef dw_loc_list_struct *value_type;
24444 typedef dw_loc_list_struct *compare_type;
24445 static inline hashval_t hash (const dw_loc_list_struct *);
24446 static inline bool equal (const dw_loc_list_struct *,
24447 const dw_loc_list_struct *);
24448 };
24449
24450 /* Return precomputed hash of location list X. */
24451
24452 inline hashval_t
24453 loc_list_hasher::hash (const dw_loc_list_struct *x)
24454 {
24455 return x->hash;
24456 }
24457
24458 /* Return true if location lists A and B are the same. */
24459
24460 inline bool
24461 loc_list_hasher::equal (const dw_loc_list_struct *a,
24462 const dw_loc_list_struct *b)
24463 {
24464 if (a == b)
24465 return 1;
24466 if (a->hash != b->hash)
24467 return 0;
24468 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24469 if (strcmp (a->begin, b->begin) != 0
24470 || strcmp (a->end, b->end) != 0
24471 || (a->section == NULL) != (b->section == NULL)
24472 || (a->section && strcmp (a->section, b->section) != 0)
24473 || !compare_locs (a->expr, b->expr))
24474 break;
24475 return a == NULL && b == NULL;
24476 }
24477
24478 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24479
24480
24481 /* Recursively optimize location lists referenced from DIE
24482 children and share them whenever possible. */
24483
24484 static void
24485 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24486 {
24487 dw_die_ref c;
24488 dw_attr_ref a;
24489 unsigned ix;
24490 dw_loc_list_struct **slot;
24491
24492 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24493 if (AT_class (a) == dw_val_class_loc_list)
24494 {
24495 dw_loc_list_ref list = AT_loc_list (a);
24496 /* TODO: perform some optimizations here, before hashing
24497 it and storing into the hash table. */
24498 hash_loc_list (list);
24499 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24500 if (*slot == NULL)
24501 *slot = list;
24502 else
24503 a->dw_attr_val.v.val_loc_list = *slot;
24504 }
24505
24506 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24507 }
24508
24509
24510 /* Recursively assign each location list a unique index into the debug_addr
24511 section. */
24512
24513 static void
24514 index_location_lists (dw_die_ref die)
24515 {
24516 dw_die_ref c;
24517 dw_attr_ref a;
24518 unsigned ix;
24519
24520 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24521 if (AT_class (a) == dw_val_class_loc_list)
24522 {
24523 dw_loc_list_ref list = AT_loc_list (a);
24524 dw_loc_list_ref curr;
24525 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24526 {
24527 /* Don't index an entry that has already been indexed
24528 or won't be output. */
24529 if (curr->begin_entry != NULL
24530 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24531 continue;
24532
24533 curr->begin_entry
24534 = add_addr_table_entry (xstrdup (curr->begin),
24535 ate_kind_label);
24536 }
24537 }
24538
24539 FOR_EACH_CHILD (die, c, index_location_lists (c));
24540 }
24541
24542 /* Optimize location lists referenced from DIE
24543 children and share them whenever possible. */
24544
24545 static void
24546 optimize_location_lists (dw_die_ref die)
24547 {
24548 loc_list_hash_type htab (500);
24549 optimize_location_lists_1 (die, &htab);
24550 }
24551 \f
24552 /* Output stuff that dwarf requires at the end of every file,
24553 and generate the DWARF-2 debugging info. */
24554
24555 static void
24556 dwarf2out_finish (const char *filename)
24557 {
24558 limbo_die_node *node, *next_node;
24559 comdat_type_node *ctnode;
24560 unsigned int i;
24561 dw_die_ref main_comp_unit_die;
24562
24563 /* PCH might result in DW_AT_producer string being restored from the
24564 header compilation, so always fill it with empty string initially
24565 and overwrite only here. */
24566 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24567 producer_string = gen_producer_string ();
24568 producer->dw_attr_val.v.val_str->refcount--;
24569 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24570
24571 gen_scheduled_generic_parms_dies ();
24572 gen_remaining_tmpl_value_param_die_attribute ();
24573
24574 /* Add the name for the main input file now. We delayed this from
24575 dwarf2out_init to avoid complications with PCH.
24576 For LTO produced units use a fixed artificial name to avoid
24577 leaking tempfile names into the dwarf. */
24578 if (!in_lto_p)
24579 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24580 else
24581 add_name_attribute (comp_unit_die (), "<artificial>");
24582 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24583 add_comp_dir_attribute (comp_unit_die ());
24584 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24585 {
24586 bool p = false;
24587 file_table->traverse<bool *, file_table_relative_p> (&p);
24588 if (p)
24589 add_comp_dir_attribute (comp_unit_die ());
24590 }
24591
24592 if (deferred_locations_list)
24593 for (i = 0; i < deferred_locations_list->length (); i++)
24594 {
24595 add_location_or_const_value_attribute (
24596 (*deferred_locations_list)[i].die,
24597 (*deferred_locations_list)[i].variable,
24598 false,
24599 DW_AT_location);
24600 }
24601
24602 /* Traverse the limbo die list, and add parent/child links. The only
24603 dies without parents that should be here are concrete instances of
24604 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24605 For concrete instances, we can get the parent die from the abstract
24606 instance. */
24607 for (node = limbo_die_list; node; node = next_node)
24608 {
24609 dw_die_ref die = node->die;
24610 next_node = node->next;
24611
24612 if (die->die_parent == NULL)
24613 {
24614 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24615
24616 if (origin && origin->die_parent)
24617 add_child_die (origin->die_parent, die);
24618 else if (is_cu_die (die))
24619 ;
24620 else if (seen_error ())
24621 /* It's OK to be confused by errors in the input. */
24622 add_child_die (comp_unit_die (), die);
24623 else
24624 {
24625 /* In certain situations, the lexical block containing a
24626 nested function can be optimized away, which results
24627 in the nested function die being orphaned. Likewise
24628 with the return type of that nested function. Force
24629 this to be a child of the containing function.
24630
24631 It may happen that even the containing function got fully
24632 inlined and optimized out. In that case we are lost and
24633 assign the empty child. This should not be big issue as
24634 the function is likely unreachable too. */
24635 gcc_assert (node->created_for);
24636
24637 if (DECL_P (node->created_for))
24638 origin = get_context_die (DECL_CONTEXT (node->created_for));
24639 else if (TYPE_P (node->created_for))
24640 origin = scope_die_for (node->created_for, comp_unit_die ());
24641 else
24642 origin = comp_unit_die ();
24643
24644 add_child_die (origin, die);
24645 }
24646 }
24647 }
24648
24649 limbo_die_list = NULL;
24650
24651 #if ENABLE_ASSERT_CHECKING
24652 {
24653 dw_die_ref die = comp_unit_die (), c;
24654 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24655 }
24656 #endif
24657 resolve_addr (comp_unit_die ());
24658 move_marked_base_types ();
24659
24660 for (node = deferred_asm_name; node; node = node->next)
24661 {
24662 tree decl = node->created_for;
24663 /* When generating LTO bytecode we can not generate new assembler
24664 names at this point and all important decls got theirs via
24665 free-lang-data. */
24666 if (((!flag_generate_lto && !flag_generate_offload)
24667 || DECL_ASSEMBLER_NAME_SET_P (decl))
24668 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24669 {
24670 add_linkage_attr (node->die, decl);
24671 move_linkage_attr (node->die);
24672 }
24673 }
24674
24675 deferred_asm_name = NULL;
24676
24677 /* Walk through the list of incomplete types again, trying once more to
24678 emit full debugging info for them. */
24679 retry_incomplete_types ();
24680
24681 if (flag_eliminate_unused_debug_types)
24682 prune_unused_types ();
24683
24684 /* Generate separate COMDAT sections for type DIEs. */
24685 if (use_debug_types)
24686 {
24687 break_out_comdat_types (comp_unit_die ());
24688
24689 /* Each new type_unit DIE was added to the limbo die list when created.
24690 Since these have all been added to comdat_type_list, clear the
24691 limbo die list. */
24692 limbo_die_list = NULL;
24693
24694 /* For each new comdat type unit, copy declarations for incomplete
24695 types to make the new unit self-contained (i.e., no direct
24696 references to the main compile unit). */
24697 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24698 copy_decls_for_unworthy_types (ctnode->root_die);
24699 copy_decls_for_unworthy_types (comp_unit_die ());
24700
24701 /* In the process of copying declarations from one unit to another,
24702 we may have left some declarations behind that are no longer
24703 referenced. Prune them. */
24704 prune_unused_types ();
24705 }
24706
24707 /* Generate separate CUs for each of the include files we've seen.
24708 They will go into limbo_die_list. */
24709 if (flag_eliminate_dwarf2_dups)
24710 break_out_includes (comp_unit_die ());
24711
24712 /* Traverse the DIE's and add add sibling attributes to those DIE's
24713 that have children. */
24714 add_sibling_attributes (comp_unit_die ());
24715 for (node = limbo_die_list; node; node = node->next)
24716 add_sibling_attributes (node->die);
24717 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24718 add_sibling_attributes (ctnode->root_die);
24719
24720 /* When splitting DWARF info, we put some attributes in the
24721 skeleton compile_unit DIE that remains in the .o, while
24722 most attributes go in the DWO compile_unit_die. */
24723 if (dwarf_split_debug_info)
24724 main_comp_unit_die = gen_compile_unit_die (NULL);
24725 else
24726 main_comp_unit_die = comp_unit_die ();
24727
24728 /* Output a terminator label for the .text section. */
24729 switch_to_section (text_section);
24730 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24731 if (cold_text_section)
24732 {
24733 switch_to_section (cold_text_section);
24734 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24735 }
24736
24737 /* We can only use the low/high_pc attributes if all of the code was
24738 in .text. */
24739 if (!have_multiple_function_sections
24740 || (dwarf_version < 3 && dwarf_strict))
24741 {
24742 /* Don't add if the CU has no associated code. */
24743 if (text_section_used)
24744 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24745 text_end_label, true);
24746 }
24747 else
24748 {
24749 unsigned fde_idx;
24750 dw_fde_ref fde;
24751 bool range_list_added = false;
24752
24753 if (text_section_used)
24754 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24755 text_end_label, &range_list_added, true);
24756 if (cold_text_section_used)
24757 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24758 cold_end_label, &range_list_added, true);
24759
24760 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24761 {
24762 if (DECL_IGNORED_P (fde->decl))
24763 continue;
24764 if (!fde->in_std_section)
24765 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24766 fde->dw_fde_end, &range_list_added,
24767 true);
24768 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24769 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24770 fde->dw_fde_second_end, &range_list_added,
24771 true);
24772 }
24773
24774 if (range_list_added)
24775 {
24776 /* We need to give .debug_loc and .debug_ranges an appropriate
24777 "base address". Use zero so that these addresses become
24778 absolute. Historically, we've emitted the unexpected
24779 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24780 Emit both to give time for other tools to adapt. */
24781 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24782 if (! dwarf_strict && dwarf_version < 4)
24783 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24784
24785 add_ranges (NULL);
24786 }
24787 }
24788
24789 if (debug_info_level >= DINFO_LEVEL_TERSE)
24790 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24791 debug_line_section_label);
24792
24793 if (have_macinfo)
24794 add_AT_macptr (comp_unit_die (),
24795 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24796 macinfo_section_label);
24797
24798 if (dwarf_split_debug_info)
24799 {
24800 /* optimize_location_lists calculates the size of the lists,
24801 so index them first, and assign indices to the entries.
24802 Although optimize_location_lists will remove entries from
24803 the table, it only does so for duplicates, and therefore
24804 only reduces ref_counts to 1. */
24805 index_location_lists (comp_unit_die ());
24806
24807 if (addr_index_table != NULL)
24808 {
24809 unsigned int index = 0;
24810 addr_index_table
24811 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24812 (&index);
24813 }
24814 }
24815
24816 if (have_location_lists)
24817 optimize_location_lists (comp_unit_die ());
24818
24819 save_macinfo_strings ();
24820
24821 if (dwarf_split_debug_info)
24822 {
24823 unsigned int index = 0;
24824
24825 /* Add attributes common to skeleton compile_units and
24826 type_units. Because these attributes include strings, it
24827 must be done before freezing the string table. Top-level
24828 skeleton die attrs are added when the skeleton type unit is
24829 created, so ensure it is created by this point. */
24830 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24831 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24832 }
24833
24834 /* Output all of the compilation units. We put the main one last so that
24835 the offsets are available to output_pubnames. */
24836 for (node = limbo_die_list; node; node = node->next)
24837 output_comp_unit (node->die, 0);
24838
24839 hash_table<comdat_type_hasher> comdat_type_table (100);
24840 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24841 {
24842 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24843
24844 /* Don't output duplicate types. */
24845 if (*slot != HTAB_EMPTY_ENTRY)
24846 continue;
24847
24848 /* Add a pointer to the line table for the main compilation unit
24849 so that the debugger can make sense of DW_AT_decl_file
24850 attributes. */
24851 if (debug_info_level >= DINFO_LEVEL_TERSE)
24852 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24853 (!dwarf_split_debug_info
24854 ? debug_line_section_label
24855 : debug_skeleton_line_section_label));
24856
24857 output_comdat_type_unit (ctnode);
24858 *slot = ctnode;
24859 }
24860
24861 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24862 both the main_cu and all skeleton TUs. Making this call unconditional
24863 would end up either adding a second copy of the AT_pubnames attribute, or
24864 requiring a special case in add_top_level_skeleton_die_attrs. */
24865 if (!dwarf_split_debug_info)
24866 add_AT_pubnames (comp_unit_die ());
24867
24868 if (dwarf_split_debug_info)
24869 {
24870 int mark;
24871 unsigned char checksum[16];
24872 struct md5_ctx ctx;
24873
24874 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24875 md5_init_ctx (&ctx);
24876 mark = 0;
24877 die_checksum (comp_unit_die (), &ctx, &mark);
24878 unmark_all_dies (comp_unit_die ());
24879 md5_finish_ctx (&ctx, checksum);
24880
24881 /* Use the first 8 bytes of the checksum as the dwo_id,
24882 and add it to both comp-unit DIEs. */
24883 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24884 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24885
24886 /* Add the base offset of the ranges table to the skeleton
24887 comp-unit DIE. */
24888 if (ranges_table_in_use)
24889 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24890 ranges_section_label);
24891
24892 switch_to_section (debug_addr_section);
24893 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24894 output_addr_table ();
24895 }
24896
24897 /* Output the main compilation unit if non-empty or if .debug_macinfo
24898 or .debug_macro will be emitted. */
24899 output_comp_unit (comp_unit_die (), have_macinfo);
24900
24901 if (dwarf_split_debug_info && info_section_emitted)
24902 output_skeleton_debug_sections (main_comp_unit_die);
24903
24904 /* Output the abbreviation table. */
24905 if (abbrev_die_table_in_use != 1)
24906 {
24907 switch_to_section (debug_abbrev_section);
24908 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24909 output_abbrev_section ();
24910 }
24911
24912 /* Output location list section if necessary. */
24913 if (have_location_lists)
24914 {
24915 /* Output the location lists info. */
24916 switch_to_section (debug_loc_section);
24917 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24918 output_location_lists (comp_unit_die ());
24919 }
24920
24921 output_pubtables ();
24922
24923 /* Output the address range information if a CU (.debug_info section)
24924 was emitted. We output an empty table even if we had no functions
24925 to put in it. This because the consumer has no way to tell the
24926 difference between an empty table that we omitted and failure to
24927 generate a table that would have contained data. */
24928 if (info_section_emitted)
24929 {
24930 unsigned long aranges_length = size_of_aranges ();
24931
24932 switch_to_section (debug_aranges_section);
24933 output_aranges (aranges_length);
24934 }
24935
24936 /* Output ranges section if necessary. */
24937 if (ranges_table_in_use)
24938 {
24939 switch_to_section (debug_ranges_section);
24940 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24941 output_ranges ();
24942 }
24943
24944 /* Have to end the macro section. */
24945 if (have_macinfo)
24946 {
24947 switch_to_section (debug_macinfo_section);
24948 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24949 output_macinfo ();
24950 dw2_asm_output_data (1, 0, "End compilation unit");
24951 }
24952
24953 /* Output the source line correspondence table. We must do this
24954 even if there is no line information. Otherwise, on an empty
24955 translation unit, we will generate a present, but empty,
24956 .debug_info section. IRIX 6.5 `nm' will then complain when
24957 examining the file. This is done late so that any filenames
24958 used by the debug_info section are marked as 'used'. */
24959 switch_to_section (debug_line_section);
24960 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24961 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24962 output_line_info (false);
24963
24964 if (dwarf_split_debug_info && info_section_emitted)
24965 {
24966 switch_to_section (debug_skeleton_line_section);
24967 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24968 output_line_info (true);
24969 }
24970
24971 /* If we emitted any indirect strings, output the string table too. */
24972 if (debug_str_hash || skeleton_debug_str_hash)
24973 output_indirect_strings ();
24974 }
24975
24976 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24977 within the same process. For use by toplev::finalize. */
24978
24979 void
24980 dwarf2out_c_finalize (void)
24981 {
24982 last_var_location_insn = NULL;
24983 cached_next_real_insn = NULL;
24984 used_rtx_array = NULL;
24985 incomplete_types = NULL;
24986 decl_scope_table = NULL;
24987 debug_info_section = NULL;
24988 debug_skeleton_info_section = NULL;
24989 debug_abbrev_section = NULL;
24990 debug_skeleton_abbrev_section = NULL;
24991 debug_aranges_section = NULL;
24992 debug_addr_section = NULL;
24993 debug_macinfo_section = NULL;
24994 debug_line_section = NULL;
24995 debug_skeleton_line_section = NULL;
24996 debug_loc_section = NULL;
24997 debug_pubnames_section = NULL;
24998 debug_pubtypes_section = NULL;
24999 debug_str_section = NULL;
25000 debug_str_dwo_section = NULL;
25001 debug_str_offsets_section = NULL;
25002 debug_ranges_section = NULL;
25003 debug_frame_section = NULL;
25004 fde_vec = NULL;
25005 debug_str_hash = NULL;
25006 skeleton_debug_str_hash = NULL;
25007 dw2_string_counter = 0;
25008 have_multiple_function_sections = false;
25009 text_section_used = false;
25010 cold_text_section_used = false;
25011 cold_text_section = NULL;
25012 current_unit_personality = NULL;
25013
25014 deferred_locations_list = NULL;
25015
25016 next_die_offset = 0;
25017 single_comp_unit_die = NULL;
25018 comdat_type_list = NULL;
25019 limbo_die_list = NULL;
25020 deferred_asm_name = NULL;
25021 file_table = NULL;
25022 decl_die_table = NULL;
25023 common_block_die_table = NULL;
25024 decl_loc_table = NULL;
25025 call_arg_locations = NULL;
25026 call_arg_loc_last = NULL;
25027 call_site_count = -1;
25028 tail_call_site_count = -1;
25029 cached_dw_loc_list_table = NULL;
25030 abbrev_die_table = NULL;
25031 abbrev_die_table_allocated = 0;
25032 abbrev_die_table_in_use = 0;
25033 line_info_label_num = 0;
25034 cur_line_info_table = NULL;
25035 text_section_line_info = NULL;
25036 cold_text_section_line_info = NULL;
25037 separate_line_info = NULL;
25038 info_section_emitted = false;
25039 pubname_table = NULL;
25040 pubtype_table = NULL;
25041 macinfo_table = NULL;
25042 ranges_table = NULL;
25043 ranges_table_allocated = 0;
25044 ranges_table_in_use = 0;
25045 ranges_by_label = 0;
25046 ranges_by_label_allocated = 0;
25047 ranges_by_label_in_use = 0;
25048 have_location_lists = false;
25049 loclabel_num = 0;
25050 poc_label_num = 0;
25051 last_emitted_file = NULL;
25052 label_num = 0;
25053 file_table_last_lookup = NULL;
25054 tmpl_value_parm_die_table = NULL;
25055 generic_type_instances = NULL;
25056 frame_pointer_fb_offset = 0;
25057 frame_pointer_fb_offset_valid = false;
25058 base_types.release ();
25059 XDELETEVEC (producer_string);
25060 producer_string = NULL;
25061 }
25062
25063 #include "gt-dwarf2out.h"