dearly: Emit DIEs for decls early in the compilation process.
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
34
35 /* DWARF2 Abbreviation Glossary:
36
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
42
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
45
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
48
49 DIE = Debugging Information Entry
50
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
54
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "function.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "except.h"
81 #include "dwarf2.h"
82 #include "dwarf2out.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "cgraph.h"
94 #include "input.h"
95 #include "ira.h"
96 #include "lra.h"
97 #include "dumpfile.h"
98 #include "opts.h"
99 #include "tree-dfa.h"
100 #include "gdb/gdb-index.h"
101 #include "rtl-iter.h"
102
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx_insn *last_var_location_insn;
105 static rtx_insn *cached_next_real_insn;
106 static void dwarf2out_decl (tree);
107
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
123
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
131
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
136
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
146
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
172
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
175
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
180
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
183
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
187
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
199
200
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
205
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207
208 struct GTY(()) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
214 };
215
216 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
217
218 /* With split_debug_info, both the comp_dir and dwo_name go in the
219 main object file, rather than the dwo, similar to the force_direct
220 parameter elsewhere but with additional complications:
221
222 1) The string is needed in both the main object file and the dwo.
223 That is, the comp_dir and dwo_name will appear in both places.
224
225 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
226 DW_FORM_GNU_str_index.
227
228 3) GCC chooses the form to use late, depending on the size and
229 reference count.
230
231 Rather than forcing the all debug string handling functions and
232 callers to deal with these complications, simply use a separate,
233 special-cased string table for any attribute that should go in the
234 main object file. This limits the complexity to just the places
235 that need it. */
236
237 static GTY ((param_is (struct indirect_string_node)))
238 htab_t skeleton_debug_str_hash;
239
240 static GTY(()) int dw2_string_counter;
241
242 /* True if the compilation unit places functions in more than one section. */
243 static GTY(()) bool have_multiple_function_sections = false;
244
245 /* Whether the default text and cold text sections have been used at all. */
246
247 static GTY(()) bool text_section_used = false;
248 static GTY(()) bool cold_text_section_used = false;
249
250 /* The default cold text section. */
251 static GTY(()) section *cold_text_section;
252
253 /* The DIE for C++14 'auto' in a function return type. */
254 static GTY(()) dw_die_ref auto_die;
255
256 /* The DIE for C++14 'decltype(auto)' in a function return type. */
257 static GTY(()) dw_die_ref decltype_auto_die;
258
259 /* Forward declarations for functions defined in this file. */
260
261 static char *stripattributes (const char *);
262 static void output_call_frame_info (int);
263 static void dwarf2out_note_section_used (void);
264
265 /* Personality decl of current unit. Used only when assembler does not support
266 personality CFI. */
267 static GTY(()) rtx current_unit_personality;
268
269 /* Data and reference forms for relocatable data. */
270 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
271 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
272
273 #ifndef DEBUG_FRAME_SECTION
274 #define DEBUG_FRAME_SECTION ".debug_frame"
275 #endif
276
277 #ifndef FUNC_BEGIN_LABEL
278 #define FUNC_BEGIN_LABEL "LFB"
279 #endif
280
281 #ifndef FUNC_END_LABEL
282 #define FUNC_END_LABEL "LFE"
283 #endif
284
285 #ifndef PROLOGUE_END_LABEL
286 #define PROLOGUE_END_LABEL "LPE"
287 #endif
288
289 #ifndef EPILOGUE_BEGIN_LABEL
290 #define EPILOGUE_BEGIN_LABEL "LEB"
291 #endif
292
293 #ifndef FRAME_BEGIN_LABEL
294 #define FRAME_BEGIN_LABEL "Lframe"
295 #endif
296 #define CIE_AFTER_SIZE_LABEL "LSCIE"
297 #define CIE_END_LABEL "LECIE"
298 #define FDE_LABEL "LSFDE"
299 #define FDE_AFTER_SIZE_LABEL "LASFDE"
300 #define FDE_END_LABEL "LEFDE"
301 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
302 #define LINE_NUMBER_END_LABEL "LELT"
303 #define LN_PROLOG_AS_LABEL "LASLTP"
304 #define LN_PROLOG_END_LABEL "LELTP"
305 #define DIE_LABEL_PREFIX "DW"
306 \f
307 /* Match the base name of a file to the base name of a compilation unit. */
308
309 static int
310 matches_main_base (const char *path)
311 {
312 /* Cache the last query. */
313 static const char *last_path = NULL;
314 static int last_match = 0;
315 if (path != last_path)
316 {
317 const char *base;
318 int length = base_of_path (path, &base);
319 last_path = path;
320 last_match = (length == main_input_baselength
321 && memcmp (base, main_input_basename, length) == 0);
322 }
323 return last_match;
324 }
325
326 #ifdef DEBUG_DEBUG_STRUCT
327
328 static int
329 dump_struct_debug (tree type, enum debug_info_usage usage,
330 enum debug_struct_file criterion, int generic,
331 int matches, int result)
332 {
333 /* Find the type name. */
334 tree type_decl = TYPE_STUB_DECL (type);
335 tree t = type_decl;
336 const char *name = 0;
337 if (TREE_CODE (t) == TYPE_DECL)
338 t = DECL_NAME (t);
339 if (t)
340 name = IDENTIFIER_POINTER (t);
341
342 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
343 criterion,
344 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
345 matches ? "bas" : "hdr",
346 generic ? "gen" : "ord",
347 usage == DINFO_USAGE_DFN ? ";" :
348 usage == DINFO_USAGE_DIR_USE ? "." : "*",
349 result,
350 (void*) type_decl, name);
351 return result;
352 }
353 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
354 dump_struct_debug (type, usage, criterion, generic, matches, result)
355
356 #else
357
358 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
359 (result)
360
361 #endif
362
363 /* Get the number of HOST_WIDE_INTs needed to represent the precision
364 of the number. */
365
366 static unsigned int
367 get_full_len (const wide_int &op)
368 {
369 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
370 / HOST_BITS_PER_WIDE_INT);
371 }
372
373 static bool
374 should_emit_struct_debug (tree type, enum debug_info_usage usage)
375 {
376 enum debug_struct_file criterion;
377 tree type_decl;
378 bool generic = lang_hooks.types.generic_p (type);
379
380 if (generic)
381 criterion = debug_struct_generic[usage];
382 else
383 criterion = debug_struct_ordinary[usage];
384
385 if (criterion == DINFO_STRUCT_FILE_NONE)
386 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
387 if (criterion == DINFO_STRUCT_FILE_ANY)
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
389
390 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
391
392 if (type_decl != NULL)
393 {
394 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
396
397 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
398 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
399 }
400
401 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
402 }
403 \f
404 /* Return a pointer to a copy of the section string name S with all
405 attributes stripped off, and an asterisk prepended (for assemble_name). */
406
407 static inline char *
408 stripattributes (const char *s)
409 {
410 char *stripped = XNEWVEC (char, strlen (s) + 2);
411 char *p = stripped;
412
413 *p++ = '*';
414
415 while (*s && *s != ',')
416 *p++ = *s++;
417
418 *p = '\0';
419 return stripped;
420 }
421
422 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
423 switch to the data section instead, and write out a synthetic start label
424 for collect2 the first time around. */
425
426 static void
427 switch_to_eh_frame_section (bool back)
428 {
429 tree label;
430
431 #ifdef EH_FRAME_SECTION_NAME
432 if (eh_frame_section == 0)
433 {
434 int flags;
435
436 if (EH_TABLES_CAN_BE_READ_ONLY)
437 {
438 int fde_encoding;
439 int per_encoding;
440 int lsda_encoding;
441
442 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
443 /*global=*/0);
444 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
445 /*global=*/1);
446 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
447 /*global=*/0);
448 flags = ((! flag_pic
449 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
450 && (fde_encoding & 0x70) != DW_EH_PE_aligned
451 && (per_encoding & 0x70) != DW_EH_PE_absptr
452 && (per_encoding & 0x70) != DW_EH_PE_aligned
453 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
454 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
455 ? 0 : SECTION_WRITE);
456 }
457 else
458 flags = SECTION_WRITE;
459 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
460 }
461 #endif /* EH_FRAME_SECTION_NAME */
462
463 if (eh_frame_section)
464 switch_to_section (eh_frame_section);
465 else
466 {
467 /* We have no special eh_frame section. Put the information in
468 the data section and emit special labels to guide collect2. */
469 switch_to_section (data_section);
470
471 if (!back)
472 {
473 label = get_file_function_name ("F");
474 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
475 targetm.asm_out.globalize_label (asm_out_file,
476 IDENTIFIER_POINTER (label));
477 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
478 }
479 }
480 }
481
482 /* Switch [BACK] to the eh or debug frame table section, depending on
483 FOR_EH. */
484
485 static void
486 switch_to_frame_table_section (int for_eh, bool back)
487 {
488 if (for_eh)
489 switch_to_eh_frame_section (back);
490 else
491 {
492 if (!debug_frame_section)
493 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
494 SECTION_DEBUG, NULL);
495 switch_to_section (debug_frame_section);
496 }
497 }
498
499 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
500
501 enum dw_cfi_oprnd_type
502 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
503 {
504 switch (cfi)
505 {
506 case DW_CFA_nop:
507 case DW_CFA_GNU_window_save:
508 case DW_CFA_remember_state:
509 case DW_CFA_restore_state:
510 return dw_cfi_oprnd_unused;
511
512 case DW_CFA_set_loc:
513 case DW_CFA_advance_loc1:
514 case DW_CFA_advance_loc2:
515 case DW_CFA_advance_loc4:
516 case DW_CFA_MIPS_advance_loc8:
517 return dw_cfi_oprnd_addr;
518
519 case DW_CFA_offset:
520 case DW_CFA_offset_extended:
521 case DW_CFA_def_cfa:
522 case DW_CFA_offset_extended_sf:
523 case DW_CFA_def_cfa_sf:
524 case DW_CFA_restore:
525 case DW_CFA_restore_extended:
526 case DW_CFA_undefined:
527 case DW_CFA_same_value:
528 case DW_CFA_def_cfa_register:
529 case DW_CFA_register:
530 case DW_CFA_expression:
531 return dw_cfi_oprnd_reg_num;
532
533 case DW_CFA_def_cfa_offset:
534 case DW_CFA_GNU_args_size:
535 case DW_CFA_def_cfa_offset_sf:
536 return dw_cfi_oprnd_offset;
537
538 case DW_CFA_def_cfa_expression:
539 return dw_cfi_oprnd_loc;
540
541 default:
542 gcc_unreachable ();
543 }
544 }
545
546 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547
548 enum dw_cfi_oprnd_type
549 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 {
551 switch (cfi)
552 {
553 case DW_CFA_def_cfa:
554 case DW_CFA_def_cfa_sf:
555 case DW_CFA_offset:
556 case DW_CFA_offset_extended_sf:
557 case DW_CFA_offset_extended:
558 return dw_cfi_oprnd_offset;
559
560 case DW_CFA_register:
561 return dw_cfi_oprnd_reg_num;
562
563 case DW_CFA_expression:
564 return dw_cfi_oprnd_loc;
565
566 default:
567 return dw_cfi_oprnd_unused;
568 }
569 }
570
571 /* Output one FDE. */
572
573 static void
574 output_fde (dw_fde_ref fde, bool for_eh, bool second,
575 char *section_start_label, int fde_encoding, char *augmentation,
576 bool any_lsda_needed, int lsda_encoding)
577 {
578 const char *begin, *end;
579 static unsigned int j;
580 char l1[20], l2[20];
581
582 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
583 /* empty */ 0);
584 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
585 for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
588 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
589 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
590 " indicating 64-bit DWARF extension");
591 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
592 "FDE Length");
593 ASM_OUTPUT_LABEL (asm_out_file, l1);
594
595 if (for_eh)
596 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
597 else
598 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
599 debug_frame_section, "FDE CIE offset");
600
601 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
602 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
603
604 if (for_eh)
605 {
606 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
607 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
608 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
609 "FDE initial location");
610 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
611 end, begin, "FDE address range");
612 }
613 else
614 {
615 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
616 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
617 }
618
619 if (augmentation[0])
620 {
621 if (any_lsda_needed)
622 {
623 int size = size_of_encoded_value (lsda_encoding);
624
625 if (lsda_encoding == DW_EH_PE_aligned)
626 {
627 int offset = ( 4 /* Length */
628 + 4 /* CIE offset */
629 + 2 * size_of_encoded_value (fde_encoding)
630 + 1 /* Augmentation size */ );
631 int pad = -offset & (PTR_SIZE - 1);
632
633 size += pad;
634 gcc_assert (size_of_uleb128 (size) == 1);
635 }
636
637 dw2_asm_output_data_uleb128 (size, "Augmentation size");
638
639 if (fde->uses_eh_lsda)
640 {
641 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
642 fde->funcdef_number);
643 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
644 gen_rtx_SYMBOL_REF (Pmode, l1),
645 false,
646 "Language Specific Data Area");
647 }
648 else
649 {
650 if (lsda_encoding == DW_EH_PE_aligned)
651 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
652 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
653 "Language Specific Data Area (none)");
654 }
655 }
656 else
657 dw2_asm_output_data_uleb128 (0, "Augmentation size");
658 }
659
660 /* Loop through the Call Frame Instructions associated with this FDE. */
661 fde->dw_fde_current_label = begin;
662 {
663 size_t from, until, i;
664
665 from = 0;
666 until = vec_safe_length (fde->dw_fde_cfi);
667
668 if (fde->dw_fde_second_begin == NULL)
669 ;
670 else if (!second)
671 until = fde->dw_fde_switch_cfi_index;
672 else
673 from = fde->dw_fde_switch_cfi_index;
674
675 for (i = from; i < until; i++)
676 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
677 }
678
679 /* If we are to emit a ref/link from function bodies to their frame tables,
680 do it now. This is typically performed to make sure that tables
681 associated with functions are dragged with them and not discarded in
682 garbage collecting links. We need to do this on a per function basis to
683 cope with -ffunction-sections. */
684
685 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
686 /* Switch to the function section, emit the ref to the tables, and
687 switch *back* into the table section. */
688 switch_to_section (function_section (fde->decl));
689 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
690 switch_to_frame_table_section (for_eh, true);
691 #endif
692
693 /* Pad the FDE out to an address sized boundary. */
694 ASM_OUTPUT_ALIGN (asm_out_file,
695 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
696 ASM_OUTPUT_LABEL (asm_out_file, l2);
697
698 j += 2;
699 }
700
701 /* Return true if frame description entry FDE is needed for EH. */
702
703 static bool
704 fde_needed_for_eh_p (dw_fde_ref fde)
705 {
706 if (flag_asynchronous_unwind_tables)
707 return true;
708
709 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
710 return true;
711
712 if (fde->uses_eh_lsda)
713 return true;
714
715 /* If exceptions are enabled, we have collected nothrow info. */
716 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
717 return false;
718
719 return true;
720 }
721
722 /* Output the call frame information used to record information
723 that relates to calculating the frame pointer, and records the
724 location of saved registers. */
725
726 static void
727 output_call_frame_info (int for_eh)
728 {
729 unsigned int i;
730 dw_fde_ref fde;
731 dw_cfi_ref cfi;
732 char l1[20], l2[20], section_start_label[20];
733 bool any_lsda_needed = false;
734 char augmentation[6];
735 int augmentation_size;
736 int fde_encoding = DW_EH_PE_absptr;
737 int per_encoding = DW_EH_PE_absptr;
738 int lsda_encoding = DW_EH_PE_absptr;
739 int return_reg;
740 rtx personality = NULL;
741 int dw_cie_version;
742
743 /* Don't emit a CIE if there won't be any FDEs. */
744 if (!fde_vec)
745 return;
746
747 /* Nothing to do if the assembler's doing it all. */
748 if (dwarf2out_do_cfi_asm ())
749 return;
750
751 /* If we don't have any functions we'll want to unwind out of, don't emit
752 any EH unwind information. If we make FDEs linkonce, we may have to
753 emit an empty label for an FDE that wouldn't otherwise be emitted. We
754 want to avoid having an FDE kept around when the function it refers to
755 is discarded. Example where this matters: a primary function template
756 in C++ requires EH information, an explicit specialization doesn't. */
757 if (for_eh)
758 {
759 bool any_eh_needed = false;
760
761 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
762 {
763 if (fde->uses_eh_lsda)
764 any_eh_needed = any_lsda_needed = true;
765 else if (fde_needed_for_eh_p (fde))
766 any_eh_needed = true;
767 else if (TARGET_USES_WEAK_UNWIND_INFO)
768 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
769 }
770
771 if (!any_eh_needed)
772 return;
773 }
774
775 /* We're going to be generating comments, so turn on app. */
776 if (flag_debug_asm)
777 app_enable ();
778
779 /* Switch to the proper frame section, first time. */
780 switch_to_frame_table_section (for_eh, false);
781
782 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
783 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
784
785 /* Output the CIE. */
786 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
787 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
788 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
789 dw2_asm_output_data (4, 0xffffffff,
790 "Initial length escape value indicating 64-bit DWARF extension");
791 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
792 "Length of Common Information Entry");
793 ASM_OUTPUT_LABEL (asm_out_file, l1);
794
795 /* Now that the CIE pointer is PC-relative for EH,
796 use 0 to identify the CIE. */
797 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
798 (for_eh ? 0 : DWARF_CIE_ID),
799 "CIE Identifier Tag");
800
801 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
802 use CIE version 1, unless that would produce incorrect results
803 due to overflowing the return register column. */
804 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
805 dw_cie_version = 1;
806 if (return_reg >= 256 || dwarf_version > 2)
807 dw_cie_version = 3;
808 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
809
810 augmentation[0] = 0;
811 augmentation_size = 0;
812
813 personality = current_unit_personality;
814 if (for_eh)
815 {
816 char *p;
817
818 /* Augmentation:
819 z Indicates that a uleb128 is present to size the
820 augmentation section.
821 L Indicates the encoding (and thus presence) of
822 an LSDA pointer in the FDE augmentation.
823 R Indicates a non-default pointer encoding for
824 FDE code pointers.
825 P Indicates the presence of an encoding + language
826 personality routine in the CIE augmentation. */
827
828 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
829 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
830 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
831
832 p = augmentation + 1;
833 if (personality)
834 {
835 *p++ = 'P';
836 augmentation_size += 1 + size_of_encoded_value (per_encoding);
837 assemble_external_libcall (personality);
838 }
839 if (any_lsda_needed)
840 {
841 *p++ = 'L';
842 augmentation_size += 1;
843 }
844 if (fde_encoding != DW_EH_PE_absptr)
845 {
846 *p++ = 'R';
847 augmentation_size += 1;
848 }
849 if (p > augmentation + 1)
850 {
851 augmentation[0] = 'z';
852 *p = '\0';
853 }
854
855 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
856 if (personality && per_encoding == DW_EH_PE_aligned)
857 {
858 int offset = ( 4 /* Length */
859 + 4 /* CIE Id */
860 + 1 /* CIE version */
861 + strlen (augmentation) + 1 /* Augmentation */
862 + size_of_uleb128 (1) /* Code alignment */
863 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
864 + 1 /* RA column */
865 + 1 /* Augmentation size */
866 + 1 /* Personality encoding */ );
867 int pad = -offset & (PTR_SIZE - 1);
868
869 augmentation_size += pad;
870
871 /* Augmentations should be small, so there's scarce need to
872 iterate for a solution. Die if we exceed one uleb128 byte. */
873 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
874 }
875 }
876
877 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
878 if (dw_cie_version >= 4)
879 {
880 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
881 dw2_asm_output_data (1, 0, "CIE Segment Size");
882 }
883 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
884 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
885 "CIE Data Alignment Factor");
886
887 if (dw_cie_version == 1)
888 dw2_asm_output_data (1, return_reg, "CIE RA Column");
889 else
890 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
891
892 if (augmentation[0])
893 {
894 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
895 if (personality)
896 {
897 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
898 eh_data_format_name (per_encoding));
899 dw2_asm_output_encoded_addr_rtx (per_encoding,
900 personality,
901 true, NULL);
902 }
903
904 if (any_lsda_needed)
905 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
906 eh_data_format_name (lsda_encoding));
907
908 if (fde_encoding != DW_EH_PE_absptr)
909 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
910 eh_data_format_name (fde_encoding));
911 }
912
913 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
914 output_cfi (cfi, NULL, for_eh);
915
916 /* Pad the CIE out to an address sized boundary. */
917 ASM_OUTPUT_ALIGN (asm_out_file,
918 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
919 ASM_OUTPUT_LABEL (asm_out_file, l2);
920
921 /* Loop through all of the FDE's. */
922 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
923 {
924 unsigned int k;
925
926 /* Don't emit EH unwind info for leaf functions that don't need it. */
927 if (for_eh && !fde_needed_for_eh_p (fde))
928 continue;
929
930 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
931 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
932 augmentation, any_lsda_needed, lsda_encoding);
933 }
934
935 if (for_eh && targetm.terminate_dw2_eh_frame_info)
936 dw2_asm_output_data (4, 0, "End of Table");
937
938 /* Turn off app to make assembly quicker. */
939 if (flag_debug_asm)
940 app_disable ();
941 }
942
943 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
944
945 static void
946 dwarf2out_do_cfi_startproc (bool second)
947 {
948 int enc;
949 rtx ref;
950 rtx personality = get_personality_function (current_function_decl);
951
952 fprintf (asm_out_file, "\t.cfi_startproc\n");
953
954 if (personality)
955 {
956 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
957 ref = personality;
958
959 /* ??? The GAS support isn't entirely consistent. We have to
960 handle indirect support ourselves, but PC-relative is done
961 in the assembler. Further, the assembler can't handle any
962 of the weirder relocation types. */
963 if (enc & DW_EH_PE_indirect)
964 ref = dw2_force_const_mem (ref, true);
965
966 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
967 output_addr_const (asm_out_file, ref);
968 fputc ('\n', asm_out_file);
969 }
970
971 if (crtl->uses_eh_lsda)
972 {
973 char lab[20];
974
975 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
976 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
977 current_function_funcdef_no);
978 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
979 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
980
981 if (enc & DW_EH_PE_indirect)
982 ref = dw2_force_const_mem (ref, true);
983
984 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
985 output_addr_const (asm_out_file, ref);
986 fputc ('\n', asm_out_file);
987 }
988 }
989
990 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
991 this allocation may be done before pass_final. */
992
993 dw_fde_ref
994 dwarf2out_alloc_current_fde (void)
995 {
996 dw_fde_ref fde;
997
998 fde = ggc_cleared_alloc<dw_fde_node> ();
999 fde->decl = current_function_decl;
1000 fde->funcdef_number = current_function_funcdef_no;
1001 fde->fde_index = vec_safe_length (fde_vec);
1002 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1003 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1004 fde->nothrow = crtl->nothrow;
1005 fde->drap_reg = INVALID_REGNUM;
1006 fde->vdrap_reg = INVALID_REGNUM;
1007
1008 /* Record the FDE associated with this function. */
1009 cfun->fde = fde;
1010 vec_safe_push (fde_vec, fde);
1011
1012 return fde;
1013 }
1014
1015 /* Output a marker (i.e. a label) for the beginning of a function, before
1016 the prologue. */
1017
1018 void
1019 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1020 const char *file ATTRIBUTE_UNUSED)
1021 {
1022 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1023 char * dup_label;
1024 dw_fde_ref fde;
1025 section *fnsec;
1026 bool do_frame;
1027
1028 current_function_func_begin_label = NULL;
1029
1030 do_frame = dwarf2out_do_frame ();
1031
1032 /* ??? current_function_func_begin_label is also used by except.c for
1033 call-site information. We must emit this label if it might be used. */
1034 if (!do_frame
1035 && (!flag_exceptions
1036 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1037 return;
1038
1039 fnsec = function_section (current_function_decl);
1040 switch_to_section (fnsec);
1041 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1042 current_function_funcdef_no);
1043 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1044 current_function_funcdef_no);
1045 dup_label = xstrdup (label);
1046 current_function_func_begin_label = dup_label;
1047
1048 /* We can elide the fde allocation if we're not emitting debug info. */
1049 if (!do_frame)
1050 return;
1051
1052 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1053 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1054 would include pass_dwarf2_frame. If we've not created the FDE yet,
1055 do so now. */
1056 fde = cfun->fde;
1057 if (fde == NULL)
1058 fde = dwarf2out_alloc_current_fde ();
1059
1060 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1061 fde->dw_fde_begin = dup_label;
1062 fde->dw_fde_current_label = dup_label;
1063 fde->in_std_section = (fnsec == text_section
1064 || (cold_text_section && fnsec == cold_text_section));
1065
1066 /* We only want to output line number information for the genuine dwarf2
1067 prologue case, not the eh frame case. */
1068 #ifdef DWARF2_DEBUGGING_INFO
1069 if (file)
1070 dwarf2out_source_line (line, file, 0, true);
1071 #endif
1072
1073 if (dwarf2out_do_cfi_asm ())
1074 dwarf2out_do_cfi_startproc (false);
1075 else
1076 {
1077 rtx personality = get_personality_function (current_function_decl);
1078 if (!current_unit_personality)
1079 current_unit_personality = personality;
1080
1081 /* We cannot keep a current personality per function as without CFI
1082 asm, at the point where we emit the CFI data, there is no current
1083 function anymore. */
1084 if (personality && current_unit_personality != personality)
1085 sorry ("multiple EH personalities are supported only with assemblers "
1086 "supporting .cfi_personality directive");
1087 }
1088 }
1089
1090 /* Output a marker (i.e. a label) for the end of the generated code
1091 for a function prologue. This gets called *after* the prologue code has
1092 been generated. */
1093
1094 void
1095 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1096 const char *file ATTRIBUTE_UNUSED)
1097 {
1098 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1099
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1105 current_function_funcdef_no);
1106 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1107 }
1108
1109 /* Output a marker (i.e. a label) for the beginning of the generated code
1110 for a function epilogue. This gets called *before* the prologue code has
1111 been generated. */
1112
1113 void
1114 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1115 const char *file ATTRIBUTE_UNUSED)
1116 {
1117 dw_fde_ref fde = cfun->fde;
1118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1119
1120 if (fde->dw_fde_vms_begin_epilogue)
1121 return;
1122
1123 /* Output a label to mark the endpoint of the code generated for this
1124 function. */
1125 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1126 current_function_funcdef_no);
1127 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1128 current_function_funcdef_no);
1129 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1130 }
1131
1132 /* Output a marker (i.e. a label) for the absolute end of the generated code
1133 for a function definition. This gets called *after* the epilogue code has
1134 been generated. */
1135
1136 void
1137 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1138 const char *file ATTRIBUTE_UNUSED)
1139 {
1140 dw_fde_ref fde;
1141 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1142
1143 last_var_location_insn = NULL;
1144 cached_next_real_insn = NULL;
1145
1146 if (dwarf2out_do_cfi_asm ())
1147 fprintf (asm_out_file, "\t.cfi_endproc\n");
1148
1149 /* Output a label to mark the endpoint of the code generated for this
1150 function. */
1151 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1152 current_function_funcdef_no);
1153 ASM_OUTPUT_LABEL (asm_out_file, label);
1154 fde = cfun->fde;
1155 gcc_assert (fde != NULL);
1156 if (fde->dw_fde_second_begin == NULL)
1157 fde->dw_fde_end = xstrdup (label);
1158 }
1159
1160 void
1161 dwarf2out_frame_finish (void)
1162 {
1163 /* Output call frame information. */
1164 if (targetm.debug_unwind_info () == UI_DWARF2)
1165 output_call_frame_info (0);
1166
1167 /* Output another copy for the unwinder. */
1168 if ((flag_unwind_tables || flag_exceptions)
1169 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1170 output_call_frame_info (1);
1171 }
1172
1173 /* Note that the current function section is being used for code. */
1174
1175 static void
1176 dwarf2out_note_section_used (void)
1177 {
1178 section *sec = current_function_section ();
1179 if (sec == text_section)
1180 text_section_used = true;
1181 else if (sec == cold_text_section)
1182 cold_text_section_used = true;
1183 }
1184
1185 static void var_location_switch_text_section (void);
1186 static void set_cur_line_info_table (section *);
1187
1188 void
1189 dwarf2out_switch_text_section (void)
1190 {
1191 section *sect;
1192 dw_fde_ref fde = cfun->fde;
1193
1194 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1195
1196 if (!in_cold_section_p)
1197 {
1198 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1199 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1200 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1201 }
1202 else
1203 {
1204 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1205 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1206 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1207 }
1208 have_multiple_function_sections = true;
1209
1210 /* There is no need to mark used sections when not debugging. */
1211 if (cold_text_section != NULL)
1212 dwarf2out_note_section_used ();
1213
1214 if (dwarf2out_do_cfi_asm ())
1215 fprintf (asm_out_file, "\t.cfi_endproc\n");
1216
1217 /* Now do the real section switch. */
1218 sect = current_function_section ();
1219 switch_to_section (sect);
1220
1221 fde->second_in_std_section
1222 = (sect == text_section
1223 || (cold_text_section && sect == cold_text_section));
1224
1225 if (dwarf2out_do_cfi_asm ())
1226 dwarf2out_do_cfi_startproc (true);
1227
1228 var_location_switch_text_section ();
1229
1230 if (cold_text_section != NULL)
1231 set_cur_line_info_table (sect);
1232 }
1233 \f
1234 /* And now, the subset of the debugging information support code necessary
1235 for emitting location expressions. */
1236
1237 /* Data about a single source file. */
1238 struct GTY(()) dwarf_file_data {
1239 const char * filename;
1240 int emitted_number;
1241 };
1242
1243 typedef struct GTY(()) deferred_locations_struct
1244 {
1245 tree variable;
1246 dw_die_ref die;
1247 } deferred_locations;
1248
1249
1250 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1251
1252
1253 /* Describe an entry into the .debug_addr section. */
1254
1255 enum ate_kind {
1256 ate_kind_rtx,
1257 ate_kind_rtx_dtprel,
1258 ate_kind_label
1259 };
1260
1261 typedef struct GTY(()) addr_table_entry_struct {
1262 enum ate_kind kind;
1263 unsigned int refcount;
1264 unsigned int index;
1265 union addr_table_entry_struct_union
1266 {
1267 rtx GTY ((tag ("0"))) rtl;
1268 char * GTY ((tag ("1"))) label;
1269 }
1270 GTY ((desc ("%1.kind"))) addr;
1271 }
1272 addr_table_entry;
1273
1274 /* Location lists are ranges + location descriptions for that range,
1275 so you can track variables that are in different places over
1276 their entire life. */
1277 typedef struct GTY(()) dw_loc_list_struct {
1278 dw_loc_list_ref dw_loc_next;
1279 const char *begin; /* Label and addr_entry for start of range */
1280 addr_table_entry *begin_entry;
1281 const char *end; /* Label for end of range */
1282 char *ll_symbol; /* Label for beginning of location list.
1283 Only on head of list */
1284 const char *section; /* Section this loclist is relative to */
1285 dw_loc_descr_ref expr;
1286 hashval_t hash;
1287 /* True if all addresses in this and subsequent lists are known to be
1288 resolved. */
1289 bool resolved_addr;
1290 /* True if this list has been replaced by dw_loc_next. */
1291 bool replaced;
1292 bool emitted;
1293 /* True if the range should be emitted even if begin and end
1294 are the same. */
1295 bool force;
1296 } dw_loc_list_node;
1297
1298 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1299
1300 /* Convert a DWARF stack opcode into its string name. */
1301
1302 static const char *
1303 dwarf_stack_op_name (unsigned int op)
1304 {
1305 const char *name = get_DW_OP_name (op);
1306
1307 if (name != NULL)
1308 return name;
1309
1310 return "OP_<unknown>";
1311 }
1312
1313 /* Return a pointer to a newly allocated location description. Location
1314 descriptions are simple expression terms that can be strung
1315 together to form more complicated location (address) descriptions. */
1316
1317 static inline dw_loc_descr_ref
1318 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1319 unsigned HOST_WIDE_INT oprnd2)
1320 {
1321 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1322
1323 descr->dw_loc_opc = op;
1324 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1325 descr->dw_loc_oprnd1.val_entry = NULL;
1326 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1327 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1328 descr->dw_loc_oprnd2.val_entry = NULL;
1329 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1330
1331 return descr;
1332 }
1333
1334 /* Return a pointer to a newly allocated location description for
1335 REG and OFFSET. */
1336
1337 static inline dw_loc_descr_ref
1338 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1339 {
1340 if (reg <= 31)
1341 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1342 offset, 0);
1343 else
1344 return new_loc_descr (DW_OP_bregx, reg, offset);
1345 }
1346
1347 /* Add a location description term to a location description expression. */
1348
1349 static inline void
1350 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1351 {
1352 dw_loc_descr_ref *d;
1353
1354 /* Find the end of the chain. */
1355 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1356 ;
1357
1358 *d = descr;
1359 }
1360
1361 /* Compare two location operands for exact equality. */
1362
1363 static bool
1364 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1365 {
1366 if (a->val_class != b->val_class)
1367 return false;
1368 switch (a->val_class)
1369 {
1370 case dw_val_class_none:
1371 return true;
1372 case dw_val_class_addr:
1373 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1374
1375 case dw_val_class_offset:
1376 case dw_val_class_unsigned_const:
1377 case dw_val_class_const:
1378 case dw_val_class_range_list:
1379 case dw_val_class_lineptr:
1380 case dw_val_class_macptr:
1381 /* These are all HOST_WIDE_INT, signed or unsigned. */
1382 return a->v.val_unsigned == b->v.val_unsigned;
1383
1384 case dw_val_class_loc:
1385 return a->v.val_loc == b->v.val_loc;
1386 case dw_val_class_loc_list:
1387 return a->v.val_loc_list == b->v.val_loc_list;
1388 case dw_val_class_die_ref:
1389 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1390 case dw_val_class_fde_ref:
1391 return a->v.val_fde_index == b->v.val_fde_index;
1392 case dw_val_class_lbl_id:
1393 case dw_val_class_high_pc:
1394 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1395 case dw_val_class_str:
1396 return a->v.val_str == b->v.val_str;
1397 case dw_val_class_flag:
1398 return a->v.val_flag == b->v.val_flag;
1399 case dw_val_class_file:
1400 return a->v.val_file == b->v.val_file;
1401 case dw_val_class_decl_ref:
1402 return a->v.val_decl_ref == b->v.val_decl_ref;
1403
1404 case dw_val_class_const_double:
1405 return (a->v.val_double.high == b->v.val_double.high
1406 && a->v.val_double.low == b->v.val_double.low);
1407
1408 case dw_val_class_wide_int:
1409 return *a->v.val_wide == *b->v.val_wide;
1410
1411 case dw_val_class_vec:
1412 {
1413 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1414 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1415
1416 return (a_len == b_len
1417 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1418 }
1419
1420 case dw_val_class_data8:
1421 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1422
1423 case dw_val_class_vms_delta:
1424 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1425 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1426 }
1427 gcc_unreachable ();
1428 }
1429
1430 /* Compare two location atoms for exact equality. */
1431
1432 static bool
1433 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1434 {
1435 if (a->dw_loc_opc != b->dw_loc_opc)
1436 return false;
1437
1438 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1439 address size, but since we always allocate cleared storage it
1440 should be zero for other types of locations. */
1441 if (a->dtprel != b->dtprel)
1442 return false;
1443
1444 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1445 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1446 }
1447
1448 /* Compare two complete location expressions for exact equality. */
1449
1450 bool
1451 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 {
1453 while (1)
1454 {
1455 if (a == b)
1456 return true;
1457 if (a == NULL || b == NULL)
1458 return false;
1459 if (!loc_descr_equal_p_1 (a, b))
1460 return false;
1461
1462 a = a->dw_loc_next;
1463 b = b->dw_loc_next;
1464 }
1465 }
1466
1467
1468 /* Add a constant OFFSET to a location expression. */
1469
1470 static void
1471 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1472 {
1473 dw_loc_descr_ref loc;
1474 HOST_WIDE_INT *p;
1475
1476 gcc_assert (*list_head != NULL);
1477
1478 if (!offset)
1479 return;
1480
1481 /* Find the end of the chain. */
1482 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1483 ;
1484
1485 p = NULL;
1486 if (loc->dw_loc_opc == DW_OP_fbreg
1487 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1488 p = &loc->dw_loc_oprnd1.v.val_int;
1489 else if (loc->dw_loc_opc == DW_OP_bregx)
1490 p = &loc->dw_loc_oprnd2.v.val_int;
1491
1492 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1493 offset. Don't optimize if an signed integer overflow would happen. */
1494 if (p != NULL
1495 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1496 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1497 *p += offset;
1498
1499 else if (offset > 0)
1500 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1501
1502 else
1503 {
1504 loc->dw_loc_next = int_loc_descriptor (-offset);
1505 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1506 }
1507 }
1508
1509 /* Add a constant OFFSET to a location list. */
1510
1511 static void
1512 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1513 {
1514 dw_loc_list_ref d;
1515 for (d = list_head; d != NULL; d = d->dw_loc_next)
1516 loc_descr_plus_const (&d->expr, offset);
1517 }
1518
1519 #define DWARF_REF_SIZE \
1520 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1521
1522 static unsigned long int get_base_type_offset (dw_die_ref);
1523
1524 /* Return the size of a location descriptor. */
1525
1526 static unsigned long
1527 size_of_loc_descr (dw_loc_descr_ref loc)
1528 {
1529 unsigned long size = 1;
1530
1531 switch (loc->dw_loc_opc)
1532 {
1533 case DW_OP_addr:
1534 size += DWARF2_ADDR_SIZE;
1535 break;
1536 case DW_OP_GNU_addr_index:
1537 case DW_OP_GNU_const_index:
1538 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1539 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1540 break;
1541 case DW_OP_const1u:
1542 case DW_OP_const1s:
1543 size += 1;
1544 break;
1545 case DW_OP_const2u:
1546 case DW_OP_const2s:
1547 size += 2;
1548 break;
1549 case DW_OP_const4u:
1550 case DW_OP_const4s:
1551 size += 4;
1552 break;
1553 case DW_OP_const8u:
1554 case DW_OP_const8s:
1555 size += 8;
1556 break;
1557 case DW_OP_constu:
1558 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1559 break;
1560 case DW_OP_consts:
1561 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1562 break;
1563 case DW_OP_pick:
1564 size += 1;
1565 break;
1566 case DW_OP_plus_uconst:
1567 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568 break;
1569 case DW_OP_skip:
1570 case DW_OP_bra:
1571 size += 2;
1572 break;
1573 case DW_OP_breg0:
1574 case DW_OP_breg1:
1575 case DW_OP_breg2:
1576 case DW_OP_breg3:
1577 case DW_OP_breg4:
1578 case DW_OP_breg5:
1579 case DW_OP_breg6:
1580 case DW_OP_breg7:
1581 case DW_OP_breg8:
1582 case DW_OP_breg9:
1583 case DW_OP_breg10:
1584 case DW_OP_breg11:
1585 case DW_OP_breg12:
1586 case DW_OP_breg13:
1587 case DW_OP_breg14:
1588 case DW_OP_breg15:
1589 case DW_OP_breg16:
1590 case DW_OP_breg17:
1591 case DW_OP_breg18:
1592 case DW_OP_breg19:
1593 case DW_OP_breg20:
1594 case DW_OP_breg21:
1595 case DW_OP_breg22:
1596 case DW_OP_breg23:
1597 case DW_OP_breg24:
1598 case DW_OP_breg25:
1599 case DW_OP_breg26:
1600 case DW_OP_breg27:
1601 case DW_OP_breg28:
1602 case DW_OP_breg29:
1603 case DW_OP_breg30:
1604 case DW_OP_breg31:
1605 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1606 break;
1607 case DW_OP_regx:
1608 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1609 break;
1610 case DW_OP_fbreg:
1611 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1612 break;
1613 case DW_OP_bregx:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1616 break;
1617 case DW_OP_piece:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 break;
1620 case DW_OP_bit_piece:
1621 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1622 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1623 break;
1624 case DW_OP_deref_size:
1625 case DW_OP_xderef_size:
1626 size += 1;
1627 break;
1628 case DW_OP_call2:
1629 size += 2;
1630 break;
1631 case DW_OP_call4:
1632 size += 4;
1633 break;
1634 case DW_OP_call_ref:
1635 size += DWARF_REF_SIZE;
1636 break;
1637 case DW_OP_implicit_value:
1638 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1639 + loc->dw_loc_oprnd1.v.val_unsigned;
1640 break;
1641 case DW_OP_GNU_implicit_pointer:
1642 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1643 break;
1644 case DW_OP_GNU_entry_value:
1645 {
1646 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1647 size += size_of_uleb128 (op_size) + op_size;
1648 break;
1649 }
1650 case DW_OP_GNU_const_type:
1651 {
1652 unsigned long o
1653 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1654 size += size_of_uleb128 (o) + 1;
1655 switch (loc->dw_loc_oprnd2.val_class)
1656 {
1657 case dw_val_class_vec:
1658 size += loc->dw_loc_oprnd2.v.val_vec.length
1659 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1660 break;
1661 case dw_val_class_const:
1662 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1663 break;
1664 case dw_val_class_const_double:
1665 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1666 break;
1667 case dw_val_class_wide_int:
1668 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1669 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1670 break;
1671 default:
1672 gcc_unreachable ();
1673 }
1674 break;
1675 }
1676 case DW_OP_GNU_regval_type:
1677 {
1678 unsigned long o
1679 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1680 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1681 + size_of_uleb128 (o);
1682 }
1683 break;
1684 case DW_OP_GNU_deref_type:
1685 {
1686 unsigned long o
1687 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1688 size += 1 + size_of_uleb128 (o);
1689 }
1690 break;
1691 case DW_OP_GNU_convert:
1692 case DW_OP_GNU_reinterpret:
1693 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1695 else
1696 {
1697 unsigned long o
1698 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1699 size += size_of_uleb128 (o);
1700 }
1701 break;
1702 case DW_OP_GNU_parameter_ref:
1703 size += 4;
1704 break;
1705 default:
1706 break;
1707 }
1708
1709 return size;
1710 }
1711
1712 /* Return the size of a series of location descriptors. */
1713
1714 unsigned long
1715 size_of_locs (dw_loc_descr_ref loc)
1716 {
1717 dw_loc_descr_ref l;
1718 unsigned long size;
1719
1720 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1721 field, to avoid writing to a PCH file. */
1722 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1723 {
1724 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1725 break;
1726 size += size_of_loc_descr (l);
1727 }
1728 if (! l)
1729 return size;
1730
1731 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1732 {
1733 l->dw_loc_addr = size;
1734 size += size_of_loc_descr (l);
1735 }
1736
1737 return size;
1738 }
1739
1740 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1741 static void get_ref_die_offset_label (char *, dw_die_ref);
1742 static unsigned long int get_ref_die_offset (dw_die_ref);
1743
1744 /* Output location description stack opcode's operands (if any).
1745 The for_eh_or_skip parameter controls whether register numbers are
1746 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1747 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1748 info). This should be suppressed for the cases that have not been converted
1749 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1750
1751 static void
1752 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1753 {
1754 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1755 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1756
1757 switch (loc->dw_loc_opc)
1758 {
1759 #ifdef DWARF2_DEBUGGING_INFO
1760 case DW_OP_const2u:
1761 case DW_OP_const2s:
1762 dw2_asm_output_data (2, val1->v.val_int, NULL);
1763 break;
1764 case DW_OP_const4u:
1765 if (loc->dtprel)
1766 {
1767 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1768 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1769 val1->v.val_addr);
1770 fputc ('\n', asm_out_file);
1771 break;
1772 }
1773 /* FALLTHRU */
1774 case DW_OP_const4s:
1775 dw2_asm_output_data (4, val1->v.val_int, NULL);
1776 break;
1777 case DW_OP_const8u:
1778 if (loc->dtprel)
1779 {
1780 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1781 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1782 val1->v.val_addr);
1783 fputc ('\n', asm_out_file);
1784 break;
1785 }
1786 /* FALLTHRU */
1787 case DW_OP_const8s:
1788 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1789 dw2_asm_output_data (8, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_skip:
1792 case DW_OP_bra:
1793 {
1794 int offset;
1795
1796 gcc_assert (val1->val_class == dw_val_class_loc);
1797 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1798
1799 dw2_asm_output_data (2, offset, NULL);
1800 }
1801 break;
1802 case DW_OP_implicit_value:
1803 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1804 switch (val2->val_class)
1805 {
1806 case dw_val_class_const:
1807 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1808 break;
1809 case dw_val_class_vec:
1810 {
1811 unsigned int elt_size = val2->v.val_vec.elt_size;
1812 unsigned int len = val2->v.val_vec.length;
1813 unsigned int i;
1814 unsigned char *p;
1815
1816 if (elt_size > sizeof (HOST_WIDE_INT))
1817 {
1818 elt_size /= 2;
1819 len *= 2;
1820 }
1821 for (i = 0, p = val2->v.val_vec.array;
1822 i < len;
1823 i++, p += elt_size)
1824 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1825 "fp or vector constant word %u", i);
1826 }
1827 break;
1828 case dw_val_class_const_double:
1829 {
1830 unsigned HOST_WIDE_INT first, second;
1831
1832 if (WORDS_BIG_ENDIAN)
1833 {
1834 first = val2->v.val_double.high;
1835 second = val2->v.val_double.low;
1836 }
1837 else
1838 {
1839 first = val2->v.val_double.low;
1840 second = val2->v.val_double.high;
1841 }
1842 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1843 first, NULL);
1844 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1845 second, NULL);
1846 }
1847 break;
1848 case dw_val_class_wide_int:
1849 {
1850 int i;
1851 int len = get_full_len (*val2->v.val_wide);
1852 if (WORDS_BIG_ENDIAN)
1853 for (i = len - 1; i >= 0; --i)
1854 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855 val2->v.val_wide->elt (i), NULL);
1856 else
1857 for (i = 0; i < len; ++i)
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 val2->v.val_wide->elt (i), NULL);
1860 }
1861 break;
1862 case dw_val_class_addr:
1863 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1864 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1865 break;
1866 default:
1867 gcc_unreachable ();
1868 }
1869 break;
1870 #else
1871 case DW_OP_const2u:
1872 case DW_OP_const2s:
1873 case DW_OP_const4u:
1874 case DW_OP_const4s:
1875 case DW_OP_const8u:
1876 case DW_OP_const8s:
1877 case DW_OP_skip:
1878 case DW_OP_bra:
1879 case DW_OP_implicit_value:
1880 /* We currently don't make any attempt to make sure these are
1881 aligned properly like we do for the main unwind info, so
1882 don't support emitting things larger than a byte if we're
1883 only doing unwinding. */
1884 gcc_unreachable ();
1885 #endif
1886 case DW_OP_const1u:
1887 case DW_OP_const1s:
1888 dw2_asm_output_data (1, val1->v.val_int, NULL);
1889 break;
1890 case DW_OP_constu:
1891 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1892 break;
1893 case DW_OP_consts:
1894 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_pick:
1897 dw2_asm_output_data (1, val1->v.val_int, NULL);
1898 break;
1899 case DW_OP_plus_uconst:
1900 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1901 break;
1902 case DW_OP_breg0:
1903 case DW_OP_breg1:
1904 case DW_OP_breg2:
1905 case DW_OP_breg3:
1906 case DW_OP_breg4:
1907 case DW_OP_breg5:
1908 case DW_OP_breg6:
1909 case DW_OP_breg7:
1910 case DW_OP_breg8:
1911 case DW_OP_breg9:
1912 case DW_OP_breg10:
1913 case DW_OP_breg11:
1914 case DW_OP_breg12:
1915 case DW_OP_breg13:
1916 case DW_OP_breg14:
1917 case DW_OP_breg15:
1918 case DW_OP_breg16:
1919 case DW_OP_breg17:
1920 case DW_OP_breg18:
1921 case DW_OP_breg19:
1922 case DW_OP_breg20:
1923 case DW_OP_breg21:
1924 case DW_OP_breg22:
1925 case DW_OP_breg23:
1926 case DW_OP_breg24:
1927 case DW_OP_breg25:
1928 case DW_OP_breg26:
1929 case DW_OP_breg27:
1930 case DW_OP_breg28:
1931 case DW_OP_breg29:
1932 case DW_OP_breg30:
1933 case DW_OP_breg31:
1934 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1935 break;
1936 case DW_OP_regx:
1937 {
1938 unsigned r = val1->v.val_unsigned;
1939 if (for_eh_or_skip >= 0)
1940 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1941 gcc_assert (size_of_uleb128 (r)
1942 == size_of_uleb128 (val1->v.val_unsigned));
1943 dw2_asm_output_data_uleb128 (r, NULL);
1944 }
1945 break;
1946 case DW_OP_fbreg:
1947 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1948 break;
1949 case DW_OP_bregx:
1950 {
1951 unsigned r = val1->v.val_unsigned;
1952 if (for_eh_or_skip >= 0)
1953 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1954 gcc_assert (size_of_uleb128 (r)
1955 == size_of_uleb128 (val1->v.val_unsigned));
1956 dw2_asm_output_data_uleb128 (r, NULL);
1957 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1958 }
1959 break;
1960 case DW_OP_piece:
1961 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1962 break;
1963 case DW_OP_bit_piece:
1964 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1965 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1966 break;
1967 case DW_OP_deref_size:
1968 case DW_OP_xderef_size:
1969 dw2_asm_output_data (1, val1->v.val_int, NULL);
1970 break;
1971
1972 case DW_OP_addr:
1973 if (loc->dtprel)
1974 {
1975 if (targetm.asm_out.output_dwarf_dtprel)
1976 {
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1978 DWARF2_ADDR_SIZE,
1979 val1->v.val_addr);
1980 fputc ('\n', asm_out_file);
1981 }
1982 else
1983 gcc_unreachable ();
1984 }
1985 else
1986 {
1987 #ifdef DWARF2_DEBUGGING_INFO
1988 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1989 #else
1990 gcc_unreachable ();
1991 #endif
1992 }
1993 break;
1994
1995 case DW_OP_GNU_addr_index:
1996 case DW_OP_GNU_const_index:
1997 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1998 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1999 "(index into .debug_addr)");
2000 break;
2001
2002 case DW_OP_GNU_implicit_pointer:
2003 {
2004 char label[MAX_ARTIFICIAL_LABEL_BYTES
2005 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2006 gcc_assert (val1->val_class == dw_val_class_die_ref);
2007 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2008 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2009 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2010 }
2011 break;
2012
2013 case DW_OP_GNU_entry_value:
2014 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2015 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2016 break;
2017
2018 case DW_OP_GNU_const_type:
2019 {
2020 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2021 gcc_assert (o);
2022 dw2_asm_output_data_uleb128 (o, NULL);
2023 switch (val2->val_class)
2024 {
2025 case dw_val_class_const:
2026 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2027 dw2_asm_output_data (1, l, NULL);
2028 dw2_asm_output_data (l, val2->v.val_int, NULL);
2029 break;
2030 case dw_val_class_vec:
2031 {
2032 unsigned int elt_size = val2->v.val_vec.elt_size;
2033 unsigned int len = val2->v.val_vec.length;
2034 unsigned int i;
2035 unsigned char *p;
2036
2037 l = len * elt_size;
2038 dw2_asm_output_data (1, l, NULL);
2039 if (elt_size > sizeof (HOST_WIDE_INT))
2040 {
2041 elt_size /= 2;
2042 len *= 2;
2043 }
2044 for (i = 0, p = val2->v.val_vec.array;
2045 i < len;
2046 i++, p += elt_size)
2047 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2048 "fp or vector constant word %u", i);
2049 }
2050 break;
2051 case dw_val_class_const_double:
2052 {
2053 unsigned HOST_WIDE_INT first, second;
2054 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055
2056 dw2_asm_output_data (1, 2 * l, NULL);
2057 if (WORDS_BIG_ENDIAN)
2058 {
2059 first = val2->v.val_double.high;
2060 second = val2->v.val_double.low;
2061 }
2062 else
2063 {
2064 first = val2->v.val_double.low;
2065 second = val2->v.val_double.high;
2066 }
2067 dw2_asm_output_data (l, first, NULL);
2068 dw2_asm_output_data (l, second, NULL);
2069 }
2070 break;
2071 case dw_val_class_wide_int:
2072 {
2073 int i;
2074 int len = get_full_len (*val2->v.val_wide);
2075 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2076
2077 dw2_asm_output_data (1, len * l, NULL);
2078 if (WORDS_BIG_ENDIAN)
2079 for (i = len - 1; i >= 0; --i)
2080 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2081 else
2082 for (i = 0; i < len; ++i)
2083 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2084 }
2085 break;
2086 default:
2087 gcc_unreachable ();
2088 }
2089 }
2090 break;
2091 case DW_OP_GNU_regval_type:
2092 {
2093 unsigned r = val1->v.val_unsigned;
2094 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2095 gcc_assert (o);
2096 if (for_eh_or_skip >= 0)
2097 {
2098 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2099 gcc_assert (size_of_uleb128 (r)
2100 == size_of_uleb128 (val1->v.val_unsigned));
2101 }
2102 dw2_asm_output_data_uleb128 (r, NULL);
2103 dw2_asm_output_data_uleb128 (o, NULL);
2104 }
2105 break;
2106 case DW_OP_GNU_deref_type:
2107 {
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 dw2_asm_output_data (1, val1->v.val_int, NULL);
2111 dw2_asm_output_data_uleb128 (o, NULL);
2112 }
2113 break;
2114 case DW_OP_GNU_convert:
2115 case DW_OP_GNU_reinterpret:
2116 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2117 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2118 else
2119 {
2120 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2121 gcc_assert (o);
2122 dw2_asm_output_data_uleb128 (o, NULL);
2123 }
2124 break;
2125
2126 case DW_OP_GNU_parameter_ref:
2127 {
2128 unsigned long o;
2129 gcc_assert (val1->val_class == dw_val_class_die_ref);
2130 o = get_ref_die_offset (val1->v.val_die_ref.die);
2131 dw2_asm_output_data (4, o, NULL);
2132 }
2133 break;
2134
2135 default:
2136 /* Other codes have no operands. */
2137 break;
2138 }
2139 }
2140
2141 /* Output a sequence of location operations.
2142 The for_eh_or_skip parameter controls whether register numbers are
2143 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2144 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2145 info). This should be suppressed for the cases that have not been converted
2146 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2147
2148 void
2149 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2150 {
2151 for (; loc != NULL; loc = loc->dw_loc_next)
2152 {
2153 enum dwarf_location_atom opc = loc->dw_loc_opc;
2154 /* Output the opcode. */
2155 if (for_eh_or_skip >= 0
2156 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2157 {
2158 unsigned r = (opc - DW_OP_breg0);
2159 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2160 gcc_assert (r <= 31);
2161 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2162 }
2163 else if (for_eh_or_skip >= 0
2164 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2165 {
2166 unsigned r = (opc - DW_OP_reg0);
2167 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2168 gcc_assert (r <= 31);
2169 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2170 }
2171
2172 dw2_asm_output_data (1, opc,
2173 "%s", dwarf_stack_op_name (opc));
2174
2175 /* Output the operand(s) (if any). */
2176 output_loc_operands (loc, for_eh_or_skip);
2177 }
2178 }
2179
2180 /* Output location description stack opcode's operands (if any).
2181 The output is single bytes on a line, suitable for .cfi_escape. */
2182
2183 static void
2184 output_loc_operands_raw (dw_loc_descr_ref loc)
2185 {
2186 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2187 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2188
2189 switch (loc->dw_loc_opc)
2190 {
2191 case DW_OP_addr:
2192 case DW_OP_GNU_addr_index:
2193 case DW_OP_GNU_const_index:
2194 case DW_OP_implicit_value:
2195 /* We cannot output addresses in .cfi_escape, only bytes. */
2196 gcc_unreachable ();
2197
2198 case DW_OP_const1u:
2199 case DW_OP_const1s:
2200 case DW_OP_pick:
2201 case DW_OP_deref_size:
2202 case DW_OP_xderef_size:
2203 fputc (',', asm_out_file);
2204 dw2_asm_output_data_raw (1, val1->v.val_int);
2205 break;
2206
2207 case DW_OP_const2u:
2208 case DW_OP_const2s:
2209 fputc (',', asm_out_file);
2210 dw2_asm_output_data_raw (2, val1->v.val_int);
2211 break;
2212
2213 case DW_OP_const4u:
2214 case DW_OP_const4s:
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (4, val1->v.val_int);
2217 break;
2218
2219 case DW_OP_const8u:
2220 case DW_OP_const8s:
2221 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2222 fputc (',', asm_out_file);
2223 dw2_asm_output_data_raw (8, val1->v.val_int);
2224 break;
2225
2226 case DW_OP_skip:
2227 case DW_OP_bra:
2228 {
2229 int offset;
2230
2231 gcc_assert (val1->val_class == dw_val_class_loc);
2232 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2233
2234 fputc (',', asm_out_file);
2235 dw2_asm_output_data_raw (2, offset);
2236 }
2237 break;
2238
2239 case DW_OP_regx:
2240 {
2241 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2242 gcc_assert (size_of_uleb128 (r)
2243 == size_of_uleb128 (val1->v.val_unsigned));
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_uleb128_raw (r);
2246 }
2247 break;
2248
2249 case DW_OP_constu:
2250 case DW_OP_plus_uconst:
2251 case DW_OP_piece:
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2254 break;
2255
2256 case DW_OP_bit_piece:
2257 fputc (',', asm_out_file);
2258 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2259 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2260 break;
2261
2262 case DW_OP_consts:
2263 case DW_OP_breg0:
2264 case DW_OP_breg1:
2265 case DW_OP_breg2:
2266 case DW_OP_breg3:
2267 case DW_OP_breg4:
2268 case DW_OP_breg5:
2269 case DW_OP_breg6:
2270 case DW_OP_breg7:
2271 case DW_OP_breg8:
2272 case DW_OP_breg9:
2273 case DW_OP_breg10:
2274 case DW_OP_breg11:
2275 case DW_OP_breg12:
2276 case DW_OP_breg13:
2277 case DW_OP_breg14:
2278 case DW_OP_breg15:
2279 case DW_OP_breg16:
2280 case DW_OP_breg17:
2281 case DW_OP_breg18:
2282 case DW_OP_breg19:
2283 case DW_OP_breg20:
2284 case DW_OP_breg21:
2285 case DW_OP_breg22:
2286 case DW_OP_breg23:
2287 case DW_OP_breg24:
2288 case DW_OP_breg25:
2289 case DW_OP_breg26:
2290 case DW_OP_breg27:
2291 case DW_OP_breg28:
2292 case DW_OP_breg29:
2293 case DW_OP_breg30:
2294 case DW_OP_breg31:
2295 case DW_OP_fbreg:
2296 fputc (',', asm_out_file);
2297 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2298 break;
2299
2300 case DW_OP_bregx:
2301 {
2302 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2303 gcc_assert (size_of_uleb128 (r)
2304 == size_of_uleb128 (val1->v.val_unsigned));
2305 fputc (',', asm_out_file);
2306 dw2_asm_output_data_uleb128_raw (r);
2307 fputc (',', asm_out_file);
2308 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2309 }
2310 break;
2311
2312 case DW_OP_GNU_implicit_pointer:
2313 case DW_OP_GNU_entry_value:
2314 case DW_OP_GNU_const_type:
2315 case DW_OP_GNU_regval_type:
2316 case DW_OP_GNU_deref_type:
2317 case DW_OP_GNU_convert:
2318 case DW_OP_GNU_reinterpret:
2319 case DW_OP_GNU_parameter_ref:
2320 gcc_unreachable ();
2321 break;
2322
2323 default:
2324 /* Other codes have no operands. */
2325 break;
2326 }
2327 }
2328
2329 void
2330 output_loc_sequence_raw (dw_loc_descr_ref loc)
2331 {
2332 while (1)
2333 {
2334 enum dwarf_location_atom opc = loc->dw_loc_opc;
2335 /* Output the opcode. */
2336 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2337 {
2338 unsigned r = (opc - DW_OP_breg0);
2339 r = DWARF2_FRAME_REG_OUT (r, 1);
2340 gcc_assert (r <= 31);
2341 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2342 }
2343 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2344 {
2345 unsigned r = (opc - DW_OP_reg0);
2346 r = DWARF2_FRAME_REG_OUT (r, 1);
2347 gcc_assert (r <= 31);
2348 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2349 }
2350 /* Output the opcode. */
2351 fprintf (asm_out_file, "%#x", opc);
2352 output_loc_operands_raw (loc);
2353
2354 if (!loc->dw_loc_next)
2355 break;
2356 loc = loc->dw_loc_next;
2357
2358 fputc (',', asm_out_file);
2359 }
2360 }
2361
2362 /* This function builds a dwarf location descriptor sequence from a
2363 dw_cfa_location, adding the given OFFSET to the result of the
2364 expression. */
2365
2366 struct dw_loc_descr_node *
2367 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2368 {
2369 struct dw_loc_descr_node *head, *tmp;
2370
2371 offset += cfa->offset;
2372
2373 if (cfa->indirect)
2374 {
2375 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2376 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2377 head->dw_loc_oprnd1.val_entry = NULL;
2378 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2379 add_loc_descr (&head, tmp);
2380 if (offset != 0)
2381 {
2382 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2383 add_loc_descr (&head, tmp);
2384 }
2385 }
2386 else
2387 head = new_reg_loc_descr (cfa->reg, offset);
2388
2389 return head;
2390 }
2391
2392 /* This function builds a dwarf location descriptor sequence for
2393 the address at OFFSET from the CFA when stack is aligned to
2394 ALIGNMENT byte. */
2395
2396 struct dw_loc_descr_node *
2397 build_cfa_aligned_loc (dw_cfa_location *cfa,
2398 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2399 {
2400 struct dw_loc_descr_node *head;
2401 unsigned int dwarf_fp
2402 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2403
2404 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2405 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2406 {
2407 head = new_reg_loc_descr (dwarf_fp, 0);
2408 add_loc_descr (&head, int_loc_descriptor (alignment));
2409 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2410 loc_descr_plus_const (&head, offset);
2411 }
2412 else
2413 head = new_reg_loc_descr (dwarf_fp, offset);
2414 return head;
2415 }
2416 \f
2417 /* And now, the support for symbolic debugging information. */
2418
2419 /* .debug_str support. */
2420 static int output_indirect_string (void **, void *);
2421
2422 static void dwarf2out_init (const char *);
2423 static void dwarf2out_finish (const char *);
2424 static void dwarf2out_assembly_start (void);
2425 static void dwarf2out_define (unsigned int, const char *);
2426 static void dwarf2out_undef (unsigned int, const char *);
2427 static void dwarf2out_start_source_file (unsigned, const char *);
2428 static void dwarf2out_end_source_file (unsigned);
2429 static void dwarf2out_function_decl (tree);
2430 static void dwarf2out_begin_block (unsigned, unsigned);
2431 static void dwarf2out_end_block (unsigned, unsigned);
2432 static bool dwarf2out_ignore_block (const_tree);
2433 static void dwarf2out_global_decl (tree);
2434 static void dwarf2out_type_decl (tree, int);
2435 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2436 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2437 dw_die_ref);
2438 static void dwarf2out_abstract_function (tree);
2439 static void dwarf2out_var_location (rtx_insn *);
2440 static void dwarf2out_begin_function (tree);
2441 static void dwarf2out_end_function (unsigned int);
2442 static void dwarf2out_set_name (tree, tree);
2443
2444 /* The debug hooks structure. */
2445
2446 const struct gcc_debug_hooks dwarf2_debug_hooks =
2447 {
2448 dwarf2out_init,
2449 dwarf2out_finish,
2450 dwarf2out_assembly_start,
2451 dwarf2out_define,
2452 dwarf2out_undef,
2453 dwarf2out_start_source_file,
2454 dwarf2out_end_source_file,
2455 dwarf2out_begin_block,
2456 dwarf2out_end_block,
2457 dwarf2out_ignore_block,
2458 dwarf2out_source_line,
2459 dwarf2out_begin_prologue,
2460 #if VMS_DEBUGGING_INFO
2461 dwarf2out_vms_end_prologue,
2462 dwarf2out_vms_begin_epilogue,
2463 #else
2464 debug_nothing_int_charstar,
2465 debug_nothing_int_charstar,
2466 #endif
2467 dwarf2out_end_epilogue,
2468 dwarf2out_begin_function,
2469 dwarf2out_end_function, /* end_function */
2470 dwarf2out_function_decl, /* function_decl */
2471 dwarf2out_global_decl,
2472 dwarf2out_type_decl, /* type_decl */
2473 dwarf2out_imported_module_or_decl,
2474 debug_nothing_tree, /* deferred_inline_function */
2475 /* The DWARF 2 backend tries to reduce debugging bloat by not
2476 emitting the abstract description of inline functions until
2477 something tries to reference them. */
2478 dwarf2out_abstract_function, /* outlining_inline_function */
2479 debug_nothing_rtx_code_label, /* label */
2480 debug_nothing_int, /* handle_pch */
2481 dwarf2out_var_location,
2482 dwarf2out_switch_text_section,
2483 dwarf2out_set_name,
2484 1, /* start_end_main_source_file */
2485 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2486 };
2487 \f
2488 /* NOTE: In the comments in this file, many references are made to
2489 "Debugging Information Entries". This term is abbreviated as `DIE'
2490 throughout the remainder of this file. */
2491
2492 /* An internal representation of the DWARF output is built, and then
2493 walked to generate the DWARF debugging info. The walk of the internal
2494 representation is done after the entire program has been compiled.
2495 The types below are used to describe the internal representation. */
2496
2497 /* Whether to put type DIEs into their own section .debug_types instead
2498 of making them part of the .debug_info section. Only supported for
2499 Dwarf V4 or higher and the user didn't disable them through
2500 -fno-debug-types-section. It is more efficient to put them in a
2501 separate comdat sections since the linker will then be able to
2502 remove duplicates. But not all tools support .debug_types sections
2503 yet. */
2504
2505 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2506
2507 /* Various DIE's use offsets relative to the beginning of the
2508 .debug_info section to refer to each other. */
2509
2510 typedef long int dw_offset;
2511
2512 /* Define typedefs here to avoid circular dependencies. */
2513
2514 typedef struct dw_attr_struct *dw_attr_ref;
2515 typedef struct dw_line_info_struct *dw_line_info_ref;
2516 typedef struct pubname_struct *pubname_ref;
2517 typedef struct dw_ranges_struct *dw_ranges_ref;
2518 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2519 typedef struct comdat_type_struct *comdat_type_node_ref;
2520
2521 /* The entries in the line_info table more-or-less mirror the opcodes
2522 that are used in the real dwarf line table. Arrays of these entries
2523 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2524 supported. */
2525
2526 enum dw_line_info_opcode {
2527 /* Emit DW_LNE_set_address; the operand is the label index. */
2528 LI_set_address,
2529
2530 /* Emit a row to the matrix with the given line. This may be done
2531 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2532 special opcodes. */
2533 LI_set_line,
2534
2535 /* Emit a DW_LNS_set_file. */
2536 LI_set_file,
2537
2538 /* Emit a DW_LNS_set_column. */
2539 LI_set_column,
2540
2541 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2542 LI_negate_stmt,
2543
2544 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2545 LI_set_prologue_end,
2546 LI_set_epilogue_begin,
2547
2548 /* Emit a DW_LNE_set_discriminator. */
2549 LI_set_discriminator
2550 };
2551
2552 typedef struct GTY(()) dw_line_info_struct {
2553 enum dw_line_info_opcode opcode;
2554 unsigned int val;
2555 } dw_line_info_entry;
2556
2557
2558 typedef struct GTY(()) dw_line_info_table_struct {
2559 /* The label that marks the end of this section. */
2560 const char *end_label;
2561
2562 /* The values for the last row of the matrix, as collected in the table.
2563 These are used to minimize the changes to the next row. */
2564 unsigned int file_num;
2565 unsigned int line_num;
2566 unsigned int column_num;
2567 int discrim_num;
2568 bool is_stmt;
2569 bool in_use;
2570
2571 vec<dw_line_info_entry, va_gc> *entries;
2572 } dw_line_info_table;
2573
2574 typedef dw_line_info_table *dw_line_info_table_p;
2575
2576
2577 /* Each DIE attribute has a field specifying the attribute kind,
2578 a link to the next attribute in the chain, and an attribute value.
2579 Attributes are typically linked below the DIE they modify. */
2580
2581 typedef struct GTY(()) dw_attr_struct {
2582 enum dwarf_attribute dw_attr;
2583 dw_val_node dw_attr_val;
2584 }
2585 dw_attr_node;
2586
2587
2588 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2589 The children of each node form a circular list linked by
2590 die_sib. die_child points to the node *before* the "first" child node. */
2591
2592 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2593 union die_symbol_or_type_node
2594 {
2595 const char * GTY ((tag ("0"))) die_symbol;
2596 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2597 }
2598 GTY ((desc ("%0.comdat_type_p"))) die_id;
2599 vec<dw_attr_node, va_gc> *die_attr;
2600 dw_die_ref die_parent;
2601 dw_die_ref die_child;
2602 dw_die_ref die_sib;
2603 dw_die_ref die_definition; /* ref from a specification to its definition */
2604 dw_offset die_offset;
2605 unsigned long die_abbrev;
2606 int die_mark;
2607 unsigned int decl_id;
2608 enum dwarf_tag die_tag;
2609 /* Die is used and must not be pruned as unused. */
2610 BOOL_BITFIELD die_perennial_p : 1;
2611 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2612 /* Lots of spare bits. */
2613 }
2614 die_node;
2615
2616 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2617 #define FOR_EACH_CHILD(die, c, expr) do { \
2618 c = die->die_child; \
2619 if (c) do { \
2620 c = c->die_sib; \
2621 expr; \
2622 } while (c != die->die_child); \
2623 } while (0)
2624
2625 /* The pubname structure */
2626
2627 typedef struct GTY(()) pubname_struct {
2628 dw_die_ref die;
2629 const char *name;
2630 }
2631 pubname_entry;
2632
2633
2634 struct GTY(()) dw_ranges_struct {
2635 /* If this is positive, it's a block number, otherwise it's a
2636 bitwise-negated index into dw_ranges_by_label. */
2637 int num;
2638 };
2639
2640 /* A structure to hold a macinfo entry. */
2641
2642 typedef struct GTY(()) macinfo_struct {
2643 unsigned char code;
2644 unsigned HOST_WIDE_INT lineno;
2645 const char *info;
2646 }
2647 macinfo_entry;
2648
2649
2650 struct GTY(()) dw_ranges_by_label_struct {
2651 const char *begin;
2652 const char *end;
2653 };
2654
2655 /* The comdat type node structure. */
2656 typedef struct GTY(()) comdat_type_struct
2657 {
2658 dw_die_ref root_die;
2659 dw_die_ref type_die;
2660 dw_die_ref skeleton_die;
2661 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2662 struct comdat_type_struct *next;
2663 }
2664 comdat_type_node;
2665
2666 /* The limbo die list structure. */
2667 typedef struct GTY(()) limbo_die_struct {
2668 dw_die_ref die;
2669 tree created_for;
2670 struct limbo_die_struct *next;
2671 }
2672 limbo_die_node;
2673
2674 typedef struct skeleton_chain_struct
2675 {
2676 dw_die_ref old_die;
2677 dw_die_ref new_die;
2678 struct skeleton_chain_struct *parent;
2679 }
2680 skeleton_chain_node;
2681
2682 /* Define a macro which returns nonzero for a TYPE_DECL which was
2683 implicitly generated for a type.
2684
2685 Note that, unlike the C front-end (which generates a NULL named
2686 TYPE_DECL node for each complete tagged type, each array type,
2687 and each function type node created) the C++ front-end generates
2688 a _named_ TYPE_DECL node for each tagged type node created.
2689 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2690 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2691 front-end, but for each type, tagged or not. */
2692
2693 #define TYPE_DECL_IS_STUB(decl) \
2694 (DECL_NAME (decl) == NULL_TREE \
2695 || (DECL_ARTIFICIAL (decl) \
2696 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2697 /* This is necessary for stub decls that \
2698 appear in nested inline functions. */ \
2699 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2700 && (decl_ultimate_origin (decl) \
2701 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2702
2703 /* Information concerning the compilation unit's programming
2704 language, and compiler version. */
2705
2706 /* Fixed size portion of the DWARF compilation unit header. */
2707 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2708 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2709
2710 /* Fixed size portion of the DWARF comdat type unit header. */
2711 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2712 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2713 + DWARF_OFFSET_SIZE)
2714
2715 /* Fixed size portion of public names info. */
2716 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2717
2718 /* Fixed size portion of the address range info. */
2719 #define DWARF_ARANGES_HEADER_SIZE \
2720 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2721 DWARF2_ADDR_SIZE * 2) \
2722 - DWARF_INITIAL_LENGTH_SIZE)
2723
2724 /* Size of padding portion in the address range info. It must be
2725 aligned to twice the pointer size. */
2726 #define DWARF_ARANGES_PAD_SIZE \
2727 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2728 DWARF2_ADDR_SIZE * 2) \
2729 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2730
2731 /* Use assembler line directives if available. */
2732 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2733 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2734 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2735 #else
2736 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2737 #endif
2738 #endif
2739
2740 /* Minimum line offset in a special line info. opcode.
2741 This value was chosen to give a reasonable range of values. */
2742 #define DWARF_LINE_BASE -10
2743
2744 /* First special line opcode - leave room for the standard opcodes. */
2745 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2746
2747 /* Range of line offsets in a special line info. opcode. */
2748 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2749
2750 /* Flag that indicates the initial value of the is_stmt_start flag.
2751 In the present implementation, we do not mark any lines as
2752 the beginning of a source statement, because that information
2753 is not made available by the GCC front-end. */
2754 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2755
2756 /* Maximum number of operations per instruction bundle. */
2757 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2758 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2759 #endif
2760
2761 /* This location is used by calc_die_sizes() to keep track
2762 the offset of each DIE within the .debug_info section. */
2763 static unsigned long next_die_offset;
2764
2765 /* Record the root of the DIE's built for the current compilation unit. */
2766 static GTY(()) dw_die_ref single_comp_unit_die;
2767
2768 /* A list of type DIEs that have been separated into comdat sections. */
2769 static GTY(()) comdat_type_node *comdat_type_list;
2770
2771 /* A list of DIEs with a NULL parent waiting to be relocated. */
2772 static GTY(()) limbo_die_node *limbo_die_list;
2773
2774 /* A list of DIEs for which we may have to generate
2775 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2776 static GTY(()) limbo_die_node *deferred_asm_name;
2777
2778 /* Filenames referenced by this compilation unit. */
2779 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2780
2781 /* A hash table of references to DIE's that describe declarations.
2782 The key is a DECL_UID() which is a unique number identifying each decl. */
2783 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2784
2785 /* A hash table of references to DIE's that describe COMMON blocks.
2786 The key is DECL_UID() ^ die_parent. */
2787 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2788
2789 typedef struct GTY(()) die_arg_entry_struct {
2790 dw_die_ref die;
2791 tree arg;
2792 } die_arg_entry;
2793
2794
2795 /* Node of the variable location list. */
2796 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2797 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2798 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2799 in mode of the EXPR_LIST node and first EXPR_LIST operand
2800 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2801 location or NULL for padding. For larger bitsizes,
2802 mode is 0 and first operand is a CONCAT with bitsize
2803 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2804 NULL as second operand. */
2805 rtx GTY (()) loc;
2806 const char * GTY (()) label;
2807 struct var_loc_node * GTY (()) next;
2808 };
2809
2810 /* Variable location list. */
2811 struct GTY (()) var_loc_list_def {
2812 struct var_loc_node * GTY (()) first;
2813
2814 /* Pointer to the last but one or last element of the
2815 chained list. If the list is empty, both first and
2816 last are NULL, if the list contains just one node
2817 or the last node certainly is not redundant, it points
2818 to the last node, otherwise points to the last but one.
2819 Do not mark it for GC because it is marked through the chain. */
2820 struct var_loc_node * GTY ((skip ("%h"))) last;
2821
2822 /* Pointer to the last element before section switch,
2823 if NULL, either sections weren't switched or first
2824 is after section switch. */
2825 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2826
2827 /* DECL_UID of the variable decl. */
2828 unsigned int decl_id;
2829 };
2830 typedef struct var_loc_list_def var_loc_list;
2831
2832 /* Call argument location list. */
2833 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2834 rtx GTY (()) call_arg_loc_note;
2835 const char * GTY (()) label;
2836 tree GTY (()) block;
2837 bool tail_call_p;
2838 rtx GTY (()) symbol_ref;
2839 struct call_arg_loc_node * GTY (()) next;
2840 };
2841
2842
2843 /* Table of decl location linked lists. */
2844 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2845
2846 /* Head and tail of call_arg_loc chain. */
2847 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2848 static struct call_arg_loc_node *call_arg_loc_last;
2849
2850 /* Number of call sites in the current function. */
2851 static int call_site_count = -1;
2852 /* Number of tail call sites in the current function. */
2853 static int tail_call_site_count = -1;
2854
2855 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2856 DIEs. */
2857 static vec<dw_die_ref> block_map;
2858
2859 /* A cached location list. */
2860 struct GTY (()) cached_dw_loc_list_def {
2861 /* The DECL_UID of the decl that this entry describes. */
2862 unsigned int decl_id;
2863
2864 /* The cached location list. */
2865 dw_loc_list_ref loc_list;
2866 };
2867 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2868
2869 /* Table of cached location lists. */
2870 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2871
2872 /* A pointer to the base of a list of references to DIE's that
2873 are uniquely identified by their tag, presence/absence of
2874 children DIE's, and list of attribute/value pairs. */
2875 static GTY((length ("abbrev_die_table_allocated")))
2876 dw_die_ref *abbrev_die_table;
2877
2878 /* Number of elements currently allocated for abbrev_die_table. */
2879 static GTY(()) unsigned abbrev_die_table_allocated;
2880
2881 /* Number of elements in type_die_table currently in use. */
2882 static GTY(()) unsigned abbrev_die_table_in_use;
2883
2884 /* Size (in elements) of increments by which we may expand the
2885 abbrev_die_table. */
2886 #define ABBREV_DIE_TABLE_INCREMENT 256
2887
2888 /* A global counter for generating labels for line number data. */
2889 static unsigned int line_info_label_num;
2890
2891 /* The current table to which we should emit line number information
2892 for the current function. This will be set up at the beginning of
2893 assembly for the function. */
2894 static dw_line_info_table *cur_line_info_table;
2895
2896 /* The two default tables of line number info. */
2897 static GTY(()) dw_line_info_table *text_section_line_info;
2898 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2899
2900 /* The set of all non-default tables of line number info. */
2901 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2902
2903 /* A flag to tell pubnames/types export if there is an info section to
2904 refer to. */
2905 static bool info_section_emitted;
2906
2907 /* A pointer to the base of a table that contains a list of publicly
2908 accessible names. */
2909 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2910
2911 /* A pointer to the base of a table that contains a list of publicly
2912 accessible types. */
2913 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2914
2915 /* A pointer to the base of a table that contains a list of macro
2916 defines/undefines (and file start/end markers). */
2917 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2918
2919 /* True if .debug_macinfo or .debug_macros section is going to be
2920 emitted. */
2921 #define have_macinfo \
2922 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2923 && !macinfo_table->is_empty ())
2924
2925 /* Array of dies for which we should generate .debug_ranges info. */
2926 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2927
2928 /* Number of elements currently allocated for ranges_table. */
2929 static GTY(()) unsigned ranges_table_allocated;
2930
2931 /* Number of elements in ranges_table currently in use. */
2932 static GTY(()) unsigned ranges_table_in_use;
2933
2934 /* Array of pairs of labels referenced in ranges_table. */
2935 static GTY ((length ("ranges_by_label_allocated")))
2936 dw_ranges_by_label_ref ranges_by_label;
2937
2938 /* Number of elements currently allocated for ranges_by_label. */
2939 static GTY(()) unsigned ranges_by_label_allocated;
2940
2941 /* Number of elements in ranges_by_label currently in use. */
2942 static GTY(()) unsigned ranges_by_label_in_use;
2943
2944 /* Size (in elements) of increments by which we may expand the
2945 ranges_table. */
2946 #define RANGES_TABLE_INCREMENT 64
2947
2948 /* Whether we have location lists that need outputting */
2949 static GTY(()) bool have_location_lists;
2950
2951 /* Unique label counter. */
2952 static GTY(()) unsigned int loclabel_num;
2953
2954 /* Unique label counter for point-of-call tables. */
2955 static GTY(()) unsigned int poc_label_num;
2956
2957 /* Record whether the function being analyzed contains inlined functions. */
2958 static int current_function_has_inlines;
2959
2960 /* The last file entry emitted by maybe_emit_file(). */
2961 static GTY(()) struct dwarf_file_data * last_emitted_file;
2962
2963 /* Number of internal labels generated by gen_internal_sym(). */
2964 static GTY(()) int label_num;
2965
2966 /* Cached result of previous call to lookup_filename. */
2967 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2968
2969 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2970
2971 /* Instances of generic types for which we need to generate debug
2972 info that describe their generic parameters and arguments. That
2973 generation needs to happen once all types are properly laid out so
2974 we do it at the end of compilation. */
2975 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2976
2977 /* Offset from the "steady-state frame pointer" to the frame base,
2978 within the current function. */
2979 static HOST_WIDE_INT frame_pointer_fb_offset;
2980 static bool frame_pointer_fb_offset_valid;
2981
2982 static vec<dw_die_ref> base_types;
2983
2984 /* Forward declarations for functions defined in this file. */
2985
2986 static int is_pseudo_reg (const_rtx);
2987 static tree type_main_variant (tree);
2988 static int is_tagged_type (const_tree);
2989 static const char *dwarf_tag_name (unsigned);
2990 static const char *dwarf_attr_name (unsigned);
2991 static const char *dwarf_form_name (unsigned);
2992 static tree decl_ultimate_origin (const_tree);
2993 static tree decl_class_context (tree);
2994 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2995 static inline enum dw_val_class AT_class (dw_attr_ref);
2996 static inline unsigned int AT_index (dw_attr_ref);
2997 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2998 static inline unsigned AT_flag (dw_attr_ref);
2999 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3000 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3001 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3002 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3003 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3004 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3005 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3006 unsigned int, unsigned char *);
3007 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3008 static hashval_t debug_str_do_hash (const void *);
3009 static int debug_str_eq (const void *, const void *);
3010 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3011 static inline const char *AT_string (dw_attr_ref);
3012 static enum dwarf_form AT_string_form (dw_attr_ref);
3013 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3014 static void add_AT_specification (dw_die_ref, dw_die_ref);
3015 static inline dw_die_ref AT_ref (dw_attr_ref);
3016 static inline int AT_ref_external (dw_attr_ref);
3017 static inline void set_AT_ref_external (dw_attr_ref, int);
3018 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3019 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3020 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3021 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3022 dw_loc_list_ref);
3023 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3024 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3025 static void remove_addr_table_entry (addr_table_entry *);
3026 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3027 static inline rtx AT_addr (dw_attr_ref);
3028 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3029 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3030 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3031 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3032 unsigned HOST_WIDE_INT);
3033 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3034 unsigned long, bool);
3035 static inline const char *AT_lbl (dw_attr_ref);
3036 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3037 static const char *get_AT_low_pc (dw_die_ref);
3038 static const char *get_AT_hi_pc (dw_die_ref);
3039 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3040 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3041 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3042 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3043 static bool is_cxx (void);
3044 static bool is_fortran (void);
3045 static bool is_ada (void);
3046 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3047 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3048 static void add_child_die (dw_die_ref, dw_die_ref);
3049 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3050 static dw_die_ref lookup_type_die (tree);
3051 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3052 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3053 static void equate_type_number_to_die (tree, dw_die_ref);
3054 static hashval_t decl_die_table_hash (const void *);
3055 static int decl_die_table_eq (const void *, const void *);
3056 static dw_die_ref lookup_decl_die (tree);
3057 static hashval_t common_block_die_table_hash (const void *);
3058 static int common_block_die_table_eq (const void *, const void *);
3059 static hashval_t decl_loc_table_hash (const void *);
3060 static int decl_loc_table_eq (const void *, const void *);
3061 static var_loc_list *lookup_decl_loc (const_tree);
3062 static void equate_decl_number_to_die (tree, dw_die_ref);
3063 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3064 static void print_spaces (FILE *);
3065 static void print_die (dw_die_ref, FILE *);
3066 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3067 static dw_die_ref pop_compile_unit (dw_die_ref);
3068 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3069 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3070 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3071 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3072 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3073 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3074 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3075 struct md5_ctx *, int *);
3076 struct checksum_attributes;
3077 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3078 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3079 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3080 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3081 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3082 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3083 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3084 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3085 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3086 static void compute_section_prefix (dw_die_ref);
3087 static int is_type_die (dw_die_ref);
3088 static int is_comdat_die (dw_die_ref);
3089 static int is_symbol_die (dw_die_ref);
3090 static inline bool is_template_instantiation (dw_die_ref);
3091 static void assign_symbol_names (dw_die_ref);
3092 static void break_out_includes (dw_die_ref);
3093 static int is_declaration_die (dw_die_ref);
3094 static int should_move_die_to_comdat (dw_die_ref);
3095 static dw_die_ref clone_as_declaration (dw_die_ref);
3096 static dw_die_ref clone_die (dw_die_ref);
3097 static dw_die_ref clone_tree (dw_die_ref);
3098 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3099 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3100 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3101 static dw_die_ref generate_skeleton (dw_die_ref);
3102 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3103 dw_die_ref,
3104 dw_die_ref);
3105 static void break_out_comdat_types (dw_die_ref);
3106 static void copy_decls_for_unworthy_types (dw_die_ref);
3107
3108 static void add_sibling_attributes (dw_die_ref);
3109 static void output_location_lists (dw_die_ref);
3110 static int constant_size (unsigned HOST_WIDE_INT);
3111 static unsigned long size_of_die (dw_die_ref);
3112 static void calc_die_sizes (dw_die_ref);
3113 static void calc_base_type_die_sizes (void);
3114 static void mark_dies (dw_die_ref);
3115 static void unmark_dies (dw_die_ref);
3116 static void unmark_all_dies (dw_die_ref);
3117 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3118 static unsigned long size_of_aranges (void);
3119 static enum dwarf_form value_format (dw_attr_ref);
3120 static void output_value_format (dw_attr_ref);
3121 static void output_abbrev_section (void);
3122 static void output_die_abbrevs (unsigned long, dw_die_ref);
3123 static void output_die_symbol (dw_die_ref);
3124 static void output_die (dw_die_ref);
3125 static void output_compilation_unit_header (void);
3126 static void output_comp_unit (dw_die_ref, int);
3127 static void output_comdat_type_unit (comdat_type_node *);
3128 static const char *dwarf2_name (tree, int);
3129 static void add_pubname (tree, dw_die_ref);
3130 static void add_enumerator_pubname (const char *, dw_die_ref);
3131 static void add_pubname_string (const char *, dw_die_ref);
3132 static void add_pubtype (tree, dw_die_ref);
3133 static void output_pubnames (vec<pubname_entry, va_gc> *);
3134 static void output_aranges (unsigned long);
3135 static unsigned int add_ranges_num (int);
3136 static unsigned int add_ranges (const_tree);
3137 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3138 bool *, bool);
3139 static void output_ranges (void);
3140 static dw_line_info_table *new_line_info_table (void);
3141 static void output_line_info (bool);
3142 static void output_file_names (void);
3143 static dw_die_ref base_type_die (tree);
3144 static int is_base_type (tree);
3145 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3146 static int decl_quals (const_tree);
3147 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3148 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3149 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3150 static int type_is_enum (const_tree);
3151 static unsigned int dbx_reg_number (const_rtx);
3152 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3153 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3154 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3155 enum var_init_status);
3156 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3157 enum var_init_status);
3158 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3159 enum var_init_status);
3160 static int is_based_loc (const_rtx);
3161 static bool resolve_one_addr (rtx *);
3162 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3163 enum var_init_status);
3164 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3165 enum var_init_status);
3166 static dw_loc_list_ref loc_list_from_tree (tree, int);
3167 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3168 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3169 static tree field_type (const_tree);
3170 static unsigned int simple_type_align_in_bits (const_tree);
3171 static unsigned int simple_decl_align_in_bits (const_tree);
3172 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3173 static HOST_WIDE_INT field_byte_offset (const_tree);
3174 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3175 dw_loc_list_ref);
3176 static void add_data_member_location_attribute (dw_die_ref, tree);
3177 static bool add_const_value_attribute (dw_die_ref, rtx);
3178 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3179 static void insert_wide_int (const wide_int &, unsigned char *, int);
3180 static void insert_float (const_rtx, unsigned char *);
3181 static rtx rtl_for_decl_location (tree);
3182 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3183 enum dwarf_attribute);
3184 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3185 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3186 static void add_name_attribute (dw_die_ref, const char *);
3187 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3188 static void add_comp_dir_attribute (dw_die_ref);
3189 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3190 static void add_subscript_info (dw_die_ref, tree, bool);
3191 static void add_byte_size_attribute (dw_die_ref, tree);
3192 static void add_bit_offset_attribute (dw_die_ref, tree);
3193 static void add_bit_size_attribute (dw_die_ref, tree);
3194 static void add_prototyped_attribute (dw_die_ref, tree);
3195 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3196 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3197 static void add_src_coords_attributes (dw_die_ref, tree);
3198 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3199 static void push_decl_scope (tree);
3200 static void pop_decl_scope (void);
3201 static dw_die_ref scope_die_for (tree, dw_die_ref);
3202 static inline int local_scope_p (dw_die_ref);
3203 static inline int class_scope_p (dw_die_ref);
3204 static inline int class_or_namespace_scope_p (dw_die_ref);
3205 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3206 static void add_calling_convention_attribute (dw_die_ref, tree);
3207 static const char *type_tag (const_tree);
3208 static tree member_declared_type (const_tree);
3209 #if 0
3210 static const char *decl_start_label (tree);
3211 #endif
3212 static void gen_array_type_die (tree, dw_die_ref);
3213 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3214 #if 0
3215 static void gen_entry_point_die (tree, dw_die_ref);
3216 #endif
3217 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3218 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3219 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3220 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3221 static void gen_formal_types_die (tree, dw_die_ref);
3222 static void gen_subprogram_die (tree, dw_die_ref);
3223 static void gen_variable_die (tree, tree, dw_die_ref);
3224 static void gen_const_die (tree, dw_die_ref);
3225 static void gen_label_die (tree, dw_die_ref);
3226 static void gen_lexical_block_die (tree, dw_die_ref, int);
3227 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3228 static void gen_field_die (tree, dw_die_ref);
3229 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3230 static dw_die_ref gen_compile_unit_die (const char *);
3231 static void gen_inheritance_die (tree, tree, dw_die_ref);
3232 static void gen_member_die (tree, dw_die_ref);
3233 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3234 enum debug_info_usage);
3235 static void gen_subroutine_type_die (tree, dw_die_ref);
3236 static void gen_typedef_die (tree, dw_die_ref);
3237 static void gen_type_die (tree, dw_die_ref);
3238 static void gen_block_die (tree, dw_die_ref, int);
3239 static void decls_for_scope (tree, dw_die_ref, int);
3240 static inline int is_redundant_typedef (const_tree);
3241 static bool is_naming_typedef_decl (const_tree);
3242 static inline dw_die_ref get_context_die (tree);
3243 static void gen_namespace_die (tree, dw_die_ref);
3244 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3245 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3246 static dw_die_ref force_decl_die (tree);
3247 static dw_die_ref force_type_die (tree);
3248 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3249 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3250 static struct dwarf_file_data * lookup_filename (const char *);
3251 static void retry_incomplete_types (void);
3252 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3253 static void gen_generic_params_dies (tree);
3254 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3255 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3256 static void splice_child_die (dw_die_ref, dw_die_ref);
3257 static int file_info_cmp (const void *, const void *);
3258 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3259 const char *, const char *);
3260 static void output_loc_list (dw_loc_list_ref);
3261 static char *gen_internal_sym (const char *);
3262 static bool want_pubnames (void);
3263
3264 static void prune_unmark_dies (dw_die_ref);
3265 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3266 static void prune_unused_types_mark (dw_die_ref, int);
3267 static void prune_unused_types_walk (dw_die_ref);
3268 static void prune_unused_types_walk_attribs (dw_die_ref);
3269 static void prune_unused_types_prune (dw_die_ref);
3270 static void prune_unused_types (void);
3271 static int maybe_emit_file (struct dwarf_file_data *fd);
3272 static inline const char *AT_vms_delta1 (dw_attr_ref);
3273 static inline const char *AT_vms_delta2 (dw_attr_ref);
3274 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3275 const char *, const char *);
3276 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3277 static void gen_remaining_tmpl_value_param_die_attribute (void);
3278 static bool generic_type_p (tree);
3279 static void schedule_generic_params_dies_gen (tree t);
3280 static void gen_scheduled_generic_parms_dies (void);
3281
3282 static const char *comp_dir_string (void);
3283
3284 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3285
3286 /* enum for tracking thread-local variables whose address is really an offset
3287 relative to the TLS pointer, which will need link-time relocation, but will
3288 not need relocation by the DWARF consumer. */
3289
3290 enum dtprel_bool
3291 {
3292 dtprel_false = 0,
3293 dtprel_true = 1
3294 };
3295
3296 /* Return the operator to use for an address of a variable. For dtprel_true, we
3297 use DW_OP_const*. For regular variables, which need both link-time
3298 relocation and consumer-level relocation (e.g., to account for shared objects
3299 loaded at a random address), we use DW_OP_addr*. */
3300
3301 static inline enum dwarf_location_atom
3302 dw_addr_op (enum dtprel_bool dtprel)
3303 {
3304 if (dtprel == dtprel_true)
3305 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3306 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3307 else
3308 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3309 }
3310
3311 /* Return a pointer to a newly allocated address location description. If
3312 dwarf_split_debug_info is true, then record the address with the appropriate
3313 relocation. */
3314 static inline dw_loc_descr_ref
3315 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3316 {
3317 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3318
3319 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3320 ref->dw_loc_oprnd1.v.val_addr = addr;
3321 ref->dtprel = dtprel;
3322 if (dwarf_split_debug_info)
3323 ref->dw_loc_oprnd1.val_entry
3324 = add_addr_table_entry (addr,
3325 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3326 else
3327 ref->dw_loc_oprnd1.val_entry = NULL;
3328
3329 return ref;
3330 }
3331
3332 /* Section names used to hold DWARF debugging information. */
3333
3334 #ifndef DEBUG_INFO_SECTION
3335 #define DEBUG_INFO_SECTION ".debug_info"
3336 #endif
3337 #ifndef DEBUG_DWO_INFO_SECTION
3338 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3339 #endif
3340 #ifndef DEBUG_ABBREV_SECTION
3341 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3342 #endif
3343 #ifndef DEBUG_DWO_ABBREV_SECTION
3344 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3345 #endif
3346 #ifndef DEBUG_ARANGES_SECTION
3347 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3348 #endif
3349 #ifndef DEBUG_ADDR_SECTION
3350 #define DEBUG_ADDR_SECTION ".debug_addr"
3351 #endif
3352 #ifndef DEBUG_NORM_MACINFO_SECTION
3353 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3354 #endif
3355 #ifndef DEBUG_DWO_MACINFO_SECTION
3356 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3357 #endif
3358 #ifndef DEBUG_MACINFO_SECTION
3359 #define DEBUG_MACINFO_SECTION \
3360 (!dwarf_split_debug_info \
3361 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3362 #endif
3363 #ifndef DEBUG_NORM_MACRO_SECTION
3364 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3365 #endif
3366 #ifndef DEBUG_DWO_MACRO_SECTION
3367 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3368 #endif
3369 #ifndef DEBUG_MACRO_SECTION
3370 #define DEBUG_MACRO_SECTION \
3371 (!dwarf_split_debug_info \
3372 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3373 #endif
3374 #ifndef DEBUG_LINE_SECTION
3375 #define DEBUG_LINE_SECTION ".debug_line"
3376 #endif
3377 #ifndef DEBUG_DWO_LINE_SECTION
3378 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3379 #endif
3380 #ifndef DEBUG_LOC_SECTION
3381 #define DEBUG_LOC_SECTION ".debug_loc"
3382 #endif
3383 #ifndef DEBUG_DWO_LOC_SECTION
3384 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3385 #endif
3386 #ifndef DEBUG_PUBNAMES_SECTION
3387 #define DEBUG_PUBNAMES_SECTION \
3388 ((debug_generate_pub_sections == 2) \
3389 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3390 #endif
3391 #ifndef DEBUG_PUBTYPES_SECTION
3392 #define DEBUG_PUBTYPES_SECTION \
3393 ((debug_generate_pub_sections == 2) \
3394 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3395 #endif
3396 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3397 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3398 #ifndef DEBUG_STR_OFFSETS_SECTION
3399 #define DEBUG_STR_OFFSETS_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3402 #endif
3403 #ifndef DEBUG_STR_DWO_SECTION
3404 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3405 #endif
3406 #ifndef DEBUG_STR_SECTION
3407 #define DEBUG_STR_SECTION ".debug_str"
3408 #endif
3409 #ifndef DEBUG_RANGES_SECTION
3410 #define DEBUG_RANGES_SECTION ".debug_ranges"
3411 #endif
3412
3413 /* Standard ELF section names for compiled code and data. */
3414 #ifndef TEXT_SECTION_NAME
3415 #define TEXT_SECTION_NAME ".text"
3416 #endif
3417
3418 /* Section flags for .debug_macinfo/.debug_macro section. */
3419 #define DEBUG_MACRO_SECTION_FLAGS \
3420 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3421
3422 /* Section flags for .debug_str section. */
3423 #define DEBUG_STR_SECTION_FLAGS \
3424 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3425 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3426 : SECTION_DEBUG)
3427
3428 /* Section flags for .debug_str.dwo section. */
3429 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3430
3431 /* Labels we insert at beginning sections we can reference instead of
3432 the section names themselves. */
3433
3434 #ifndef TEXT_SECTION_LABEL
3435 #define TEXT_SECTION_LABEL "Ltext"
3436 #endif
3437 #ifndef COLD_TEXT_SECTION_LABEL
3438 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3439 #endif
3440 #ifndef DEBUG_LINE_SECTION_LABEL
3441 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3442 #endif
3443 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3444 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3445 #endif
3446 #ifndef DEBUG_INFO_SECTION_LABEL
3447 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3448 #endif
3449 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3450 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3451 #endif
3452 #ifndef DEBUG_ABBREV_SECTION_LABEL
3453 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3454 #endif
3455 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3456 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3457 #endif
3458 #ifndef DEBUG_ADDR_SECTION_LABEL
3459 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3460 #endif
3461 #ifndef DEBUG_LOC_SECTION_LABEL
3462 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3463 #endif
3464 #ifndef DEBUG_RANGES_SECTION_LABEL
3465 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3466 #endif
3467 #ifndef DEBUG_MACINFO_SECTION_LABEL
3468 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3469 #endif
3470 #ifndef DEBUG_MACRO_SECTION_LABEL
3471 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3472 #endif
3473 #define SKELETON_COMP_DIE_ABBREV 1
3474 #define SKELETON_TYPE_DIE_ABBREV 2
3475
3476 /* Definitions of defaults for formats and names of various special
3477 (artificial) labels which may be generated within this file (when the -g
3478 options is used and DWARF2_DEBUGGING_INFO is in effect.
3479 If necessary, these may be overridden from within the tm.h file, but
3480 typically, overriding these defaults is unnecessary. */
3481
3482 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3495 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3496
3497 #ifndef TEXT_END_LABEL
3498 #define TEXT_END_LABEL "Letext"
3499 #endif
3500 #ifndef COLD_END_LABEL
3501 #define COLD_END_LABEL "Letext_cold"
3502 #endif
3503 #ifndef BLOCK_BEGIN_LABEL
3504 #define BLOCK_BEGIN_LABEL "LBB"
3505 #endif
3506 #ifndef BLOCK_END_LABEL
3507 #define BLOCK_END_LABEL "LBE"
3508 #endif
3509 #ifndef LINE_CODE_LABEL
3510 #define LINE_CODE_LABEL "LM"
3511 #endif
3512
3513 \f
3514 /* Return the root of the DIE's built for the current compilation unit. */
3515 static dw_die_ref
3516 comp_unit_die (void)
3517 {
3518 if (!single_comp_unit_die)
3519 single_comp_unit_die = gen_compile_unit_die (NULL);
3520 return single_comp_unit_die;
3521 }
3522
3523 /* We allow a language front-end to designate a function that is to be
3524 called to "demangle" any name before it is put into a DIE. */
3525
3526 static const char *(*demangle_name_func) (const char *);
3527
3528 void
3529 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3530 {
3531 demangle_name_func = func;
3532 }
3533
3534 /* Test if rtl node points to a pseudo register. */
3535
3536 static inline int
3537 is_pseudo_reg (const_rtx rtl)
3538 {
3539 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3540 || (GET_CODE (rtl) == SUBREG
3541 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3542 }
3543
3544 /* Return a reference to a type, with its const and volatile qualifiers
3545 removed. */
3546
3547 static inline tree
3548 type_main_variant (tree type)
3549 {
3550 type = TYPE_MAIN_VARIANT (type);
3551
3552 /* ??? There really should be only one main variant among any group of
3553 variants of a given type (and all of the MAIN_VARIANT values for all
3554 members of the group should point to that one type) but sometimes the C
3555 front-end messes this up for array types, so we work around that bug
3556 here. */
3557 if (TREE_CODE (type) == ARRAY_TYPE)
3558 while (type != TYPE_MAIN_VARIANT (type))
3559 type = TYPE_MAIN_VARIANT (type);
3560
3561 return type;
3562 }
3563
3564 /* Return nonzero if the given type node represents a tagged type. */
3565
3566 static inline int
3567 is_tagged_type (const_tree type)
3568 {
3569 enum tree_code code = TREE_CODE (type);
3570
3571 return (code == RECORD_TYPE || code == UNION_TYPE
3572 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3573 }
3574
3575 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3576
3577 static void
3578 get_ref_die_offset_label (char *label, dw_die_ref ref)
3579 {
3580 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3581 }
3582
3583 /* Return die_offset of a DIE reference to a base type. */
3584
3585 static unsigned long int
3586 get_base_type_offset (dw_die_ref ref)
3587 {
3588 if (ref->die_offset)
3589 return ref->die_offset;
3590 if (comp_unit_die ()->die_abbrev)
3591 {
3592 calc_base_type_die_sizes ();
3593 gcc_assert (ref->die_offset);
3594 }
3595 return ref->die_offset;
3596 }
3597
3598 /* Return die_offset of a DIE reference other than base type. */
3599
3600 static unsigned long int
3601 get_ref_die_offset (dw_die_ref ref)
3602 {
3603 gcc_assert (ref->die_offset);
3604 return ref->die_offset;
3605 }
3606
3607 /* Convert a DIE tag into its string name. */
3608
3609 static const char *
3610 dwarf_tag_name (unsigned int tag)
3611 {
3612 const char *name = get_DW_TAG_name (tag);
3613
3614 if (name != NULL)
3615 return name;
3616
3617 return "DW_TAG_<unknown>";
3618 }
3619
3620 /* Convert a DWARF attribute code into its string name. */
3621
3622 static const char *
3623 dwarf_attr_name (unsigned int attr)
3624 {
3625 const char *name;
3626
3627 switch (attr)
3628 {
3629 #if VMS_DEBUGGING_INFO
3630 case DW_AT_HP_prologue:
3631 return "DW_AT_HP_prologue";
3632 #else
3633 case DW_AT_MIPS_loop_unroll_factor:
3634 return "DW_AT_MIPS_loop_unroll_factor";
3635 #endif
3636
3637 #if VMS_DEBUGGING_INFO
3638 case DW_AT_HP_epilogue:
3639 return "DW_AT_HP_epilogue";
3640 #else
3641 case DW_AT_MIPS_stride:
3642 return "DW_AT_MIPS_stride";
3643 #endif
3644 }
3645
3646 name = get_DW_AT_name (attr);
3647
3648 if (name != NULL)
3649 return name;
3650
3651 return "DW_AT_<unknown>";
3652 }
3653
3654 /* Convert a DWARF value form code into its string name. */
3655
3656 static const char *
3657 dwarf_form_name (unsigned int form)
3658 {
3659 const char *name = get_DW_FORM_name (form);
3660
3661 if (name != NULL)
3662 return name;
3663
3664 return "DW_FORM_<unknown>";
3665 }
3666 \f
3667 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3668 instance of an inlined instance of a decl which is local to an inline
3669 function, so we have to trace all of the way back through the origin chain
3670 to find out what sort of node actually served as the original seed for the
3671 given block. */
3672
3673 static tree
3674 decl_ultimate_origin (const_tree decl)
3675 {
3676 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3677 return NULL_TREE;
3678
3679 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3680 nodes in the function to point to themselves; ignore that if
3681 we're trying to output the abstract instance of this function. */
3682 if (/*DECL_ABSTRACT (decl) &&*/ DECL_ABSTRACT_ORIGIN (decl) == decl)
3683 return NULL_TREE;
3684
3685 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3686 most distant ancestor, this should never happen. */
3687 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3688
3689 return DECL_ABSTRACT_ORIGIN (decl);
3690 }
3691
3692 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3693 of a virtual function may refer to a base class, so we check the 'this'
3694 parameter. */
3695
3696 static tree
3697 decl_class_context (tree decl)
3698 {
3699 tree context = NULL_TREE;
3700
3701 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3702 context = DECL_CONTEXT (decl);
3703 else
3704 context = TYPE_MAIN_VARIANT
3705 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3706
3707 if (context && !TYPE_P (context))
3708 context = NULL_TREE;
3709
3710 return context;
3711 }
3712 \f
3713 /* Add an attribute/value pair to a DIE. */
3714
3715 static inline void
3716 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3717 {
3718 /* Maybe this should be an assert? */
3719 if (die == NULL)
3720 return;
3721
3722 vec_safe_reserve (die->die_attr, 1);
3723 vec_safe_push (die->die_attr, *attr);
3724 }
3725
3726 static inline enum dw_val_class
3727 AT_class (dw_attr_ref a)
3728 {
3729 return a->dw_attr_val.val_class;
3730 }
3731
3732 /* Return the index for any attribute that will be referenced with a
3733 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3734 are stored in dw_attr_val.v.val_str for reference counting
3735 pruning. */
3736
3737 static inline unsigned int
3738 AT_index (dw_attr_ref a)
3739 {
3740 if (AT_class (a) == dw_val_class_str)
3741 return a->dw_attr_val.v.val_str->index;
3742 else if (a->dw_attr_val.val_entry != NULL)
3743 return a->dw_attr_val.val_entry->index;
3744 return NOT_INDEXED;
3745 }
3746
3747 /* Add a flag value attribute to a DIE. */
3748
3749 static inline void
3750 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3751 {
3752 dw_attr_node attr;
3753
3754 attr.dw_attr = attr_kind;
3755 attr.dw_attr_val.val_class = dw_val_class_flag;
3756 attr.dw_attr_val.val_entry = NULL;
3757 attr.dw_attr_val.v.val_flag = flag;
3758 add_dwarf_attr (die, &attr);
3759 }
3760
3761 static inline unsigned
3762 AT_flag (dw_attr_ref a)
3763 {
3764 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3765 return a->dw_attr_val.v.val_flag;
3766 }
3767
3768 /* Add a signed integer attribute value to a DIE. */
3769
3770 static inline void
3771 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3772 {
3773 dw_attr_node attr;
3774
3775 attr.dw_attr = attr_kind;
3776 attr.dw_attr_val.val_class = dw_val_class_const;
3777 attr.dw_attr_val.val_entry = NULL;
3778 attr.dw_attr_val.v.val_int = int_val;
3779 add_dwarf_attr (die, &attr);
3780 }
3781
3782 static inline HOST_WIDE_INT
3783 AT_int (dw_attr_ref a)
3784 {
3785 gcc_assert (a && AT_class (a) == dw_val_class_const);
3786 return a->dw_attr_val.v.val_int;
3787 }
3788
3789 /* Add an unsigned integer attribute value to a DIE. */
3790
3791 static inline void
3792 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3793 unsigned HOST_WIDE_INT unsigned_val)
3794 {
3795 dw_attr_node attr;
3796
3797 attr.dw_attr = attr_kind;
3798 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3799 attr.dw_attr_val.val_entry = NULL;
3800 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3801 add_dwarf_attr (die, &attr);
3802 }
3803
3804 static inline unsigned HOST_WIDE_INT
3805 AT_unsigned (dw_attr_ref a)
3806 {
3807 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3808 return a->dw_attr_val.v.val_unsigned;
3809 }
3810
3811 /* Add an unsigned wide integer attribute value to a DIE. */
3812
3813 static inline void
3814 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3815 const wide_int& w)
3816 {
3817 dw_attr_node attr;
3818
3819 attr.dw_attr = attr_kind;
3820 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3821 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3822 *attr.dw_attr_val.v.val_wide = w;
3823 add_dwarf_attr (die, &attr);
3824 }
3825
3826 /* Add an unsigned double integer attribute value to a DIE. */
3827
3828 static inline void
3829 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3830 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3831 {
3832 dw_attr_node attr;
3833
3834 attr.dw_attr = attr_kind;
3835 attr.dw_attr_val.val_class = dw_val_class_const_double;
3836 attr.dw_attr_val.val_entry = NULL;
3837 attr.dw_attr_val.v.val_double.high = high;
3838 attr.dw_attr_val.v.val_double.low = low;
3839 add_dwarf_attr (die, &attr);
3840 }
3841
3842 /* Add a floating point attribute value to a DIE and return it. */
3843
3844 static inline void
3845 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3846 unsigned int length, unsigned int elt_size, unsigned char *array)
3847 {
3848 dw_attr_node attr;
3849
3850 attr.dw_attr = attr_kind;
3851 attr.dw_attr_val.val_class = dw_val_class_vec;
3852 attr.dw_attr_val.val_entry = NULL;
3853 attr.dw_attr_val.v.val_vec.length = length;
3854 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3855 attr.dw_attr_val.v.val_vec.array = array;
3856 add_dwarf_attr (die, &attr);
3857 }
3858
3859 /* Add an 8-byte data attribute value to a DIE. */
3860
3861 static inline void
3862 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3863 unsigned char data8[8])
3864 {
3865 dw_attr_node attr;
3866
3867 attr.dw_attr = attr_kind;
3868 attr.dw_attr_val.val_class = dw_val_class_data8;
3869 attr.dw_attr_val.val_entry = NULL;
3870 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3871 add_dwarf_attr (die, &attr);
3872 }
3873
3874 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3875 dwarf_split_debug_info, address attributes in dies destined for the
3876 final executable have force_direct set to avoid using indexed
3877 references. */
3878
3879 static inline void
3880 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3881 bool force_direct)
3882 {
3883 dw_attr_node attr;
3884 char * lbl_id;
3885
3886 lbl_id = xstrdup (lbl_low);
3887 attr.dw_attr = DW_AT_low_pc;
3888 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3889 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3890 if (dwarf_split_debug_info && !force_direct)
3891 attr.dw_attr_val.val_entry
3892 = add_addr_table_entry (lbl_id, ate_kind_label);
3893 else
3894 attr.dw_attr_val.val_entry = NULL;
3895 add_dwarf_attr (die, &attr);
3896
3897 attr.dw_attr = DW_AT_high_pc;
3898 if (dwarf_version < 4)
3899 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3900 else
3901 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3902 lbl_id = xstrdup (lbl_high);
3903 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3904 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3905 && dwarf_split_debug_info && !force_direct)
3906 attr.dw_attr_val.val_entry
3907 = add_addr_table_entry (lbl_id, ate_kind_label);
3908 else
3909 attr.dw_attr_val.val_entry = NULL;
3910 add_dwarf_attr (die, &attr);
3911 }
3912
3913 /* Hash and equality functions for debug_str_hash. */
3914
3915 static hashval_t
3916 debug_str_do_hash (const void *x)
3917 {
3918 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3919 }
3920
3921 static int
3922 debug_str_eq (const void *x1, const void *x2)
3923 {
3924 return strcmp ((((const struct indirect_string_node *)x1)->str),
3925 (const char *)x2) == 0;
3926 }
3927
3928 /* Add STR to the given string hash table. */
3929
3930 static struct indirect_string_node *
3931 find_AT_string_in_table (const char *str, htab_t table)
3932 {
3933 struct indirect_string_node *node;
3934 void **slot;
3935
3936 slot = htab_find_slot_with_hash (table, str,
3937 htab_hash_string (str), INSERT);
3938 if (*slot == NULL)
3939 {
3940 node = ggc_cleared_alloc<indirect_string_node> ();
3941 node->str = ggc_strdup (str);
3942 *slot = node;
3943 }
3944 else
3945 node = (struct indirect_string_node *) *slot;
3946
3947 node->refcount++;
3948 return node;
3949 }
3950
3951 /* Add STR to the indirect string hash table. */
3952
3953 static struct indirect_string_node *
3954 find_AT_string (const char *str)
3955 {
3956 if (! debug_str_hash)
3957 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3958 debug_str_eq, NULL);
3959
3960 return find_AT_string_in_table (str, debug_str_hash);
3961 }
3962
3963 /* Add a string attribute value to a DIE. */
3964
3965 static inline void
3966 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3967 {
3968 dw_attr_node attr;
3969 struct indirect_string_node *node;
3970
3971 node = find_AT_string (str);
3972
3973 attr.dw_attr = attr_kind;
3974 attr.dw_attr_val.val_class = dw_val_class_str;
3975 attr.dw_attr_val.val_entry = NULL;
3976 attr.dw_attr_val.v.val_str = node;
3977 add_dwarf_attr (die, &attr);
3978 }
3979
3980 static inline const char *
3981 AT_string (dw_attr_ref a)
3982 {
3983 gcc_assert (a && AT_class (a) == dw_val_class_str);
3984 return a->dw_attr_val.v.val_str->str;
3985 }
3986
3987 /* Call this function directly to bypass AT_string_form's logic to put
3988 the string inline in the die. */
3989
3990 static void
3991 set_indirect_string (struct indirect_string_node *node)
3992 {
3993 char label[32];
3994 /* Already indirect is a no op. */
3995 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3996 {
3997 gcc_assert (node->label);
3998 return;
3999 }
4000 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4001 ++dw2_string_counter;
4002 node->label = xstrdup (label);
4003
4004 if (!dwarf_split_debug_info)
4005 {
4006 node->form = DW_FORM_strp;
4007 node->index = NOT_INDEXED;
4008 }
4009 else
4010 {
4011 node->form = DW_FORM_GNU_str_index;
4012 node->index = NO_INDEX_ASSIGNED;
4013 }
4014 }
4015
4016 /* Find out whether a string should be output inline in DIE
4017 or out-of-line in .debug_str section. */
4018
4019 static enum dwarf_form
4020 find_string_form (struct indirect_string_node *node)
4021 {
4022 unsigned int len;
4023
4024 if (node->form)
4025 return node->form;
4026
4027 len = strlen (node->str) + 1;
4028
4029 /* If the string is shorter or equal to the size of the reference, it is
4030 always better to put it inline. */
4031 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4032 return node->form = DW_FORM_string;
4033
4034 /* If we cannot expect the linker to merge strings in .debug_str
4035 section, only put it into .debug_str if it is worth even in this
4036 single module. */
4037 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4038 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4039 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4040 return node->form = DW_FORM_string;
4041
4042 set_indirect_string (node);
4043
4044 return node->form;
4045 }
4046
4047 /* Find out whether the string referenced from the attribute should be
4048 output inline in DIE or out-of-line in .debug_str section. */
4049
4050 static enum dwarf_form
4051 AT_string_form (dw_attr_ref a)
4052 {
4053 gcc_assert (a && AT_class (a) == dw_val_class_str);
4054 return find_string_form (a->dw_attr_val.v.val_str);
4055 }
4056
4057 /* Add a DIE reference attribute value to a DIE. */
4058
4059 static inline void
4060 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4061 {
4062 dw_attr_node attr;
4063
4064 #ifdef ENABLE_CHECKING
4065 gcc_assert (targ_die != NULL);
4066 #else
4067 /* With LTO we can end up trying to reference something we didn't create
4068 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4069 if (targ_die == NULL)
4070 return;
4071 #endif
4072
4073 attr.dw_attr = attr_kind;
4074 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4075 attr.dw_attr_val.val_entry = NULL;
4076 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4077 attr.dw_attr_val.v.val_die_ref.external = 0;
4078 add_dwarf_attr (die, &attr);
4079 }
4080
4081 /* Change DIE reference REF to point to NEW_DIE instead. */
4082
4083 static inline void
4084 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4085 {
4086 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4087 ref->dw_attr_val.v.val_die_ref.die = new_die;
4088 ref->dw_attr_val.v.val_die_ref.external = 0;
4089 }
4090
4091 /* Add an AT_specification attribute to a DIE, and also make the back
4092 pointer from the specification to the definition. */
4093
4094 static inline void
4095 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4096 {
4097 add_AT_die_ref (die, DW_AT_specification, targ_die);
4098 gcc_assert (!targ_die->die_definition);
4099 targ_die->die_definition = die;
4100 }
4101
4102 static inline dw_die_ref
4103 AT_ref (dw_attr_ref a)
4104 {
4105 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4106 return a->dw_attr_val.v.val_die_ref.die;
4107 }
4108
4109 static inline int
4110 AT_ref_external (dw_attr_ref a)
4111 {
4112 if (a && AT_class (a) == dw_val_class_die_ref)
4113 return a->dw_attr_val.v.val_die_ref.external;
4114
4115 return 0;
4116 }
4117
4118 static inline void
4119 set_AT_ref_external (dw_attr_ref a, int i)
4120 {
4121 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4122 a->dw_attr_val.v.val_die_ref.external = i;
4123 }
4124
4125 /* Add an FDE reference attribute value to a DIE. */
4126
4127 static inline void
4128 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4129 {
4130 dw_attr_node attr;
4131
4132 attr.dw_attr = attr_kind;
4133 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4134 attr.dw_attr_val.val_entry = NULL;
4135 attr.dw_attr_val.v.val_fde_index = targ_fde;
4136 add_dwarf_attr (die, &attr);
4137 }
4138
4139 /* Add a location description attribute value to a DIE. */
4140
4141 static inline void
4142 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4143 {
4144 dw_attr_node attr;
4145
4146 attr.dw_attr = attr_kind;
4147 attr.dw_attr_val.val_class = dw_val_class_loc;
4148 attr.dw_attr_val.val_entry = NULL;
4149 attr.dw_attr_val.v.val_loc = loc;
4150 add_dwarf_attr (die, &attr);
4151 }
4152
4153 static inline dw_loc_descr_ref
4154 AT_loc (dw_attr_ref a)
4155 {
4156 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4157 return a->dw_attr_val.v.val_loc;
4158 }
4159
4160 static inline void
4161 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4162 {
4163 dw_attr_node attr;
4164
4165 attr.dw_attr = attr_kind;
4166 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4167 attr.dw_attr_val.val_entry = NULL;
4168 attr.dw_attr_val.v.val_loc_list = loc_list;
4169 add_dwarf_attr (die, &attr);
4170 have_location_lists = true;
4171 }
4172
4173 static inline dw_loc_list_ref
4174 AT_loc_list (dw_attr_ref a)
4175 {
4176 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4177 return a->dw_attr_val.v.val_loc_list;
4178 }
4179
4180 static inline dw_loc_list_ref *
4181 AT_loc_list_ptr (dw_attr_ref a)
4182 {
4183 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4184 return &a->dw_attr_val.v.val_loc_list;
4185 }
4186
4187 /* Table of entries into the .debug_addr section. */
4188
4189 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4190
4191 /* Hash an address_table_entry. */
4192
4193 static hashval_t
4194 addr_table_entry_do_hash (const void *x)
4195 {
4196 const addr_table_entry *a = (const addr_table_entry *) x;
4197 inchash::hash hstate;
4198 switch (a->kind)
4199 {
4200 case ate_kind_rtx:
4201 hstate.add_int (0);
4202 break;
4203 case ate_kind_rtx_dtprel:
4204 hstate.add_int (1);
4205 break;
4206 case ate_kind_label:
4207 return htab_hash_string (a->addr.label);
4208 default:
4209 gcc_unreachable ();
4210 }
4211 inchash::add_rtx (a->addr.rtl, hstate);
4212 return hstate.end ();
4213 }
4214
4215 /* Determine equality for two address_table_entries. */
4216
4217 static int
4218 addr_table_entry_eq (const void *x1, const void *x2)
4219 {
4220 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4221 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4222
4223 if (a1->kind != a2->kind)
4224 return 0;
4225 switch (a1->kind)
4226 {
4227 case ate_kind_rtx:
4228 case ate_kind_rtx_dtprel:
4229 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4230 case ate_kind_label:
4231 return strcmp (a1->addr.label, a2->addr.label) == 0;
4232 default:
4233 gcc_unreachable ();
4234 }
4235 }
4236
4237 /* Initialize an addr_table_entry. */
4238
4239 void
4240 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4241 {
4242 e->kind = kind;
4243 switch (kind)
4244 {
4245 case ate_kind_rtx:
4246 case ate_kind_rtx_dtprel:
4247 e->addr.rtl = (rtx) addr;
4248 break;
4249 case ate_kind_label:
4250 e->addr.label = (char *) addr;
4251 break;
4252 }
4253 e->refcount = 0;
4254 e->index = NO_INDEX_ASSIGNED;
4255 }
4256
4257 /* Add attr to the address table entry to the table. Defer setting an
4258 index until output time. */
4259
4260 static addr_table_entry *
4261 add_addr_table_entry (void *addr, enum ate_kind kind)
4262 {
4263 addr_table_entry *node;
4264 addr_table_entry finder;
4265 void **slot;
4266
4267 gcc_assert (dwarf_split_debug_info);
4268 if (! addr_index_table)
4269 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4270 addr_table_entry_eq, NULL);
4271 init_addr_table_entry (&finder, kind, addr);
4272 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4273
4274 if (*slot == HTAB_EMPTY_ENTRY)
4275 {
4276 node = ggc_cleared_alloc<addr_table_entry> ();
4277 init_addr_table_entry (node, kind, addr);
4278 *slot = node;
4279 }
4280 else
4281 node = (addr_table_entry *) *slot;
4282
4283 node->refcount++;
4284 return node;
4285 }
4286
4287 /* Remove an entry from the addr table by decrementing its refcount.
4288 Strictly, decrementing the refcount would be enough, but the
4289 assertion that the entry is actually in the table has found
4290 bugs. */
4291
4292 static void
4293 remove_addr_table_entry (addr_table_entry *entry)
4294 {
4295 gcc_assert (dwarf_split_debug_info && addr_index_table);
4296 /* After an index is assigned, the table is frozen. */
4297 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4298 entry->refcount--;
4299 }
4300
4301 /* Given a location list, remove all addresses it refers to from the
4302 address_table. */
4303
4304 static void
4305 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4306 {
4307 for (; descr; descr = descr->dw_loc_next)
4308 if (descr->dw_loc_oprnd1.val_entry != NULL)
4309 {
4310 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4311 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4312 }
4313 }
4314
4315 /* A helper function for dwarf2out_finish called through
4316 htab_traverse. Assign an addr_table_entry its index. All entries
4317 must be collected into the table when this function is called,
4318 because the indexing code relies on htab_traverse to traverse nodes
4319 in the same order for each run. */
4320
4321 static int
4322 index_addr_table_entry (void **h, void *v)
4323 {
4324 addr_table_entry *node = (addr_table_entry *) *h;
4325 unsigned int *index = (unsigned int *) v;
4326
4327 /* Don't index unreferenced nodes. */
4328 if (node->refcount == 0)
4329 return 1;
4330
4331 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4332 node->index = *index;
4333 *index += 1;
4334
4335 return 1;
4336 }
4337
4338 /* Add an address constant attribute value to a DIE. When using
4339 dwarf_split_debug_info, address attributes in dies destined for the
4340 final executable should be direct references--setting the parameter
4341 force_direct ensures this behavior. */
4342
4343 static inline void
4344 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4345 bool force_direct)
4346 {
4347 dw_attr_node attr;
4348
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_addr;
4351 attr.dw_attr_val.v.val_addr = addr;
4352 if (dwarf_split_debug_info && !force_direct)
4353 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4354 else
4355 attr.dw_attr_val.val_entry = NULL;
4356 add_dwarf_attr (die, &attr);
4357 }
4358
4359 /* Get the RTX from to an address DIE attribute. */
4360
4361 static inline rtx
4362 AT_addr (dw_attr_ref a)
4363 {
4364 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4365 return a->dw_attr_val.v.val_addr;
4366 }
4367
4368 /* Add a file attribute value to a DIE. */
4369
4370 static inline void
4371 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4372 struct dwarf_file_data *fd)
4373 {
4374 dw_attr_node attr;
4375
4376 attr.dw_attr = attr_kind;
4377 attr.dw_attr_val.val_class = dw_val_class_file;
4378 attr.dw_attr_val.val_entry = NULL;
4379 attr.dw_attr_val.v.val_file = fd;
4380 add_dwarf_attr (die, &attr);
4381 }
4382
4383 /* Get the dwarf_file_data from a file DIE attribute. */
4384
4385 static inline struct dwarf_file_data *
4386 AT_file (dw_attr_ref a)
4387 {
4388 gcc_assert (a && AT_class (a) == dw_val_class_file);
4389 return a->dw_attr_val.v.val_file;
4390 }
4391
4392 /* Add a vms delta attribute value to a DIE. */
4393
4394 static inline void
4395 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4396 const char *lbl1, const char *lbl2)
4397 {
4398 dw_attr_node attr;
4399
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4402 attr.dw_attr_val.val_entry = NULL;
4403 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4404 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4405 add_dwarf_attr (die, &attr);
4406 }
4407
4408 /* Add a label identifier attribute value to a DIE. */
4409
4410 static inline void
4411 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4412 const char *lbl_id)
4413 {
4414 dw_attr_node attr;
4415
4416 attr.dw_attr = attr_kind;
4417 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4418 attr.dw_attr_val.val_entry = NULL;
4419 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4420 if (dwarf_split_debug_info)
4421 attr.dw_attr_val.val_entry
4422 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4423 ate_kind_label);
4424 add_dwarf_attr (die, &attr);
4425 }
4426
4427 /* Add a section offset attribute value to a DIE, an offset into the
4428 debug_line section. */
4429
4430 static inline void
4431 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4432 const char *label)
4433 {
4434 dw_attr_node attr;
4435
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4440 add_dwarf_attr (die, &attr);
4441 }
4442
4443 /* Add a section offset attribute value to a DIE, an offset into the
4444 debug_macinfo section. */
4445
4446 static inline void
4447 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4448 const char *label)
4449 {
4450 dw_attr_node attr;
4451
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_macptr;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4456 add_dwarf_attr (die, &attr);
4457 }
4458
4459 /* Add an offset attribute value to a DIE. */
4460
4461 static inline void
4462 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 unsigned HOST_WIDE_INT offset)
4464 {
4465 dw_attr_node attr;
4466
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_offset;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_offset = offset;
4471 add_dwarf_attr (die, &attr);
4472 }
4473
4474 /* Add a range_list attribute value to a DIE. When using
4475 dwarf_split_debug_info, address attributes in dies destined for the
4476 final executable should be direct references--setting the parameter
4477 force_direct ensures this behavior. */
4478
4479 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4480 #define RELOCATED_OFFSET (NULL)
4481
4482 static void
4483 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4484 long unsigned int offset, bool force_direct)
4485 {
4486 dw_attr_node attr;
4487
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_range_list;
4490 /* For the range_list attribute, use val_entry to store whether the
4491 offset should follow split-debug-info or normal semantics. This
4492 value is read in output_range_list_offset. */
4493 if (dwarf_split_debug_info && !force_direct)
4494 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4495 else
4496 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4497 attr.dw_attr_val.v.val_offset = offset;
4498 add_dwarf_attr (die, &attr);
4499 }
4500
4501 /* Return the start label of a delta attribute. */
4502
4503 static inline const char *
4504 AT_vms_delta1 (dw_attr_ref a)
4505 {
4506 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4507 return a->dw_attr_val.v.val_vms_delta.lbl1;
4508 }
4509
4510 /* Return the end label of a delta attribute. */
4511
4512 static inline const char *
4513 AT_vms_delta2 (dw_attr_ref a)
4514 {
4515 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4516 return a->dw_attr_val.v.val_vms_delta.lbl2;
4517 }
4518
4519 static inline const char *
4520 AT_lbl (dw_attr_ref a)
4521 {
4522 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4523 || AT_class (a) == dw_val_class_lineptr
4524 || AT_class (a) == dw_val_class_macptr
4525 || AT_class (a) == dw_val_class_high_pc));
4526 return a->dw_attr_val.v.val_lbl_id;
4527 }
4528
4529 /* Get the attribute of type attr_kind. */
4530
4531 static dw_attr_ref
4532 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4533 {
4534 dw_attr_ref a;
4535 unsigned ix;
4536 dw_die_ref spec = NULL;
4537
4538 if (! die)
4539 return NULL;
4540
4541 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4542 if (a->dw_attr == attr_kind)
4543 return a;
4544 else if (a->dw_attr == DW_AT_specification
4545 || a->dw_attr == DW_AT_abstract_origin)
4546 spec = AT_ref (a);
4547
4548 if (spec)
4549 return get_AT (spec, attr_kind);
4550
4551 return NULL;
4552 }
4553
4554 /* Returns the parent of the declaration of DIE. */
4555
4556 static dw_die_ref
4557 get_die_parent (dw_die_ref die)
4558 {
4559 dw_die_ref t;
4560
4561 if (!die)
4562 return NULL;
4563
4564 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4565 || (t = get_AT_ref (die, DW_AT_specification)))
4566 die = t;
4567
4568 return die->die_parent;
4569 }
4570
4571 /* Return the "low pc" attribute value, typically associated with a subprogram
4572 DIE. Return null if the "low pc" attribute is either not present, or if it
4573 cannot be represented as an assembler label identifier. */
4574
4575 static inline const char *
4576 get_AT_low_pc (dw_die_ref die)
4577 {
4578 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4579
4580 return a ? AT_lbl (a) : NULL;
4581 }
4582
4583 /* Return the "high pc" attribute value, typically associated with a subprogram
4584 DIE. Return null if the "high pc" attribute is either not present, or if it
4585 cannot be represented as an assembler label identifier. */
4586
4587 static inline const char *
4588 get_AT_hi_pc (dw_die_ref die)
4589 {
4590 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4591
4592 return a ? AT_lbl (a) : NULL;
4593 }
4594
4595 /* Return the value of the string attribute designated by ATTR_KIND, or
4596 NULL if it is not present. */
4597
4598 static inline const char *
4599 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4600 {
4601 dw_attr_ref a = get_AT (die, attr_kind);
4602
4603 return a ? AT_string (a) : NULL;
4604 }
4605
4606 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4607 if it is not present. */
4608
4609 static inline int
4610 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4611 {
4612 dw_attr_ref a = get_AT (die, attr_kind);
4613
4614 return a ? AT_flag (a) : 0;
4615 }
4616
4617 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4618 if it is not present. */
4619
4620 static inline unsigned
4621 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4622 {
4623 dw_attr_ref a = get_AT (die, attr_kind);
4624
4625 return a ? AT_unsigned (a) : 0;
4626 }
4627
4628 static inline dw_die_ref
4629 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4630 {
4631 dw_attr_ref a = get_AT (die, attr_kind);
4632
4633 return a ? AT_ref (a) : NULL;
4634 }
4635
4636 static inline struct dwarf_file_data *
4637 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4638 {
4639 dw_attr_ref a = get_AT (die, attr_kind);
4640
4641 return a ? AT_file (a) : NULL;
4642 }
4643
4644 /* Return TRUE if the language is C++. */
4645
4646 static inline bool
4647 is_cxx (void)
4648 {
4649 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4650
4651 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4652 }
4653
4654 /* Return TRUE if the language is Java. */
4655
4656 static inline bool
4657 is_java (void)
4658 {
4659 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4660
4661 return lang == DW_LANG_Java;
4662 }
4663
4664 /* Return TRUE if the language is Fortran. */
4665
4666 static inline bool
4667 is_fortran (void)
4668 {
4669 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4670
4671 return (lang == DW_LANG_Fortran77
4672 || lang == DW_LANG_Fortran90
4673 || lang == DW_LANG_Fortran95);
4674 }
4675
4676 /* Return TRUE if the language is Ada. */
4677
4678 static inline bool
4679 is_ada (void)
4680 {
4681 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4682
4683 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4684 }
4685
4686 /* Remove the specified attribute if present. */
4687
4688 static void
4689 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 {
4691 dw_attr_ref a;
4692 unsigned ix;
4693
4694 if (! die)
4695 return;
4696
4697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4698 if (a->dw_attr == attr_kind)
4699 {
4700 if (AT_class (a) == dw_val_class_str)
4701 if (a->dw_attr_val.v.val_str->refcount)
4702 a->dw_attr_val.v.val_str->refcount--;
4703
4704 /* vec::ordered_remove should help reduce the number of abbrevs
4705 that are needed. */
4706 die->die_attr->ordered_remove (ix);
4707 return;
4708 }
4709 }
4710
4711 /* Remove CHILD from its parent. PREV must have the property that
4712 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4713
4714 static void
4715 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4716 {
4717 gcc_assert (child->die_parent == prev->die_parent);
4718 gcc_assert (prev->die_sib == child);
4719 if (prev == child)
4720 {
4721 gcc_assert (child->die_parent->die_child == child);
4722 prev = NULL;
4723 }
4724 else
4725 prev->die_sib = child->die_sib;
4726 if (child->die_parent->die_child == child)
4727 child->die_parent->die_child = prev;
4728 }
4729
4730 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4731 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4732
4733 static void
4734 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4735 {
4736 dw_die_ref parent = old_child->die_parent;
4737
4738 gcc_assert (parent == prev->die_parent);
4739 gcc_assert (prev->die_sib == old_child);
4740
4741 new_child->die_parent = parent;
4742 if (prev == old_child)
4743 {
4744 gcc_assert (parent->die_child == old_child);
4745 new_child->die_sib = new_child;
4746 }
4747 else
4748 {
4749 prev->die_sib = new_child;
4750 new_child->die_sib = old_child->die_sib;
4751 }
4752 if (old_child->die_parent->die_child == old_child)
4753 old_child->die_parent->die_child = new_child;
4754 }
4755
4756 /* Move all children from OLD_PARENT to NEW_PARENT. */
4757
4758 static void
4759 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4760 {
4761 dw_die_ref c;
4762 new_parent->die_child = old_parent->die_child;
4763 old_parent->die_child = NULL;
4764 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4765 }
4766
4767 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4768 matches TAG. */
4769
4770 static void
4771 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4772 {
4773 dw_die_ref c;
4774
4775 c = die->die_child;
4776 if (c) do {
4777 dw_die_ref prev = c;
4778 c = c->die_sib;
4779 while (c->die_tag == tag)
4780 {
4781 remove_child_with_prev (c, prev);
4782 c->die_parent = NULL;
4783 /* Might have removed every child. */
4784 if (c == c->die_sib)
4785 return;
4786 c = c->die_sib;
4787 }
4788 } while (c != die->die_child);
4789 }
4790
4791 /* Add a CHILD_DIE as the last child of DIE. */
4792
4793 static void
4794 add_child_die (dw_die_ref die, dw_die_ref child_die)
4795 {
4796 /* FIXME this should probably be an assert. */
4797 if (! die || ! child_die)
4798 return;
4799 gcc_assert (die != child_die);
4800
4801 child_die->die_parent = die;
4802 if (die->die_child)
4803 {
4804 child_die->die_sib = die->die_child->die_sib;
4805 die->die_child->die_sib = child_die;
4806 }
4807 else
4808 child_die->die_sib = child_die;
4809 die->die_child = child_die;
4810 }
4811
4812 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4813 is the specification, to the end of PARENT's list of children.
4814 This is done by removing and re-adding it. */
4815
4816 static void
4817 splice_child_die (dw_die_ref parent, dw_die_ref child)
4818 {
4819 dw_die_ref p;
4820
4821 /* We want the declaration DIE from inside the class, not the
4822 specification DIE at toplevel. */
4823 if (child->die_parent != parent)
4824 {
4825 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4826
4827 if (tmp)
4828 child = tmp;
4829 }
4830
4831 gcc_assert (child->die_parent == parent
4832 || (child->die_parent
4833 == get_AT_ref (parent, DW_AT_specification)));
4834
4835 for (p = child->die_parent->die_child; ; p = p->die_sib)
4836 if (p->die_sib == child)
4837 {
4838 remove_child_with_prev (child, p);
4839 break;
4840 }
4841
4842 add_child_die (parent, child);
4843 }
4844
4845 /* Return a pointer to a newly created DIE node. */
4846
4847 static inline dw_die_ref
4848 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4849 {
4850 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4851
4852 die->die_tag = tag_value;
4853
4854 if (parent_die != NULL)
4855 add_child_die (parent_die, die);
4856 else
4857 {
4858 limbo_die_node *limbo_node;
4859
4860 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4861 limbo_node->die = die;
4862 limbo_node->created_for = t;
4863 limbo_node->next = limbo_die_list;
4864 limbo_die_list = limbo_node;
4865 }
4866
4867 return die;
4868 }
4869
4870 /* Return the DIE associated with the given type specifier. */
4871
4872 static inline dw_die_ref
4873 lookup_type_die (tree type)
4874 {
4875 return TYPE_SYMTAB_DIE (type);
4876 }
4877
4878 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4879 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4880 anonymous type instead the one of the naming typedef. */
4881
4882 static inline dw_die_ref
4883 strip_naming_typedef (tree type, dw_die_ref type_die)
4884 {
4885 if (type
4886 && TREE_CODE (type) == RECORD_TYPE
4887 && type_die
4888 && type_die->die_tag == DW_TAG_typedef
4889 && is_naming_typedef_decl (TYPE_NAME (type)))
4890 type_die = get_AT_ref (type_die, DW_AT_type);
4891 return type_die;
4892 }
4893
4894 /* Like lookup_type_die, but if type is an anonymous type named by a
4895 typedef[1], return the DIE of the anonymous type instead the one of
4896 the naming typedef. This is because in gen_typedef_die, we did
4897 equate the anonymous struct named by the typedef with the DIE of
4898 the naming typedef. So by default, lookup_type_die on an anonymous
4899 struct yields the DIE of the naming typedef.
4900
4901 [1]: Read the comment of is_naming_typedef_decl to learn about what
4902 a naming typedef is. */
4903
4904 static inline dw_die_ref
4905 lookup_type_die_strip_naming_typedef (tree type)
4906 {
4907 dw_die_ref die = lookup_type_die (type);
4908 return strip_naming_typedef (type, die);
4909 }
4910
4911 /* Equate a DIE to a given type specifier. */
4912
4913 static inline void
4914 equate_type_number_to_die (tree type, dw_die_ref type_die)
4915 {
4916 TYPE_SYMTAB_DIE (type) = type_die;
4917 }
4918
4919 /* Returns a hash value for X (which really is a die_struct). */
4920
4921 static hashval_t
4922 decl_die_table_hash (const void *x)
4923 {
4924 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4925 }
4926
4927 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4928
4929 static int
4930 decl_die_table_eq (const void *x, const void *y)
4931 {
4932 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4933 }
4934
4935 /* Return the DIE associated with a given declaration. */
4936
4937 static inline dw_die_ref
4938 lookup_decl_die (tree decl)
4939 {
4940 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4941 }
4942
4943 /* Returns a hash value for X (which really is a var_loc_list). */
4944
4945 static hashval_t
4946 decl_loc_table_hash (const void *x)
4947 {
4948 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4949 }
4950
4951 /* Return nonzero if decl_id of var_loc_list X is the same as
4952 UID of decl *Y. */
4953
4954 static int
4955 decl_loc_table_eq (const void *x, const void *y)
4956 {
4957 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4958 }
4959
4960 /* Return the var_loc list associated with a given declaration. */
4961
4962 static inline var_loc_list *
4963 lookup_decl_loc (const_tree decl)
4964 {
4965 if (!decl_loc_table)
4966 return NULL;
4967 return (var_loc_list *)
4968 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4969 }
4970
4971 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4972
4973 static hashval_t
4974 cached_dw_loc_list_table_hash (const void *x)
4975 {
4976 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4977 }
4978
4979 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4980 UID of decl *Y. */
4981
4982 static int
4983 cached_dw_loc_list_table_eq (const void *x, const void *y)
4984 {
4985 return (((const cached_dw_loc_list *) x)->decl_id
4986 == DECL_UID ((const_tree) y));
4987 }
4988
4989 /* Equate a DIE to a particular declaration. */
4990
4991 static void
4992 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4993 {
4994 unsigned int decl_id = DECL_UID (decl);
4995 void **slot;
4996
4997 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4998 *slot = decl_die;
4999 decl_die->decl_id = decl_id;
5000 }
5001
5002 /* Return how many bits covers PIECE EXPR_LIST. */
5003
5004 static int
5005 decl_piece_bitsize (rtx piece)
5006 {
5007 int ret = (int) GET_MODE (piece);
5008 if (ret)
5009 return ret;
5010 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5011 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5012 return INTVAL (XEXP (XEXP (piece, 0), 0));
5013 }
5014
5015 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5016
5017 static rtx *
5018 decl_piece_varloc_ptr (rtx piece)
5019 {
5020 if ((int) GET_MODE (piece))
5021 return &XEXP (piece, 0);
5022 else
5023 return &XEXP (XEXP (piece, 0), 1);
5024 }
5025
5026 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5027 Next is the chain of following piece nodes. */
5028
5029 static rtx_expr_list *
5030 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5031 {
5032 if (bitsize <= (int) MAX_MACHINE_MODE)
5033 return alloc_EXPR_LIST (bitsize, loc_note, next);
5034 else
5035 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5036 GEN_INT (bitsize),
5037 loc_note), next);
5038 }
5039
5040 /* Return rtx that should be stored into loc field for
5041 LOC_NOTE and BITPOS/BITSIZE. */
5042
5043 static rtx
5044 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5045 HOST_WIDE_INT bitsize)
5046 {
5047 if (bitsize != -1)
5048 {
5049 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5050 if (bitpos != 0)
5051 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5052 }
5053 return loc_note;
5054 }
5055
5056 /* This function either modifies location piece list *DEST in
5057 place (if SRC and INNER is NULL), or copies location piece list
5058 *SRC to *DEST while modifying it. Location BITPOS is modified
5059 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5060 not copied and if needed some padding around it is added.
5061 When modifying in place, DEST should point to EXPR_LIST where
5062 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5063 to the start of the whole list and INNER points to the EXPR_LIST
5064 where earlier pieces cover PIECE_BITPOS bits. */
5065
5066 static void
5067 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5068 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5069 HOST_WIDE_INT bitsize, rtx loc_note)
5070 {
5071 int diff;
5072 bool copy = inner != NULL;
5073
5074 if (copy)
5075 {
5076 /* First copy all nodes preceding the current bitpos. */
5077 while (src != inner)
5078 {
5079 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5080 decl_piece_bitsize (*src), NULL_RTX);
5081 dest = &XEXP (*dest, 1);
5082 src = &XEXP (*src, 1);
5083 }
5084 }
5085 /* Add padding if needed. */
5086 if (bitpos != piece_bitpos)
5087 {
5088 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5089 copy ? NULL_RTX : *dest);
5090 dest = &XEXP (*dest, 1);
5091 }
5092 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5093 {
5094 gcc_assert (!copy);
5095 /* A piece with correct bitpos and bitsize already exist,
5096 just update the location for it and return. */
5097 *decl_piece_varloc_ptr (*dest) = loc_note;
5098 return;
5099 }
5100 /* Add the piece that changed. */
5101 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5102 dest = &XEXP (*dest, 1);
5103 /* Skip over pieces that overlap it. */
5104 diff = bitpos - piece_bitpos + bitsize;
5105 if (!copy)
5106 src = dest;
5107 while (diff > 0 && *src)
5108 {
5109 rtx piece = *src;
5110 diff -= decl_piece_bitsize (piece);
5111 if (copy)
5112 src = &XEXP (piece, 1);
5113 else
5114 {
5115 *src = XEXP (piece, 1);
5116 free_EXPR_LIST_node (piece);
5117 }
5118 }
5119 /* Add padding if needed. */
5120 if (diff < 0 && *src)
5121 {
5122 if (!copy)
5123 dest = src;
5124 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5125 dest = &XEXP (*dest, 1);
5126 }
5127 if (!copy)
5128 return;
5129 /* Finally copy all nodes following it. */
5130 while (*src)
5131 {
5132 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5133 decl_piece_bitsize (*src), NULL_RTX);
5134 dest = &XEXP (*dest, 1);
5135 src = &XEXP (*src, 1);
5136 }
5137 }
5138
5139 /* Add a variable location node to the linked list for DECL. */
5140
5141 static struct var_loc_node *
5142 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5143 {
5144 unsigned int decl_id;
5145 var_loc_list *temp;
5146 void **slot;
5147 struct var_loc_node *loc = NULL;
5148 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5149
5150 if (TREE_CODE (decl) == VAR_DECL
5151 && DECL_HAS_DEBUG_EXPR_P (decl))
5152 {
5153 tree realdecl = DECL_DEBUG_EXPR (decl);
5154 if (handled_component_p (realdecl)
5155 || (TREE_CODE (realdecl) == MEM_REF
5156 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5157 {
5158 HOST_WIDE_INT maxsize;
5159 tree innerdecl;
5160 innerdecl
5161 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5162 if (!DECL_P (innerdecl)
5163 || DECL_IGNORED_P (innerdecl)
5164 || TREE_STATIC (innerdecl)
5165 || bitsize <= 0
5166 || bitpos + bitsize > 256
5167 || bitsize != maxsize)
5168 return NULL;
5169 decl = innerdecl;
5170 }
5171 }
5172
5173 decl_id = DECL_UID (decl);
5174 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5175 if (*slot == NULL)
5176 {
5177 temp = ggc_cleared_alloc<var_loc_list> ();
5178 temp->decl_id = decl_id;
5179 *slot = temp;
5180 }
5181 else
5182 temp = (var_loc_list *) *slot;
5183
5184 /* For PARM_DECLs try to keep around the original incoming value,
5185 even if that means we'll emit a zero-range .debug_loc entry. */
5186 if (temp->last
5187 && temp->first == temp->last
5188 && TREE_CODE (decl) == PARM_DECL
5189 && NOTE_P (temp->first->loc)
5190 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5191 && DECL_INCOMING_RTL (decl)
5192 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5193 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5194 == GET_CODE (DECL_INCOMING_RTL (decl))
5195 && prev_real_insn (temp->first->loc) == NULL_RTX
5196 && (bitsize != -1
5197 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5198 NOTE_VAR_LOCATION_LOC (loc_note))
5199 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5200 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5201 {
5202 loc = ggc_cleared_alloc<var_loc_node> ();
5203 temp->first->next = loc;
5204 temp->last = loc;
5205 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5206 }
5207 else if (temp->last)
5208 {
5209 struct var_loc_node *last = temp->last, *unused = NULL;
5210 rtx *piece_loc = NULL, last_loc_note;
5211 int piece_bitpos = 0;
5212 if (last->next)
5213 {
5214 last = last->next;
5215 gcc_assert (last->next == NULL);
5216 }
5217 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5218 {
5219 piece_loc = &last->loc;
5220 do
5221 {
5222 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5223 if (piece_bitpos + cur_bitsize > bitpos)
5224 break;
5225 piece_bitpos += cur_bitsize;
5226 piece_loc = &XEXP (*piece_loc, 1);
5227 }
5228 while (*piece_loc);
5229 }
5230 /* TEMP->LAST here is either pointer to the last but one or
5231 last element in the chained list, LAST is pointer to the
5232 last element. */
5233 if (label && strcmp (last->label, label) == 0)
5234 {
5235 /* For SRA optimized variables if there weren't any real
5236 insns since last note, just modify the last node. */
5237 if (piece_loc != NULL)
5238 {
5239 adjust_piece_list (piece_loc, NULL, NULL,
5240 bitpos, piece_bitpos, bitsize, loc_note);
5241 return NULL;
5242 }
5243 /* If the last note doesn't cover any instructions, remove it. */
5244 if (temp->last != last)
5245 {
5246 temp->last->next = NULL;
5247 unused = last;
5248 last = temp->last;
5249 gcc_assert (strcmp (last->label, label) != 0);
5250 }
5251 else
5252 {
5253 gcc_assert (temp->first == temp->last
5254 || (temp->first->next == temp->last
5255 && TREE_CODE (decl) == PARM_DECL));
5256 memset (temp->last, '\0', sizeof (*temp->last));
5257 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5258 return temp->last;
5259 }
5260 }
5261 if (bitsize == -1 && NOTE_P (last->loc))
5262 last_loc_note = last->loc;
5263 else if (piece_loc != NULL
5264 && *piece_loc != NULL_RTX
5265 && piece_bitpos == bitpos
5266 && decl_piece_bitsize (*piece_loc) == bitsize)
5267 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5268 else
5269 last_loc_note = NULL_RTX;
5270 /* If the current location is the same as the end of the list,
5271 and either both or neither of the locations is uninitialized,
5272 we have nothing to do. */
5273 if (last_loc_note == NULL_RTX
5274 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5275 NOTE_VAR_LOCATION_LOC (loc_note)))
5276 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5277 != NOTE_VAR_LOCATION_STATUS (loc_note))
5278 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5279 == VAR_INIT_STATUS_UNINITIALIZED)
5280 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5281 == VAR_INIT_STATUS_UNINITIALIZED))))
5282 {
5283 /* Add LOC to the end of list and update LAST. If the last
5284 element of the list has been removed above, reuse its
5285 memory for the new node, otherwise allocate a new one. */
5286 if (unused)
5287 {
5288 loc = unused;
5289 memset (loc, '\0', sizeof (*loc));
5290 }
5291 else
5292 loc = ggc_cleared_alloc<var_loc_node> ();
5293 if (bitsize == -1 || piece_loc == NULL)
5294 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5295 else
5296 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5297 bitpos, piece_bitpos, bitsize, loc_note);
5298 last->next = loc;
5299 /* Ensure TEMP->LAST will point either to the new last but one
5300 element of the chain, or to the last element in it. */
5301 if (last != temp->last)
5302 temp->last = last;
5303 }
5304 else if (unused)
5305 ggc_free (unused);
5306 }
5307 else
5308 {
5309 loc = ggc_cleared_alloc<var_loc_node> ();
5310 temp->first = loc;
5311 temp->last = loc;
5312 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5313 }
5314 return loc;
5315 }
5316 \f
5317 /* Keep track of the number of spaces used to indent the
5318 output of the debugging routines that print the structure of
5319 the DIE internal representation. */
5320 static int print_indent;
5321
5322 /* Indent the line the number of spaces given by print_indent. */
5323
5324 static inline void
5325 print_spaces (FILE *outfile)
5326 {
5327 fprintf (outfile, "%*s", print_indent, "");
5328 }
5329
5330 /* Print a type signature in hex. */
5331
5332 static inline void
5333 print_signature (FILE *outfile, char *sig)
5334 {
5335 int i;
5336
5337 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5338 fprintf (outfile, "%02x", sig[i] & 0xff);
5339 }
5340
5341 /* Print the information associated with a given DIE, and its children.
5342 This routine is a debugging aid only. */
5343
5344 static void
5345 print_die (dw_die_ref die, FILE *outfile)
5346 {
5347 dw_attr_ref a;
5348 dw_die_ref c;
5349 unsigned ix;
5350
5351 print_spaces (outfile);
5352 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5353 die->die_offset, dwarf_tag_name (die->die_tag),
5354 (void*) die);
5355 print_spaces (outfile);
5356 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5357 fprintf (outfile, " offset: %ld", die->die_offset);
5358 fprintf (outfile, " mark: %d\n", die->die_mark);
5359
5360 if (die->comdat_type_p)
5361 {
5362 print_spaces (outfile);
5363 fprintf (outfile, " signature: ");
5364 print_signature (outfile, die->die_id.die_type_node->signature);
5365 fprintf (outfile, "\n");
5366 }
5367
5368 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5369 {
5370 print_spaces (outfile);
5371 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5372
5373 switch (AT_class (a))
5374 {
5375 case dw_val_class_addr:
5376 fprintf (outfile, "address");
5377 break;
5378 case dw_val_class_offset:
5379 fprintf (outfile, "offset");
5380 break;
5381 case dw_val_class_loc:
5382 fprintf (outfile, "location descriptor");
5383 break;
5384 case dw_val_class_loc_list:
5385 fprintf (outfile, "location list -> label:%s",
5386 AT_loc_list (a)->ll_symbol);
5387 break;
5388 case dw_val_class_range_list:
5389 fprintf (outfile, "range list");
5390 break;
5391 case dw_val_class_const:
5392 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5393 break;
5394 case dw_val_class_unsigned_const:
5395 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5396 break;
5397 case dw_val_class_const_double:
5398 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5399 HOST_WIDE_INT_PRINT_UNSIGNED")",
5400 a->dw_attr_val.v.val_double.high,
5401 a->dw_attr_val.v.val_double.low);
5402 break;
5403 case dw_val_class_wide_int:
5404 {
5405 int i = a->dw_attr_val.v.val_wide->get_len ();
5406 fprintf (outfile, "constant (");
5407 gcc_assert (i > 0);
5408 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5409 fprintf (outfile, "0x");
5410 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5411 a->dw_attr_val.v.val_wide->elt (--i));
5412 while (--i >= 0)
5413 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5414 a->dw_attr_val.v.val_wide->elt (i));
5415 fprintf (outfile, ")");
5416 break;
5417 }
5418 case dw_val_class_vec:
5419 fprintf (outfile, "floating-point or vector constant");
5420 break;
5421 case dw_val_class_flag:
5422 fprintf (outfile, "%u", AT_flag (a));
5423 break;
5424 case dw_val_class_die_ref:
5425 if (AT_ref (a) != NULL)
5426 {
5427 if (AT_ref (a)->comdat_type_p)
5428 {
5429 fprintf (outfile, "die -> signature: ");
5430 print_signature (outfile,
5431 AT_ref (a)->die_id.die_type_node->signature);
5432 }
5433 else if (AT_ref (a)->die_id.die_symbol)
5434 fprintf (outfile, "die -> label: %s",
5435 AT_ref (a)->die_id.die_symbol);
5436 else
5437 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5438 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5439 }
5440 else
5441 fprintf (outfile, "die -> <null>");
5442 break;
5443 case dw_val_class_vms_delta:
5444 fprintf (outfile, "delta: @slotcount(%s-%s)",
5445 AT_vms_delta2 (a), AT_vms_delta1 (a));
5446 break;
5447 case dw_val_class_lbl_id:
5448 case dw_val_class_lineptr:
5449 case dw_val_class_macptr:
5450 case dw_val_class_high_pc:
5451 fprintf (outfile, "label: %s", AT_lbl (a));
5452 break;
5453 case dw_val_class_str:
5454 if (AT_string (a) != NULL)
5455 fprintf (outfile, "\"%s\"", AT_string (a));
5456 else
5457 fprintf (outfile, "<null>");
5458 break;
5459 case dw_val_class_file:
5460 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5461 AT_file (a)->emitted_number);
5462 break;
5463 case dw_val_class_data8:
5464 {
5465 int i;
5466
5467 for (i = 0; i < 8; i++)
5468 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5469 break;
5470 }
5471 default:
5472 break;
5473 }
5474
5475 fprintf (outfile, "\n");
5476 }
5477
5478 if (die->die_child != NULL)
5479 {
5480 print_indent += 4;
5481 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5482 print_indent -= 4;
5483 }
5484 if (print_indent == 0)
5485 fprintf (outfile, "\n");
5486 }
5487
5488 /* Print the information collected for a given DIE. */
5489
5490 DEBUG_FUNCTION void
5491 debug_dwarf_die (dw_die_ref die)
5492 {
5493 print_die (die, stderr);
5494 }
5495
5496 DEBUG_FUNCTION void
5497 debug (die_struct &ref)
5498 {
5499 print_die (&ref, stderr);
5500 }
5501
5502 DEBUG_FUNCTION void
5503 debug (die_struct *ptr)
5504 {
5505 if (ptr)
5506 debug (*ptr);
5507 else
5508 fprintf (stderr, "<nil>\n");
5509 }
5510
5511
5512 /* Print all DWARF information collected for the compilation unit.
5513 This routine is a debugging aid only. */
5514
5515 DEBUG_FUNCTION void
5516 debug_dwarf (void)
5517 {
5518 print_indent = 0;
5519 print_die (comp_unit_die (), stderr);
5520 }
5521
5522 /* Perform some sanity checks on DIEs after they have been generated
5523 earlier in the compilation process. */
5524
5525 static void
5526 check_die (dw_die_ref die, unsigned level)
5527 {
5528 static unsigned long mark = 1;
5529 dw_die_ref c, p;
5530 /* Check that all our childs have their parent set to us. */
5531 c = die->die_child;
5532 if (c) do {
5533 c = c->die_sib;
5534 gcc_assert (c->die_parent == die);
5535 } while (c != die->die_child);
5536
5537 /* Check the we are part of our parent's child list. */
5538 mark++;
5539 p = die->die_parent;
5540 if (p)
5541 {
5542 c = p->die_child;
5543 gcc_assert (c);
5544 do {
5545 c = c->die_sib;
5546 /* Found it. */
5547 if (c == die)
5548 break;
5549 /* If we're at start --> not found. */
5550 gcc_assert (c != p->die_child);
5551 /* If we've seen this node already the circular list doesn't
5552 even go back to start. */
5553 gcc_assert (c->die_abbrev != mark);
5554 c->die_abbrev = mark;
5555 } while (1);
5556 }
5557
5558 if (!level)
5559 return;
5560
5561 FOR_EACH_CHILD (die, c, check_die (c, level - 1));
5562 }
5563
5564 \f
5565 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5566 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5567 DIE that marks the start of the DIEs for this include file. */
5568
5569 static dw_die_ref
5570 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5571 {
5572 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5573 dw_die_ref new_unit = gen_compile_unit_die (filename);
5574
5575 new_unit->die_sib = old_unit;
5576 return new_unit;
5577 }
5578
5579 /* Close an include-file CU and reopen the enclosing one. */
5580
5581 static dw_die_ref
5582 pop_compile_unit (dw_die_ref old_unit)
5583 {
5584 dw_die_ref new_unit = old_unit->die_sib;
5585
5586 old_unit->die_sib = NULL;
5587 return new_unit;
5588 }
5589
5590 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5591 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5592 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5593
5594 /* Calculate the checksum of a location expression. */
5595
5596 static inline void
5597 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5598 {
5599 int tem;
5600 inchash::hash hstate;
5601 hashval_t hash;
5602
5603 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5604 CHECKSUM (tem);
5605 hash_loc_operands (loc, hstate);
5606 hash = hstate.end();
5607 CHECKSUM (hash);
5608 }
5609
5610 /* Calculate the checksum of an attribute. */
5611
5612 static void
5613 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5614 {
5615 dw_loc_descr_ref loc;
5616 rtx r;
5617
5618 CHECKSUM (at->dw_attr);
5619
5620 /* We don't care that this was compiled with a different compiler
5621 snapshot; if the output is the same, that's what matters. */
5622 if (at->dw_attr == DW_AT_producer)
5623 return;
5624
5625 switch (AT_class (at))
5626 {
5627 case dw_val_class_const:
5628 CHECKSUM (at->dw_attr_val.v.val_int);
5629 break;
5630 case dw_val_class_unsigned_const:
5631 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5632 break;
5633 case dw_val_class_const_double:
5634 CHECKSUM (at->dw_attr_val.v.val_double);
5635 break;
5636 case dw_val_class_wide_int:
5637 CHECKSUM (*at->dw_attr_val.v.val_wide);
5638 break;
5639 case dw_val_class_vec:
5640 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5641 (at->dw_attr_val.v.val_vec.length
5642 * at->dw_attr_val.v.val_vec.elt_size));
5643 break;
5644 case dw_val_class_flag:
5645 CHECKSUM (at->dw_attr_val.v.val_flag);
5646 break;
5647 case dw_val_class_str:
5648 CHECKSUM_STRING (AT_string (at));
5649 break;
5650
5651 case dw_val_class_addr:
5652 r = AT_addr (at);
5653 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5654 CHECKSUM_STRING (XSTR (r, 0));
5655 break;
5656
5657 case dw_val_class_offset:
5658 CHECKSUM (at->dw_attr_val.v.val_offset);
5659 break;
5660
5661 case dw_val_class_loc:
5662 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5663 loc_checksum (loc, ctx);
5664 break;
5665
5666 case dw_val_class_die_ref:
5667 die_checksum (AT_ref (at), ctx, mark);
5668 break;
5669
5670 case dw_val_class_fde_ref:
5671 case dw_val_class_vms_delta:
5672 case dw_val_class_lbl_id:
5673 case dw_val_class_lineptr:
5674 case dw_val_class_macptr:
5675 case dw_val_class_high_pc:
5676 break;
5677
5678 case dw_val_class_file:
5679 CHECKSUM_STRING (AT_file (at)->filename);
5680 break;
5681
5682 case dw_val_class_data8:
5683 CHECKSUM (at->dw_attr_val.v.val_data8);
5684 break;
5685
5686 default:
5687 break;
5688 }
5689 }
5690
5691 /* Calculate the checksum of a DIE. */
5692
5693 static void
5694 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5695 {
5696 dw_die_ref c;
5697 dw_attr_ref a;
5698 unsigned ix;
5699
5700 /* To avoid infinite recursion. */
5701 if (die->die_mark)
5702 {
5703 CHECKSUM (die->die_mark);
5704 return;
5705 }
5706 die->die_mark = ++(*mark);
5707
5708 CHECKSUM (die->die_tag);
5709
5710 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5711 attr_checksum (a, ctx, mark);
5712
5713 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5714 }
5715
5716 #undef CHECKSUM
5717 #undef CHECKSUM_BLOCK
5718 #undef CHECKSUM_STRING
5719
5720 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5721 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5722 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5723 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5724 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5725 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5726 #define CHECKSUM_ATTR(FOO) \
5727 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5728
5729 /* Calculate the checksum of a number in signed LEB128 format. */
5730
5731 static void
5732 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5733 {
5734 unsigned char byte;
5735 bool more;
5736
5737 while (1)
5738 {
5739 byte = (value & 0x7f);
5740 value >>= 7;
5741 more = !((value == 0 && (byte & 0x40) == 0)
5742 || (value == -1 && (byte & 0x40) != 0));
5743 if (more)
5744 byte |= 0x80;
5745 CHECKSUM (byte);
5746 if (!more)
5747 break;
5748 }
5749 }
5750
5751 /* Calculate the checksum of a number in unsigned LEB128 format. */
5752
5753 static void
5754 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5755 {
5756 while (1)
5757 {
5758 unsigned char byte = (value & 0x7f);
5759 value >>= 7;
5760 if (value != 0)
5761 /* More bytes to follow. */
5762 byte |= 0x80;
5763 CHECKSUM (byte);
5764 if (value == 0)
5765 break;
5766 }
5767 }
5768
5769 /* Checksum the context of the DIE. This adds the names of any
5770 surrounding namespaces or structures to the checksum. */
5771
5772 static void
5773 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5774 {
5775 const char *name;
5776 dw_die_ref spec;
5777 int tag = die->die_tag;
5778
5779 if (tag != DW_TAG_namespace
5780 && tag != DW_TAG_structure_type
5781 && tag != DW_TAG_class_type)
5782 return;
5783
5784 name = get_AT_string (die, DW_AT_name);
5785
5786 spec = get_AT_ref (die, DW_AT_specification);
5787 if (spec != NULL)
5788 die = spec;
5789
5790 if (die->die_parent != NULL)
5791 checksum_die_context (die->die_parent, ctx);
5792
5793 CHECKSUM_ULEB128 ('C');
5794 CHECKSUM_ULEB128 (tag);
5795 if (name != NULL)
5796 CHECKSUM_STRING (name);
5797 }
5798
5799 /* Calculate the checksum of a location expression. */
5800
5801 static inline void
5802 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5803 {
5804 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5805 were emitted as a DW_FORM_sdata instead of a location expression. */
5806 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5807 {
5808 CHECKSUM_ULEB128 (DW_FORM_sdata);
5809 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5810 return;
5811 }
5812
5813 /* Otherwise, just checksum the raw location expression. */
5814 while (loc != NULL)
5815 {
5816 inchash::hash hstate;
5817 hashval_t hash;
5818
5819 CHECKSUM_ULEB128 (loc->dtprel);
5820 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5821 hash_loc_operands (loc, hstate);
5822 hash = hstate.end ();
5823 CHECKSUM (hash);
5824 loc = loc->dw_loc_next;
5825 }
5826 }
5827
5828 /* Calculate the checksum of an attribute. */
5829
5830 static void
5831 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5832 struct md5_ctx *ctx, int *mark)
5833 {
5834 dw_loc_descr_ref loc;
5835 rtx r;
5836
5837 if (AT_class (at) == dw_val_class_die_ref)
5838 {
5839 dw_die_ref target_die = AT_ref (at);
5840
5841 /* For pointer and reference types, we checksum only the (qualified)
5842 name of the target type (if there is a name). For friend entries,
5843 we checksum only the (qualified) name of the target type or function.
5844 This allows the checksum to remain the same whether the target type
5845 is complete or not. */
5846 if ((at->dw_attr == DW_AT_type
5847 && (tag == DW_TAG_pointer_type
5848 || tag == DW_TAG_reference_type
5849 || tag == DW_TAG_rvalue_reference_type
5850 || tag == DW_TAG_ptr_to_member_type))
5851 || (at->dw_attr == DW_AT_friend
5852 && tag == DW_TAG_friend))
5853 {
5854 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5855
5856 if (name_attr != NULL)
5857 {
5858 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5859
5860 if (decl == NULL)
5861 decl = target_die;
5862 CHECKSUM_ULEB128 ('N');
5863 CHECKSUM_ULEB128 (at->dw_attr);
5864 if (decl->die_parent != NULL)
5865 checksum_die_context (decl->die_parent, ctx);
5866 CHECKSUM_ULEB128 ('E');
5867 CHECKSUM_STRING (AT_string (name_attr));
5868 return;
5869 }
5870 }
5871
5872 /* For all other references to another DIE, we check to see if the
5873 target DIE has already been visited. If it has, we emit a
5874 backward reference; if not, we descend recursively. */
5875 if (target_die->die_mark > 0)
5876 {
5877 CHECKSUM_ULEB128 ('R');
5878 CHECKSUM_ULEB128 (at->dw_attr);
5879 CHECKSUM_ULEB128 (target_die->die_mark);
5880 }
5881 else
5882 {
5883 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5884
5885 if (decl == NULL)
5886 decl = target_die;
5887 target_die->die_mark = ++(*mark);
5888 CHECKSUM_ULEB128 ('T');
5889 CHECKSUM_ULEB128 (at->dw_attr);
5890 if (decl->die_parent != NULL)
5891 checksum_die_context (decl->die_parent, ctx);
5892 die_checksum_ordered (target_die, ctx, mark);
5893 }
5894 return;
5895 }
5896
5897 CHECKSUM_ULEB128 ('A');
5898 CHECKSUM_ULEB128 (at->dw_attr);
5899
5900 switch (AT_class (at))
5901 {
5902 case dw_val_class_const:
5903 CHECKSUM_ULEB128 (DW_FORM_sdata);
5904 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5905 break;
5906
5907 case dw_val_class_unsigned_const:
5908 CHECKSUM_ULEB128 (DW_FORM_sdata);
5909 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5910 break;
5911
5912 case dw_val_class_const_double:
5913 CHECKSUM_ULEB128 (DW_FORM_block);
5914 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5915 CHECKSUM (at->dw_attr_val.v.val_double);
5916 break;
5917
5918 case dw_val_class_wide_int:
5919 CHECKSUM_ULEB128 (DW_FORM_block);
5920 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5921 CHECKSUM (*at->dw_attr_val.v.val_wide);
5922 break;
5923
5924 case dw_val_class_vec:
5925 CHECKSUM_ULEB128 (DW_FORM_block);
5926 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5927 * at->dw_attr_val.v.val_vec.elt_size);
5928 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5929 (at->dw_attr_val.v.val_vec.length
5930 * at->dw_attr_val.v.val_vec.elt_size));
5931 break;
5932
5933 case dw_val_class_flag:
5934 CHECKSUM_ULEB128 (DW_FORM_flag);
5935 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5936 break;
5937
5938 case dw_val_class_str:
5939 CHECKSUM_ULEB128 (DW_FORM_string);
5940 CHECKSUM_STRING (AT_string (at));
5941 break;
5942
5943 case dw_val_class_addr:
5944 r = AT_addr (at);
5945 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5946 CHECKSUM_ULEB128 (DW_FORM_string);
5947 CHECKSUM_STRING (XSTR (r, 0));
5948 break;
5949
5950 case dw_val_class_offset:
5951 CHECKSUM_ULEB128 (DW_FORM_sdata);
5952 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5953 break;
5954
5955 case dw_val_class_loc:
5956 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5957 loc_checksum_ordered (loc, ctx);
5958 break;
5959
5960 case dw_val_class_fde_ref:
5961 case dw_val_class_lbl_id:
5962 case dw_val_class_lineptr:
5963 case dw_val_class_macptr:
5964 case dw_val_class_high_pc:
5965 break;
5966
5967 case dw_val_class_file:
5968 CHECKSUM_ULEB128 (DW_FORM_string);
5969 CHECKSUM_STRING (AT_file (at)->filename);
5970 break;
5971
5972 case dw_val_class_data8:
5973 CHECKSUM (at->dw_attr_val.v.val_data8);
5974 break;
5975
5976 default:
5977 break;
5978 }
5979 }
5980
5981 struct checksum_attributes
5982 {
5983 dw_attr_ref at_name;
5984 dw_attr_ref at_type;
5985 dw_attr_ref at_friend;
5986 dw_attr_ref at_accessibility;
5987 dw_attr_ref at_address_class;
5988 dw_attr_ref at_allocated;
5989 dw_attr_ref at_artificial;
5990 dw_attr_ref at_associated;
5991 dw_attr_ref at_binary_scale;
5992 dw_attr_ref at_bit_offset;
5993 dw_attr_ref at_bit_size;
5994 dw_attr_ref at_bit_stride;
5995 dw_attr_ref at_byte_size;
5996 dw_attr_ref at_byte_stride;
5997 dw_attr_ref at_const_value;
5998 dw_attr_ref at_containing_type;
5999 dw_attr_ref at_count;
6000 dw_attr_ref at_data_location;
6001 dw_attr_ref at_data_member_location;
6002 dw_attr_ref at_decimal_scale;
6003 dw_attr_ref at_decimal_sign;
6004 dw_attr_ref at_default_value;
6005 dw_attr_ref at_digit_count;
6006 dw_attr_ref at_discr;
6007 dw_attr_ref at_discr_list;
6008 dw_attr_ref at_discr_value;
6009 dw_attr_ref at_encoding;
6010 dw_attr_ref at_endianity;
6011 dw_attr_ref at_explicit;
6012 dw_attr_ref at_is_optional;
6013 dw_attr_ref at_location;
6014 dw_attr_ref at_lower_bound;
6015 dw_attr_ref at_mutable;
6016 dw_attr_ref at_ordering;
6017 dw_attr_ref at_picture_string;
6018 dw_attr_ref at_prototyped;
6019 dw_attr_ref at_small;
6020 dw_attr_ref at_segment;
6021 dw_attr_ref at_string_length;
6022 dw_attr_ref at_threads_scaled;
6023 dw_attr_ref at_upper_bound;
6024 dw_attr_ref at_use_location;
6025 dw_attr_ref at_use_UTF8;
6026 dw_attr_ref at_variable_parameter;
6027 dw_attr_ref at_virtuality;
6028 dw_attr_ref at_visibility;
6029 dw_attr_ref at_vtable_elem_location;
6030 };
6031
6032 /* Collect the attributes that we will want to use for the checksum. */
6033
6034 static void
6035 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6036 {
6037 dw_attr_ref a;
6038 unsigned ix;
6039
6040 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6041 {
6042 switch (a->dw_attr)
6043 {
6044 case DW_AT_name:
6045 attrs->at_name = a;
6046 break;
6047 case DW_AT_type:
6048 attrs->at_type = a;
6049 break;
6050 case DW_AT_friend:
6051 attrs->at_friend = a;
6052 break;
6053 case DW_AT_accessibility:
6054 attrs->at_accessibility = a;
6055 break;
6056 case DW_AT_address_class:
6057 attrs->at_address_class = a;
6058 break;
6059 case DW_AT_allocated:
6060 attrs->at_allocated = a;
6061 break;
6062 case DW_AT_artificial:
6063 attrs->at_artificial = a;
6064 break;
6065 case DW_AT_associated:
6066 attrs->at_associated = a;
6067 break;
6068 case DW_AT_binary_scale:
6069 attrs->at_binary_scale = a;
6070 break;
6071 case DW_AT_bit_offset:
6072 attrs->at_bit_offset = a;
6073 break;
6074 case DW_AT_bit_size:
6075 attrs->at_bit_size = a;
6076 break;
6077 case DW_AT_bit_stride:
6078 attrs->at_bit_stride = a;
6079 break;
6080 case DW_AT_byte_size:
6081 attrs->at_byte_size = a;
6082 break;
6083 case DW_AT_byte_stride:
6084 attrs->at_byte_stride = a;
6085 break;
6086 case DW_AT_const_value:
6087 attrs->at_const_value = a;
6088 break;
6089 case DW_AT_containing_type:
6090 attrs->at_containing_type = a;
6091 break;
6092 case DW_AT_count:
6093 attrs->at_count = a;
6094 break;
6095 case DW_AT_data_location:
6096 attrs->at_data_location = a;
6097 break;
6098 case DW_AT_data_member_location:
6099 attrs->at_data_member_location = a;
6100 break;
6101 case DW_AT_decimal_scale:
6102 attrs->at_decimal_scale = a;
6103 break;
6104 case DW_AT_decimal_sign:
6105 attrs->at_decimal_sign = a;
6106 break;
6107 case DW_AT_default_value:
6108 attrs->at_default_value = a;
6109 break;
6110 case DW_AT_digit_count:
6111 attrs->at_digit_count = a;
6112 break;
6113 case DW_AT_discr:
6114 attrs->at_discr = a;
6115 break;
6116 case DW_AT_discr_list:
6117 attrs->at_discr_list = a;
6118 break;
6119 case DW_AT_discr_value:
6120 attrs->at_discr_value = a;
6121 break;
6122 case DW_AT_encoding:
6123 attrs->at_encoding = a;
6124 break;
6125 case DW_AT_endianity:
6126 attrs->at_endianity = a;
6127 break;
6128 case DW_AT_explicit:
6129 attrs->at_explicit = a;
6130 break;
6131 case DW_AT_is_optional:
6132 attrs->at_is_optional = a;
6133 break;
6134 case DW_AT_location:
6135 attrs->at_location = a;
6136 break;
6137 case DW_AT_lower_bound:
6138 attrs->at_lower_bound = a;
6139 break;
6140 case DW_AT_mutable:
6141 attrs->at_mutable = a;
6142 break;
6143 case DW_AT_ordering:
6144 attrs->at_ordering = a;
6145 break;
6146 case DW_AT_picture_string:
6147 attrs->at_picture_string = a;
6148 break;
6149 case DW_AT_prototyped:
6150 attrs->at_prototyped = a;
6151 break;
6152 case DW_AT_small:
6153 attrs->at_small = a;
6154 break;
6155 case DW_AT_segment:
6156 attrs->at_segment = a;
6157 break;
6158 case DW_AT_string_length:
6159 attrs->at_string_length = a;
6160 break;
6161 case DW_AT_threads_scaled:
6162 attrs->at_threads_scaled = a;
6163 break;
6164 case DW_AT_upper_bound:
6165 attrs->at_upper_bound = a;
6166 break;
6167 case DW_AT_use_location:
6168 attrs->at_use_location = a;
6169 break;
6170 case DW_AT_use_UTF8:
6171 attrs->at_use_UTF8 = a;
6172 break;
6173 case DW_AT_variable_parameter:
6174 attrs->at_variable_parameter = a;
6175 break;
6176 case DW_AT_virtuality:
6177 attrs->at_virtuality = a;
6178 break;
6179 case DW_AT_visibility:
6180 attrs->at_visibility = a;
6181 break;
6182 case DW_AT_vtable_elem_location:
6183 attrs->at_vtable_elem_location = a;
6184 break;
6185 default:
6186 break;
6187 }
6188 }
6189 }
6190
6191 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6192
6193 static void
6194 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6195 {
6196 dw_die_ref c;
6197 dw_die_ref decl;
6198 struct checksum_attributes attrs;
6199
6200 CHECKSUM_ULEB128 ('D');
6201 CHECKSUM_ULEB128 (die->die_tag);
6202
6203 memset (&attrs, 0, sizeof (attrs));
6204
6205 decl = get_AT_ref (die, DW_AT_specification);
6206 if (decl != NULL)
6207 collect_checksum_attributes (&attrs, decl);
6208 collect_checksum_attributes (&attrs, die);
6209
6210 CHECKSUM_ATTR (attrs.at_name);
6211 CHECKSUM_ATTR (attrs.at_accessibility);
6212 CHECKSUM_ATTR (attrs.at_address_class);
6213 CHECKSUM_ATTR (attrs.at_allocated);
6214 CHECKSUM_ATTR (attrs.at_artificial);
6215 CHECKSUM_ATTR (attrs.at_associated);
6216 CHECKSUM_ATTR (attrs.at_binary_scale);
6217 CHECKSUM_ATTR (attrs.at_bit_offset);
6218 CHECKSUM_ATTR (attrs.at_bit_size);
6219 CHECKSUM_ATTR (attrs.at_bit_stride);
6220 CHECKSUM_ATTR (attrs.at_byte_size);
6221 CHECKSUM_ATTR (attrs.at_byte_stride);
6222 CHECKSUM_ATTR (attrs.at_const_value);
6223 CHECKSUM_ATTR (attrs.at_containing_type);
6224 CHECKSUM_ATTR (attrs.at_count);
6225 CHECKSUM_ATTR (attrs.at_data_location);
6226 CHECKSUM_ATTR (attrs.at_data_member_location);
6227 CHECKSUM_ATTR (attrs.at_decimal_scale);
6228 CHECKSUM_ATTR (attrs.at_decimal_sign);
6229 CHECKSUM_ATTR (attrs.at_default_value);
6230 CHECKSUM_ATTR (attrs.at_digit_count);
6231 CHECKSUM_ATTR (attrs.at_discr);
6232 CHECKSUM_ATTR (attrs.at_discr_list);
6233 CHECKSUM_ATTR (attrs.at_discr_value);
6234 CHECKSUM_ATTR (attrs.at_encoding);
6235 CHECKSUM_ATTR (attrs.at_endianity);
6236 CHECKSUM_ATTR (attrs.at_explicit);
6237 CHECKSUM_ATTR (attrs.at_is_optional);
6238 CHECKSUM_ATTR (attrs.at_location);
6239 CHECKSUM_ATTR (attrs.at_lower_bound);
6240 CHECKSUM_ATTR (attrs.at_mutable);
6241 CHECKSUM_ATTR (attrs.at_ordering);
6242 CHECKSUM_ATTR (attrs.at_picture_string);
6243 CHECKSUM_ATTR (attrs.at_prototyped);
6244 CHECKSUM_ATTR (attrs.at_small);
6245 CHECKSUM_ATTR (attrs.at_segment);
6246 CHECKSUM_ATTR (attrs.at_string_length);
6247 CHECKSUM_ATTR (attrs.at_threads_scaled);
6248 CHECKSUM_ATTR (attrs.at_upper_bound);
6249 CHECKSUM_ATTR (attrs.at_use_location);
6250 CHECKSUM_ATTR (attrs.at_use_UTF8);
6251 CHECKSUM_ATTR (attrs.at_variable_parameter);
6252 CHECKSUM_ATTR (attrs.at_virtuality);
6253 CHECKSUM_ATTR (attrs.at_visibility);
6254 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6255 CHECKSUM_ATTR (attrs.at_type);
6256 CHECKSUM_ATTR (attrs.at_friend);
6257
6258 /* Checksum the child DIEs. */
6259 c = die->die_child;
6260 if (c) do {
6261 dw_attr_ref name_attr;
6262
6263 c = c->die_sib;
6264 name_attr = get_AT (c, DW_AT_name);
6265 if (is_template_instantiation (c))
6266 {
6267 /* Ignore instantiations of member type and function templates. */
6268 }
6269 else if (name_attr != NULL
6270 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6271 {
6272 /* Use a shallow checksum for named nested types and member
6273 functions. */
6274 CHECKSUM_ULEB128 ('S');
6275 CHECKSUM_ULEB128 (c->die_tag);
6276 CHECKSUM_STRING (AT_string (name_attr));
6277 }
6278 else
6279 {
6280 /* Use a deep checksum for other children. */
6281 /* Mark this DIE so it gets processed when unmarking. */
6282 if (c->die_mark == 0)
6283 c->die_mark = -1;
6284 die_checksum_ordered (c, ctx, mark);
6285 }
6286 } while (c != die->die_child);
6287
6288 CHECKSUM_ULEB128 (0);
6289 }
6290
6291 /* Add a type name and tag to a hash. */
6292 static void
6293 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6294 {
6295 CHECKSUM_ULEB128 (tag);
6296 CHECKSUM_STRING (name);
6297 }
6298
6299 #undef CHECKSUM
6300 #undef CHECKSUM_STRING
6301 #undef CHECKSUM_ATTR
6302 #undef CHECKSUM_LEB128
6303 #undef CHECKSUM_ULEB128
6304
6305 /* Generate the type signature for DIE. This is computed by generating an
6306 MD5 checksum over the DIE's tag, its relevant attributes, and its
6307 children. Attributes that are references to other DIEs are processed
6308 by recursion, using the MARK field to prevent infinite recursion.
6309 If the DIE is nested inside a namespace or another type, we also
6310 need to include that context in the signature. The lower 64 bits
6311 of the resulting MD5 checksum comprise the signature. */
6312
6313 static void
6314 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6315 {
6316 int mark;
6317 const char *name;
6318 unsigned char checksum[16];
6319 struct md5_ctx ctx;
6320 dw_die_ref decl;
6321 dw_die_ref parent;
6322
6323 name = get_AT_string (die, DW_AT_name);
6324 decl = get_AT_ref (die, DW_AT_specification);
6325 parent = get_die_parent (die);
6326
6327 /* First, compute a signature for just the type name (and its surrounding
6328 context, if any. This is stored in the type unit DIE for link-time
6329 ODR (one-definition rule) checking. */
6330
6331 if (is_cxx () && name != NULL)
6332 {
6333 md5_init_ctx (&ctx);
6334
6335 /* Checksum the names of surrounding namespaces and structures. */
6336 if (parent != NULL)
6337 checksum_die_context (parent, &ctx);
6338
6339 /* Checksum the current DIE. */
6340 die_odr_checksum (die->die_tag, name, &ctx);
6341 md5_finish_ctx (&ctx, checksum);
6342
6343 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6344 }
6345
6346 /* Next, compute the complete type signature. */
6347
6348 md5_init_ctx (&ctx);
6349 mark = 1;
6350 die->die_mark = mark;
6351
6352 /* Checksum the names of surrounding namespaces and structures. */
6353 if (parent != NULL)
6354 checksum_die_context (parent, &ctx);
6355
6356 /* Checksum the DIE and its children. */
6357 die_checksum_ordered (die, &ctx, &mark);
6358 unmark_all_dies (die);
6359 md5_finish_ctx (&ctx, checksum);
6360
6361 /* Store the signature in the type node and link the type DIE and the
6362 type node together. */
6363 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6364 DWARF_TYPE_SIGNATURE_SIZE);
6365 die->comdat_type_p = true;
6366 die->die_id.die_type_node = type_node;
6367 type_node->type_die = die;
6368
6369 /* If the DIE is a specification, link its declaration to the type node
6370 as well. */
6371 if (decl != NULL)
6372 {
6373 decl->comdat_type_p = true;
6374 decl->die_id.die_type_node = type_node;
6375 }
6376 }
6377
6378 /* Do the location expressions look same? */
6379 static inline int
6380 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6381 {
6382 return loc1->dw_loc_opc == loc2->dw_loc_opc
6383 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6384 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6385 }
6386
6387 /* Do the values look the same? */
6388 static int
6389 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6390 {
6391 dw_loc_descr_ref loc1, loc2;
6392 rtx r1, r2;
6393
6394 if (v1->val_class != v2->val_class)
6395 return 0;
6396
6397 switch (v1->val_class)
6398 {
6399 case dw_val_class_const:
6400 return v1->v.val_int == v2->v.val_int;
6401 case dw_val_class_unsigned_const:
6402 return v1->v.val_unsigned == v2->v.val_unsigned;
6403 case dw_val_class_const_double:
6404 return v1->v.val_double.high == v2->v.val_double.high
6405 && v1->v.val_double.low == v2->v.val_double.low;
6406 case dw_val_class_wide_int:
6407 return *v1->v.val_wide == *v2->v.val_wide;
6408 case dw_val_class_vec:
6409 if (v1->v.val_vec.length != v2->v.val_vec.length
6410 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6411 return 0;
6412 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6413 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6414 return 0;
6415 return 1;
6416 case dw_val_class_flag:
6417 return v1->v.val_flag == v2->v.val_flag;
6418 case dw_val_class_str:
6419 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6420
6421 case dw_val_class_addr:
6422 r1 = v1->v.val_addr;
6423 r2 = v2->v.val_addr;
6424 if (GET_CODE (r1) != GET_CODE (r2))
6425 return 0;
6426 return !rtx_equal_p (r1, r2);
6427
6428 case dw_val_class_offset:
6429 return v1->v.val_offset == v2->v.val_offset;
6430
6431 case dw_val_class_loc:
6432 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6433 loc1 && loc2;
6434 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6435 if (!same_loc_p (loc1, loc2, mark))
6436 return 0;
6437 return !loc1 && !loc2;
6438
6439 case dw_val_class_die_ref:
6440 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6441
6442 case dw_val_class_fde_ref:
6443 case dw_val_class_vms_delta:
6444 case dw_val_class_lbl_id:
6445 case dw_val_class_lineptr:
6446 case dw_val_class_macptr:
6447 case dw_val_class_high_pc:
6448 return 1;
6449
6450 case dw_val_class_file:
6451 return v1->v.val_file == v2->v.val_file;
6452
6453 case dw_val_class_data8:
6454 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6455
6456 default:
6457 return 1;
6458 }
6459 }
6460
6461 /* Do the attributes look the same? */
6462
6463 static int
6464 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6465 {
6466 if (at1->dw_attr != at2->dw_attr)
6467 return 0;
6468
6469 /* We don't care that this was compiled with a different compiler
6470 snapshot; if the output is the same, that's what matters. */
6471 if (at1->dw_attr == DW_AT_producer)
6472 return 1;
6473
6474 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6475 }
6476
6477 /* Do the dies look the same? */
6478
6479 static int
6480 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6481 {
6482 dw_die_ref c1, c2;
6483 dw_attr_ref a1;
6484 unsigned ix;
6485
6486 /* To avoid infinite recursion. */
6487 if (die1->die_mark)
6488 return die1->die_mark == die2->die_mark;
6489 die1->die_mark = die2->die_mark = ++(*mark);
6490
6491 if (die1->die_tag != die2->die_tag)
6492 return 0;
6493
6494 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6495 return 0;
6496
6497 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6498 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6499 return 0;
6500
6501 c1 = die1->die_child;
6502 c2 = die2->die_child;
6503 if (! c1)
6504 {
6505 if (c2)
6506 return 0;
6507 }
6508 else
6509 for (;;)
6510 {
6511 if (!same_die_p (c1, c2, mark))
6512 return 0;
6513 c1 = c1->die_sib;
6514 c2 = c2->die_sib;
6515 if (c1 == die1->die_child)
6516 {
6517 if (c2 == die2->die_child)
6518 break;
6519 else
6520 return 0;
6521 }
6522 }
6523
6524 return 1;
6525 }
6526
6527 /* Do the dies look the same? Wrapper around same_die_p. */
6528
6529 static int
6530 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6531 {
6532 int mark = 0;
6533 int ret = same_die_p (die1, die2, &mark);
6534
6535 unmark_all_dies (die1);
6536 unmark_all_dies (die2);
6537
6538 return ret;
6539 }
6540
6541 /* The prefix to attach to symbols on DIEs in the current comdat debug
6542 info section. */
6543 static const char *comdat_symbol_id;
6544
6545 /* The index of the current symbol within the current comdat CU. */
6546 static unsigned int comdat_symbol_number;
6547
6548 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6549 children, and set comdat_symbol_id accordingly. */
6550
6551 static void
6552 compute_section_prefix (dw_die_ref unit_die)
6553 {
6554 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6555 const char *base = die_name ? lbasename (die_name) : "anonymous";
6556 char *name = XALLOCAVEC (char, strlen (base) + 64);
6557 char *p;
6558 int i, mark;
6559 unsigned char checksum[16];
6560 struct md5_ctx ctx;
6561
6562 /* Compute the checksum of the DIE, then append part of it as hex digits to
6563 the name filename of the unit. */
6564
6565 md5_init_ctx (&ctx);
6566 mark = 0;
6567 die_checksum (unit_die, &ctx, &mark);
6568 unmark_all_dies (unit_die);
6569 md5_finish_ctx (&ctx, checksum);
6570
6571 sprintf (name, "%s.", base);
6572 clean_symbol_name (name);
6573
6574 p = name + strlen (name);
6575 for (i = 0; i < 4; i++)
6576 {
6577 sprintf (p, "%.2x", checksum[i]);
6578 p += 2;
6579 }
6580
6581 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6582 comdat_symbol_number = 0;
6583 }
6584
6585 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6586
6587 static int
6588 is_type_die (dw_die_ref die)
6589 {
6590 switch (die->die_tag)
6591 {
6592 case DW_TAG_array_type:
6593 case DW_TAG_class_type:
6594 case DW_TAG_interface_type:
6595 case DW_TAG_enumeration_type:
6596 case DW_TAG_pointer_type:
6597 case DW_TAG_reference_type:
6598 case DW_TAG_rvalue_reference_type:
6599 case DW_TAG_string_type:
6600 case DW_TAG_structure_type:
6601 case DW_TAG_subroutine_type:
6602 case DW_TAG_union_type:
6603 case DW_TAG_ptr_to_member_type:
6604 case DW_TAG_set_type:
6605 case DW_TAG_subrange_type:
6606 case DW_TAG_base_type:
6607 case DW_TAG_const_type:
6608 case DW_TAG_file_type:
6609 case DW_TAG_packed_type:
6610 case DW_TAG_volatile_type:
6611 case DW_TAG_typedef:
6612 return 1;
6613 default:
6614 return 0;
6615 }
6616 }
6617
6618 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6619 Basically, we want to choose the bits that are likely to be shared between
6620 compilations (types) and leave out the bits that are specific to individual
6621 compilations (functions). */
6622
6623 static int
6624 is_comdat_die (dw_die_ref c)
6625 {
6626 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6627 we do for stabs. The advantage is a greater likelihood of sharing between
6628 objects that don't include headers in the same order (and therefore would
6629 put the base types in a different comdat). jason 8/28/00 */
6630
6631 if (c->die_tag == DW_TAG_base_type)
6632 return 0;
6633
6634 if (c->die_tag == DW_TAG_pointer_type
6635 || c->die_tag == DW_TAG_reference_type
6636 || c->die_tag == DW_TAG_rvalue_reference_type
6637 || c->die_tag == DW_TAG_const_type
6638 || c->die_tag == DW_TAG_volatile_type)
6639 {
6640 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6641
6642 return t ? is_comdat_die (t) : 0;
6643 }
6644
6645 return is_type_die (c);
6646 }
6647
6648 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6649 compilation unit. */
6650
6651 static int
6652 is_symbol_die (dw_die_ref c)
6653 {
6654 return (is_type_die (c)
6655 || is_declaration_die (c)
6656 || c->die_tag == DW_TAG_namespace
6657 || c->die_tag == DW_TAG_module);
6658 }
6659
6660 /* Returns true iff C is a compile-unit DIE. */
6661
6662 static inline bool
6663 is_cu_die (dw_die_ref c)
6664 {
6665 return c && c->die_tag == DW_TAG_compile_unit;
6666 }
6667
6668 /* Returns true iff C is a unit DIE of some sort. */
6669
6670 static inline bool
6671 is_unit_die (dw_die_ref c)
6672 {
6673 return c && (c->die_tag == DW_TAG_compile_unit
6674 || c->die_tag == DW_TAG_partial_unit
6675 || c->die_tag == DW_TAG_type_unit);
6676 }
6677
6678 /* Returns true iff C is a namespace DIE. */
6679
6680 static inline bool
6681 is_namespace_die (dw_die_ref c)
6682 {
6683 return c && c->die_tag == DW_TAG_namespace;
6684 }
6685
6686 /* Returns true iff C is a class or structure DIE. */
6687
6688 static inline bool
6689 is_class_die (dw_die_ref c)
6690 {
6691 return c && (c->die_tag == DW_TAG_class_type
6692 || c->die_tag == DW_TAG_structure_type);
6693 }
6694
6695 /* Return non-zero if this DIE is a template parameter. */
6696
6697 static inline bool
6698 is_template_parameter (dw_die_ref die)
6699 {
6700 switch (die->die_tag)
6701 {
6702 case DW_TAG_template_type_param:
6703 case DW_TAG_template_value_param:
6704 case DW_TAG_GNU_template_template_param:
6705 case DW_TAG_GNU_template_parameter_pack:
6706 return true;
6707 default:
6708 return false;
6709 }
6710 }
6711
6712 /* Return non-zero if this DIE represents a template instantiation. */
6713
6714 static inline bool
6715 is_template_instantiation (dw_die_ref die)
6716 {
6717 dw_die_ref c;
6718
6719 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6720 return false;
6721 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6722 return false;
6723 }
6724
6725 static char *
6726 gen_internal_sym (const char *prefix)
6727 {
6728 char buf[256];
6729
6730 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6731 return xstrdup (buf);
6732 }
6733
6734 /* Assign symbols to all worthy DIEs under DIE. */
6735
6736 static void
6737 assign_symbol_names (dw_die_ref die)
6738 {
6739 dw_die_ref c;
6740
6741 if (is_symbol_die (die) && !die->comdat_type_p)
6742 {
6743 if (comdat_symbol_id)
6744 {
6745 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6746
6747 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6748 comdat_symbol_id, comdat_symbol_number++);
6749 die->die_id.die_symbol = xstrdup (p);
6750 }
6751 else
6752 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6753 }
6754
6755 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6756 }
6757
6758 struct cu_hash_table_entry
6759 {
6760 dw_die_ref cu;
6761 unsigned min_comdat_num, max_comdat_num;
6762 struct cu_hash_table_entry *next;
6763 };
6764
6765 /* Helpers to manipulate hash table of CUs. */
6766
6767 struct cu_hash_table_entry_hasher
6768 {
6769 typedef cu_hash_table_entry value_type;
6770 typedef die_struct compare_type;
6771 static inline hashval_t hash (const value_type *);
6772 static inline bool equal (const value_type *, const compare_type *);
6773 static inline void remove (value_type *);
6774 };
6775
6776 inline hashval_t
6777 cu_hash_table_entry_hasher::hash (const value_type *entry)
6778 {
6779 return htab_hash_string (entry->cu->die_id.die_symbol);
6780 }
6781
6782 inline bool
6783 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6784 const compare_type *entry2)
6785 {
6786 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6787 }
6788
6789 inline void
6790 cu_hash_table_entry_hasher::remove (value_type *entry)
6791 {
6792 struct cu_hash_table_entry *next;
6793
6794 while (entry)
6795 {
6796 next = entry->next;
6797 free (entry);
6798 entry = next;
6799 }
6800 }
6801
6802 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6803
6804 /* Check whether we have already seen this CU and set up SYM_NUM
6805 accordingly. */
6806 static int
6807 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6808 {
6809 struct cu_hash_table_entry dummy;
6810 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6811
6812 dummy.max_comdat_num = 0;
6813
6814 slot = htable->find_slot_with_hash (cu,
6815 htab_hash_string (cu->die_id.die_symbol),
6816 INSERT);
6817 entry = *slot;
6818
6819 for (; entry; last = entry, entry = entry->next)
6820 {
6821 if (same_die_p_wrap (cu, entry->cu))
6822 break;
6823 }
6824
6825 if (entry)
6826 {
6827 *sym_num = entry->min_comdat_num;
6828 return 1;
6829 }
6830
6831 entry = XCNEW (struct cu_hash_table_entry);
6832 entry->cu = cu;
6833 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6834 entry->next = *slot;
6835 *slot = entry;
6836
6837 return 0;
6838 }
6839
6840 /* Record SYM_NUM to record of CU in HTABLE. */
6841 static void
6842 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6843 unsigned int sym_num)
6844 {
6845 struct cu_hash_table_entry **slot, *entry;
6846
6847 slot = htable->find_slot_with_hash (cu,
6848 htab_hash_string (cu->die_id.die_symbol),
6849 NO_INSERT);
6850 entry = *slot;
6851
6852 entry->max_comdat_num = sym_num;
6853 }
6854
6855 /* Traverse the DIE (which is always comp_unit_die), and set up
6856 additional compilation units for each of the include files we see
6857 bracketed by BINCL/EINCL. */
6858
6859 static void
6860 break_out_includes (dw_die_ref die)
6861 {
6862 dw_die_ref c;
6863 dw_die_ref unit = NULL;
6864 limbo_die_node *node, **pnode;
6865
6866 c = die->die_child;
6867 if (c) do {
6868 dw_die_ref prev = c;
6869 c = c->die_sib;
6870 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6871 || (unit && is_comdat_die (c)))
6872 {
6873 dw_die_ref next = c->die_sib;
6874
6875 /* This DIE is for a secondary CU; remove it from the main one. */
6876 remove_child_with_prev (c, prev);
6877
6878 if (c->die_tag == DW_TAG_GNU_BINCL)
6879 unit = push_new_compile_unit (unit, c);
6880 else if (c->die_tag == DW_TAG_GNU_EINCL)
6881 unit = pop_compile_unit (unit);
6882 else
6883 add_child_die (unit, c);
6884 c = next;
6885 if (c == die->die_child)
6886 break;
6887 }
6888 } while (c != die->die_child);
6889
6890 #if 0
6891 /* We can only use this in debugging, since the frontend doesn't check
6892 to make sure that we leave every include file we enter. */
6893 gcc_assert (!unit);
6894 #endif
6895
6896 assign_symbol_names (die);
6897 cu_hash_type cu_hash_table (10);
6898 for (node = limbo_die_list, pnode = &limbo_die_list;
6899 node;
6900 node = node->next)
6901 {
6902 int is_dupl;
6903
6904 compute_section_prefix (node->die);
6905 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6906 &comdat_symbol_number);
6907 assign_symbol_names (node->die);
6908 if (is_dupl)
6909 *pnode = node->next;
6910 else
6911 {
6912 pnode = &node->next;
6913 record_comdat_symbol_number (node->die, &cu_hash_table,
6914 comdat_symbol_number);
6915 }
6916 }
6917 }
6918
6919 /* Return non-zero if this DIE is a declaration. */
6920
6921 static int
6922 is_declaration_die (dw_die_ref die)
6923 {
6924 dw_attr_ref a;
6925 unsigned ix;
6926
6927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6928 if (a->dw_attr == DW_AT_declaration)
6929 return 1;
6930
6931 return 0;
6932 }
6933
6934 /* Return non-zero if this DIE is nested inside a subprogram. */
6935
6936 static int
6937 is_nested_in_subprogram (dw_die_ref die)
6938 {
6939 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6940
6941 if (decl == NULL)
6942 decl = die;
6943 return local_scope_p (decl);
6944 }
6945
6946 /* Return non-zero if this DIE contains a defining declaration of a
6947 subprogram. */
6948
6949 static int
6950 contains_subprogram_definition (dw_die_ref die)
6951 {
6952 dw_die_ref c;
6953
6954 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6955 return 1;
6956 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6957 return 0;
6958 }
6959
6960 /* Return non-zero if this is a type DIE that should be moved to a
6961 COMDAT .debug_types section. */
6962
6963 static int
6964 should_move_die_to_comdat (dw_die_ref die)
6965 {
6966 switch (die->die_tag)
6967 {
6968 case DW_TAG_class_type:
6969 case DW_TAG_structure_type:
6970 case DW_TAG_enumeration_type:
6971 case DW_TAG_union_type:
6972 /* Don't move declarations, inlined instances, types nested in a
6973 subprogram, or types that contain subprogram definitions. */
6974 if (is_declaration_die (die)
6975 || get_AT (die, DW_AT_abstract_origin)
6976 || is_nested_in_subprogram (die)
6977 || contains_subprogram_definition (die))
6978 return 0;
6979 return 1;
6980 case DW_TAG_array_type:
6981 case DW_TAG_interface_type:
6982 case DW_TAG_pointer_type:
6983 case DW_TAG_reference_type:
6984 case DW_TAG_rvalue_reference_type:
6985 case DW_TAG_string_type:
6986 case DW_TAG_subroutine_type:
6987 case DW_TAG_ptr_to_member_type:
6988 case DW_TAG_set_type:
6989 case DW_TAG_subrange_type:
6990 case DW_TAG_base_type:
6991 case DW_TAG_const_type:
6992 case DW_TAG_file_type:
6993 case DW_TAG_packed_type:
6994 case DW_TAG_volatile_type:
6995 case DW_TAG_typedef:
6996 default:
6997 return 0;
6998 }
6999 }
7000
7001 /* Make a clone of DIE. */
7002
7003 static dw_die_ref
7004 clone_die (dw_die_ref die)
7005 {
7006 dw_die_ref clone;
7007 dw_attr_ref a;
7008 unsigned ix;
7009
7010 clone = ggc_cleared_alloc<die_node> ();
7011 clone->die_tag = die->die_tag;
7012
7013 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7014 add_dwarf_attr (clone, a);
7015
7016 return clone;
7017 }
7018
7019 /* Make a clone of the tree rooted at DIE. */
7020
7021 static dw_die_ref
7022 clone_tree (dw_die_ref die)
7023 {
7024 dw_die_ref c;
7025 dw_die_ref clone = clone_die (die);
7026
7027 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7028
7029 return clone;
7030 }
7031
7032 /* Make a clone of DIE as a declaration. */
7033
7034 static dw_die_ref
7035 clone_as_declaration (dw_die_ref die)
7036 {
7037 dw_die_ref clone;
7038 dw_die_ref decl;
7039 dw_attr_ref a;
7040 unsigned ix;
7041
7042 /* If the DIE is already a declaration, just clone it. */
7043 if (is_declaration_die (die))
7044 return clone_die (die);
7045
7046 /* If the DIE is a specification, just clone its declaration DIE. */
7047 decl = get_AT_ref (die, DW_AT_specification);
7048 if (decl != NULL)
7049 {
7050 clone = clone_die (decl);
7051 if (die->comdat_type_p)
7052 add_AT_die_ref (clone, DW_AT_signature, die);
7053 return clone;
7054 }
7055
7056 clone = ggc_cleared_alloc<die_node> ();
7057 clone->die_tag = die->die_tag;
7058
7059 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7060 {
7061 /* We don't want to copy over all attributes.
7062 For example we don't want DW_AT_byte_size because otherwise we will no
7063 longer have a declaration and GDB will treat it as a definition. */
7064
7065 switch (a->dw_attr)
7066 {
7067 case DW_AT_abstract_origin:
7068 case DW_AT_artificial:
7069 case DW_AT_containing_type:
7070 case DW_AT_external:
7071 case DW_AT_name:
7072 case DW_AT_type:
7073 case DW_AT_virtuality:
7074 case DW_AT_linkage_name:
7075 case DW_AT_MIPS_linkage_name:
7076 add_dwarf_attr (clone, a);
7077 break;
7078 case DW_AT_byte_size:
7079 default:
7080 break;
7081 }
7082 }
7083
7084 if (die->comdat_type_p)
7085 add_AT_die_ref (clone, DW_AT_signature, die);
7086
7087 add_AT_flag (clone, DW_AT_declaration, 1);
7088 return clone;
7089 }
7090
7091
7092 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7093
7094 struct decl_table_entry
7095 {
7096 dw_die_ref orig;
7097 dw_die_ref copy;
7098 };
7099
7100 /* Helpers to manipulate hash table of copied declarations. */
7101
7102 /* Hashtable helpers. */
7103
7104 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7105 {
7106 typedef decl_table_entry value_type;
7107 typedef die_struct compare_type;
7108 static inline hashval_t hash (const value_type *);
7109 static inline bool equal (const value_type *, const compare_type *);
7110 };
7111
7112 inline hashval_t
7113 decl_table_entry_hasher::hash (const value_type *entry)
7114 {
7115 return htab_hash_pointer (entry->orig);
7116 }
7117
7118 inline bool
7119 decl_table_entry_hasher::equal (const value_type *entry1,
7120 const compare_type *entry2)
7121 {
7122 return entry1->orig == entry2;
7123 }
7124
7125 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7126
7127 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7128 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7129 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7130 to check if the ancestor has already been copied into UNIT. */
7131
7132 static dw_die_ref
7133 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7134 decl_hash_type *decl_table)
7135 {
7136 dw_die_ref parent = die->die_parent;
7137 dw_die_ref new_parent = unit;
7138 dw_die_ref copy;
7139 decl_table_entry **slot = NULL;
7140 struct decl_table_entry *entry = NULL;
7141
7142 if (decl_table)
7143 {
7144 /* Check if the entry has already been copied to UNIT. */
7145 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7146 INSERT);
7147 if (*slot != HTAB_EMPTY_ENTRY)
7148 {
7149 entry = *slot;
7150 return entry->copy;
7151 }
7152
7153 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7154 entry = XCNEW (struct decl_table_entry);
7155 entry->orig = die;
7156 entry->copy = NULL;
7157 *slot = entry;
7158 }
7159
7160 if (parent != NULL)
7161 {
7162 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7163 if (spec != NULL)
7164 parent = spec;
7165 if (!is_unit_die (parent))
7166 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7167 }
7168
7169 copy = clone_as_declaration (die);
7170 add_child_die (new_parent, copy);
7171
7172 if (decl_table)
7173 {
7174 /* Record the pointer to the copy. */
7175 entry->copy = copy;
7176 }
7177
7178 return copy;
7179 }
7180 /* Copy the declaration context to the new type unit DIE. This includes
7181 any surrounding namespace or type declarations. If the DIE has an
7182 AT_specification attribute, it also includes attributes and children
7183 attached to the specification, and returns a pointer to the original
7184 parent of the declaration DIE. Returns NULL otherwise. */
7185
7186 static dw_die_ref
7187 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7188 {
7189 dw_die_ref decl;
7190 dw_die_ref new_decl;
7191 dw_die_ref orig_parent = NULL;
7192
7193 decl = get_AT_ref (die, DW_AT_specification);
7194 if (decl == NULL)
7195 decl = die;
7196 else
7197 {
7198 unsigned ix;
7199 dw_die_ref c;
7200 dw_attr_ref a;
7201
7202 /* The original DIE will be changed to a declaration, and must
7203 be moved to be a child of the original declaration DIE. */
7204 orig_parent = decl->die_parent;
7205
7206 /* Copy the type node pointer from the new DIE to the original
7207 declaration DIE so we can forward references later. */
7208 decl->comdat_type_p = true;
7209 decl->die_id.die_type_node = die->die_id.die_type_node;
7210
7211 remove_AT (die, DW_AT_specification);
7212
7213 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7214 {
7215 if (a->dw_attr != DW_AT_name
7216 && a->dw_attr != DW_AT_declaration
7217 && a->dw_attr != DW_AT_external)
7218 add_dwarf_attr (die, a);
7219 }
7220
7221 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7222 }
7223
7224 if (decl->die_parent != NULL
7225 && !is_unit_die (decl->die_parent))
7226 {
7227 new_decl = copy_ancestor_tree (unit, decl, NULL);
7228 if (new_decl != NULL)
7229 {
7230 remove_AT (new_decl, DW_AT_signature);
7231 add_AT_specification (die, new_decl);
7232 }
7233 }
7234
7235 return orig_parent;
7236 }
7237
7238 /* Generate the skeleton ancestor tree for the given NODE, then clone
7239 the DIE and add the clone into the tree. */
7240
7241 static void
7242 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7243 {
7244 if (node->new_die != NULL)
7245 return;
7246
7247 node->new_die = clone_as_declaration (node->old_die);
7248
7249 if (node->parent != NULL)
7250 {
7251 generate_skeleton_ancestor_tree (node->parent);
7252 add_child_die (node->parent->new_die, node->new_die);
7253 }
7254 }
7255
7256 /* Generate a skeleton tree of DIEs containing any declarations that are
7257 found in the original tree. We traverse the tree looking for declaration
7258 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7259
7260 static void
7261 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7262 {
7263 skeleton_chain_node node;
7264 dw_die_ref c;
7265 dw_die_ref first;
7266 dw_die_ref prev = NULL;
7267 dw_die_ref next = NULL;
7268
7269 node.parent = parent;
7270
7271 first = c = parent->old_die->die_child;
7272 if (c)
7273 next = c->die_sib;
7274 if (c) do {
7275 if (prev == NULL || prev->die_sib == c)
7276 prev = c;
7277 c = next;
7278 next = (c == first ? NULL : c->die_sib);
7279 node.old_die = c;
7280 node.new_die = NULL;
7281 if (is_declaration_die (c))
7282 {
7283 if (is_template_instantiation (c))
7284 {
7285 /* Instantiated templates do not need to be cloned into the
7286 type unit. Just move the DIE and its children back to
7287 the skeleton tree (in the main CU). */
7288 remove_child_with_prev (c, prev);
7289 add_child_die (parent->new_die, c);
7290 c = prev;
7291 }
7292 else
7293 {
7294 /* Clone the existing DIE, move the original to the skeleton
7295 tree (which is in the main CU), and put the clone, with
7296 all the original's children, where the original came from
7297 (which is about to be moved to the type unit). */
7298 dw_die_ref clone = clone_die (c);
7299 move_all_children (c, clone);
7300
7301 /* If the original has a DW_AT_object_pointer attribute,
7302 it would now point to a child DIE just moved to the
7303 cloned tree, so we need to remove that attribute from
7304 the original. */
7305 remove_AT (c, DW_AT_object_pointer);
7306
7307 replace_child (c, clone, prev);
7308 generate_skeleton_ancestor_tree (parent);
7309 add_child_die (parent->new_die, c);
7310 node.new_die = c;
7311 c = clone;
7312 }
7313 }
7314 generate_skeleton_bottom_up (&node);
7315 } while (next != NULL);
7316 }
7317
7318 /* Wrapper function for generate_skeleton_bottom_up. */
7319
7320 static dw_die_ref
7321 generate_skeleton (dw_die_ref die)
7322 {
7323 skeleton_chain_node node;
7324
7325 node.old_die = die;
7326 node.new_die = NULL;
7327 node.parent = NULL;
7328
7329 /* If this type definition is nested inside another type,
7330 and is not an instantiation of a template, always leave
7331 at least a declaration in its place. */
7332 if (die->die_parent != NULL
7333 && is_type_die (die->die_parent)
7334 && !is_template_instantiation (die))
7335 node.new_die = clone_as_declaration (die);
7336
7337 generate_skeleton_bottom_up (&node);
7338 return node.new_die;
7339 }
7340
7341 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7342 declaration. The original DIE is moved to a new compile unit so that
7343 existing references to it follow it to the new location. If any of the
7344 original DIE's descendants is a declaration, we need to replace the
7345 original DIE with a skeleton tree and move the declarations back into the
7346 skeleton tree. */
7347
7348 static dw_die_ref
7349 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7350 dw_die_ref prev)
7351 {
7352 dw_die_ref skeleton, orig_parent;
7353
7354 /* Copy the declaration context to the type unit DIE. If the returned
7355 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7356 that DIE. */
7357 orig_parent = copy_declaration_context (unit, child);
7358
7359 skeleton = generate_skeleton (child);
7360 if (skeleton == NULL)
7361 remove_child_with_prev (child, prev);
7362 else
7363 {
7364 skeleton->comdat_type_p = true;
7365 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7366
7367 /* If the original DIE was a specification, we need to put
7368 the skeleton under the parent DIE of the declaration.
7369 This leaves the original declaration in the tree, but
7370 it will be pruned later since there are no longer any
7371 references to it. */
7372 if (orig_parent != NULL)
7373 {
7374 remove_child_with_prev (child, prev);
7375 add_child_die (orig_parent, skeleton);
7376 }
7377 else
7378 replace_child (child, skeleton, prev);
7379 }
7380
7381 return skeleton;
7382 }
7383
7384 /* Traverse the DIE and set up additional .debug_types sections for each
7385 type worthy of being placed in a COMDAT section. */
7386
7387 static void
7388 break_out_comdat_types (dw_die_ref die)
7389 {
7390 dw_die_ref c;
7391 dw_die_ref first;
7392 dw_die_ref prev = NULL;
7393 dw_die_ref next = NULL;
7394 dw_die_ref unit = NULL;
7395
7396 first = c = die->die_child;
7397 if (c)
7398 next = c->die_sib;
7399 if (c) do {
7400 if (prev == NULL || prev->die_sib == c)
7401 prev = c;
7402 c = next;
7403 next = (c == first ? NULL : c->die_sib);
7404 if (should_move_die_to_comdat (c))
7405 {
7406 dw_die_ref replacement;
7407 comdat_type_node_ref type_node;
7408
7409 /* Break out nested types into their own type units. */
7410 break_out_comdat_types (c);
7411
7412 /* Create a new type unit DIE as the root for the new tree, and
7413 add it to the list of comdat types. */
7414 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7415 add_AT_unsigned (unit, DW_AT_language,
7416 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7417 type_node = ggc_cleared_alloc<comdat_type_node> ();
7418 type_node->root_die = unit;
7419 type_node->next = comdat_type_list;
7420 comdat_type_list = type_node;
7421
7422 /* Generate the type signature. */
7423 generate_type_signature (c, type_node);
7424
7425 /* Copy the declaration context, attributes, and children of the
7426 declaration into the new type unit DIE, then remove this DIE
7427 from the main CU (or replace it with a skeleton if necessary). */
7428 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7429 type_node->skeleton_die = replacement;
7430
7431 /* Add the DIE to the new compunit. */
7432 add_child_die (unit, c);
7433
7434 if (replacement != NULL)
7435 c = replacement;
7436 }
7437 else if (c->die_tag == DW_TAG_namespace
7438 || c->die_tag == DW_TAG_class_type
7439 || c->die_tag == DW_TAG_structure_type
7440 || c->die_tag == DW_TAG_union_type)
7441 {
7442 /* Look for nested types that can be broken out. */
7443 break_out_comdat_types (c);
7444 }
7445 } while (next != NULL);
7446 }
7447
7448 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7449 Enter all the cloned children into the hash table decl_table. */
7450
7451 static dw_die_ref
7452 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7453 {
7454 dw_die_ref c;
7455 dw_die_ref clone;
7456 struct decl_table_entry *entry;
7457 decl_table_entry **slot;
7458
7459 if (die->die_tag == DW_TAG_subprogram)
7460 clone = clone_as_declaration (die);
7461 else
7462 clone = clone_die (die);
7463
7464 slot = decl_table->find_slot_with_hash (die,
7465 htab_hash_pointer (die), INSERT);
7466
7467 /* Assert that DIE isn't in the hash table yet. If it would be there
7468 before, the ancestors would be necessarily there as well, therefore
7469 clone_tree_partial wouldn't be called. */
7470 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7471
7472 entry = XCNEW (struct decl_table_entry);
7473 entry->orig = die;
7474 entry->copy = clone;
7475 *slot = entry;
7476
7477 if (die->die_tag != DW_TAG_subprogram)
7478 FOR_EACH_CHILD (die, c,
7479 add_child_die (clone, clone_tree_partial (c, decl_table)));
7480
7481 return clone;
7482 }
7483
7484 /* Walk the DIE and its children, looking for references to incomplete
7485 or trivial types that are unmarked (i.e., that are not in the current
7486 type_unit). */
7487
7488 static void
7489 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7490 {
7491 dw_die_ref c;
7492 dw_attr_ref a;
7493 unsigned ix;
7494
7495 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7496 {
7497 if (AT_class (a) == dw_val_class_die_ref)
7498 {
7499 dw_die_ref targ = AT_ref (a);
7500 decl_table_entry **slot;
7501 struct decl_table_entry *entry;
7502
7503 if (targ->die_mark != 0 || targ->comdat_type_p)
7504 continue;
7505
7506 slot = decl_table->find_slot_with_hash (targ,
7507 htab_hash_pointer (targ),
7508 INSERT);
7509
7510 if (*slot != HTAB_EMPTY_ENTRY)
7511 {
7512 /* TARG has already been copied, so we just need to
7513 modify the reference to point to the copy. */
7514 entry = *slot;
7515 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7516 }
7517 else
7518 {
7519 dw_die_ref parent = unit;
7520 dw_die_ref copy = clone_die (targ);
7521
7522 /* Record in DECL_TABLE that TARG has been copied.
7523 Need to do this now, before the recursive call,
7524 because DECL_TABLE may be expanded and SLOT
7525 would no longer be a valid pointer. */
7526 entry = XCNEW (struct decl_table_entry);
7527 entry->orig = targ;
7528 entry->copy = copy;
7529 *slot = entry;
7530
7531 /* If TARG is not a declaration DIE, we need to copy its
7532 children. */
7533 if (!is_declaration_die (targ))
7534 {
7535 FOR_EACH_CHILD (
7536 targ, c,
7537 add_child_die (copy,
7538 clone_tree_partial (c, decl_table)));
7539 }
7540
7541 /* Make sure the cloned tree is marked as part of the
7542 type unit. */
7543 mark_dies (copy);
7544
7545 /* If TARG has surrounding context, copy its ancestor tree
7546 into the new type unit. */
7547 if (targ->die_parent != NULL
7548 && !is_unit_die (targ->die_parent))
7549 parent = copy_ancestor_tree (unit, targ->die_parent,
7550 decl_table);
7551
7552 add_child_die (parent, copy);
7553 a->dw_attr_val.v.val_die_ref.die = copy;
7554
7555 /* Make sure the newly-copied DIE is walked. If it was
7556 installed in a previously-added context, it won't
7557 get visited otherwise. */
7558 if (parent != unit)
7559 {
7560 /* Find the highest point of the newly-added tree,
7561 mark each node along the way, and walk from there. */
7562 parent->die_mark = 1;
7563 while (parent->die_parent
7564 && parent->die_parent->die_mark == 0)
7565 {
7566 parent = parent->die_parent;
7567 parent->die_mark = 1;
7568 }
7569 copy_decls_walk (unit, parent, decl_table);
7570 }
7571 }
7572 }
7573 }
7574
7575 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7576 }
7577
7578 /* Copy declarations for "unworthy" types into the new comdat section.
7579 Incomplete types, modified types, and certain other types aren't broken
7580 out into comdat sections of their own, so they don't have a signature,
7581 and we need to copy the declaration into the same section so that we
7582 don't have an external reference. */
7583
7584 static void
7585 copy_decls_for_unworthy_types (dw_die_ref unit)
7586 {
7587 mark_dies (unit);
7588 decl_hash_type decl_table (10);
7589 copy_decls_walk (unit, unit, &decl_table);
7590 unmark_dies (unit);
7591 }
7592
7593 /* Traverse the DIE and add a sibling attribute if it may have the
7594 effect of speeding up access to siblings. To save some space,
7595 avoid generating sibling attributes for DIE's without children. */
7596
7597 static void
7598 add_sibling_attributes (dw_die_ref die)
7599 {
7600 dw_die_ref c;
7601
7602 if (! die->die_child)
7603 return;
7604
7605 if (die->die_parent && die != die->die_parent->die_child)
7606 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7607
7608 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7609 }
7610
7611 /* Output all location lists for the DIE and its children. */
7612
7613 static void
7614 output_location_lists (dw_die_ref die)
7615 {
7616 dw_die_ref c;
7617 dw_attr_ref a;
7618 unsigned ix;
7619
7620 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7621 if (AT_class (a) == dw_val_class_loc_list)
7622 output_loc_list (AT_loc_list (a));
7623
7624 FOR_EACH_CHILD (die, c, output_location_lists (c));
7625 }
7626
7627 /* We want to limit the number of external references, because they are
7628 larger than local references: a relocation takes multiple words, and
7629 even a sig8 reference is always eight bytes, whereas a local reference
7630 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7631 So if we encounter multiple external references to the same type DIE, we
7632 make a local typedef stub for it and redirect all references there.
7633
7634 This is the element of the hash table for keeping track of these
7635 references. */
7636
7637 struct external_ref
7638 {
7639 dw_die_ref type;
7640 dw_die_ref stub;
7641 unsigned n_refs;
7642 };
7643
7644 /* Hashtable helpers. */
7645
7646 struct external_ref_hasher : typed_free_remove <external_ref>
7647 {
7648 typedef external_ref value_type;
7649 typedef external_ref compare_type;
7650 static inline hashval_t hash (const value_type *);
7651 static inline bool equal (const value_type *, const compare_type *);
7652 };
7653
7654 inline hashval_t
7655 external_ref_hasher::hash (const value_type *r)
7656 {
7657 dw_die_ref die = r->type;
7658 hashval_t h = 0;
7659
7660 /* We can't use the address of the DIE for hashing, because
7661 that will make the order of the stub DIEs non-deterministic. */
7662 if (! die->comdat_type_p)
7663 /* We have a symbol; use it to compute a hash. */
7664 h = htab_hash_string (die->die_id.die_symbol);
7665 else
7666 {
7667 /* We have a type signature; use a subset of the bits as the hash.
7668 The 8-byte signature is at least as large as hashval_t. */
7669 comdat_type_node_ref type_node = die->die_id.die_type_node;
7670 memcpy (&h, type_node->signature, sizeof (h));
7671 }
7672 return h;
7673 }
7674
7675 inline bool
7676 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7677 {
7678 return r1->type == r2->type;
7679 }
7680
7681 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7682
7683 /* Return a pointer to the external_ref for references to DIE. */
7684
7685 static struct external_ref *
7686 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7687 {
7688 struct external_ref ref, *ref_p;
7689 external_ref **slot;
7690
7691 ref.type = die;
7692 slot = map->find_slot (&ref, INSERT);
7693 if (*slot != HTAB_EMPTY_ENTRY)
7694 return *slot;
7695
7696 ref_p = XCNEW (struct external_ref);
7697 ref_p->type = die;
7698 *slot = ref_p;
7699 return ref_p;
7700 }
7701
7702 /* Subroutine of optimize_external_refs, below.
7703
7704 If we see a type skeleton, record it as our stub. If we see external
7705 references, remember how many we've seen. */
7706
7707 static void
7708 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7709 {
7710 dw_die_ref c;
7711 dw_attr_ref a;
7712 unsigned ix;
7713 struct external_ref *ref_p;
7714
7715 if (is_type_die (die)
7716 && (c = get_AT_ref (die, DW_AT_signature)))
7717 {
7718 /* This is a local skeleton; use it for local references. */
7719 ref_p = lookup_external_ref (map, c);
7720 ref_p->stub = die;
7721 }
7722
7723 /* Scan the DIE references, and remember any that refer to DIEs from
7724 other CUs (i.e. those which are not marked). */
7725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7726 if (AT_class (a) == dw_val_class_die_ref
7727 && (c = AT_ref (a))->die_mark == 0
7728 && is_type_die (c))
7729 {
7730 ref_p = lookup_external_ref (map, c);
7731 ref_p->n_refs++;
7732 }
7733
7734 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7735 }
7736
7737 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7738 points to an external_ref, DATA is the CU we're processing. If we don't
7739 already have a local stub, and we have multiple refs, build a stub. */
7740
7741 int
7742 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7743 {
7744 struct external_ref *ref_p = *slot;
7745
7746 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7747 {
7748 /* We have multiple references to this type, so build a small stub.
7749 Both of these forms are a bit dodgy from the perspective of the
7750 DWARF standard, since technically they should have names. */
7751 dw_die_ref cu = data;
7752 dw_die_ref type = ref_p->type;
7753 dw_die_ref stub = NULL;
7754
7755 if (type->comdat_type_p)
7756 {
7757 /* If we refer to this type via sig8, use AT_signature. */
7758 stub = new_die (type->die_tag, cu, NULL_TREE);
7759 add_AT_die_ref (stub, DW_AT_signature, type);
7760 }
7761 else
7762 {
7763 /* Otherwise, use a typedef with no name. */
7764 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7765 add_AT_die_ref (stub, DW_AT_type, type);
7766 }
7767
7768 stub->die_mark++;
7769 ref_p->stub = stub;
7770 }
7771 return 1;
7772 }
7773
7774 /* DIE is a unit; look through all the DIE references to see if there are
7775 any external references to types, and if so, create local stubs for
7776 them which will be applied in build_abbrev_table. This is useful because
7777 references to local DIEs are smaller. */
7778
7779 static external_ref_hash_type *
7780 optimize_external_refs (dw_die_ref die)
7781 {
7782 external_ref_hash_type *map = new external_ref_hash_type (10);
7783 optimize_external_refs_1 (die, map);
7784 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7785 return map;
7786 }
7787
7788 /* The format of each DIE (and its attribute value pairs) is encoded in an
7789 abbreviation table. This routine builds the abbreviation table and assigns
7790 a unique abbreviation id for each abbreviation entry. The children of each
7791 die are visited recursively. */
7792
7793 static void
7794 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7795 {
7796 unsigned long abbrev_id;
7797 unsigned int n_alloc;
7798 dw_die_ref c;
7799 dw_attr_ref a;
7800 unsigned ix;
7801
7802 /* Scan the DIE references, and replace any that refer to
7803 DIEs from other CUs (i.e. those which are not marked) with
7804 the local stubs we built in optimize_external_refs. */
7805 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7806 if (AT_class (a) == dw_val_class_die_ref
7807 && (c = AT_ref (a))->die_mark == 0)
7808 {
7809 struct external_ref *ref_p;
7810 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7811
7812 ref_p = lookup_external_ref (extern_map, c);
7813 if (ref_p->stub && ref_p->stub != die)
7814 change_AT_die_ref (a, ref_p->stub);
7815 else
7816 /* We aren't changing this reference, so mark it external. */
7817 set_AT_ref_external (a, 1);
7818 }
7819
7820 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7821 {
7822 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7823 dw_attr_ref die_a, abbrev_a;
7824 unsigned ix;
7825 bool ok = true;
7826
7827 if (abbrev->die_tag != die->die_tag)
7828 continue;
7829 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7830 continue;
7831
7832 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7833 continue;
7834
7835 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7836 {
7837 abbrev_a = &(*abbrev->die_attr)[ix];
7838 if ((abbrev_a->dw_attr != die_a->dw_attr)
7839 || (value_format (abbrev_a) != value_format (die_a)))
7840 {
7841 ok = false;
7842 break;
7843 }
7844 }
7845 if (ok)
7846 break;
7847 }
7848
7849 if (abbrev_id >= abbrev_die_table_in_use)
7850 {
7851 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7852 {
7853 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7854 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7855 n_alloc);
7856
7857 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7858 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7859 abbrev_die_table_allocated = n_alloc;
7860 }
7861
7862 ++abbrev_die_table_in_use;
7863 abbrev_die_table[abbrev_id] = die;
7864 }
7865
7866 die->die_abbrev = abbrev_id;
7867 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7868 }
7869 \f
7870 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7871
7872 static int
7873 constant_size (unsigned HOST_WIDE_INT value)
7874 {
7875 int log;
7876
7877 if (value == 0)
7878 log = 0;
7879 else
7880 log = floor_log2 (value);
7881
7882 log = log / 8;
7883 log = 1 << (floor_log2 (log) + 1);
7884
7885 return log;
7886 }
7887
7888 /* Return the size of a DIE as it is represented in the
7889 .debug_info section. */
7890
7891 static unsigned long
7892 size_of_die (dw_die_ref die)
7893 {
7894 unsigned long size = 0;
7895 dw_attr_ref a;
7896 unsigned ix;
7897 enum dwarf_form form;
7898
7899 size += size_of_uleb128 (die->die_abbrev);
7900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7901 {
7902 switch (AT_class (a))
7903 {
7904 case dw_val_class_addr:
7905 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7906 {
7907 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7908 size += size_of_uleb128 (AT_index (a));
7909 }
7910 else
7911 size += DWARF2_ADDR_SIZE;
7912 break;
7913 case dw_val_class_offset:
7914 size += DWARF_OFFSET_SIZE;
7915 break;
7916 case dw_val_class_loc:
7917 {
7918 unsigned long lsize = size_of_locs (AT_loc (a));
7919
7920 /* Block length. */
7921 if (dwarf_version >= 4)
7922 size += size_of_uleb128 (lsize);
7923 else
7924 size += constant_size (lsize);
7925 size += lsize;
7926 }
7927 break;
7928 case dw_val_class_loc_list:
7929 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7930 {
7931 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7932 size += size_of_uleb128 (AT_index (a));
7933 }
7934 else
7935 size += DWARF_OFFSET_SIZE;
7936 break;
7937 case dw_val_class_range_list:
7938 size += DWARF_OFFSET_SIZE;
7939 break;
7940 case dw_val_class_const:
7941 size += size_of_sleb128 (AT_int (a));
7942 break;
7943 case dw_val_class_unsigned_const:
7944 {
7945 int csize = constant_size (AT_unsigned (a));
7946 if (dwarf_version == 3
7947 && a->dw_attr == DW_AT_data_member_location
7948 && csize >= 4)
7949 size += size_of_uleb128 (AT_unsigned (a));
7950 else
7951 size += csize;
7952 }
7953 break;
7954 case dw_val_class_const_double:
7955 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7956 if (HOST_BITS_PER_WIDE_INT >= 64)
7957 size++; /* block */
7958 break;
7959 case dw_val_class_wide_int:
7960 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7961 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7962 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7963 > 64)
7964 size++; /* block */
7965 break;
7966 case dw_val_class_vec:
7967 size += constant_size (a->dw_attr_val.v.val_vec.length
7968 * a->dw_attr_val.v.val_vec.elt_size)
7969 + a->dw_attr_val.v.val_vec.length
7970 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7971 break;
7972 case dw_val_class_flag:
7973 if (dwarf_version >= 4)
7974 /* Currently all add_AT_flag calls pass in 1 as last argument,
7975 so DW_FORM_flag_present can be used. If that ever changes,
7976 we'll need to use DW_FORM_flag and have some optimization
7977 in build_abbrev_table that will change those to
7978 DW_FORM_flag_present if it is set to 1 in all DIEs using
7979 the same abbrev entry. */
7980 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7981 else
7982 size += 1;
7983 break;
7984 case dw_val_class_die_ref:
7985 if (AT_ref_external (a))
7986 {
7987 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7988 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7989 is sized by target address length, whereas in DWARF3
7990 it's always sized as an offset. */
7991 if (use_debug_types)
7992 size += DWARF_TYPE_SIGNATURE_SIZE;
7993 else if (dwarf_version == 2)
7994 size += DWARF2_ADDR_SIZE;
7995 else
7996 size += DWARF_OFFSET_SIZE;
7997 }
7998 else
7999 size += DWARF_OFFSET_SIZE;
8000 break;
8001 case dw_val_class_fde_ref:
8002 size += DWARF_OFFSET_SIZE;
8003 break;
8004 case dw_val_class_lbl_id:
8005 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8006 {
8007 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8008 size += size_of_uleb128 (AT_index (a));
8009 }
8010 else
8011 size += DWARF2_ADDR_SIZE;
8012 break;
8013 case dw_val_class_lineptr:
8014 case dw_val_class_macptr:
8015 size += DWARF_OFFSET_SIZE;
8016 break;
8017 case dw_val_class_str:
8018 form = AT_string_form (a);
8019 if (form == DW_FORM_strp)
8020 size += DWARF_OFFSET_SIZE;
8021 else if (form == DW_FORM_GNU_str_index)
8022 size += size_of_uleb128 (AT_index (a));
8023 else
8024 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8025 break;
8026 case dw_val_class_file:
8027 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8028 break;
8029 case dw_val_class_data8:
8030 size += 8;
8031 break;
8032 case dw_val_class_vms_delta:
8033 size += DWARF_OFFSET_SIZE;
8034 break;
8035 case dw_val_class_high_pc:
8036 size += DWARF2_ADDR_SIZE;
8037 break;
8038 default:
8039 gcc_unreachable ();
8040 }
8041 }
8042
8043 return size;
8044 }
8045
8046 /* Size the debugging information associated with a given DIE. Visits the
8047 DIE's children recursively. Updates the global variable next_die_offset, on
8048 each time through. Uses the current value of next_die_offset to update the
8049 die_offset field in each DIE. */
8050
8051 static void
8052 calc_die_sizes (dw_die_ref die)
8053 {
8054 dw_die_ref c;
8055
8056 gcc_assert (die->die_offset == 0
8057 || (unsigned long int) die->die_offset == next_die_offset);
8058 die->die_offset = next_die_offset;
8059 next_die_offset += size_of_die (die);
8060
8061 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8062
8063 if (die->die_child != NULL)
8064 /* Count the null byte used to terminate sibling lists. */
8065 next_die_offset += 1;
8066 }
8067
8068 /* Size just the base type children at the start of the CU.
8069 This is needed because build_abbrev needs to size locs
8070 and sizing of type based stack ops needs to know die_offset
8071 values for the base types. */
8072
8073 static void
8074 calc_base_type_die_sizes (void)
8075 {
8076 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8077 unsigned int i;
8078 dw_die_ref base_type;
8079 #if ENABLE_ASSERT_CHECKING
8080 dw_die_ref prev = comp_unit_die ()->die_child;
8081 #endif
8082
8083 die_offset += size_of_die (comp_unit_die ());
8084 for (i = 0; base_types.iterate (i, &base_type); i++)
8085 {
8086 #if ENABLE_ASSERT_CHECKING
8087 gcc_assert (base_type->die_offset == 0
8088 && prev->die_sib == base_type
8089 && base_type->die_child == NULL
8090 && base_type->die_abbrev);
8091 prev = base_type;
8092 #endif
8093 base_type->die_offset = die_offset;
8094 die_offset += size_of_die (base_type);
8095 }
8096 }
8097
8098 /* Set the marks for a die and its children. We do this so
8099 that we know whether or not a reference needs to use FORM_ref_addr; only
8100 DIEs in the same CU will be marked. We used to clear out the offset
8101 and use that as the flag, but ran into ordering problems. */
8102
8103 static void
8104 mark_dies (dw_die_ref die)
8105 {
8106 dw_die_ref c;
8107
8108 gcc_assert (!die->die_mark);
8109
8110 die->die_mark = 1;
8111 FOR_EACH_CHILD (die, c, mark_dies (c));
8112 }
8113
8114 /* Clear the marks for a die and its children. */
8115
8116 static void
8117 unmark_dies (dw_die_ref die)
8118 {
8119 dw_die_ref c;
8120
8121 if (! use_debug_types)
8122 gcc_assert (die->die_mark);
8123
8124 die->die_mark = 0;
8125 FOR_EACH_CHILD (die, c, unmark_dies (c));
8126 }
8127
8128 /* Clear the marks for a die, its children and referred dies. */
8129
8130 static void
8131 unmark_all_dies (dw_die_ref die)
8132 {
8133 dw_die_ref c;
8134 dw_attr_ref a;
8135 unsigned ix;
8136
8137 if (!die->die_mark)
8138 return;
8139 die->die_mark = 0;
8140
8141 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8142
8143 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8144 if (AT_class (a) == dw_val_class_die_ref)
8145 unmark_all_dies (AT_ref (a));
8146 }
8147
8148 /* Calculate if the entry should appear in the final output file. It may be
8149 from a pruned a type. */
8150
8151 static bool
8152 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8153 {
8154 /* By limiting gnu pubnames to definitions only, gold can generate a
8155 gdb index without entries for declarations, which don't include
8156 enough information to be useful. */
8157 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8158 return false;
8159
8160 if (table == pubname_table)
8161 {
8162 /* Enumerator names are part of the pubname table, but the
8163 parent DW_TAG_enumeration_type die may have been pruned.
8164 Don't output them if that is the case. */
8165 if (p->die->die_tag == DW_TAG_enumerator &&
8166 (p->die->die_parent == NULL
8167 || !p->die->die_parent->die_perennial_p))
8168 return false;
8169
8170 /* Everything else in the pubname table is included. */
8171 return true;
8172 }
8173
8174 /* The pubtypes table shouldn't include types that have been
8175 pruned. */
8176 return (p->die->die_offset != 0
8177 || !flag_eliminate_unused_debug_types);
8178 }
8179
8180 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8181 generated for the compilation unit. */
8182
8183 static unsigned long
8184 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8185 {
8186 unsigned long size;
8187 unsigned i;
8188 pubname_ref p;
8189 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8190
8191 size = DWARF_PUBNAMES_HEADER_SIZE;
8192 FOR_EACH_VEC_ELT (*names, i, p)
8193 if (include_pubname_in_output (names, p))
8194 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8195
8196 size += DWARF_OFFSET_SIZE;
8197 return size;
8198 }
8199
8200 /* Return the size of the information in the .debug_aranges section. */
8201
8202 static unsigned long
8203 size_of_aranges (void)
8204 {
8205 unsigned long size;
8206
8207 size = DWARF_ARANGES_HEADER_SIZE;
8208
8209 /* Count the address/length pair for this compilation unit. */
8210 if (text_section_used)
8211 size += 2 * DWARF2_ADDR_SIZE;
8212 if (cold_text_section_used)
8213 size += 2 * DWARF2_ADDR_SIZE;
8214 if (have_multiple_function_sections)
8215 {
8216 unsigned fde_idx;
8217 dw_fde_ref fde;
8218
8219 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8220 {
8221 if (DECL_IGNORED_P (fde->decl))
8222 continue;
8223 if (!fde->in_std_section)
8224 size += 2 * DWARF2_ADDR_SIZE;
8225 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8226 size += 2 * DWARF2_ADDR_SIZE;
8227 }
8228 }
8229
8230 /* Count the two zero words used to terminated the address range table. */
8231 size += 2 * DWARF2_ADDR_SIZE;
8232 return size;
8233 }
8234 \f
8235 /* Select the encoding of an attribute value. */
8236
8237 static enum dwarf_form
8238 value_format (dw_attr_ref a)
8239 {
8240 switch (AT_class (a))
8241 {
8242 case dw_val_class_addr:
8243 /* Only very few attributes allow DW_FORM_addr. */
8244 switch (a->dw_attr)
8245 {
8246 case DW_AT_low_pc:
8247 case DW_AT_high_pc:
8248 case DW_AT_entry_pc:
8249 case DW_AT_trampoline:
8250 return (AT_index (a) == NOT_INDEXED
8251 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8252 default:
8253 break;
8254 }
8255 switch (DWARF2_ADDR_SIZE)
8256 {
8257 case 1:
8258 return DW_FORM_data1;
8259 case 2:
8260 return DW_FORM_data2;
8261 case 4:
8262 return DW_FORM_data4;
8263 case 8:
8264 return DW_FORM_data8;
8265 default:
8266 gcc_unreachable ();
8267 }
8268 case dw_val_class_range_list:
8269 case dw_val_class_loc_list:
8270 if (dwarf_version >= 4)
8271 return DW_FORM_sec_offset;
8272 /* FALLTHRU */
8273 case dw_val_class_vms_delta:
8274 case dw_val_class_offset:
8275 switch (DWARF_OFFSET_SIZE)
8276 {
8277 case 4:
8278 return DW_FORM_data4;
8279 case 8:
8280 return DW_FORM_data8;
8281 default:
8282 gcc_unreachable ();
8283 }
8284 case dw_val_class_loc:
8285 if (dwarf_version >= 4)
8286 return DW_FORM_exprloc;
8287 switch (constant_size (size_of_locs (AT_loc (a))))
8288 {
8289 case 1:
8290 return DW_FORM_block1;
8291 case 2:
8292 return DW_FORM_block2;
8293 case 4:
8294 return DW_FORM_block4;
8295 default:
8296 gcc_unreachable ();
8297 }
8298 case dw_val_class_const:
8299 return DW_FORM_sdata;
8300 case dw_val_class_unsigned_const:
8301 switch (constant_size (AT_unsigned (a)))
8302 {
8303 case 1:
8304 return DW_FORM_data1;
8305 case 2:
8306 return DW_FORM_data2;
8307 case 4:
8308 /* In DWARF3 DW_AT_data_member_location with
8309 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8310 constant, so we need to use DW_FORM_udata if we need
8311 a large constant. */
8312 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8313 return DW_FORM_udata;
8314 return DW_FORM_data4;
8315 case 8:
8316 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8317 return DW_FORM_udata;
8318 return DW_FORM_data8;
8319 default:
8320 gcc_unreachable ();
8321 }
8322 case dw_val_class_const_double:
8323 switch (HOST_BITS_PER_WIDE_INT)
8324 {
8325 case 8:
8326 return DW_FORM_data2;
8327 case 16:
8328 return DW_FORM_data4;
8329 case 32:
8330 return DW_FORM_data8;
8331 case 64:
8332 default:
8333 return DW_FORM_block1;
8334 }
8335 case dw_val_class_wide_int:
8336 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8337 {
8338 case 8:
8339 return DW_FORM_data1;
8340 case 16:
8341 return DW_FORM_data2;
8342 case 32:
8343 return DW_FORM_data4;
8344 case 64:
8345 return DW_FORM_data8;
8346 default:
8347 return DW_FORM_block1;
8348 }
8349 case dw_val_class_vec:
8350 switch (constant_size (a->dw_attr_val.v.val_vec.length
8351 * a->dw_attr_val.v.val_vec.elt_size))
8352 {
8353 case 1:
8354 return DW_FORM_block1;
8355 case 2:
8356 return DW_FORM_block2;
8357 case 4:
8358 return DW_FORM_block4;
8359 default:
8360 gcc_unreachable ();
8361 }
8362 case dw_val_class_flag:
8363 if (dwarf_version >= 4)
8364 {
8365 /* Currently all add_AT_flag calls pass in 1 as last argument,
8366 so DW_FORM_flag_present can be used. If that ever changes,
8367 we'll need to use DW_FORM_flag and have some optimization
8368 in build_abbrev_table that will change those to
8369 DW_FORM_flag_present if it is set to 1 in all DIEs using
8370 the same abbrev entry. */
8371 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8372 return DW_FORM_flag_present;
8373 }
8374 return DW_FORM_flag;
8375 case dw_val_class_die_ref:
8376 if (AT_ref_external (a))
8377 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8378 else
8379 return DW_FORM_ref;
8380 case dw_val_class_fde_ref:
8381 return DW_FORM_data;
8382 case dw_val_class_lbl_id:
8383 return (AT_index (a) == NOT_INDEXED
8384 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8385 case dw_val_class_lineptr:
8386 case dw_val_class_macptr:
8387 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8388 case dw_val_class_str:
8389 return AT_string_form (a);
8390 case dw_val_class_file:
8391 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8392 {
8393 case 1:
8394 return DW_FORM_data1;
8395 case 2:
8396 return DW_FORM_data2;
8397 case 4:
8398 return DW_FORM_data4;
8399 default:
8400 gcc_unreachable ();
8401 }
8402
8403 case dw_val_class_data8:
8404 return DW_FORM_data8;
8405
8406 case dw_val_class_high_pc:
8407 switch (DWARF2_ADDR_SIZE)
8408 {
8409 case 1:
8410 return DW_FORM_data1;
8411 case 2:
8412 return DW_FORM_data2;
8413 case 4:
8414 return DW_FORM_data4;
8415 case 8:
8416 return DW_FORM_data8;
8417 default:
8418 gcc_unreachable ();
8419 }
8420
8421 default:
8422 gcc_unreachable ();
8423 }
8424 }
8425
8426 /* Output the encoding of an attribute value. */
8427
8428 static void
8429 output_value_format (dw_attr_ref a)
8430 {
8431 enum dwarf_form form = value_format (a);
8432
8433 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8434 }
8435
8436 /* Given a die and id, produce the appropriate abbreviations. */
8437
8438 static void
8439 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8440 {
8441 unsigned ix;
8442 dw_attr_ref a_attr;
8443
8444 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8445 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8446 dwarf_tag_name (abbrev->die_tag));
8447
8448 if (abbrev->die_child != NULL)
8449 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8450 else
8451 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8452
8453 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8454 {
8455 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8456 dwarf_attr_name (a_attr->dw_attr));
8457 output_value_format (a_attr);
8458 }
8459
8460 dw2_asm_output_data (1, 0, NULL);
8461 dw2_asm_output_data (1, 0, NULL);
8462 }
8463
8464
8465 /* Output the .debug_abbrev section which defines the DIE abbreviation
8466 table. */
8467
8468 static void
8469 output_abbrev_section (void)
8470 {
8471 unsigned long abbrev_id;
8472
8473 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8474 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8475
8476 /* Terminate the table. */
8477 dw2_asm_output_data (1, 0, NULL);
8478 }
8479
8480 /* Output a symbol we can use to refer to this DIE from another CU. */
8481
8482 static inline void
8483 output_die_symbol (dw_die_ref die)
8484 {
8485 const char *sym = die->die_id.die_symbol;
8486
8487 gcc_assert (!die->comdat_type_p);
8488
8489 if (sym == 0)
8490 return;
8491
8492 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8493 /* We make these global, not weak; if the target doesn't support
8494 .linkonce, it doesn't support combining the sections, so debugging
8495 will break. */
8496 targetm.asm_out.globalize_label (asm_out_file, sym);
8497
8498 ASM_OUTPUT_LABEL (asm_out_file, sym);
8499 }
8500
8501 /* Return a new location list, given the begin and end range, and the
8502 expression. */
8503
8504 static inline dw_loc_list_ref
8505 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8506 const char *section)
8507 {
8508 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8509
8510 retlist->begin = begin;
8511 retlist->begin_entry = NULL;
8512 retlist->end = end;
8513 retlist->expr = expr;
8514 retlist->section = section;
8515
8516 return retlist;
8517 }
8518
8519 /* Generate a new internal symbol for this location list node, if it
8520 hasn't got one yet. */
8521
8522 static inline void
8523 gen_llsym (dw_loc_list_ref list)
8524 {
8525 gcc_assert (!list->ll_symbol);
8526 list->ll_symbol = gen_internal_sym ("LLST");
8527 }
8528
8529 /* Output the location list given to us. */
8530
8531 static void
8532 output_loc_list (dw_loc_list_ref list_head)
8533 {
8534 dw_loc_list_ref curr = list_head;
8535
8536 if (list_head->emitted)
8537 return;
8538 list_head->emitted = true;
8539
8540 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8541
8542 /* Walk the location list, and output each range + expression. */
8543 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8544 {
8545 unsigned long size;
8546 /* Don't output an entry that starts and ends at the same address. */
8547 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8548 continue;
8549 size = size_of_locs (curr->expr);
8550 /* If the expression is too large, drop it on the floor. We could
8551 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8552 in the expression, but >= 64KB expressions for a single value
8553 in a single range are unlikely very useful. */
8554 if (size > 0xffff)
8555 continue;
8556 if (dwarf_split_debug_info)
8557 {
8558 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8559 "Location list start/length entry (%s)",
8560 list_head->ll_symbol);
8561 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8562 "Location list range start index (%s)",
8563 curr->begin);
8564 /* The length field is 4 bytes. If we ever need to support
8565 an 8-byte length, we can add a new DW_LLE code or fall back
8566 to DW_LLE_GNU_start_end_entry. */
8567 dw2_asm_output_delta (4, curr->end, curr->begin,
8568 "Location list range length (%s)",
8569 list_head->ll_symbol);
8570 }
8571 else if (!have_multiple_function_sections)
8572 {
8573 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8574 "Location list begin address (%s)",
8575 list_head->ll_symbol);
8576 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8577 "Location list end address (%s)",
8578 list_head->ll_symbol);
8579 }
8580 else
8581 {
8582 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8583 "Location list begin address (%s)",
8584 list_head->ll_symbol);
8585 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8586 "Location list end address (%s)",
8587 list_head->ll_symbol);
8588 }
8589
8590 /* Output the block length for this list of location operations. */
8591 gcc_assert (size <= 0xffff);
8592 dw2_asm_output_data (2, size, "%s", "Location expression size");
8593
8594 output_loc_sequence (curr->expr, -1);
8595 }
8596
8597 if (dwarf_split_debug_info)
8598 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8599 "Location list terminator (%s)",
8600 list_head->ll_symbol);
8601 else
8602 {
8603 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8604 "Location list terminator begin (%s)",
8605 list_head->ll_symbol);
8606 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8607 "Location list terminator end (%s)",
8608 list_head->ll_symbol);
8609 }
8610 }
8611
8612 /* Output a range_list offset into the debug_range section. Emit a
8613 relocated reference if val_entry is NULL, otherwise, emit an
8614 indirect reference. */
8615
8616 static void
8617 output_range_list_offset (dw_attr_ref a)
8618 {
8619 const char *name = dwarf_attr_name (a->dw_attr);
8620
8621 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8622 {
8623 char *p = strchr (ranges_section_label, '\0');
8624 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8625 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8626 debug_ranges_section, "%s", name);
8627 *p = '\0';
8628 }
8629 else
8630 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8631 "%s (offset from %s)", name, ranges_section_label);
8632 }
8633
8634 /* Output the offset into the debug_loc section. */
8635
8636 static void
8637 output_loc_list_offset (dw_attr_ref a)
8638 {
8639 char *sym = AT_loc_list (a)->ll_symbol;
8640
8641 gcc_assert (sym);
8642 if (dwarf_split_debug_info)
8643 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8644 "%s", dwarf_attr_name (a->dw_attr));
8645 else
8646 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8647 "%s", dwarf_attr_name (a->dw_attr));
8648 }
8649
8650 /* Output an attribute's index or value appropriately. */
8651
8652 static void
8653 output_attr_index_or_value (dw_attr_ref a)
8654 {
8655 const char *name = dwarf_attr_name (a->dw_attr);
8656
8657 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8658 {
8659 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8660 return;
8661 }
8662 switch (AT_class (a))
8663 {
8664 case dw_val_class_addr:
8665 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8666 break;
8667 case dw_val_class_high_pc:
8668 case dw_val_class_lbl_id:
8669 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8670 break;
8671 case dw_val_class_loc_list:
8672 output_loc_list_offset (a);
8673 break;
8674 default:
8675 gcc_unreachable ();
8676 }
8677 }
8678
8679 /* Output a type signature. */
8680
8681 static inline void
8682 output_signature (const char *sig, const char *name)
8683 {
8684 int i;
8685
8686 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8687 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8688 }
8689
8690 /* Output the DIE and its attributes. Called recursively to generate
8691 the definitions of each child DIE. */
8692
8693 static void
8694 output_die (dw_die_ref die)
8695 {
8696 dw_attr_ref a;
8697 dw_die_ref c;
8698 unsigned long size;
8699 unsigned ix;
8700
8701 /* If someone in another CU might refer to us, set up a symbol for
8702 them to point to. */
8703 if (! die->comdat_type_p && die->die_id.die_symbol)
8704 output_die_symbol (die);
8705
8706 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8707 (unsigned long)die->die_offset,
8708 dwarf_tag_name (die->die_tag));
8709
8710 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8711 {
8712 const char *name = dwarf_attr_name (a->dw_attr);
8713
8714 switch (AT_class (a))
8715 {
8716 case dw_val_class_addr:
8717 output_attr_index_or_value (a);
8718 break;
8719
8720 case dw_val_class_offset:
8721 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8722 "%s", name);
8723 break;
8724
8725 case dw_val_class_range_list:
8726 output_range_list_offset (a);
8727 break;
8728
8729 case dw_val_class_loc:
8730 size = size_of_locs (AT_loc (a));
8731
8732 /* Output the block length for this list of location operations. */
8733 if (dwarf_version >= 4)
8734 dw2_asm_output_data_uleb128 (size, "%s", name);
8735 else
8736 dw2_asm_output_data (constant_size (size), size, "%s", name);
8737
8738 output_loc_sequence (AT_loc (a), -1);
8739 break;
8740
8741 case dw_val_class_const:
8742 /* ??? It would be slightly more efficient to use a scheme like is
8743 used for unsigned constants below, but gdb 4.x does not sign
8744 extend. Gdb 5.x does sign extend. */
8745 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8746 break;
8747
8748 case dw_val_class_unsigned_const:
8749 {
8750 int csize = constant_size (AT_unsigned (a));
8751 if (dwarf_version == 3
8752 && a->dw_attr == DW_AT_data_member_location
8753 && csize >= 4)
8754 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8755 else
8756 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8757 }
8758 break;
8759
8760 case dw_val_class_const_double:
8761 {
8762 unsigned HOST_WIDE_INT first, second;
8763
8764 if (HOST_BITS_PER_WIDE_INT >= 64)
8765 dw2_asm_output_data (1,
8766 HOST_BITS_PER_DOUBLE_INT
8767 / HOST_BITS_PER_CHAR,
8768 NULL);
8769
8770 if (WORDS_BIG_ENDIAN)
8771 {
8772 first = a->dw_attr_val.v.val_double.high;
8773 second = a->dw_attr_val.v.val_double.low;
8774 }
8775 else
8776 {
8777 first = a->dw_attr_val.v.val_double.low;
8778 second = a->dw_attr_val.v.val_double.high;
8779 }
8780
8781 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8782 first, "%s", name);
8783 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8784 second, NULL);
8785 }
8786 break;
8787
8788 case dw_val_class_wide_int:
8789 {
8790 int i;
8791 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8792 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8793 if (len * HOST_BITS_PER_WIDE_INT > 64)
8794 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8795 NULL);
8796
8797 if (WORDS_BIG_ENDIAN)
8798 for (i = len - 1; i >= 0; --i)
8799 {
8800 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8801 name);
8802 name = NULL;
8803 }
8804 else
8805 for (i = 0; i < len; ++i)
8806 {
8807 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8808 name);
8809 name = NULL;
8810 }
8811 }
8812 break;
8813
8814 case dw_val_class_vec:
8815 {
8816 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8817 unsigned int len = a->dw_attr_val.v.val_vec.length;
8818 unsigned int i;
8819 unsigned char *p;
8820
8821 dw2_asm_output_data (constant_size (len * elt_size),
8822 len * elt_size, "%s", name);
8823 if (elt_size > sizeof (HOST_WIDE_INT))
8824 {
8825 elt_size /= 2;
8826 len *= 2;
8827 }
8828 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8829 i < len;
8830 i++, p += elt_size)
8831 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8832 "fp or vector constant word %u", i);
8833 break;
8834 }
8835
8836 case dw_val_class_flag:
8837 if (dwarf_version >= 4)
8838 {
8839 /* Currently all add_AT_flag calls pass in 1 as last argument,
8840 so DW_FORM_flag_present can be used. If that ever changes,
8841 we'll need to use DW_FORM_flag and have some optimization
8842 in build_abbrev_table that will change those to
8843 DW_FORM_flag_present if it is set to 1 in all DIEs using
8844 the same abbrev entry. */
8845 gcc_assert (AT_flag (a) == 1);
8846 if (flag_debug_asm)
8847 fprintf (asm_out_file, "\t\t\t%s %s\n",
8848 ASM_COMMENT_START, name);
8849 break;
8850 }
8851 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8852 break;
8853
8854 case dw_val_class_loc_list:
8855 output_attr_index_or_value (a);
8856 break;
8857
8858 case dw_val_class_die_ref:
8859 if (AT_ref_external (a))
8860 {
8861 if (AT_ref (a)->comdat_type_p)
8862 {
8863 comdat_type_node_ref type_node =
8864 AT_ref (a)->die_id.die_type_node;
8865
8866 gcc_assert (type_node);
8867 output_signature (type_node->signature, name);
8868 }
8869 else
8870 {
8871 const char *sym = AT_ref (a)->die_id.die_symbol;
8872 int size;
8873
8874 gcc_assert (sym);
8875 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8876 length, whereas in DWARF3 it's always sized as an
8877 offset. */
8878 if (dwarf_version == 2)
8879 size = DWARF2_ADDR_SIZE;
8880 else
8881 size = DWARF_OFFSET_SIZE;
8882 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8883 name);
8884 }
8885 }
8886 else
8887 {
8888 gcc_assert (AT_ref (a)->die_offset);
8889 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8890 "%s", name);
8891 }
8892 break;
8893
8894 case dw_val_class_fde_ref:
8895 {
8896 char l1[20];
8897
8898 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8899 a->dw_attr_val.v.val_fde_index * 2);
8900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8901 "%s", name);
8902 }
8903 break;
8904
8905 case dw_val_class_vms_delta:
8906 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8907 AT_vms_delta2 (a), AT_vms_delta1 (a),
8908 "%s", name);
8909 break;
8910
8911 case dw_val_class_lbl_id:
8912 output_attr_index_or_value (a);
8913 break;
8914
8915 case dw_val_class_lineptr:
8916 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8917 debug_line_section, "%s", name);
8918 break;
8919
8920 case dw_val_class_macptr:
8921 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8922 debug_macinfo_section, "%s", name);
8923 break;
8924
8925 case dw_val_class_str:
8926 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8927 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8928 a->dw_attr_val.v.val_str->label,
8929 debug_str_section,
8930 "%s: \"%s\"", name, AT_string (a));
8931 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8932 dw2_asm_output_data_uleb128 (AT_index (a),
8933 "%s: \"%s\"", name, AT_string (a));
8934 else
8935 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8936 break;
8937
8938 case dw_val_class_file:
8939 {
8940 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8941
8942 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8943 a->dw_attr_val.v.val_file->filename);
8944 break;
8945 }
8946
8947 case dw_val_class_data8:
8948 {
8949 int i;
8950
8951 for (i = 0; i < 8; i++)
8952 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8953 i == 0 ? "%s" : NULL, name);
8954 break;
8955 }
8956
8957 case dw_val_class_high_pc:
8958 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8959 get_AT_low_pc (die), "DW_AT_high_pc");
8960 break;
8961
8962 default:
8963 gcc_unreachable ();
8964 }
8965 }
8966
8967 FOR_EACH_CHILD (die, c, output_die (c));
8968
8969 /* Add null byte to terminate sibling list. */
8970 if (die->die_child != NULL)
8971 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8972 (unsigned long) die->die_offset);
8973 }
8974
8975 /* Output the compilation unit that appears at the beginning of the
8976 .debug_info section, and precedes the DIE descriptions. */
8977
8978 static void
8979 output_compilation_unit_header (void)
8980 {
8981 int ver = dwarf_version;
8982
8983 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8984 dw2_asm_output_data (4, 0xffffffff,
8985 "Initial length escape value indicating 64-bit DWARF extension");
8986 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8987 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8988 "Length of Compilation Unit Info");
8989 dw2_asm_output_data (2, ver, "DWARF version number");
8990 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8991 debug_abbrev_section,
8992 "Offset Into Abbrev. Section");
8993 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8994 }
8995
8996 /* Output the compilation unit DIE and its children. */
8997
8998 static void
8999 output_comp_unit (dw_die_ref die, int output_if_empty)
9000 {
9001 const char *secname, *oldsym;
9002 char *tmp;
9003
9004 /* Unless we are outputting main CU, we may throw away empty ones. */
9005 if (!output_if_empty && die->die_child == NULL)
9006 return;
9007
9008 /* Even if there are no children of this DIE, we must output the information
9009 about the compilation unit. Otherwise, on an empty translation unit, we
9010 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9011 will then complain when examining the file. First mark all the DIEs in
9012 this CU so we know which get local refs. */
9013 mark_dies (die);
9014
9015 external_ref_hash_type *extern_map = optimize_external_refs (die);
9016
9017 build_abbrev_table (die, extern_map);
9018
9019 delete extern_map;
9020
9021 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9022 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9023 calc_die_sizes (die);
9024
9025 oldsym = die->die_id.die_symbol;
9026 if (oldsym)
9027 {
9028 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9029
9030 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9031 secname = tmp;
9032 die->die_id.die_symbol = NULL;
9033 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9034 }
9035 else
9036 {
9037 switch_to_section (debug_info_section);
9038 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9039 info_section_emitted = true;
9040 }
9041
9042 /* Output debugging information. */
9043 output_compilation_unit_header ();
9044 output_die (die);
9045
9046 /* Leave the marks on the main CU, so we can check them in
9047 output_pubnames. */
9048 if (oldsym)
9049 {
9050 unmark_dies (die);
9051 die->die_id.die_symbol = oldsym;
9052 }
9053 }
9054
9055 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9056 and .debug_pubtypes. This is configured per-target, but can be
9057 overridden by the -gpubnames or -gno-pubnames options. */
9058
9059 static inline bool
9060 want_pubnames (void)
9061 {
9062 if (debug_info_level <= DINFO_LEVEL_TERSE)
9063 return false;
9064 if (debug_generate_pub_sections != -1)
9065 return debug_generate_pub_sections;
9066 return targetm.want_debug_pub_sections;
9067 }
9068
9069 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9070
9071 static void
9072 add_AT_pubnames (dw_die_ref die)
9073 {
9074 if (want_pubnames ())
9075 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9076 }
9077
9078 /* Add a string attribute value to a skeleton DIE. */
9079
9080 static inline void
9081 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9082 const char *str)
9083 {
9084 dw_attr_node attr;
9085 struct indirect_string_node *node;
9086
9087 if (! skeleton_debug_str_hash)
9088 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9089 debug_str_eq, NULL);
9090
9091 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9092 find_string_form (node);
9093 if (node->form == DW_FORM_GNU_str_index)
9094 node->form = DW_FORM_strp;
9095
9096 attr.dw_attr = attr_kind;
9097 attr.dw_attr_val.val_class = dw_val_class_str;
9098 attr.dw_attr_val.val_entry = NULL;
9099 attr.dw_attr_val.v.val_str = node;
9100 add_dwarf_attr (die, &attr);
9101 }
9102
9103 /* Helper function to generate top-level dies for skeleton debug_info and
9104 debug_types. */
9105
9106 static void
9107 add_top_level_skeleton_die_attrs (dw_die_ref die)
9108 {
9109 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9110 const char *comp_dir = comp_dir_string ();
9111
9112 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9113 if (comp_dir != NULL)
9114 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9115 add_AT_pubnames (die);
9116 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9117 }
9118
9119 /* Output skeleton debug sections that point to the dwo file. */
9120
9121 static void
9122 output_skeleton_debug_sections (dw_die_ref comp_unit)
9123 {
9124 /* These attributes will be found in the full debug_info section. */
9125 remove_AT (comp_unit, DW_AT_producer);
9126 remove_AT (comp_unit, DW_AT_language);
9127
9128 switch_to_section (debug_skeleton_info_section);
9129 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9130
9131 /* Produce the skeleton compilation-unit header. This one differs enough from
9132 a normal CU header that it's better not to call output_compilation_unit
9133 header. */
9134 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9135 dw2_asm_output_data (4, 0xffffffff,
9136 "Initial length escape value indicating 64-bit DWARF extension");
9137
9138 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9139 DWARF_COMPILE_UNIT_HEADER_SIZE
9140 - DWARF_INITIAL_LENGTH_SIZE
9141 + size_of_die (comp_unit),
9142 "Length of Compilation Unit Info");
9143 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9145 debug_abbrev_section,
9146 "Offset Into Abbrev. Section");
9147 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9148
9149 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9150 output_die (comp_unit);
9151
9152 /* Build the skeleton debug_abbrev section. */
9153 switch_to_section (debug_skeleton_abbrev_section);
9154 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9155
9156 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9157
9158 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9159 }
9160
9161 /* Output a comdat type unit DIE and its children. */
9162
9163 static void
9164 output_comdat_type_unit (comdat_type_node *node)
9165 {
9166 const char *secname;
9167 char *tmp;
9168 int i;
9169 #if defined (OBJECT_FORMAT_ELF)
9170 tree comdat_key;
9171 #endif
9172
9173 /* First mark all the DIEs in this CU so we know which get local refs. */
9174 mark_dies (node->root_die);
9175
9176 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9177
9178 build_abbrev_table (node->root_die, extern_map);
9179
9180 delete extern_map;
9181 extern_map = NULL;
9182
9183 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9184 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9185 calc_die_sizes (node->root_die);
9186
9187 #if defined (OBJECT_FORMAT_ELF)
9188 if (!dwarf_split_debug_info)
9189 secname = ".debug_types";
9190 else
9191 secname = ".debug_types.dwo";
9192
9193 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9194 sprintf (tmp, "wt.");
9195 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9196 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9197 comdat_key = get_identifier (tmp);
9198 targetm.asm_out.named_section (secname,
9199 SECTION_DEBUG | SECTION_LINKONCE,
9200 comdat_key);
9201 #else
9202 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9203 sprintf (tmp, ".gnu.linkonce.wt.");
9204 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9205 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9206 secname = tmp;
9207 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9208 #endif
9209
9210 /* Output debugging information. */
9211 output_compilation_unit_header ();
9212 output_signature (node->signature, "Type Signature");
9213 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9214 "Offset to Type DIE");
9215 output_die (node->root_die);
9216
9217 unmark_dies (node->root_die);
9218 }
9219
9220 /* Return the DWARF2/3 pubname associated with a decl. */
9221
9222 static const char *
9223 dwarf2_name (tree decl, int scope)
9224 {
9225 if (DECL_NAMELESS (decl))
9226 return NULL;
9227 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9228 }
9229
9230 /* Add a new entry to .debug_pubnames if appropriate. */
9231
9232 static void
9233 add_pubname_string (const char *str, dw_die_ref die)
9234 {
9235 pubname_entry e;
9236
9237 e.die = die;
9238 e.name = xstrdup (str);
9239 vec_safe_push (pubname_table, e);
9240 }
9241
9242 static void
9243 add_pubname (tree decl, dw_die_ref die)
9244 {
9245 if (!want_pubnames ())
9246 return;
9247
9248 /* Don't add items to the table when we expect that the consumer will have
9249 just read the enclosing die. For example, if the consumer is looking at a
9250 class_member, it will either be inside the class already, or will have just
9251 looked up the class to find the member. Either way, searching the class is
9252 faster than searching the index. */
9253 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9254 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9255 {
9256 const char *name = dwarf2_name (decl, 1);
9257
9258 if (name)
9259 add_pubname_string (name, die);
9260 }
9261 }
9262
9263 /* Add an enumerator to the pubnames section. */
9264
9265 static void
9266 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9267 {
9268 pubname_entry e;
9269
9270 gcc_assert (scope_name);
9271 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9272 e.die = die;
9273 vec_safe_push (pubname_table, e);
9274 }
9275
9276 /* Add a new entry to .debug_pubtypes if appropriate. */
9277
9278 static void
9279 add_pubtype (tree decl, dw_die_ref die)
9280 {
9281 pubname_entry e;
9282
9283 if (!want_pubnames ())
9284 return;
9285
9286 if ((TREE_PUBLIC (decl)
9287 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9288 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9289 {
9290 tree scope = NULL;
9291 const char *scope_name = "";
9292 const char *sep = is_cxx () ? "::" : ".";
9293 const char *name;
9294
9295 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9296 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9297 {
9298 scope_name = lang_hooks.dwarf_name (scope, 1);
9299 if (scope_name != NULL && scope_name[0] != '\0')
9300 scope_name = concat (scope_name, sep, NULL);
9301 else
9302 scope_name = "";
9303 }
9304
9305 if (TYPE_P (decl))
9306 name = type_tag (decl);
9307 else
9308 name = lang_hooks.dwarf_name (decl, 1);
9309
9310 /* If we don't have a name for the type, there's no point in adding
9311 it to the table. */
9312 if (name != NULL && name[0] != '\0')
9313 {
9314 e.die = die;
9315 e.name = concat (scope_name, name, NULL);
9316 vec_safe_push (pubtype_table, e);
9317 }
9318
9319 /* Although it might be more consistent to add the pubinfo for the
9320 enumerators as their dies are created, they should only be added if the
9321 enum type meets the criteria above. So rather than re-check the parent
9322 enum type whenever an enumerator die is created, just output them all
9323 here. This isn't protected by the name conditional because anonymous
9324 enums don't have names. */
9325 if (die->die_tag == DW_TAG_enumeration_type)
9326 {
9327 dw_die_ref c;
9328
9329 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9330 }
9331 }
9332 }
9333
9334 /* Output a single entry in the pubnames table. */
9335
9336 static void
9337 output_pubname (dw_offset die_offset, pubname_entry *entry)
9338 {
9339 dw_die_ref die = entry->die;
9340 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9341
9342 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9343
9344 if (debug_generate_pub_sections == 2)
9345 {
9346 /* This logic follows gdb's method for determining the value of the flag
9347 byte. */
9348 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9349 switch (die->die_tag)
9350 {
9351 case DW_TAG_typedef:
9352 case DW_TAG_base_type:
9353 case DW_TAG_subrange_type:
9354 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9355 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9356 break;
9357 case DW_TAG_enumerator:
9358 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9359 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9360 if (!is_cxx () && !is_java ())
9361 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9362 break;
9363 case DW_TAG_subprogram:
9364 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9365 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9366 if (!is_ada ())
9367 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9368 break;
9369 case DW_TAG_constant:
9370 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9371 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9372 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9373 break;
9374 case DW_TAG_variable:
9375 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9376 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9377 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9378 break;
9379 case DW_TAG_namespace:
9380 case DW_TAG_imported_declaration:
9381 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9382 break;
9383 case DW_TAG_class_type:
9384 case DW_TAG_interface_type:
9385 case DW_TAG_structure_type:
9386 case DW_TAG_union_type:
9387 case DW_TAG_enumeration_type:
9388 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9389 if (!is_cxx () && !is_java ())
9390 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9391 break;
9392 default:
9393 /* An unusual tag. Leave the flag-byte empty. */
9394 break;
9395 }
9396 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9397 "GDB-index flags");
9398 }
9399
9400 dw2_asm_output_nstring (entry->name, -1, "external name");
9401 }
9402
9403
9404 /* Output the public names table used to speed up access to externally
9405 visible names; or the public types table used to find type definitions. */
9406
9407 static void
9408 output_pubnames (vec<pubname_entry, va_gc> *names)
9409 {
9410 unsigned i;
9411 unsigned long pubnames_length = size_of_pubnames (names);
9412 pubname_ref pub;
9413
9414 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9415 dw2_asm_output_data (4, 0xffffffff,
9416 "Initial length escape value indicating 64-bit DWARF extension");
9417 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9418
9419 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9420 dw2_asm_output_data (2, 2, "DWARF Version");
9421
9422 if (dwarf_split_debug_info)
9423 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9424 debug_skeleton_info_section,
9425 "Offset of Compilation Unit Info");
9426 else
9427 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9428 debug_info_section,
9429 "Offset of Compilation Unit Info");
9430 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9431 "Compilation Unit Length");
9432
9433 FOR_EACH_VEC_ELT (*names, i, pub)
9434 {
9435 if (include_pubname_in_output (names, pub))
9436 {
9437 dw_offset die_offset = pub->die->die_offset;
9438
9439 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9440 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9441 gcc_assert (pub->die->die_mark);
9442
9443 /* If we're putting types in their own .debug_types sections,
9444 the .debug_pubtypes table will still point to the compile
9445 unit (not the type unit), so we want to use the offset of
9446 the skeleton DIE (if there is one). */
9447 if (pub->die->comdat_type_p && names == pubtype_table)
9448 {
9449 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9450
9451 if (type_node != NULL)
9452 die_offset = (type_node->skeleton_die != NULL
9453 ? type_node->skeleton_die->die_offset
9454 : comp_unit_die ()->die_offset);
9455 }
9456
9457 output_pubname (die_offset, pub);
9458 }
9459 }
9460
9461 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9462 }
9463
9464 /* Output public names and types tables if necessary. */
9465
9466 static void
9467 output_pubtables (void)
9468 {
9469 if (!want_pubnames () || !info_section_emitted)
9470 return;
9471
9472 switch_to_section (debug_pubnames_section);
9473 output_pubnames (pubname_table);
9474 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9475 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9476 simply won't look for the section. */
9477 switch_to_section (debug_pubtypes_section);
9478 output_pubnames (pubtype_table);
9479 }
9480
9481
9482 /* Output the information that goes into the .debug_aranges table.
9483 Namely, define the beginning and ending address range of the
9484 text section generated for this compilation unit. */
9485
9486 static void
9487 output_aranges (unsigned long aranges_length)
9488 {
9489 unsigned i;
9490
9491 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9492 dw2_asm_output_data (4, 0xffffffff,
9493 "Initial length escape value indicating 64-bit DWARF extension");
9494 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9495 "Length of Address Ranges Info");
9496 /* Version number for aranges is still 2, even in DWARF3. */
9497 dw2_asm_output_data (2, 2, "DWARF Version");
9498 if (dwarf_split_debug_info)
9499 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9500 debug_skeleton_info_section,
9501 "Offset of Compilation Unit Info");
9502 else
9503 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9504 debug_info_section,
9505 "Offset of Compilation Unit Info");
9506 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9507 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9508
9509 /* We need to align to twice the pointer size here. */
9510 if (DWARF_ARANGES_PAD_SIZE)
9511 {
9512 /* Pad using a 2 byte words so that padding is correct for any
9513 pointer size. */
9514 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9515 2 * DWARF2_ADDR_SIZE);
9516 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9517 dw2_asm_output_data (2, 0, NULL);
9518 }
9519
9520 /* It is necessary not to output these entries if the sections were
9521 not used; if the sections were not used, the length will be 0 and
9522 the address may end up as 0 if the section is discarded by ld
9523 --gc-sections, leaving an invalid (0, 0) entry that can be
9524 confused with the terminator. */
9525 if (text_section_used)
9526 {
9527 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9529 text_section_label, "Length");
9530 }
9531 if (cold_text_section_used)
9532 {
9533 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9534 "Address");
9535 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9536 cold_text_section_label, "Length");
9537 }
9538
9539 if (have_multiple_function_sections)
9540 {
9541 unsigned fde_idx;
9542 dw_fde_ref fde;
9543
9544 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9545 {
9546 if (DECL_IGNORED_P (fde->decl))
9547 continue;
9548 if (!fde->in_std_section)
9549 {
9550 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9551 "Address");
9552 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9553 fde->dw_fde_begin, "Length");
9554 }
9555 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9556 {
9557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9558 "Address");
9559 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9560 fde->dw_fde_second_begin, "Length");
9561 }
9562 }
9563 }
9564
9565 /* Output the terminator words. */
9566 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9567 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9568 }
9569
9570 /* Add a new entry to .debug_ranges. Return the offset at which it
9571 was placed. */
9572
9573 static unsigned int
9574 add_ranges_num (int num)
9575 {
9576 unsigned int in_use = ranges_table_in_use;
9577
9578 if (in_use == ranges_table_allocated)
9579 {
9580 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9581 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9582 ranges_table_allocated);
9583 memset (ranges_table + ranges_table_in_use, 0,
9584 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9585 }
9586
9587 ranges_table[in_use].num = num;
9588 ranges_table_in_use = in_use + 1;
9589
9590 return in_use * 2 * DWARF2_ADDR_SIZE;
9591 }
9592
9593 /* Add a new entry to .debug_ranges corresponding to a block, or a
9594 range terminator if BLOCK is NULL. */
9595
9596 static unsigned int
9597 add_ranges (const_tree block)
9598 {
9599 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9600 }
9601
9602 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9603 When using dwarf_split_debug_info, address attributes in dies destined
9604 for the final executable should be direct references--setting the
9605 parameter force_direct ensures this behavior. */
9606
9607 static void
9608 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9609 bool *added, bool force_direct)
9610 {
9611 unsigned int in_use = ranges_by_label_in_use;
9612 unsigned int offset;
9613
9614 if (in_use == ranges_by_label_allocated)
9615 {
9616 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9617 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9618 ranges_by_label,
9619 ranges_by_label_allocated);
9620 memset (ranges_by_label + ranges_by_label_in_use, 0,
9621 RANGES_TABLE_INCREMENT
9622 * sizeof (struct dw_ranges_by_label_struct));
9623 }
9624
9625 ranges_by_label[in_use].begin = begin;
9626 ranges_by_label[in_use].end = end;
9627 ranges_by_label_in_use = in_use + 1;
9628
9629 offset = add_ranges_num (-(int)in_use - 1);
9630 if (!*added)
9631 {
9632 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9633 *added = true;
9634 }
9635 }
9636
9637 static void
9638 output_ranges (void)
9639 {
9640 unsigned i;
9641 static const char *const start_fmt = "Offset %#x";
9642 const char *fmt = start_fmt;
9643
9644 for (i = 0; i < ranges_table_in_use; i++)
9645 {
9646 int block_num = ranges_table[i].num;
9647
9648 if (block_num > 0)
9649 {
9650 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9651 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9652
9653 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9654 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9655
9656 /* If all code is in the text section, then the compilation
9657 unit base address defaults to DW_AT_low_pc, which is the
9658 base of the text section. */
9659 if (!have_multiple_function_sections)
9660 {
9661 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9662 text_section_label,
9663 fmt, i * 2 * DWARF2_ADDR_SIZE);
9664 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9665 text_section_label, NULL);
9666 }
9667
9668 /* Otherwise, the compilation unit base address is zero,
9669 which allows us to use absolute addresses, and not worry
9670 about whether the target supports cross-section
9671 arithmetic. */
9672 else
9673 {
9674 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9675 fmt, i * 2 * DWARF2_ADDR_SIZE);
9676 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9677 }
9678
9679 fmt = NULL;
9680 }
9681
9682 /* Negative block_num stands for an index into ranges_by_label. */
9683 else if (block_num < 0)
9684 {
9685 int lab_idx = - block_num - 1;
9686
9687 if (!have_multiple_function_sections)
9688 {
9689 gcc_unreachable ();
9690 #if 0
9691 /* If we ever use add_ranges_by_labels () for a single
9692 function section, all we have to do is to take out
9693 the #if 0 above. */
9694 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9695 ranges_by_label[lab_idx].begin,
9696 text_section_label,
9697 fmt, i * 2 * DWARF2_ADDR_SIZE);
9698 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9699 ranges_by_label[lab_idx].end,
9700 text_section_label, NULL);
9701 #endif
9702 }
9703 else
9704 {
9705 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9706 ranges_by_label[lab_idx].begin,
9707 fmt, i * 2 * DWARF2_ADDR_SIZE);
9708 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9709 ranges_by_label[lab_idx].end,
9710 NULL);
9711 }
9712 }
9713 else
9714 {
9715 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9716 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9717 fmt = start_fmt;
9718 }
9719 }
9720 }
9721
9722 /* Data structure containing information about input files. */
9723 struct file_info
9724 {
9725 const char *path; /* Complete file name. */
9726 const char *fname; /* File name part. */
9727 int length; /* Length of entire string. */
9728 struct dwarf_file_data * file_idx; /* Index in input file table. */
9729 int dir_idx; /* Index in directory table. */
9730 };
9731
9732 /* Data structure containing information about directories with source
9733 files. */
9734 struct dir_info
9735 {
9736 const char *path; /* Path including directory name. */
9737 int length; /* Path length. */
9738 int prefix; /* Index of directory entry which is a prefix. */
9739 int count; /* Number of files in this directory. */
9740 int dir_idx; /* Index of directory used as base. */
9741 };
9742
9743 /* Callback function for file_info comparison. We sort by looking at
9744 the directories in the path. */
9745
9746 static int
9747 file_info_cmp (const void *p1, const void *p2)
9748 {
9749 const struct file_info *const s1 = (const struct file_info *) p1;
9750 const struct file_info *const s2 = (const struct file_info *) p2;
9751 const unsigned char *cp1;
9752 const unsigned char *cp2;
9753
9754 /* Take care of file names without directories. We need to make sure that
9755 we return consistent values to qsort since some will get confused if
9756 we return the same value when identical operands are passed in opposite
9757 orders. So if neither has a directory, return 0 and otherwise return
9758 1 or -1 depending on which one has the directory. */
9759 if ((s1->path == s1->fname || s2->path == s2->fname))
9760 return (s2->path == s2->fname) - (s1->path == s1->fname);
9761
9762 cp1 = (const unsigned char *) s1->path;
9763 cp2 = (const unsigned char *) s2->path;
9764
9765 while (1)
9766 {
9767 ++cp1;
9768 ++cp2;
9769 /* Reached the end of the first path? If so, handle like above. */
9770 if ((cp1 == (const unsigned char *) s1->fname)
9771 || (cp2 == (const unsigned char *) s2->fname))
9772 return ((cp2 == (const unsigned char *) s2->fname)
9773 - (cp1 == (const unsigned char *) s1->fname));
9774
9775 /* Character of current path component the same? */
9776 else if (*cp1 != *cp2)
9777 return *cp1 - *cp2;
9778 }
9779 }
9780
9781 struct file_name_acquire_data
9782 {
9783 struct file_info *files;
9784 int used_files;
9785 int max_files;
9786 };
9787
9788 /* Traversal function for the hash table. */
9789
9790 static int
9791 file_name_acquire (void ** slot, void *data)
9792 {
9793 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9794 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9795 struct file_info *fi;
9796 const char *f;
9797
9798 gcc_assert (fnad->max_files >= d->emitted_number);
9799
9800 if (! d->emitted_number)
9801 return 1;
9802
9803 gcc_assert (fnad->max_files != fnad->used_files);
9804
9805 fi = fnad->files + fnad->used_files++;
9806
9807 /* Skip all leading "./". */
9808 f = d->filename;
9809 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9810 f += 2;
9811
9812 /* Create a new array entry. */
9813 fi->path = f;
9814 fi->length = strlen (f);
9815 fi->file_idx = d;
9816
9817 /* Search for the file name part. */
9818 f = strrchr (f, DIR_SEPARATOR);
9819 #if defined (DIR_SEPARATOR_2)
9820 {
9821 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9822
9823 if (g != NULL)
9824 {
9825 if (f == NULL || f < g)
9826 f = g;
9827 }
9828 }
9829 #endif
9830
9831 fi->fname = f == NULL ? fi->path : f + 1;
9832 return 1;
9833 }
9834
9835 /* Output the directory table and the file name table. We try to minimize
9836 the total amount of memory needed. A heuristic is used to avoid large
9837 slowdowns with many input files. */
9838
9839 static void
9840 output_file_names (void)
9841 {
9842 struct file_name_acquire_data fnad;
9843 int numfiles;
9844 struct file_info *files;
9845 struct dir_info *dirs;
9846 int *saved;
9847 int *savehere;
9848 int *backmap;
9849 int ndirs;
9850 int idx_offset;
9851 int i;
9852
9853 if (!last_emitted_file)
9854 {
9855 dw2_asm_output_data (1, 0, "End directory table");
9856 dw2_asm_output_data (1, 0, "End file name table");
9857 return;
9858 }
9859
9860 numfiles = last_emitted_file->emitted_number;
9861
9862 /* Allocate the various arrays we need. */
9863 files = XALLOCAVEC (struct file_info, numfiles);
9864 dirs = XALLOCAVEC (struct dir_info, numfiles);
9865
9866 fnad.files = files;
9867 fnad.used_files = 0;
9868 fnad.max_files = numfiles;
9869 htab_traverse (file_table, file_name_acquire, &fnad);
9870 gcc_assert (fnad.used_files == fnad.max_files);
9871
9872 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9873
9874 /* Find all the different directories used. */
9875 dirs[0].path = files[0].path;
9876 dirs[0].length = files[0].fname - files[0].path;
9877 dirs[0].prefix = -1;
9878 dirs[0].count = 1;
9879 dirs[0].dir_idx = 0;
9880 files[0].dir_idx = 0;
9881 ndirs = 1;
9882
9883 for (i = 1; i < numfiles; i++)
9884 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9885 && memcmp (dirs[ndirs - 1].path, files[i].path,
9886 dirs[ndirs - 1].length) == 0)
9887 {
9888 /* Same directory as last entry. */
9889 files[i].dir_idx = ndirs - 1;
9890 ++dirs[ndirs - 1].count;
9891 }
9892 else
9893 {
9894 int j;
9895
9896 /* This is a new directory. */
9897 dirs[ndirs].path = files[i].path;
9898 dirs[ndirs].length = files[i].fname - files[i].path;
9899 dirs[ndirs].count = 1;
9900 dirs[ndirs].dir_idx = ndirs;
9901 files[i].dir_idx = ndirs;
9902
9903 /* Search for a prefix. */
9904 dirs[ndirs].prefix = -1;
9905 for (j = 0; j < ndirs; j++)
9906 if (dirs[j].length < dirs[ndirs].length
9907 && dirs[j].length > 1
9908 && (dirs[ndirs].prefix == -1
9909 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9910 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9911 dirs[ndirs].prefix = j;
9912
9913 ++ndirs;
9914 }
9915
9916 /* Now to the actual work. We have to find a subset of the directories which
9917 allow expressing the file name using references to the directory table
9918 with the least amount of characters. We do not do an exhaustive search
9919 where we would have to check out every combination of every single
9920 possible prefix. Instead we use a heuristic which provides nearly optimal
9921 results in most cases and never is much off. */
9922 saved = XALLOCAVEC (int, ndirs);
9923 savehere = XALLOCAVEC (int, ndirs);
9924
9925 memset (saved, '\0', ndirs * sizeof (saved[0]));
9926 for (i = 0; i < ndirs; i++)
9927 {
9928 int j;
9929 int total;
9930
9931 /* We can always save some space for the current directory. But this
9932 does not mean it will be enough to justify adding the directory. */
9933 savehere[i] = dirs[i].length;
9934 total = (savehere[i] - saved[i]) * dirs[i].count;
9935
9936 for (j = i + 1; j < ndirs; j++)
9937 {
9938 savehere[j] = 0;
9939 if (saved[j] < dirs[i].length)
9940 {
9941 /* Determine whether the dirs[i] path is a prefix of the
9942 dirs[j] path. */
9943 int k;
9944
9945 k = dirs[j].prefix;
9946 while (k != -1 && k != (int) i)
9947 k = dirs[k].prefix;
9948
9949 if (k == (int) i)
9950 {
9951 /* Yes it is. We can possibly save some memory by
9952 writing the filenames in dirs[j] relative to
9953 dirs[i]. */
9954 savehere[j] = dirs[i].length;
9955 total += (savehere[j] - saved[j]) * dirs[j].count;
9956 }
9957 }
9958 }
9959
9960 /* Check whether we can save enough to justify adding the dirs[i]
9961 directory. */
9962 if (total > dirs[i].length + 1)
9963 {
9964 /* It's worthwhile adding. */
9965 for (j = i; j < ndirs; j++)
9966 if (savehere[j] > 0)
9967 {
9968 /* Remember how much we saved for this directory so far. */
9969 saved[j] = savehere[j];
9970
9971 /* Remember the prefix directory. */
9972 dirs[j].dir_idx = i;
9973 }
9974 }
9975 }
9976
9977 /* Emit the directory name table. */
9978 idx_offset = dirs[0].length > 0 ? 1 : 0;
9979 for (i = 1 - idx_offset; i < ndirs; i++)
9980 dw2_asm_output_nstring (dirs[i].path,
9981 dirs[i].length
9982 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9983 "Directory Entry: %#x", i + idx_offset);
9984
9985 dw2_asm_output_data (1, 0, "End directory table");
9986
9987 /* We have to emit them in the order of emitted_number since that's
9988 used in the debug info generation. To do this efficiently we
9989 generate a back-mapping of the indices first. */
9990 backmap = XALLOCAVEC (int, numfiles);
9991 for (i = 0; i < numfiles; i++)
9992 backmap[files[i].file_idx->emitted_number - 1] = i;
9993
9994 /* Now write all the file names. */
9995 for (i = 0; i < numfiles; i++)
9996 {
9997 int file_idx = backmap[i];
9998 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9999
10000 #ifdef VMS_DEBUGGING_INFO
10001 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10002
10003 /* Setting these fields can lead to debugger miscomparisons,
10004 but VMS Debug requires them to be set correctly. */
10005
10006 int ver;
10007 long long cdt;
10008 long siz;
10009 int maxfilelen = strlen (files[file_idx].path)
10010 + dirs[dir_idx].length
10011 + MAX_VMS_VERSION_LEN + 1;
10012 char *filebuf = XALLOCAVEC (char, maxfilelen);
10013
10014 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10015 snprintf (filebuf, maxfilelen, "%s;%d",
10016 files[file_idx].path + dirs[dir_idx].length, ver);
10017
10018 dw2_asm_output_nstring
10019 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10020
10021 /* Include directory index. */
10022 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10023
10024 /* Modification time. */
10025 dw2_asm_output_data_uleb128
10026 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10027 ? cdt : 0,
10028 NULL);
10029
10030 /* File length in bytes. */
10031 dw2_asm_output_data_uleb128
10032 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10033 ? siz : 0,
10034 NULL);
10035 #else
10036 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10037 "File Entry: %#x", (unsigned) i + 1);
10038
10039 /* Include directory index. */
10040 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10041
10042 /* Modification time. */
10043 dw2_asm_output_data_uleb128 (0, NULL);
10044
10045 /* File length in bytes. */
10046 dw2_asm_output_data_uleb128 (0, NULL);
10047 #endif /* VMS_DEBUGGING_INFO */
10048 }
10049
10050 dw2_asm_output_data (1, 0, "End file name table");
10051 }
10052
10053
10054 /* Output one line number table into the .debug_line section. */
10055
10056 static void
10057 output_one_line_info_table (dw_line_info_table *table)
10058 {
10059 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10060 unsigned int current_line = 1;
10061 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10062 dw_line_info_entry *ent;
10063 size_t i;
10064
10065 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10066 {
10067 switch (ent->opcode)
10068 {
10069 case LI_set_address:
10070 /* ??? Unfortunately, we have little choice here currently, and
10071 must always use the most general form. GCC does not know the
10072 address delta itself, so we can't use DW_LNS_advance_pc. Many
10073 ports do have length attributes which will give an upper bound
10074 on the address range. We could perhaps use length attributes
10075 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10076 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10077
10078 /* This can handle any delta. This takes
10079 4+DWARF2_ADDR_SIZE bytes. */
10080 dw2_asm_output_data (1, 0, "set address %s", line_label);
10081 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10082 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10083 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10084 break;
10085
10086 case LI_set_line:
10087 if (ent->val == current_line)
10088 {
10089 /* We still need to start a new row, so output a copy insn. */
10090 dw2_asm_output_data (1, DW_LNS_copy,
10091 "copy line %u", current_line);
10092 }
10093 else
10094 {
10095 int line_offset = ent->val - current_line;
10096 int line_delta = line_offset - DWARF_LINE_BASE;
10097
10098 current_line = ent->val;
10099 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10100 {
10101 /* This can handle deltas from -10 to 234, using the current
10102 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10103 This takes 1 byte. */
10104 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10105 "line %u", current_line);
10106 }
10107 else
10108 {
10109 /* This can handle any delta. This takes at least 4 bytes,
10110 depending on the value being encoded. */
10111 dw2_asm_output_data (1, DW_LNS_advance_line,
10112 "advance to line %u", current_line);
10113 dw2_asm_output_data_sleb128 (line_offset, NULL);
10114 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10115 }
10116 }
10117 break;
10118
10119 case LI_set_file:
10120 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10121 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10122 break;
10123
10124 case LI_set_column:
10125 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10126 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10127 break;
10128
10129 case LI_negate_stmt:
10130 current_is_stmt = !current_is_stmt;
10131 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10132 "is_stmt %d", current_is_stmt);
10133 break;
10134
10135 case LI_set_prologue_end:
10136 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10137 "set prologue end");
10138 break;
10139
10140 case LI_set_epilogue_begin:
10141 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10142 "set epilogue begin");
10143 break;
10144
10145 case LI_set_discriminator:
10146 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10147 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10148 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10149 dw2_asm_output_data_uleb128 (ent->val, NULL);
10150 break;
10151 }
10152 }
10153
10154 /* Emit debug info for the address of the end of the table. */
10155 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10156 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10157 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10158 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10159
10160 dw2_asm_output_data (1, 0, "end sequence");
10161 dw2_asm_output_data_uleb128 (1, NULL);
10162 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10163 }
10164
10165 /* Output the source line number correspondence information. This
10166 information goes into the .debug_line section. */
10167
10168 static void
10169 output_line_info (bool prologue_only)
10170 {
10171 char l1[20], l2[20], p1[20], p2[20];
10172 int ver = dwarf_version;
10173 bool saw_one = false;
10174 int opc;
10175
10176 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10177 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10178 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10179 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10180
10181 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10182 dw2_asm_output_data (4, 0xffffffff,
10183 "Initial length escape value indicating 64-bit DWARF extension");
10184 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10185 "Length of Source Line Info");
10186 ASM_OUTPUT_LABEL (asm_out_file, l1);
10187
10188 dw2_asm_output_data (2, ver, "DWARF Version");
10189 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10190 ASM_OUTPUT_LABEL (asm_out_file, p1);
10191
10192 /* Define the architecture-dependent minimum instruction length (in bytes).
10193 In this implementation of DWARF, this field is used for information
10194 purposes only. Since GCC generates assembly language, we have no
10195 a priori knowledge of how many instruction bytes are generated for each
10196 source line, and therefore can use only the DW_LNE_set_address and
10197 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10198 this as '1', which is "correct enough" for all architectures,
10199 and don't let the target override. */
10200 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10201
10202 if (ver >= 4)
10203 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10204 "Maximum Operations Per Instruction");
10205 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10206 "Default is_stmt_start flag");
10207 dw2_asm_output_data (1, DWARF_LINE_BASE,
10208 "Line Base Value (Special Opcodes)");
10209 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10210 "Line Range Value (Special Opcodes)");
10211 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10212 "Special Opcode Base");
10213
10214 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10215 {
10216 int n_op_args;
10217 switch (opc)
10218 {
10219 case DW_LNS_advance_pc:
10220 case DW_LNS_advance_line:
10221 case DW_LNS_set_file:
10222 case DW_LNS_set_column:
10223 case DW_LNS_fixed_advance_pc:
10224 case DW_LNS_set_isa:
10225 n_op_args = 1;
10226 break;
10227 default:
10228 n_op_args = 0;
10229 break;
10230 }
10231
10232 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10233 opc, n_op_args);
10234 }
10235
10236 /* Write out the information about the files we use. */
10237 output_file_names ();
10238 ASM_OUTPUT_LABEL (asm_out_file, p2);
10239 if (prologue_only)
10240 {
10241 /* Output the marker for the end of the line number info. */
10242 ASM_OUTPUT_LABEL (asm_out_file, l2);
10243 return;
10244 }
10245
10246 if (separate_line_info)
10247 {
10248 dw_line_info_table *table;
10249 size_t i;
10250
10251 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10252 if (table->in_use)
10253 {
10254 output_one_line_info_table (table);
10255 saw_one = true;
10256 }
10257 }
10258 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10259 {
10260 output_one_line_info_table (cold_text_section_line_info);
10261 saw_one = true;
10262 }
10263
10264 /* ??? Some Darwin linkers crash on a .debug_line section with no
10265 sequences. Further, merely a DW_LNE_end_sequence entry is not
10266 sufficient -- the address column must also be initialized.
10267 Make sure to output at least one set_address/end_sequence pair,
10268 choosing .text since that section is always present. */
10269 if (text_section_line_info->in_use || !saw_one)
10270 output_one_line_info_table (text_section_line_info);
10271
10272 /* Output the marker for the end of the line number info. */
10273 ASM_OUTPUT_LABEL (asm_out_file, l2);
10274 }
10275 \f
10276 /* Given a pointer to a tree node for some base type, return a pointer to
10277 a DIE that describes the given type.
10278
10279 This routine must only be called for GCC type nodes that correspond to
10280 Dwarf base (fundamental) types. */
10281
10282 static dw_die_ref
10283 base_type_die (tree type)
10284 {
10285 dw_die_ref base_type_result;
10286 enum dwarf_type encoding;
10287
10288 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10289 return 0;
10290
10291 /* If this is a subtype that should not be emitted as a subrange type,
10292 use the base type. See subrange_type_for_debug_p. */
10293 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10294 type = TREE_TYPE (type);
10295
10296 switch (TREE_CODE (type))
10297 {
10298 case INTEGER_TYPE:
10299 if ((dwarf_version >= 4 || !dwarf_strict)
10300 && TYPE_NAME (type)
10301 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10302 && DECL_IS_BUILTIN (TYPE_NAME (type))
10303 && DECL_NAME (TYPE_NAME (type)))
10304 {
10305 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10306 if (strcmp (name, "char16_t") == 0
10307 || strcmp (name, "char32_t") == 0)
10308 {
10309 encoding = DW_ATE_UTF;
10310 break;
10311 }
10312 }
10313 if (TYPE_STRING_FLAG (type))
10314 {
10315 if (TYPE_UNSIGNED (type))
10316 encoding = DW_ATE_unsigned_char;
10317 else
10318 encoding = DW_ATE_signed_char;
10319 }
10320 else if (TYPE_UNSIGNED (type))
10321 encoding = DW_ATE_unsigned;
10322 else
10323 encoding = DW_ATE_signed;
10324 break;
10325
10326 case REAL_TYPE:
10327 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10328 {
10329 if (dwarf_version >= 3 || !dwarf_strict)
10330 encoding = DW_ATE_decimal_float;
10331 else
10332 encoding = DW_ATE_lo_user;
10333 }
10334 else
10335 encoding = DW_ATE_float;
10336 break;
10337
10338 case FIXED_POINT_TYPE:
10339 if (!(dwarf_version >= 3 || !dwarf_strict))
10340 encoding = DW_ATE_lo_user;
10341 else if (TYPE_UNSIGNED (type))
10342 encoding = DW_ATE_unsigned_fixed;
10343 else
10344 encoding = DW_ATE_signed_fixed;
10345 break;
10346
10347 /* Dwarf2 doesn't know anything about complex ints, so use
10348 a user defined type for it. */
10349 case COMPLEX_TYPE:
10350 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10351 encoding = DW_ATE_complex_float;
10352 else
10353 encoding = DW_ATE_lo_user;
10354 break;
10355
10356 case BOOLEAN_TYPE:
10357 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10358 encoding = DW_ATE_boolean;
10359 break;
10360
10361 default:
10362 /* No other TREE_CODEs are Dwarf fundamental types. */
10363 gcc_unreachable ();
10364 }
10365
10366 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10367
10368 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10369 int_size_in_bytes (type));
10370 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10371 add_pubtype (type, base_type_result);
10372
10373 return base_type_result;
10374 }
10375
10376 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10377 named 'auto' in its type: return true for it, false otherwise. */
10378
10379 static inline bool
10380 is_cxx_auto (tree type)
10381 {
10382 if (is_cxx ())
10383 {
10384 tree name = TYPE_IDENTIFIER (type);
10385 if (name == get_identifier ("auto")
10386 || name == get_identifier ("decltype(auto)"))
10387 return true;
10388 }
10389 return false;
10390 }
10391
10392 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10393 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10394
10395 static inline int
10396 is_base_type (tree type)
10397 {
10398 switch (TREE_CODE (type))
10399 {
10400 case ERROR_MARK:
10401 case VOID_TYPE:
10402 case INTEGER_TYPE:
10403 case REAL_TYPE:
10404 case FIXED_POINT_TYPE:
10405 case COMPLEX_TYPE:
10406 case BOOLEAN_TYPE:
10407 return 1;
10408
10409 case ARRAY_TYPE:
10410 case RECORD_TYPE:
10411 case UNION_TYPE:
10412 case QUAL_UNION_TYPE:
10413 case ENUMERAL_TYPE:
10414 case FUNCTION_TYPE:
10415 case METHOD_TYPE:
10416 case POINTER_TYPE:
10417 case REFERENCE_TYPE:
10418 case NULLPTR_TYPE:
10419 case OFFSET_TYPE:
10420 case LANG_TYPE:
10421 case VECTOR_TYPE:
10422 return 0;
10423
10424 default:
10425 if (is_cxx_auto (type))
10426 return 0;
10427 gcc_unreachable ();
10428 }
10429
10430 return 0;
10431 }
10432
10433 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10434 node, return the size in bits for the type if it is a constant, or else
10435 return the alignment for the type if the type's size is not constant, or
10436 else return BITS_PER_WORD if the type actually turns out to be an
10437 ERROR_MARK node. */
10438
10439 static inline unsigned HOST_WIDE_INT
10440 simple_type_size_in_bits (const_tree type)
10441 {
10442 if (TREE_CODE (type) == ERROR_MARK)
10443 return BITS_PER_WORD;
10444 else if (TYPE_SIZE (type) == NULL_TREE)
10445 return 0;
10446 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10447 return tree_to_uhwi (TYPE_SIZE (type));
10448 else
10449 return TYPE_ALIGN (type);
10450 }
10451
10452 /* Similarly, but return an offset_int instead of UHWI. */
10453
10454 static inline offset_int
10455 offset_int_type_size_in_bits (const_tree type)
10456 {
10457 if (TREE_CODE (type) == ERROR_MARK)
10458 return BITS_PER_WORD;
10459 else if (TYPE_SIZE (type) == NULL_TREE)
10460 return 0;
10461 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10462 return wi::to_offset (TYPE_SIZE (type));
10463 else
10464 return TYPE_ALIGN (type);
10465 }
10466
10467 /* Given a pointer to a tree node for a subrange type, return a pointer
10468 to a DIE that describes the given type. */
10469
10470 static dw_die_ref
10471 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10472 {
10473 dw_die_ref subrange_die;
10474 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10475
10476 if (context_die == NULL)
10477 context_die = comp_unit_die ();
10478
10479 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10480
10481 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10482 {
10483 /* The size of the subrange type and its base type do not match,
10484 so we need to generate a size attribute for the subrange type. */
10485 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10486 }
10487
10488 if (low)
10489 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10490 if (high)
10491 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10492
10493 return subrange_die;
10494 }
10495
10496 /* Returns the (const and/or volatile) cv_qualifiers associated with
10497 the decl node. This will normally be augmented with the
10498 cv_qualifiers of the underlying type in add_type_attribute. */
10499
10500 static int
10501 decl_quals (const_tree decl)
10502 {
10503 return ((TREE_READONLY (decl)
10504 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10505 | (TREE_THIS_VOLATILE (decl)
10506 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10507 }
10508
10509 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10510 entry that chains various modifiers in front of the given type. */
10511
10512 static dw_die_ref
10513 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10514 {
10515 enum tree_code code = TREE_CODE (type);
10516 dw_die_ref mod_type_die;
10517 dw_die_ref sub_die = NULL;
10518 tree item_type = NULL;
10519 tree qualified_type;
10520 tree name, low, high;
10521 dw_die_ref mod_scope;
10522
10523 if (code == ERROR_MARK)
10524 return NULL;
10525
10526 /* Only these cv-qualifiers are currently handled. */
10527 cv_quals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT);
10528
10529 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10530 tag modifier (and not an attribute) old consumers won't be able
10531 to handle it. */
10532 if (dwarf_version < 3)
10533 cv_quals &= ~TYPE_QUAL_RESTRICT;
10534
10535 /* See if we already have the appropriately qualified variant of
10536 this type. */
10537 qualified_type = get_qualified_type (type, cv_quals);
10538
10539 if (qualified_type == sizetype
10540 && TYPE_NAME (qualified_type)
10541 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10542 {
10543 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10544
10545 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10546 && TYPE_PRECISION (t)
10547 == TYPE_PRECISION (qualified_type)
10548 && TYPE_UNSIGNED (t)
10549 == TYPE_UNSIGNED (qualified_type));
10550 qualified_type = t;
10551 }
10552
10553 /* If we do, then we can just use its DIE, if it exists. */
10554 if (qualified_type)
10555 {
10556 mod_type_die = lookup_type_die (qualified_type);
10557 if (mod_type_die)
10558 return mod_type_die;
10559 }
10560
10561 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10562
10563 /* Handle C typedef types. */
10564 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10565 && !DECL_ARTIFICIAL (name))
10566 {
10567 tree dtype = TREE_TYPE (name);
10568
10569 if (qualified_type == dtype)
10570 {
10571 /* For a named type, use the typedef. */
10572 gen_type_die (qualified_type, context_die);
10573 return lookup_type_die (qualified_type);
10574 }
10575 else
10576 {
10577 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10578 dquals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT);
10579 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10580 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10581 /* cv-unqualified version of named type. Just use
10582 the unnamed type to which it refers. */
10583 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10584 cv_quals, context_die);
10585 /* Else cv-qualified version of named type; fall through. */
10586 }
10587 }
10588
10589 mod_scope = scope_die_for (type, context_die);
10590
10591 if ((cv_quals & TYPE_QUAL_CONST)
10592 /* If there are multiple type modifiers, prefer a path which
10593 leads to a qualified type. */
10594 && (((cv_quals & ~TYPE_QUAL_CONST) == TYPE_UNQUALIFIED)
10595 || get_qualified_type (type, cv_quals) == NULL_TREE
10596 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_CONST)
10597 != NULL_TREE)))
10598 {
10599 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10600 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_CONST,
10601 context_die);
10602 }
10603 else if ((cv_quals & TYPE_QUAL_VOLATILE)
10604 && (((cv_quals & ~TYPE_QUAL_VOLATILE) == TYPE_UNQUALIFIED)
10605 || get_qualified_type (type, cv_quals) == NULL_TREE
10606 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_VOLATILE)
10607 != NULL_TREE)))
10608 {
10609 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10610 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_VOLATILE,
10611 context_die);
10612 }
10613 else if (cv_quals & TYPE_QUAL_RESTRICT)
10614 {
10615 mod_type_die = new_die (DW_TAG_restrict_type, mod_scope, type);
10616 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_RESTRICT,
10617 context_die);
10618 }
10619 else if (code == POINTER_TYPE)
10620 {
10621 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10622 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10623 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10624 item_type = TREE_TYPE (type);
10625 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10626 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10627 TYPE_ADDR_SPACE (item_type));
10628 }
10629 else if (code == REFERENCE_TYPE)
10630 {
10631 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10632 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10633 type);
10634 else
10635 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10636 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10637 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10638 item_type = TREE_TYPE (type);
10639 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10640 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10641 TYPE_ADDR_SPACE (item_type));
10642 }
10643 else if (code == INTEGER_TYPE
10644 && TREE_TYPE (type) != NULL_TREE
10645 && subrange_type_for_debug_p (type, &low, &high))
10646 {
10647 mod_type_die = subrange_type_die (type, low, high, context_die);
10648 item_type = TREE_TYPE (type);
10649 }
10650 else if (is_base_type (type))
10651 mod_type_die = base_type_die (type);
10652 else
10653 {
10654 gen_type_die (type, context_die);
10655
10656 /* We have to get the type_main_variant here (and pass that to the
10657 `lookup_type_die' routine) because the ..._TYPE node we have
10658 might simply be a *copy* of some original type node (where the
10659 copy was created to help us keep track of typedef names) and
10660 that copy might have a different TYPE_UID from the original
10661 ..._TYPE node. */
10662 if (TREE_CODE (type) != VECTOR_TYPE)
10663 return lookup_type_die (type_main_variant (type));
10664 else
10665 /* Vectors have the debugging information in the type,
10666 not the main variant. */
10667 return lookup_type_die (type);
10668 }
10669
10670 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10671 don't output a DW_TAG_typedef, since there isn't one in the
10672 user's program; just attach a DW_AT_name to the type.
10673 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10674 if the base type already has the same name. */
10675 if (name
10676 && ((TREE_CODE (name) != TYPE_DECL
10677 && (qualified_type == TYPE_MAIN_VARIANT (type)
10678 || (cv_quals == TYPE_UNQUALIFIED)))
10679 || (TREE_CODE (name) == TYPE_DECL
10680 && TREE_TYPE (name) == qualified_type
10681 && DECL_NAME (name))))
10682 {
10683 if (TREE_CODE (name) == TYPE_DECL)
10684 /* Could just call add_name_and_src_coords_attributes here,
10685 but since this is a builtin type it doesn't have any
10686 useful source coordinates anyway. */
10687 name = DECL_NAME (name);
10688 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10689 }
10690 /* This probably indicates a bug. */
10691 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10692 {
10693 name = TYPE_IDENTIFIER (type);
10694 add_name_attribute (mod_type_die,
10695 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10696 }
10697
10698 if (qualified_type)
10699 equate_type_number_to_die (qualified_type, mod_type_die);
10700
10701 if (item_type)
10702 /* We must do this after the equate_type_number_to_die call, in case
10703 this is a recursive type. This ensures that the modified_type_die
10704 recursion will terminate even if the type is recursive. Recursive
10705 types are possible in Ada. */
10706 sub_die = modified_type_die (item_type,
10707 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10708 context_die);
10709
10710 if (sub_die != NULL)
10711 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10712
10713 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10714 if (TYPE_ARTIFICIAL (type))
10715 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10716
10717 return mod_type_die;
10718 }
10719
10720 /* Generate DIEs for the generic parameters of T.
10721 T must be either a generic type or a generic function.
10722 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10723
10724 static void
10725 gen_generic_params_dies (tree t)
10726 {
10727 tree parms, args;
10728 int parms_num, i;
10729 dw_die_ref die = NULL;
10730 int non_default;
10731
10732 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10733 return;
10734
10735 if (TYPE_P (t))
10736 die = lookup_type_die (t);
10737 else if (DECL_P (t))
10738 die = lookup_decl_die (t);
10739
10740 gcc_assert (die);
10741
10742 parms = lang_hooks.get_innermost_generic_parms (t);
10743 if (!parms)
10744 /* T has no generic parameter. It means T is neither a generic type
10745 or function. End of story. */
10746 return;
10747
10748 parms_num = TREE_VEC_LENGTH (parms);
10749 args = lang_hooks.get_innermost_generic_args (t);
10750 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10751 non_default = int_cst_value (TREE_CHAIN (args));
10752 else
10753 non_default = TREE_VEC_LENGTH (args);
10754 for (i = 0; i < parms_num; i++)
10755 {
10756 tree parm, arg, arg_pack_elems;
10757 dw_die_ref parm_die;
10758
10759 parm = TREE_VEC_ELT (parms, i);
10760 arg = TREE_VEC_ELT (args, i);
10761 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10762 gcc_assert (parm && TREE_VALUE (parm) && arg);
10763
10764 if (parm && TREE_VALUE (parm) && arg)
10765 {
10766 /* If PARM represents a template parameter pack,
10767 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10768 by DW_TAG_template_*_parameter DIEs for the argument
10769 pack elements of ARG. Note that ARG would then be
10770 an argument pack. */
10771 if (arg_pack_elems)
10772 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10773 arg_pack_elems,
10774 die);
10775 else
10776 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10777 true /* emit name */, die);
10778 if (i >= non_default)
10779 add_AT_flag (parm_die, DW_AT_default_value, 1);
10780 }
10781 }
10782 }
10783
10784 /* Create and return a DIE for PARM which should be
10785 the representation of a generic type parameter.
10786 For instance, in the C++ front end, PARM would be a template parameter.
10787 ARG is the argument to PARM.
10788 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10789 name of the PARM.
10790 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10791 as a child node. */
10792
10793 static dw_die_ref
10794 generic_parameter_die (tree parm, tree arg,
10795 bool emit_name_p,
10796 dw_die_ref parent_die)
10797 {
10798 dw_die_ref tmpl_die = NULL;
10799 const char *name = NULL;
10800
10801 if (!parm || !DECL_NAME (parm) || !arg)
10802 return NULL;
10803
10804 /* We support non-type generic parameters and arguments,
10805 type generic parameters and arguments, as well as
10806 generic generic parameters (a.k.a. template template parameters in C++)
10807 and arguments. */
10808 if (TREE_CODE (parm) == PARM_DECL)
10809 /* PARM is a nontype generic parameter */
10810 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10811 else if (TREE_CODE (parm) == TYPE_DECL)
10812 /* PARM is a type generic parameter. */
10813 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10814 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10815 /* PARM is a generic generic parameter.
10816 Its DIE is a GNU extension. It shall have a
10817 DW_AT_name attribute to represent the name of the template template
10818 parameter, and a DW_AT_GNU_template_name attribute to represent the
10819 name of the template template argument. */
10820 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10821 parent_die, parm);
10822 else
10823 gcc_unreachable ();
10824
10825 if (tmpl_die)
10826 {
10827 tree tmpl_type;
10828
10829 /* If PARM is a generic parameter pack, it means we are
10830 emitting debug info for a template argument pack element.
10831 In other terms, ARG is a template argument pack element.
10832 In that case, we don't emit any DW_AT_name attribute for
10833 the die. */
10834 if (emit_name_p)
10835 {
10836 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10837 gcc_assert (name);
10838 add_AT_string (tmpl_die, DW_AT_name, name);
10839 }
10840
10841 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10842 {
10843 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10844 TMPL_DIE should have a child DW_AT_type attribute that is set
10845 to the type of the argument to PARM, which is ARG.
10846 If PARM is a type generic parameter, TMPL_DIE should have a
10847 child DW_AT_type that is set to ARG. */
10848 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10849 add_type_attribute (tmpl_die, tmpl_type,
10850 (TREE_THIS_VOLATILE (tmpl_type)
10851 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10852 parent_die);
10853 }
10854 else
10855 {
10856 /* So TMPL_DIE is a DIE representing a
10857 a generic generic template parameter, a.k.a template template
10858 parameter in C++ and arg is a template. */
10859
10860 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10861 to the name of the argument. */
10862 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10863 if (name)
10864 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10865 }
10866
10867 if (TREE_CODE (parm) == PARM_DECL)
10868 /* So PARM is a non-type generic parameter.
10869 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10870 attribute of TMPL_DIE which value represents the value
10871 of ARG.
10872 We must be careful here:
10873 The value of ARG might reference some function decls.
10874 We might currently be emitting debug info for a generic
10875 type and types are emitted before function decls, we don't
10876 know if the function decls referenced by ARG will actually be
10877 emitted after cgraph computations.
10878 So must defer the generation of the DW_AT_const_value to
10879 after cgraph is ready. */
10880 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10881 }
10882
10883 return tmpl_die;
10884 }
10885
10886 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10887 PARM_PACK must be a template parameter pack. The returned DIE
10888 will be child DIE of PARENT_DIE. */
10889
10890 static dw_die_ref
10891 template_parameter_pack_die (tree parm_pack,
10892 tree parm_pack_args,
10893 dw_die_ref parent_die)
10894 {
10895 dw_die_ref die;
10896 int j;
10897
10898 gcc_assert (parent_die && parm_pack);
10899
10900 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10901 add_name_and_src_coords_attributes (die, parm_pack);
10902 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10903 generic_parameter_die (parm_pack,
10904 TREE_VEC_ELT (parm_pack_args, j),
10905 false /* Don't emit DW_AT_name */,
10906 die);
10907 return die;
10908 }
10909
10910 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10911 an enumerated type. */
10912
10913 static inline int
10914 type_is_enum (const_tree type)
10915 {
10916 return TREE_CODE (type) == ENUMERAL_TYPE;
10917 }
10918
10919 /* Return the DBX register number described by a given RTL node. */
10920
10921 static unsigned int
10922 dbx_reg_number (const_rtx rtl)
10923 {
10924 unsigned regno = REGNO (rtl);
10925
10926 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10927
10928 #ifdef LEAF_REG_REMAP
10929 if (crtl->uses_only_leaf_regs)
10930 {
10931 int leaf_reg = LEAF_REG_REMAP (regno);
10932 if (leaf_reg != -1)
10933 regno = (unsigned) leaf_reg;
10934 }
10935 #endif
10936
10937 regno = DBX_REGISTER_NUMBER (regno);
10938 gcc_assert (regno != INVALID_REGNUM);
10939 return regno;
10940 }
10941
10942 /* Optionally add a DW_OP_piece term to a location description expression.
10943 DW_OP_piece is only added if the location description expression already
10944 doesn't end with DW_OP_piece. */
10945
10946 static void
10947 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10948 {
10949 dw_loc_descr_ref loc;
10950
10951 if (*list_head != NULL)
10952 {
10953 /* Find the end of the chain. */
10954 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10955 ;
10956
10957 if (loc->dw_loc_opc != DW_OP_piece)
10958 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10959 }
10960 }
10961
10962 /* Return a location descriptor that designates a machine register or
10963 zero if there is none. */
10964
10965 static dw_loc_descr_ref
10966 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10967 {
10968 rtx regs;
10969
10970 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10971 return 0;
10972
10973 /* We only use "frame base" when we're sure we're talking about the
10974 post-prologue local stack frame. We do this by *not* running
10975 register elimination until this point, and recognizing the special
10976 argument pointer and soft frame pointer rtx's.
10977 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10978 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10979 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10980 {
10981 dw_loc_descr_ref result = NULL;
10982
10983 if (dwarf_version >= 4 || !dwarf_strict)
10984 {
10985 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10986 initialized);
10987 if (result)
10988 add_loc_descr (&result,
10989 new_loc_descr (DW_OP_stack_value, 0, 0));
10990 }
10991 return result;
10992 }
10993
10994 regs = targetm.dwarf_register_span (rtl);
10995
10996 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10997 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10998 else
10999 {
11000 unsigned int dbx_regnum = dbx_reg_number (rtl);
11001 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11002 return 0;
11003 return one_reg_loc_descriptor (dbx_regnum, initialized);
11004 }
11005 }
11006
11007 /* Return a location descriptor that designates a machine register for
11008 a given hard register number. */
11009
11010 static dw_loc_descr_ref
11011 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11012 {
11013 dw_loc_descr_ref reg_loc_descr;
11014
11015 if (regno <= 31)
11016 reg_loc_descr
11017 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11018 else
11019 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11020
11021 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11022 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11023
11024 return reg_loc_descr;
11025 }
11026
11027 /* Given an RTL of a register, return a location descriptor that
11028 designates a value that spans more than one register. */
11029
11030 static dw_loc_descr_ref
11031 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11032 enum var_init_status initialized)
11033 {
11034 int size, i;
11035 dw_loc_descr_ref loc_result = NULL;
11036
11037 /* Simple, contiguous registers. */
11038 if (regs == NULL_RTX)
11039 {
11040 unsigned reg = REGNO (rtl);
11041 int nregs;
11042
11043 #ifdef LEAF_REG_REMAP
11044 if (crtl->uses_only_leaf_regs)
11045 {
11046 int leaf_reg = LEAF_REG_REMAP (reg);
11047 if (leaf_reg != -1)
11048 reg = (unsigned) leaf_reg;
11049 }
11050 #endif
11051
11052 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11053 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11054
11055 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11056
11057 loc_result = NULL;
11058 while (nregs--)
11059 {
11060 dw_loc_descr_ref t;
11061
11062 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11063 VAR_INIT_STATUS_INITIALIZED);
11064 add_loc_descr (&loc_result, t);
11065 add_loc_descr_op_piece (&loc_result, size);
11066 ++reg;
11067 }
11068 return loc_result;
11069 }
11070
11071 /* Now onto stupid register sets in non contiguous locations. */
11072
11073 gcc_assert (GET_CODE (regs) == PARALLEL);
11074
11075 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11076 loc_result = NULL;
11077
11078 for (i = 0; i < XVECLEN (regs, 0); ++i)
11079 {
11080 dw_loc_descr_ref t;
11081
11082 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11083 VAR_INIT_STATUS_INITIALIZED);
11084 add_loc_descr (&loc_result, t);
11085 add_loc_descr_op_piece (&loc_result, size);
11086 }
11087
11088 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11089 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11090 return loc_result;
11091 }
11092
11093 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11094
11095 /* Return a location descriptor that designates a constant i,
11096 as a compound operation from constant (i >> shift), constant shift
11097 and DW_OP_shl. */
11098
11099 static dw_loc_descr_ref
11100 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11101 {
11102 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11103 add_loc_descr (&ret, int_loc_descriptor (shift));
11104 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11105 return ret;
11106 }
11107
11108 /* Return a location descriptor that designates a constant. */
11109
11110 static dw_loc_descr_ref
11111 int_loc_descriptor (HOST_WIDE_INT i)
11112 {
11113 enum dwarf_location_atom op;
11114
11115 /* Pick the smallest representation of a constant, rather than just
11116 defaulting to the LEB encoding. */
11117 if (i >= 0)
11118 {
11119 int clz = clz_hwi (i);
11120 int ctz = ctz_hwi (i);
11121 if (i <= 31)
11122 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11123 else if (i <= 0xff)
11124 op = DW_OP_const1u;
11125 else if (i <= 0xffff)
11126 op = DW_OP_const2u;
11127 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11128 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11129 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11130 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11131 while DW_OP_const4u is 5 bytes. */
11132 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11133 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11134 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11135 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11136 while DW_OP_const4u is 5 bytes. */
11137 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11138 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11139 op = DW_OP_const4u;
11140 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11141 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11142 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11143 while DW_OP_constu of constant >= 0x100000000 takes at least
11144 6 bytes. */
11145 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11146 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11147 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11148 >= HOST_BITS_PER_WIDE_INT)
11149 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11150 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11151 while DW_OP_constu takes in this case at least 6 bytes. */
11152 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11153 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11154 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11155 && size_of_uleb128 (i) > 6)
11156 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11157 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11158 else
11159 op = DW_OP_constu;
11160 }
11161 else
11162 {
11163 if (i >= -0x80)
11164 op = DW_OP_const1s;
11165 else if (i >= -0x8000)
11166 op = DW_OP_const2s;
11167 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11168 {
11169 if (size_of_int_loc_descriptor (i) < 5)
11170 {
11171 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11172 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11173 return ret;
11174 }
11175 op = DW_OP_const4s;
11176 }
11177 else
11178 {
11179 if (size_of_int_loc_descriptor (i)
11180 < (unsigned long) 1 + size_of_sleb128 (i))
11181 {
11182 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11183 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11184 return ret;
11185 }
11186 op = DW_OP_consts;
11187 }
11188 }
11189
11190 return new_loc_descr (op, i, 0);
11191 }
11192
11193 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11194 without actually allocating it. */
11195
11196 static unsigned long
11197 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11198 {
11199 return size_of_int_loc_descriptor (i >> shift)
11200 + size_of_int_loc_descriptor (shift)
11201 + 1;
11202 }
11203
11204 /* Return size_of_locs (int_loc_descriptor (i)) without
11205 actually allocating it. */
11206
11207 static unsigned long
11208 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11209 {
11210 unsigned long s;
11211
11212 if (i >= 0)
11213 {
11214 int clz, ctz;
11215 if (i <= 31)
11216 return 1;
11217 else if (i <= 0xff)
11218 return 2;
11219 else if (i <= 0xffff)
11220 return 3;
11221 clz = clz_hwi (i);
11222 ctz = ctz_hwi (i);
11223 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11224 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11225 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11226 - clz - 5);
11227 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11228 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11229 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11230 - clz - 8);
11231 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11232 return 5;
11233 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11234 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11235 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11236 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11237 - clz - 8);
11238 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11239 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11240 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11241 - clz - 16);
11242 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11243 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11244 && s > 6)
11245 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11246 - clz - 32);
11247 else
11248 return 1 + s;
11249 }
11250 else
11251 {
11252 if (i >= -0x80)
11253 return 2;
11254 else if (i >= -0x8000)
11255 return 3;
11256 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11257 {
11258 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11259 {
11260 s = size_of_int_loc_descriptor (-i) + 1;
11261 if (s < 5)
11262 return s;
11263 }
11264 return 5;
11265 }
11266 else
11267 {
11268 unsigned long r = 1 + size_of_sleb128 (i);
11269 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11270 {
11271 s = size_of_int_loc_descriptor (-i) + 1;
11272 if (s < r)
11273 return s;
11274 }
11275 return r;
11276 }
11277 }
11278 }
11279
11280 /* Return loc description representing "address" of integer value.
11281 This can appear only as toplevel expression. */
11282
11283 static dw_loc_descr_ref
11284 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11285 {
11286 int litsize;
11287 dw_loc_descr_ref loc_result = NULL;
11288
11289 if (!(dwarf_version >= 4 || !dwarf_strict))
11290 return NULL;
11291
11292 litsize = size_of_int_loc_descriptor (i);
11293 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11294 is more compact. For DW_OP_stack_value we need:
11295 litsize + 1 (DW_OP_stack_value)
11296 and for DW_OP_implicit_value:
11297 1 (DW_OP_implicit_value) + 1 (length) + size. */
11298 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11299 {
11300 loc_result = int_loc_descriptor (i);
11301 add_loc_descr (&loc_result,
11302 new_loc_descr (DW_OP_stack_value, 0, 0));
11303 return loc_result;
11304 }
11305
11306 loc_result = new_loc_descr (DW_OP_implicit_value,
11307 size, 0);
11308 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11309 loc_result->dw_loc_oprnd2.v.val_int = i;
11310 return loc_result;
11311 }
11312
11313 /* Return a location descriptor that designates a base+offset location. */
11314
11315 static dw_loc_descr_ref
11316 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11317 enum var_init_status initialized)
11318 {
11319 unsigned int regno;
11320 dw_loc_descr_ref result;
11321 dw_fde_ref fde = cfun->fde;
11322
11323 /* We only use "frame base" when we're sure we're talking about the
11324 post-prologue local stack frame. We do this by *not* running
11325 register elimination until this point, and recognizing the special
11326 argument pointer and soft frame pointer rtx's. */
11327 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11328 {
11329 rtx elim = (ira_use_lra_p
11330 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11331 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11332
11333 if (elim != reg)
11334 {
11335 if (GET_CODE (elim) == PLUS)
11336 {
11337 offset += INTVAL (XEXP (elim, 1));
11338 elim = XEXP (elim, 0);
11339 }
11340 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11341 && (elim == hard_frame_pointer_rtx
11342 || elim == stack_pointer_rtx))
11343 || elim == (frame_pointer_needed
11344 ? hard_frame_pointer_rtx
11345 : stack_pointer_rtx));
11346
11347 /* If drap register is used to align stack, use frame
11348 pointer + offset to access stack variables. If stack
11349 is aligned without drap, use stack pointer + offset to
11350 access stack variables. */
11351 if (crtl->stack_realign_tried
11352 && reg == frame_pointer_rtx)
11353 {
11354 int base_reg
11355 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11356 ? HARD_FRAME_POINTER_REGNUM
11357 : REGNO (elim));
11358 return new_reg_loc_descr (base_reg, offset);
11359 }
11360
11361 gcc_assert (frame_pointer_fb_offset_valid);
11362 offset += frame_pointer_fb_offset;
11363 return new_loc_descr (DW_OP_fbreg, offset, 0);
11364 }
11365 }
11366
11367 regno = REGNO (reg);
11368 #ifdef LEAF_REG_REMAP
11369 if (crtl->uses_only_leaf_regs)
11370 {
11371 int leaf_reg = LEAF_REG_REMAP (regno);
11372 if (leaf_reg != -1)
11373 regno = (unsigned) leaf_reg;
11374 }
11375 #endif
11376 regno = DWARF_FRAME_REGNUM (regno);
11377
11378 if (!optimize && fde
11379 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11380 {
11381 /* Use cfa+offset to represent the location of arguments passed
11382 on the stack when drap is used to align stack.
11383 Only do this when not optimizing, for optimized code var-tracking
11384 is supposed to track where the arguments live and the register
11385 used as vdrap or drap in some spot might be used for something
11386 else in other part of the routine. */
11387 return new_loc_descr (DW_OP_fbreg, offset, 0);
11388 }
11389
11390 if (regno <= 31)
11391 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11392 offset, 0);
11393 else
11394 result = new_loc_descr (DW_OP_bregx, regno, offset);
11395
11396 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11397 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11398
11399 return result;
11400 }
11401
11402 /* Return true if this RTL expression describes a base+offset calculation. */
11403
11404 static inline int
11405 is_based_loc (const_rtx rtl)
11406 {
11407 return (GET_CODE (rtl) == PLUS
11408 && ((REG_P (XEXP (rtl, 0))
11409 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11410 && CONST_INT_P (XEXP (rtl, 1)))));
11411 }
11412
11413 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11414 failed. */
11415
11416 static dw_loc_descr_ref
11417 tls_mem_loc_descriptor (rtx mem)
11418 {
11419 tree base;
11420 dw_loc_descr_ref loc_result;
11421
11422 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11423 return NULL;
11424
11425 base = get_base_address (MEM_EXPR (mem));
11426 if (base == NULL
11427 || TREE_CODE (base) != VAR_DECL
11428 || !DECL_THREAD_LOCAL_P (base))
11429 return NULL;
11430
11431 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11432 if (loc_result == NULL)
11433 return NULL;
11434
11435 if (MEM_OFFSET (mem))
11436 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11437
11438 return loc_result;
11439 }
11440
11441 /* Output debug info about reason why we failed to expand expression as dwarf
11442 expression. */
11443
11444 static void
11445 expansion_failed (tree expr, rtx rtl, char const *reason)
11446 {
11447 if (dump_file && (dump_flags & TDF_DETAILS))
11448 {
11449 fprintf (dump_file, "Failed to expand as dwarf: ");
11450 if (expr)
11451 print_generic_expr (dump_file, expr, dump_flags);
11452 if (rtl)
11453 {
11454 fprintf (dump_file, "\n");
11455 print_rtl (dump_file, rtl);
11456 }
11457 fprintf (dump_file, "\nReason: %s\n", reason);
11458 }
11459 }
11460
11461 /* Helper function for const_ok_for_output. */
11462
11463 static bool
11464 const_ok_for_output_1 (rtx rtl)
11465 {
11466 if (GET_CODE (rtl) == UNSPEC)
11467 {
11468 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11469 we can't express it in the debug info. */
11470 #ifdef ENABLE_CHECKING
11471 /* Don't complain about TLS UNSPECs, those are just too hard to
11472 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11473 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11474 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11475 if (XVECLEN (rtl, 0) == 0
11476 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11477 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11478 inform (current_function_decl
11479 ? DECL_SOURCE_LOCATION (current_function_decl)
11480 : UNKNOWN_LOCATION,
11481 #if NUM_UNSPEC_VALUES > 0
11482 "non-delegitimized UNSPEC %s (%d) found in variable location",
11483 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11484 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11485 XINT (rtl, 1));
11486 #else
11487 "non-delegitimized UNSPEC %d found in variable location",
11488 XINT (rtl, 1));
11489 #endif
11490 #endif
11491 expansion_failed (NULL_TREE, rtl,
11492 "UNSPEC hasn't been delegitimized.\n");
11493 return false;
11494 }
11495
11496 if (targetm.const_not_ok_for_debug_p (rtl))
11497 {
11498 expansion_failed (NULL_TREE, rtl,
11499 "Expression rejected for debug by the backend.\n");
11500 return false;
11501 }
11502
11503 /* FIXME: Refer to PR60655. It is possible for simplification
11504 of rtl expressions in var tracking to produce such expressions.
11505 We should really identify / validate expressions
11506 enclosed in CONST that can be handled by assemblers on various
11507 targets and only handle legitimate cases here. */
11508 if (GET_CODE (rtl) != SYMBOL_REF)
11509 {
11510 if (GET_CODE (rtl) == NOT)
11511 return false;
11512 return true;
11513 }
11514
11515 if (CONSTANT_POOL_ADDRESS_P (rtl))
11516 {
11517 bool marked;
11518 get_pool_constant_mark (rtl, &marked);
11519 /* If all references to this pool constant were optimized away,
11520 it was not output and thus we can't represent it. */
11521 if (!marked)
11522 {
11523 expansion_failed (NULL_TREE, rtl,
11524 "Constant was removed from constant pool.\n");
11525 return false;
11526 }
11527 }
11528
11529 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11530 return false;
11531
11532 /* Avoid references to external symbols in debug info, on several targets
11533 the linker might even refuse to link when linking a shared library,
11534 and in many other cases the relocations for .debug_info/.debug_loc are
11535 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11536 to be defined within the same shared library or executable are fine. */
11537 if (SYMBOL_REF_EXTERNAL_P (rtl))
11538 {
11539 tree decl = SYMBOL_REF_DECL (rtl);
11540
11541 if (decl == NULL || !targetm.binds_local_p (decl))
11542 {
11543 expansion_failed (NULL_TREE, rtl,
11544 "Symbol not defined in current TU.\n");
11545 return false;
11546 }
11547 }
11548
11549 return true;
11550 }
11551
11552 /* Return true if constant RTL can be emitted in DW_OP_addr or
11553 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11554 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11555
11556 static bool
11557 const_ok_for_output (rtx rtl)
11558 {
11559 if (GET_CODE (rtl) == SYMBOL_REF)
11560 return const_ok_for_output_1 (rtl);
11561
11562 if (GET_CODE (rtl) == CONST)
11563 {
11564 subrtx_var_iterator::array_type array;
11565 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11566 if (!const_ok_for_output_1 (*iter))
11567 return false;
11568 return true;
11569 }
11570
11571 return true;
11572 }
11573
11574 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11575 if possible, NULL otherwise. */
11576
11577 static dw_die_ref
11578 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11579 {
11580 dw_die_ref type_die;
11581 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11582
11583 if (type == NULL)
11584 return NULL;
11585 switch (TREE_CODE (type))
11586 {
11587 case INTEGER_TYPE:
11588 case REAL_TYPE:
11589 break;
11590 default:
11591 return NULL;
11592 }
11593 type_die = lookup_type_die (type);
11594 if (!type_die)
11595 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11596 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11597 return NULL;
11598 return type_die;
11599 }
11600
11601 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11602 type matching MODE, or, if MODE is narrower than or as wide as
11603 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11604 possible. */
11605
11606 static dw_loc_descr_ref
11607 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11608 {
11609 enum machine_mode outer_mode = mode;
11610 dw_die_ref type_die;
11611 dw_loc_descr_ref cvt;
11612
11613 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11614 {
11615 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11616 return op;
11617 }
11618 type_die = base_type_for_mode (outer_mode, 1);
11619 if (type_die == NULL)
11620 return NULL;
11621 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11622 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11623 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11624 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11625 add_loc_descr (&op, cvt);
11626 return op;
11627 }
11628
11629 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11630
11631 static dw_loc_descr_ref
11632 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11633 dw_loc_descr_ref op1)
11634 {
11635 dw_loc_descr_ref ret = op0;
11636 add_loc_descr (&ret, op1);
11637 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11638 if (STORE_FLAG_VALUE != 1)
11639 {
11640 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11641 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11642 }
11643 return ret;
11644 }
11645
11646 /* Return location descriptor for signed comparison OP RTL. */
11647
11648 static dw_loc_descr_ref
11649 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11650 enum machine_mode mem_mode)
11651 {
11652 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11653 dw_loc_descr_ref op0, op1;
11654 int shift;
11655
11656 if (op_mode == VOIDmode)
11657 op_mode = GET_MODE (XEXP (rtl, 1));
11658 if (op_mode == VOIDmode)
11659 return NULL;
11660
11661 if (dwarf_strict
11662 && (GET_MODE_CLASS (op_mode) != MODE_INT
11663 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11664 return NULL;
11665
11666 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11667 VAR_INIT_STATUS_INITIALIZED);
11668 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11669 VAR_INIT_STATUS_INITIALIZED);
11670
11671 if (op0 == NULL || op1 == NULL)
11672 return NULL;
11673
11674 if (GET_MODE_CLASS (op_mode) != MODE_INT
11675 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11676 return compare_loc_descriptor (op, op0, op1);
11677
11678 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11679 {
11680 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11681 dw_loc_descr_ref cvt;
11682
11683 if (type_die == NULL)
11684 return NULL;
11685 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11686 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11687 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11688 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11689 add_loc_descr (&op0, cvt);
11690 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11691 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11692 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11693 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11694 add_loc_descr (&op1, cvt);
11695 return compare_loc_descriptor (op, op0, op1);
11696 }
11697
11698 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11699 /* For eq/ne, if the operands are known to be zero-extended,
11700 there is no need to do the fancy shifting up. */
11701 if (op == DW_OP_eq || op == DW_OP_ne)
11702 {
11703 dw_loc_descr_ref last0, last1;
11704 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11705 ;
11706 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11707 ;
11708 /* deref_size zero extends, and for constants we can check
11709 whether they are zero extended or not. */
11710 if (((last0->dw_loc_opc == DW_OP_deref_size
11711 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11712 || (CONST_INT_P (XEXP (rtl, 0))
11713 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11714 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11715 && ((last1->dw_loc_opc == DW_OP_deref_size
11716 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11717 || (CONST_INT_P (XEXP (rtl, 1))
11718 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11719 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11720 return compare_loc_descriptor (op, op0, op1);
11721
11722 /* EQ/NE comparison against constant in narrower type than
11723 DWARF2_ADDR_SIZE can be performed either as
11724 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11725 DW_OP_{eq,ne}
11726 or
11727 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11728 DW_OP_{eq,ne}. Pick whatever is shorter. */
11729 if (CONST_INT_P (XEXP (rtl, 1))
11730 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11731 && (size_of_int_loc_descriptor (shift) + 1
11732 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11733 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11734 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11735 & GET_MODE_MASK (op_mode))))
11736 {
11737 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11738 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11739 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11740 & GET_MODE_MASK (op_mode));
11741 return compare_loc_descriptor (op, op0, op1);
11742 }
11743 }
11744 add_loc_descr (&op0, int_loc_descriptor (shift));
11745 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11746 if (CONST_INT_P (XEXP (rtl, 1)))
11747 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11748 else
11749 {
11750 add_loc_descr (&op1, int_loc_descriptor (shift));
11751 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11752 }
11753 return compare_loc_descriptor (op, op0, op1);
11754 }
11755
11756 /* Return location descriptor for unsigned comparison OP RTL. */
11757
11758 static dw_loc_descr_ref
11759 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11760 enum machine_mode mem_mode)
11761 {
11762 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11763 dw_loc_descr_ref op0, op1;
11764
11765 if (op_mode == VOIDmode)
11766 op_mode = GET_MODE (XEXP (rtl, 1));
11767 if (op_mode == VOIDmode)
11768 return NULL;
11769 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11770 return NULL;
11771
11772 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11773 return NULL;
11774
11775 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11776 VAR_INIT_STATUS_INITIALIZED);
11777 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11778 VAR_INIT_STATUS_INITIALIZED);
11779
11780 if (op0 == NULL || op1 == NULL)
11781 return NULL;
11782
11783 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11784 {
11785 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11786 dw_loc_descr_ref last0, last1;
11787 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11788 ;
11789 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11790 ;
11791 if (CONST_INT_P (XEXP (rtl, 0)))
11792 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11793 /* deref_size zero extends, so no need to mask it again. */
11794 else if (last0->dw_loc_opc != DW_OP_deref_size
11795 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11796 {
11797 add_loc_descr (&op0, int_loc_descriptor (mask));
11798 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11799 }
11800 if (CONST_INT_P (XEXP (rtl, 1)))
11801 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11802 /* deref_size zero extends, so no need to mask it again. */
11803 else if (last1->dw_loc_opc != DW_OP_deref_size
11804 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11805 {
11806 add_loc_descr (&op1, int_loc_descriptor (mask));
11807 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11808 }
11809 }
11810 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11811 {
11812 HOST_WIDE_INT bias = 1;
11813 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11814 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11815 if (CONST_INT_P (XEXP (rtl, 1)))
11816 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11817 + INTVAL (XEXP (rtl, 1)));
11818 else
11819 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11820 bias, 0));
11821 }
11822 return compare_loc_descriptor (op, op0, op1);
11823 }
11824
11825 /* Return location descriptor for {U,S}{MIN,MAX}. */
11826
11827 static dw_loc_descr_ref
11828 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11829 enum machine_mode mem_mode)
11830 {
11831 enum dwarf_location_atom op;
11832 dw_loc_descr_ref op0, op1, ret;
11833 dw_loc_descr_ref bra_node, drop_node;
11834
11835 if (dwarf_strict
11836 && (GET_MODE_CLASS (mode) != MODE_INT
11837 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11838 return NULL;
11839
11840 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11841 VAR_INIT_STATUS_INITIALIZED);
11842 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11843 VAR_INIT_STATUS_INITIALIZED);
11844
11845 if (op0 == NULL || op1 == NULL)
11846 return NULL;
11847
11848 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11849 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11850 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11851 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11852 {
11853 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11854 {
11855 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11856 add_loc_descr (&op0, int_loc_descriptor (mask));
11857 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11858 add_loc_descr (&op1, int_loc_descriptor (mask));
11859 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11860 }
11861 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11862 {
11863 HOST_WIDE_INT bias = 1;
11864 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11865 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11866 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11867 }
11868 }
11869 else if (GET_MODE_CLASS (mode) == MODE_INT
11870 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11871 {
11872 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11873 add_loc_descr (&op0, int_loc_descriptor (shift));
11874 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11875 add_loc_descr (&op1, int_loc_descriptor (shift));
11876 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11877 }
11878 else if (GET_MODE_CLASS (mode) == MODE_INT
11879 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11880 {
11881 dw_die_ref type_die = base_type_for_mode (mode, 0);
11882 dw_loc_descr_ref cvt;
11883 if (type_die == NULL)
11884 return NULL;
11885 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11886 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11887 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11888 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11889 add_loc_descr (&op0, cvt);
11890 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11891 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11892 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11893 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11894 add_loc_descr (&op1, cvt);
11895 }
11896
11897 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11898 op = DW_OP_lt;
11899 else
11900 op = DW_OP_gt;
11901 ret = op0;
11902 add_loc_descr (&ret, op1);
11903 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11904 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11905 add_loc_descr (&ret, bra_node);
11906 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11907 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11908 add_loc_descr (&ret, drop_node);
11909 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11910 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11911 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11912 && GET_MODE_CLASS (mode) == MODE_INT
11913 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11914 ret = convert_descriptor_to_mode (mode, ret);
11915 return ret;
11916 }
11917
11918 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11919 but after converting arguments to type_die, afterwards
11920 convert back to unsigned. */
11921
11922 static dw_loc_descr_ref
11923 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11924 enum machine_mode mode, enum machine_mode mem_mode)
11925 {
11926 dw_loc_descr_ref cvt, op0, op1;
11927
11928 if (type_die == NULL)
11929 return NULL;
11930 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11931 VAR_INIT_STATUS_INITIALIZED);
11932 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11933 VAR_INIT_STATUS_INITIALIZED);
11934 if (op0 == NULL || op1 == NULL)
11935 return NULL;
11936 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11937 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11938 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11939 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11940 add_loc_descr (&op0, cvt);
11941 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11942 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11943 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11944 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11945 add_loc_descr (&op1, cvt);
11946 add_loc_descr (&op0, op1);
11947 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11948 return convert_descriptor_to_mode (mode, op0);
11949 }
11950
11951 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11952 const0 is DW_OP_lit0 or corresponding typed constant,
11953 const1 is DW_OP_lit1 or corresponding typed constant
11954 and constMSB is constant with just the MSB bit set
11955 for the mode):
11956 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11957 L1: const0 DW_OP_swap
11958 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11959 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11960 L3: DW_OP_drop
11961 L4: DW_OP_nop
11962
11963 CTZ is similar:
11964 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11965 L1: const0 DW_OP_swap
11966 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11967 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11968 L3: DW_OP_drop
11969 L4: DW_OP_nop
11970
11971 FFS is similar:
11972 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11973 L1: const1 DW_OP_swap
11974 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11975 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11976 L3: DW_OP_drop
11977 L4: DW_OP_nop */
11978
11979 static dw_loc_descr_ref
11980 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11981 enum machine_mode mem_mode)
11982 {
11983 dw_loc_descr_ref op0, ret, tmp;
11984 HOST_WIDE_INT valv;
11985 dw_loc_descr_ref l1jump, l1label;
11986 dw_loc_descr_ref l2jump, l2label;
11987 dw_loc_descr_ref l3jump, l3label;
11988 dw_loc_descr_ref l4jump, l4label;
11989 rtx msb;
11990
11991 if (GET_MODE_CLASS (mode) != MODE_INT
11992 || GET_MODE (XEXP (rtl, 0)) != mode)
11993 return NULL;
11994
11995 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11996 VAR_INIT_STATUS_INITIALIZED);
11997 if (op0 == NULL)
11998 return NULL;
11999 ret = op0;
12000 if (GET_CODE (rtl) == CLZ)
12001 {
12002 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12003 valv = GET_MODE_BITSIZE (mode);
12004 }
12005 else if (GET_CODE (rtl) == FFS)
12006 valv = 0;
12007 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12008 valv = GET_MODE_BITSIZE (mode);
12009 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12010 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12011 add_loc_descr (&ret, l1jump);
12012 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12013 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12014 VAR_INIT_STATUS_INITIALIZED);
12015 if (tmp == NULL)
12016 return NULL;
12017 add_loc_descr (&ret, tmp);
12018 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12019 add_loc_descr (&ret, l4jump);
12020 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12021 ? const1_rtx : const0_rtx,
12022 mode, mem_mode,
12023 VAR_INIT_STATUS_INITIALIZED);
12024 if (l1label == NULL)
12025 return NULL;
12026 add_loc_descr (&ret, l1label);
12027 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12028 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12029 add_loc_descr (&ret, l2label);
12030 if (GET_CODE (rtl) != CLZ)
12031 msb = const1_rtx;
12032 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12033 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12034 << (GET_MODE_BITSIZE (mode) - 1));
12035 else
12036 msb = immed_wide_int_const
12037 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12038 GET_MODE_PRECISION (mode)), mode);
12039 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12040 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12041 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12042 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12043 else
12044 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12045 VAR_INIT_STATUS_INITIALIZED);
12046 if (tmp == NULL)
12047 return NULL;
12048 add_loc_descr (&ret, tmp);
12049 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12050 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12051 add_loc_descr (&ret, l3jump);
12052 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12053 VAR_INIT_STATUS_INITIALIZED);
12054 if (tmp == NULL)
12055 return NULL;
12056 add_loc_descr (&ret, tmp);
12057 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12058 ? DW_OP_shl : DW_OP_shr, 0, 0));
12059 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12060 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12061 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12062 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12063 add_loc_descr (&ret, l2jump);
12064 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12065 add_loc_descr (&ret, l3label);
12066 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12067 add_loc_descr (&ret, l4label);
12068 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12069 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12070 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12071 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12072 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12073 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12074 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12075 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12076 return ret;
12077 }
12078
12079 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12080 const1 is DW_OP_lit1 or corresponding typed constant):
12081 const0 DW_OP_swap
12082 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12083 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12084 L2: DW_OP_drop
12085
12086 PARITY is similar:
12087 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12088 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12089 L2: DW_OP_drop */
12090
12091 static dw_loc_descr_ref
12092 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12093 enum machine_mode mem_mode)
12094 {
12095 dw_loc_descr_ref op0, ret, tmp;
12096 dw_loc_descr_ref l1jump, l1label;
12097 dw_loc_descr_ref l2jump, l2label;
12098
12099 if (GET_MODE_CLASS (mode) != MODE_INT
12100 || GET_MODE (XEXP (rtl, 0)) != mode)
12101 return NULL;
12102
12103 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12104 VAR_INIT_STATUS_INITIALIZED);
12105 if (op0 == NULL)
12106 return NULL;
12107 ret = op0;
12108 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12109 VAR_INIT_STATUS_INITIALIZED);
12110 if (tmp == NULL)
12111 return NULL;
12112 add_loc_descr (&ret, tmp);
12113 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12114 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12115 add_loc_descr (&ret, l1label);
12116 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12117 add_loc_descr (&ret, l2jump);
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12119 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12120 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12121 VAR_INIT_STATUS_INITIALIZED);
12122 if (tmp == NULL)
12123 return NULL;
12124 add_loc_descr (&ret, tmp);
12125 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12126 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12127 ? DW_OP_plus : DW_OP_xor, 0, 0));
12128 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12129 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12130 VAR_INIT_STATUS_INITIALIZED);
12131 add_loc_descr (&ret, tmp);
12132 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12133 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12134 add_loc_descr (&ret, l1jump);
12135 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12136 add_loc_descr (&ret, l2label);
12137 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12138 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12139 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12140 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12141 return ret;
12142 }
12143
12144 /* BSWAP (constS is initial shift count, either 56 or 24):
12145 constS const0
12146 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12147 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12148 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12149 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12150 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12151
12152 static dw_loc_descr_ref
12153 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12154 enum machine_mode mem_mode)
12155 {
12156 dw_loc_descr_ref op0, ret, tmp;
12157 dw_loc_descr_ref l1jump, l1label;
12158 dw_loc_descr_ref l2jump, l2label;
12159
12160 if (GET_MODE_CLASS (mode) != MODE_INT
12161 || BITS_PER_UNIT != 8
12162 || (GET_MODE_BITSIZE (mode) != 32
12163 && GET_MODE_BITSIZE (mode) != 64))
12164 return NULL;
12165
12166 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12167 VAR_INIT_STATUS_INITIALIZED);
12168 if (op0 == NULL)
12169 return NULL;
12170
12171 ret = op0;
12172 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12173 mode, mem_mode,
12174 VAR_INIT_STATUS_INITIALIZED);
12175 if (tmp == NULL)
12176 return NULL;
12177 add_loc_descr (&ret, tmp);
12178 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12179 VAR_INIT_STATUS_INITIALIZED);
12180 if (tmp == NULL)
12181 return NULL;
12182 add_loc_descr (&ret, tmp);
12183 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12184 add_loc_descr (&ret, l1label);
12185 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12186 mode, mem_mode,
12187 VAR_INIT_STATUS_INITIALIZED);
12188 add_loc_descr (&ret, tmp);
12189 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12190 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12191 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12192 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12193 VAR_INIT_STATUS_INITIALIZED);
12194 if (tmp == NULL)
12195 return NULL;
12196 add_loc_descr (&ret, tmp);
12197 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12198 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12199 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12200 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12201 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12202 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12203 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12204 VAR_INIT_STATUS_INITIALIZED);
12205 add_loc_descr (&ret, tmp);
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12207 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12208 add_loc_descr (&ret, l2jump);
12209 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12210 VAR_INIT_STATUS_INITIALIZED);
12211 add_loc_descr (&ret, tmp);
12212 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12214 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12215 add_loc_descr (&ret, l1jump);
12216 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12217 add_loc_descr (&ret, l2label);
12218 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12219 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12220 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12221 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12222 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12223 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12224 return ret;
12225 }
12226
12227 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12228 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12229 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12230 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12231
12232 ROTATERT is similar:
12233 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12234 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12235 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12236
12237 static dw_loc_descr_ref
12238 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12239 enum machine_mode mem_mode)
12240 {
12241 rtx rtlop1 = XEXP (rtl, 1);
12242 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12243 int i;
12244
12245 if (GET_MODE_CLASS (mode) != MODE_INT)
12246 return NULL;
12247
12248 if (GET_MODE (rtlop1) != VOIDmode
12249 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12250 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12251 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12252 VAR_INIT_STATUS_INITIALIZED);
12253 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12254 VAR_INIT_STATUS_INITIALIZED);
12255 if (op0 == NULL || op1 == NULL)
12256 return NULL;
12257 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12258 for (i = 0; i < 2; i++)
12259 {
12260 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12261 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12262 mode, mem_mode,
12263 VAR_INIT_STATUS_INITIALIZED);
12264 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12265 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12266 ? DW_OP_const4u
12267 : HOST_BITS_PER_WIDE_INT == 64
12268 ? DW_OP_const8u : DW_OP_constu,
12269 GET_MODE_MASK (mode), 0);
12270 else
12271 mask[i] = NULL;
12272 if (mask[i] == NULL)
12273 return NULL;
12274 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12275 }
12276 ret = op0;
12277 add_loc_descr (&ret, op1);
12278 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12279 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12280 if (GET_CODE (rtl) == ROTATERT)
12281 {
12282 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12283 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12284 GET_MODE_BITSIZE (mode), 0));
12285 }
12286 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12287 if (mask[0] != NULL)
12288 add_loc_descr (&ret, mask[0]);
12289 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12290 if (mask[1] != NULL)
12291 {
12292 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12293 add_loc_descr (&ret, mask[1]);
12294 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12295 }
12296 if (GET_CODE (rtl) == ROTATE)
12297 {
12298 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12299 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12300 GET_MODE_BITSIZE (mode), 0));
12301 }
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12304 return ret;
12305 }
12306
12307 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12308 for DEBUG_PARAMETER_REF RTL. */
12309
12310 static dw_loc_descr_ref
12311 parameter_ref_descriptor (rtx rtl)
12312 {
12313 dw_loc_descr_ref ret;
12314 dw_die_ref ref;
12315
12316 if (dwarf_strict)
12317 return NULL;
12318 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12319 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12320 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12321 if (ref)
12322 {
12323 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12324 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12325 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12326 }
12327 else
12328 {
12329 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12330 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12331 }
12332 return ret;
12333 }
12334
12335 /* The following routine converts the RTL for a variable or parameter
12336 (resident in memory) into an equivalent Dwarf representation of a
12337 mechanism for getting the address of that same variable onto the top of a
12338 hypothetical "address evaluation" stack.
12339
12340 When creating memory location descriptors, we are effectively transforming
12341 the RTL for a memory-resident object into its Dwarf postfix expression
12342 equivalent. This routine recursively descends an RTL tree, turning
12343 it into Dwarf postfix code as it goes.
12344
12345 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12346
12347 MEM_MODE is the mode of the memory reference, needed to handle some
12348 autoincrement addressing modes.
12349
12350 Return 0 if we can't represent the location. */
12351
12352 dw_loc_descr_ref
12353 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12354 enum machine_mode mem_mode,
12355 enum var_init_status initialized)
12356 {
12357 dw_loc_descr_ref mem_loc_result = NULL;
12358 enum dwarf_location_atom op;
12359 dw_loc_descr_ref op0, op1;
12360 rtx inner = NULL_RTX;
12361
12362 if (mode == VOIDmode)
12363 mode = GET_MODE (rtl);
12364
12365 /* Note that for a dynamically sized array, the location we will generate a
12366 description of here will be the lowest numbered location which is
12367 actually within the array. That's *not* necessarily the same as the
12368 zeroth element of the array. */
12369
12370 rtl = targetm.delegitimize_address (rtl);
12371
12372 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12373 return NULL;
12374
12375 switch (GET_CODE (rtl))
12376 {
12377 case POST_INC:
12378 case POST_DEC:
12379 case POST_MODIFY:
12380 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12381
12382 case SUBREG:
12383 /* The case of a subreg may arise when we have a local (register)
12384 variable or a formal (register) parameter which doesn't quite fill
12385 up an entire register. For now, just assume that it is
12386 legitimate to make the Dwarf info refer to the whole register which
12387 contains the given subreg. */
12388 if (!subreg_lowpart_p (rtl))
12389 break;
12390 inner = SUBREG_REG (rtl);
12391 case TRUNCATE:
12392 if (inner == NULL_RTX)
12393 inner = XEXP (rtl, 0);
12394 if (GET_MODE_CLASS (mode) == MODE_INT
12395 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12396 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12397 #ifdef POINTERS_EXTEND_UNSIGNED
12398 || (mode == Pmode && mem_mode != VOIDmode)
12399 #endif
12400 )
12401 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12402 {
12403 mem_loc_result = mem_loc_descriptor (inner,
12404 GET_MODE (inner),
12405 mem_mode, initialized);
12406 break;
12407 }
12408 if (dwarf_strict)
12409 break;
12410 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12411 break;
12412 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12413 && (GET_MODE_CLASS (mode) != MODE_INT
12414 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12415 break;
12416 else
12417 {
12418 dw_die_ref type_die;
12419 dw_loc_descr_ref cvt;
12420
12421 mem_loc_result = mem_loc_descriptor (inner,
12422 GET_MODE (inner),
12423 mem_mode, initialized);
12424 if (mem_loc_result == NULL)
12425 break;
12426 type_die = base_type_for_mode (mode,
12427 GET_MODE_CLASS (mode) == MODE_INT);
12428 if (type_die == NULL)
12429 {
12430 mem_loc_result = NULL;
12431 break;
12432 }
12433 if (GET_MODE_SIZE (mode)
12434 != GET_MODE_SIZE (GET_MODE (inner)))
12435 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12436 else
12437 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12438 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12439 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12440 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12441 add_loc_descr (&mem_loc_result, cvt);
12442 }
12443 break;
12444
12445 case REG:
12446 if (GET_MODE_CLASS (mode) != MODE_INT
12447 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12448 && rtl != arg_pointer_rtx
12449 && rtl != frame_pointer_rtx
12450 #ifdef POINTERS_EXTEND_UNSIGNED
12451 && (mode != Pmode || mem_mode == VOIDmode)
12452 #endif
12453 ))
12454 {
12455 dw_die_ref type_die;
12456 unsigned int dbx_regnum;
12457
12458 if (dwarf_strict)
12459 break;
12460 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12461 break;
12462 type_die = base_type_for_mode (mode,
12463 GET_MODE_CLASS (mode) == MODE_INT);
12464 if (type_die == NULL)
12465 break;
12466
12467 dbx_regnum = dbx_reg_number (rtl);
12468 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12469 break;
12470 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12471 dbx_regnum, 0);
12472 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12473 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12474 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12475 break;
12476 }
12477 /* Whenever a register number forms a part of the description of the
12478 method for calculating the (dynamic) address of a memory resident
12479 object, DWARF rules require the register number be referred to as
12480 a "base register". This distinction is not based in any way upon
12481 what category of register the hardware believes the given register
12482 belongs to. This is strictly DWARF terminology we're dealing with
12483 here. Note that in cases where the location of a memory-resident
12484 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12485 OP_CONST (0)) the actual DWARF location descriptor that we generate
12486 may just be OP_BASEREG (basereg). This may look deceptively like
12487 the object in question was allocated to a register (rather than in
12488 memory) so DWARF consumers need to be aware of the subtle
12489 distinction between OP_REG and OP_BASEREG. */
12490 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12491 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12492 else if (stack_realign_drap
12493 && crtl->drap_reg
12494 && crtl->args.internal_arg_pointer == rtl
12495 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12496 {
12497 /* If RTL is internal_arg_pointer, which has been optimized
12498 out, use DRAP instead. */
12499 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12500 VAR_INIT_STATUS_INITIALIZED);
12501 }
12502 break;
12503
12504 case SIGN_EXTEND:
12505 case ZERO_EXTEND:
12506 if (GET_MODE_CLASS (mode) != MODE_INT)
12507 break;
12508 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12509 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12510 if (op0 == 0)
12511 break;
12512 else if (GET_CODE (rtl) == ZERO_EXTEND
12513 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12514 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12515 < HOST_BITS_PER_WIDE_INT
12516 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12517 to expand zero extend as two shifts instead of
12518 masking. */
12519 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12520 {
12521 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12522 mem_loc_result = op0;
12523 add_loc_descr (&mem_loc_result,
12524 int_loc_descriptor (GET_MODE_MASK (imode)));
12525 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12526 }
12527 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12528 {
12529 int shift = DWARF2_ADDR_SIZE
12530 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12531 shift *= BITS_PER_UNIT;
12532 if (GET_CODE (rtl) == SIGN_EXTEND)
12533 op = DW_OP_shra;
12534 else
12535 op = DW_OP_shr;
12536 mem_loc_result = op0;
12537 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12538 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12539 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12540 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12541 }
12542 else if (!dwarf_strict)
12543 {
12544 dw_die_ref type_die1, type_die2;
12545 dw_loc_descr_ref cvt;
12546
12547 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12548 GET_CODE (rtl) == ZERO_EXTEND);
12549 if (type_die1 == NULL)
12550 break;
12551 type_die2 = base_type_for_mode (mode, 1);
12552 if (type_die2 == NULL)
12553 break;
12554 mem_loc_result = op0;
12555 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12556 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12557 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12558 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12559 add_loc_descr (&mem_loc_result, cvt);
12560 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12561 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12562 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12563 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12564 add_loc_descr (&mem_loc_result, cvt);
12565 }
12566 break;
12567
12568 case MEM:
12569 {
12570 rtx new_rtl = avoid_constant_pool_reference (rtl);
12571 if (new_rtl != rtl)
12572 {
12573 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12574 initialized);
12575 if (mem_loc_result != NULL)
12576 return mem_loc_result;
12577 }
12578 }
12579 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12580 get_address_mode (rtl), mode,
12581 VAR_INIT_STATUS_INITIALIZED);
12582 if (mem_loc_result == NULL)
12583 mem_loc_result = tls_mem_loc_descriptor (rtl);
12584 if (mem_loc_result != NULL)
12585 {
12586 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12587 || GET_MODE_CLASS (mode) != MODE_INT)
12588 {
12589 dw_die_ref type_die;
12590 dw_loc_descr_ref deref;
12591
12592 if (dwarf_strict)
12593 return NULL;
12594 type_die
12595 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12596 if (type_die == NULL)
12597 return NULL;
12598 deref = new_loc_descr (DW_OP_GNU_deref_type,
12599 GET_MODE_SIZE (mode), 0);
12600 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12601 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12602 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12603 add_loc_descr (&mem_loc_result, deref);
12604 }
12605 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12606 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12607 else
12608 add_loc_descr (&mem_loc_result,
12609 new_loc_descr (DW_OP_deref_size,
12610 GET_MODE_SIZE (mode), 0));
12611 }
12612 break;
12613
12614 case LO_SUM:
12615 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12616
12617 case LABEL_REF:
12618 /* Some ports can transform a symbol ref into a label ref, because
12619 the symbol ref is too far away and has to be dumped into a constant
12620 pool. */
12621 case CONST:
12622 case SYMBOL_REF:
12623 if ((GET_MODE_CLASS (mode) != MODE_INT
12624 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12625 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12626 #ifdef POINTERS_EXTEND_UNSIGNED
12627 && (mode != Pmode || mem_mode == VOIDmode)
12628 #endif
12629 ))
12630 break;
12631 if (GET_CODE (rtl) == SYMBOL_REF
12632 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12633 {
12634 dw_loc_descr_ref temp;
12635
12636 /* If this is not defined, we have no way to emit the data. */
12637 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12638 break;
12639
12640 temp = new_addr_loc_descr (rtl, dtprel_true);
12641
12642 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12643 add_loc_descr (&mem_loc_result, temp);
12644
12645 break;
12646 }
12647
12648 if (!const_ok_for_output (rtl))
12649 break;
12650
12651 symref:
12652 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12653 vec_safe_push (used_rtx_array, rtl);
12654 break;
12655
12656 case CONCAT:
12657 case CONCATN:
12658 case VAR_LOCATION:
12659 case DEBUG_IMPLICIT_PTR:
12660 expansion_failed (NULL_TREE, rtl,
12661 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12662 return 0;
12663
12664 case ENTRY_VALUE:
12665 if (dwarf_strict)
12666 return NULL;
12667 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12668 {
12669 if (GET_MODE_CLASS (mode) != MODE_INT
12670 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12671 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12672 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12673 else
12674 {
12675 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12676 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12677 return NULL;
12678 op0 = one_reg_loc_descriptor (dbx_regnum,
12679 VAR_INIT_STATUS_INITIALIZED);
12680 }
12681 }
12682 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12683 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12684 {
12685 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12686 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12687 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12688 return NULL;
12689 }
12690 else
12691 gcc_unreachable ();
12692 if (op0 == NULL)
12693 return NULL;
12694 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12695 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12696 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12697 break;
12698
12699 case DEBUG_PARAMETER_REF:
12700 mem_loc_result = parameter_ref_descriptor (rtl);
12701 break;
12702
12703 case PRE_MODIFY:
12704 /* Extract the PLUS expression nested inside and fall into
12705 PLUS code below. */
12706 rtl = XEXP (rtl, 1);
12707 goto plus;
12708
12709 case PRE_INC:
12710 case PRE_DEC:
12711 /* Turn these into a PLUS expression and fall into the PLUS code
12712 below. */
12713 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12714 gen_int_mode (GET_CODE (rtl) == PRE_INC
12715 ? GET_MODE_UNIT_SIZE (mem_mode)
12716 : -GET_MODE_UNIT_SIZE (mem_mode),
12717 mode));
12718
12719 /* ... fall through ... */
12720
12721 case PLUS:
12722 plus:
12723 if (is_based_loc (rtl)
12724 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12725 || XEXP (rtl, 0) == arg_pointer_rtx
12726 || XEXP (rtl, 0) == frame_pointer_rtx)
12727 && GET_MODE_CLASS (mode) == MODE_INT)
12728 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12729 INTVAL (XEXP (rtl, 1)),
12730 VAR_INIT_STATUS_INITIALIZED);
12731 else
12732 {
12733 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12734 VAR_INIT_STATUS_INITIALIZED);
12735 if (mem_loc_result == 0)
12736 break;
12737
12738 if (CONST_INT_P (XEXP (rtl, 1))
12739 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12740 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12741 else
12742 {
12743 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12744 VAR_INIT_STATUS_INITIALIZED);
12745 if (op1 == 0)
12746 return NULL;
12747 add_loc_descr (&mem_loc_result, op1);
12748 add_loc_descr (&mem_loc_result,
12749 new_loc_descr (DW_OP_plus, 0, 0));
12750 }
12751 }
12752 break;
12753
12754 /* If a pseudo-reg is optimized away, it is possible for it to
12755 be replaced with a MEM containing a multiply or shift. */
12756 case MINUS:
12757 op = DW_OP_minus;
12758 goto do_binop;
12759
12760 case MULT:
12761 op = DW_OP_mul;
12762 goto do_binop;
12763
12764 case DIV:
12765 if (!dwarf_strict
12766 && GET_MODE_CLASS (mode) == MODE_INT
12767 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12768 {
12769 mem_loc_result = typed_binop (DW_OP_div, rtl,
12770 base_type_for_mode (mode, 0),
12771 mode, mem_mode);
12772 break;
12773 }
12774 op = DW_OP_div;
12775 goto do_binop;
12776
12777 case UMOD:
12778 op = DW_OP_mod;
12779 goto do_binop;
12780
12781 case ASHIFT:
12782 op = DW_OP_shl;
12783 goto do_shift;
12784
12785 case ASHIFTRT:
12786 op = DW_OP_shra;
12787 goto do_shift;
12788
12789 case LSHIFTRT:
12790 op = DW_OP_shr;
12791 goto do_shift;
12792
12793 do_shift:
12794 if (GET_MODE_CLASS (mode) != MODE_INT)
12795 break;
12796 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12797 VAR_INIT_STATUS_INITIALIZED);
12798 {
12799 rtx rtlop1 = XEXP (rtl, 1);
12800 if (GET_MODE (rtlop1) != VOIDmode
12801 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12802 < GET_MODE_BITSIZE (mode))
12803 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12804 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12805 VAR_INIT_STATUS_INITIALIZED);
12806 }
12807
12808 if (op0 == 0 || op1 == 0)
12809 break;
12810
12811 mem_loc_result = op0;
12812 add_loc_descr (&mem_loc_result, op1);
12813 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12814 break;
12815
12816 case AND:
12817 op = DW_OP_and;
12818 goto do_binop;
12819
12820 case IOR:
12821 op = DW_OP_or;
12822 goto do_binop;
12823
12824 case XOR:
12825 op = DW_OP_xor;
12826 goto do_binop;
12827
12828 do_binop:
12829 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12830 VAR_INIT_STATUS_INITIALIZED);
12831 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12832 VAR_INIT_STATUS_INITIALIZED);
12833
12834 if (op0 == 0 || op1 == 0)
12835 break;
12836
12837 mem_loc_result = op0;
12838 add_loc_descr (&mem_loc_result, op1);
12839 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12840 break;
12841
12842 case MOD:
12843 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12844 {
12845 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12846 base_type_for_mode (mode, 0),
12847 mode, mem_mode);
12848 break;
12849 }
12850
12851 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12852 VAR_INIT_STATUS_INITIALIZED);
12853 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12854 VAR_INIT_STATUS_INITIALIZED);
12855
12856 if (op0 == 0 || op1 == 0)
12857 break;
12858
12859 mem_loc_result = op0;
12860 add_loc_descr (&mem_loc_result, op1);
12861 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12862 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12863 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12864 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12865 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12866 break;
12867
12868 case UDIV:
12869 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12870 {
12871 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12872 {
12873 op = DW_OP_div;
12874 goto do_binop;
12875 }
12876 mem_loc_result = typed_binop (DW_OP_div, rtl,
12877 base_type_for_mode (mode, 1),
12878 mode, mem_mode);
12879 }
12880 break;
12881
12882 case NOT:
12883 op = DW_OP_not;
12884 goto do_unop;
12885
12886 case ABS:
12887 op = DW_OP_abs;
12888 goto do_unop;
12889
12890 case NEG:
12891 op = DW_OP_neg;
12892 goto do_unop;
12893
12894 do_unop:
12895 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12896 VAR_INIT_STATUS_INITIALIZED);
12897
12898 if (op0 == 0)
12899 break;
12900
12901 mem_loc_result = op0;
12902 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12903 break;
12904
12905 case CONST_INT:
12906 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12907 #ifdef POINTERS_EXTEND_UNSIGNED
12908 || (mode == Pmode
12909 && mem_mode != VOIDmode
12910 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12911 #endif
12912 )
12913 {
12914 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12915 break;
12916 }
12917 if (!dwarf_strict
12918 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12919 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12920 {
12921 dw_die_ref type_die = base_type_for_mode (mode, 1);
12922 enum machine_mode amode;
12923 if (type_die == NULL)
12924 return NULL;
12925 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12926 MODE_INT, 0);
12927 if (INTVAL (rtl) >= 0
12928 && amode != BLKmode
12929 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12930 /* const DW_OP_GNU_convert <XXX> vs.
12931 DW_OP_GNU_const_type <XXX, 1, const>. */
12932 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12933 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12934 {
12935 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12936 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12937 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12938 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12939 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12940 add_loc_descr (&mem_loc_result, op0);
12941 return mem_loc_result;
12942 }
12943 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12944 INTVAL (rtl));
12945 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12946 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12947 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12948 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12949 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12950 else
12951 {
12952 mem_loc_result->dw_loc_oprnd2.val_class
12953 = dw_val_class_const_double;
12954 mem_loc_result->dw_loc_oprnd2.v.val_double
12955 = double_int::from_shwi (INTVAL (rtl));
12956 }
12957 }
12958 break;
12959
12960 case CONST_DOUBLE:
12961 if (!dwarf_strict)
12962 {
12963 dw_die_ref type_die;
12964
12965 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12966 CONST_DOUBLE rtx could represent either a large integer
12967 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12968 the value is always a floating point constant.
12969
12970 When it is an integer, a CONST_DOUBLE is used whenever
12971 the constant requires 2 HWIs to be adequately represented.
12972 We output CONST_DOUBLEs as blocks. */
12973 if (mode == VOIDmode
12974 || (GET_MODE (rtl) == VOIDmode
12975 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12976 break;
12977 type_die = base_type_for_mode (mode,
12978 GET_MODE_CLASS (mode) == MODE_INT);
12979 if (type_die == NULL)
12980 return NULL;
12981 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12982 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12983 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12984 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12985 #if TARGET_SUPPORTS_WIDE_INT == 0
12986 if (!SCALAR_FLOAT_MODE_P (mode))
12987 {
12988 mem_loc_result->dw_loc_oprnd2.val_class
12989 = dw_val_class_const_double;
12990 mem_loc_result->dw_loc_oprnd2.v.val_double
12991 = rtx_to_double_int (rtl);
12992 }
12993 else
12994 #endif
12995 {
12996 unsigned int length = GET_MODE_SIZE (mode);
12997 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12998
12999 insert_float (rtl, array);
13000 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13001 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13002 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13003 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13004 }
13005 }
13006 break;
13007
13008 case CONST_WIDE_INT:
13009 if (!dwarf_strict)
13010 {
13011 dw_die_ref type_die;
13012
13013 type_die = base_type_for_mode (mode,
13014 GET_MODE_CLASS (mode) == MODE_INT);
13015 if (type_die == NULL)
13016 return NULL;
13017 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13018 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13019 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13020 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13021 mem_loc_result->dw_loc_oprnd2.val_class
13022 = dw_val_class_wide_int;
13023 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13024 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13025 }
13026 break;
13027
13028 case EQ:
13029 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13030 break;
13031
13032 case GE:
13033 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13034 break;
13035
13036 case GT:
13037 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13038 break;
13039
13040 case LE:
13041 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13042 break;
13043
13044 case LT:
13045 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13046 break;
13047
13048 case NE:
13049 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13050 break;
13051
13052 case GEU:
13053 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13054 break;
13055
13056 case GTU:
13057 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13058 break;
13059
13060 case LEU:
13061 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13062 break;
13063
13064 case LTU:
13065 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13066 break;
13067
13068 case UMIN:
13069 case UMAX:
13070 if (GET_MODE_CLASS (mode) != MODE_INT)
13071 break;
13072 /* FALLTHRU */
13073 case SMIN:
13074 case SMAX:
13075 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13076 break;
13077
13078 case ZERO_EXTRACT:
13079 case SIGN_EXTRACT:
13080 if (CONST_INT_P (XEXP (rtl, 1))
13081 && CONST_INT_P (XEXP (rtl, 2))
13082 && ((unsigned) INTVAL (XEXP (rtl, 1))
13083 + (unsigned) INTVAL (XEXP (rtl, 2))
13084 <= GET_MODE_BITSIZE (mode))
13085 && GET_MODE_CLASS (mode) == MODE_INT
13086 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13087 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13088 {
13089 int shift, size;
13090 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13091 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13092 if (op0 == 0)
13093 break;
13094 if (GET_CODE (rtl) == SIGN_EXTRACT)
13095 op = DW_OP_shra;
13096 else
13097 op = DW_OP_shr;
13098 mem_loc_result = op0;
13099 size = INTVAL (XEXP (rtl, 1));
13100 shift = INTVAL (XEXP (rtl, 2));
13101 if (BITS_BIG_ENDIAN)
13102 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13103 - shift - size;
13104 if (shift + size != (int) DWARF2_ADDR_SIZE)
13105 {
13106 add_loc_descr (&mem_loc_result,
13107 int_loc_descriptor (DWARF2_ADDR_SIZE
13108 - shift - size));
13109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13110 }
13111 if (size != (int) DWARF2_ADDR_SIZE)
13112 {
13113 add_loc_descr (&mem_loc_result,
13114 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13115 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13116 }
13117 }
13118 break;
13119
13120 case IF_THEN_ELSE:
13121 {
13122 dw_loc_descr_ref op2, bra_node, drop_node;
13123 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13124 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13125 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13126 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13127 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13128 VAR_INIT_STATUS_INITIALIZED);
13129 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13130 VAR_INIT_STATUS_INITIALIZED);
13131 if (op0 == NULL || op1 == NULL || op2 == NULL)
13132 break;
13133
13134 mem_loc_result = op1;
13135 add_loc_descr (&mem_loc_result, op2);
13136 add_loc_descr (&mem_loc_result, op0);
13137 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13138 add_loc_descr (&mem_loc_result, bra_node);
13139 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13140 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13141 add_loc_descr (&mem_loc_result, drop_node);
13142 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13143 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13144 }
13145 break;
13146
13147 case FLOAT_EXTEND:
13148 case FLOAT_TRUNCATE:
13149 case FLOAT:
13150 case UNSIGNED_FLOAT:
13151 case FIX:
13152 case UNSIGNED_FIX:
13153 if (!dwarf_strict)
13154 {
13155 dw_die_ref type_die;
13156 dw_loc_descr_ref cvt;
13157
13158 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13159 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13160 if (op0 == NULL)
13161 break;
13162 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13163 && (GET_CODE (rtl) == FLOAT
13164 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13165 <= DWARF2_ADDR_SIZE))
13166 {
13167 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13168 GET_CODE (rtl) == UNSIGNED_FLOAT);
13169 if (type_die == NULL)
13170 break;
13171 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13172 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13173 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13174 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13175 add_loc_descr (&op0, cvt);
13176 }
13177 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13178 if (type_die == NULL)
13179 break;
13180 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13181 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13182 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13183 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13184 add_loc_descr (&op0, cvt);
13185 if (GET_MODE_CLASS (mode) == MODE_INT
13186 && (GET_CODE (rtl) == FIX
13187 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13188 {
13189 op0 = convert_descriptor_to_mode (mode, op0);
13190 if (op0 == NULL)
13191 break;
13192 }
13193 mem_loc_result = op0;
13194 }
13195 break;
13196
13197 case CLZ:
13198 case CTZ:
13199 case FFS:
13200 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13201 break;
13202
13203 case POPCOUNT:
13204 case PARITY:
13205 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13206 break;
13207
13208 case BSWAP:
13209 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13210 break;
13211
13212 case ROTATE:
13213 case ROTATERT:
13214 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13215 break;
13216
13217 case COMPARE:
13218 /* In theory, we could implement the above. */
13219 /* DWARF cannot represent the unsigned compare operations
13220 natively. */
13221 case SS_MULT:
13222 case US_MULT:
13223 case SS_DIV:
13224 case US_DIV:
13225 case SS_PLUS:
13226 case US_PLUS:
13227 case SS_MINUS:
13228 case US_MINUS:
13229 case SS_NEG:
13230 case US_NEG:
13231 case SS_ABS:
13232 case SS_ASHIFT:
13233 case US_ASHIFT:
13234 case SS_TRUNCATE:
13235 case US_TRUNCATE:
13236 case UNORDERED:
13237 case ORDERED:
13238 case UNEQ:
13239 case UNGE:
13240 case UNGT:
13241 case UNLE:
13242 case UNLT:
13243 case LTGT:
13244 case FRACT_CONVERT:
13245 case UNSIGNED_FRACT_CONVERT:
13246 case SAT_FRACT:
13247 case UNSIGNED_SAT_FRACT:
13248 case SQRT:
13249 case ASM_OPERANDS:
13250 case VEC_MERGE:
13251 case VEC_SELECT:
13252 case VEC_CONCAT:
13253 case VEC_DUPLICATE:
13254 case UNSPEC:
13255 case HIGH:
13256 case FMA:
13257 case STRICT_LOW_PART:
13258 case CONST_VECTOR:
13259 case CONST_FIXED:
13260 case CLRSB:
13261 case CLOBBER:
13262 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13263 can't express it in the debug info. This can happen e.g. with some
13264 TLS UNSPECs. */
13265 break;
13266
13267 case CONST_STRING:
13268 resolve_one_addr (&rtl);
13269 goto symref;
13270
13271 default:
13272 #ifdef ENABLE_CHECKING
13273 print_rtl (stderr, rtl);
13274 gcc_unreachable ();
13275 #else
13276 break;
13277 #endif
13278 }
13279
13280 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13281 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13282
13283 return mem_loc_result;
13284 }
13285
13286 /* Return a descriptor that describes the concatenation of two locations.
13287 This is typically a complex variable. */
13288
13289 static dw_loc_descr_ref
13290 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13291 {
13292 dw_loc_descr_ref cc_loc_result = NULL;
13293 dw_loc_descr_ref x0_ref
13294 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13295 dw_loc_descr_ref x1_ref
13296 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13297
13298 if (x0_ref == 0 || x1_ref == 0)
13299 return 0;
13300
13301 cc_loc_result = x0_ref;
13302 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13303
13304 add_loc_descr (&cc_loc_result, x1_ref);
13305 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13306
13307 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13308 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13309
13310 return cc_loc_result;
13311 }
13312
13313 /* Return a descriptor that describes the concatenation of N
13314 locations. */
13315
13316 static dw_loc_descr_ref
13317 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13318 {
13319 unsigned int i;
13320 dw_loc_descr_ref cc_loc_result = NULL;
13321 unsigned int n = XVECLEN (concatn, 0);
13322
13323 for (i = 0; i < n; ++i)
13324 {
13325 dw_loc_descr_ref ref;
13326 rtx x = XVECEXP (concatn, 0, i);
13327
13328 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13329 if (ref == NULL)
13330 return NULL;
13331
13332 add_loc_descr (&cc_loc_result, ref);
13333 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13334 }
13335
13336 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13337 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13338
13339 return cc_loc_result;
13340 }
13341
13342 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13343 for DEBUG_IMPLICIT_PTR RTL. */
13344
13345 static dw_loc_descr_ref
13346 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13347 {
13348 dw_loc_descr_ref ret;
13349 dw_die_ref ref;
13350
13351 if (dwarf_strict)
13352 return NULL;
13353 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13354 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13355 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13356 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13357 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13358 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13359 if (ref)
13360 {
13361 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13362 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13363 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13364 }
13365 else
13366 {
13367 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13368 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13369 }
13370 return ret;
13371 }
13372
13373 /* Output a proper Dwarf location descriptor for a variable or parameter
13374 which is either allocated in a register or in a memory location. For a
13375 register, we just generate an OP_REG and the register number. For a
13376 memory location we provide a Dwarf postfix expression describing how to
13377 generate the (dynamic) address of the object onto the address stack.
13378
13379 MODE is mode of the decl if this loc_descriptor is going to be used in
13380 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13381 allowed, VOIDmode otherwise.
13382
13383 If we don't know how to describe it, return 0. */
13384
13385 static dw_loc_descr_ref
13386 loc_descriptor (rtx rtl, enum machine_mode mode,
13387 enum var_init_status initialized)
13388 {
13389 dw_loc_descr_ref loc_result = NULL;
13390
13391 switch (GET_CODE (rtl))
13392 {
13393 case SUBREG:
13394 /* The case of a subreg may arise when we have a local (register)
13395 variable or a formal (register) parameter which doesn't quite fill
13396 up an entire register. For now, just assume that it is
13397 legitimate to make the Dwarf info refer to the whole register which
13398 contains the given subreg. */
13399 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13400 loc_result = loc_descriptor (SUBREG_REG (rtl),
13401 GET_MODE (SUBREG_REG (rtl)), initialized);
13402 else
13403 goto do_default;
13404 break;
13405
13406 case REG:
13407 loc_result = reg_loc_descriptor (rtl, initialized);
13408 break;
13409
13410 case MEM:
13411 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13412 GET_MODE (rtl), initialized);
13413 if (loc_result == NULL)
13414 loc_result = tls_mem_loc_descriptor (rtl);
13415 if (loc_result == NULL)
13416 {
13417 rtx new_rtl = avoid_constant_pool_reference (rtl);
13418 if (new_rtl != rtl)
13419 loc_result = loc_descriptor (new_rtl, mode, initialized);
13420 }
13421 break;
13422
13423 case CONCAT:
13424 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13425 initialized);
13426 break;
13427
13428 case CONCATN:
13429 loc_result = concatn_loc_descriptor (rtl, initialized);
13430 break;
13431
13432 case VAR_LOCATION:
13433 /* Single part. */
13434 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13435 {
13436 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13437 if (GET_CODE (loc) == EXPR_LIST)
13438 loc = XEXP (loc, 0);
13439 loc_result = loc_descriptor (loc, mode, initialized);
13440 break;
13441 }
13442
13443 rtl = XEXP (rtl, 1);
13444 /* FALLTHRU */
13445
13446 case PARALLEL:
13447 {
13448 rtvec par_elems = XVEC (rtl, 0);
13449 int num_elem = GET_NUM_ELEM (par_elems);
13450 enum machine_mode mode;
13451 int i;
13452
13453 /* Create the first one, so we have something to add to. */
13454 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13455 VOIDmode, initialized);
13456 if (loc_result == NULL)
13457 return NULL;
13458 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13459 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13460 for (i = 1; i < num_elem; i++)
13461 {
13462 dw_loc_descr_ref temp;
13463
13464 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13465 VOIDmode, initialized);
13466 if (temp == NULL)
13467 return NULL;
13468 add_loc_descr (&loc_result, temp);
13469 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13470 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13471 }
13472 }
13473 break;
13474
13475 case CONST_INT:
13476 if (mode != VOIDmode && mode != BLKmode)
13477 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13478 INTVAL (rtl));
13479 break;
13480
13481 case CONST_DOUBLE:
13482 if (mode == VOIDmode)
13483 mode = GET_MODE (rtl);
13484
13485 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13486 {
13487 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13488
13489 /* Note that a CONST_DOUBLE rtx could represent either an integer
13490 or a floating-point constant. A CONST_DOUBLE is used whenever
13491 the constant requires more than one word in order to be
13492 adequately represented. We output CONST_DOUBLEs as blocks. */
13493 loc_result = new_loc_descr (DW_OP_implicit_value,
13494 GET_MODE_SIZE (mode), 0);
13495 #if TARGET_SUPPORTS_WIDE_INT == 0
13496 if (!SCALAR_FLOAT_MODE_P (mode))
13497 {
13498 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13499 loc_result->dw_loc_oprnd2.v.val_double
13500 = rtx_to_double_int (rtl);
13501 }
13502 else
13503 #endif
13504 {
13505 unsigned int length = GET_MODE_SIZE (mode);
13506 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13507
13508 insert_float (rtl, array);
13509 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13510 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13511 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13512 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13513 }
13514 }
13515 break;
13516
13517 case CONST_WIDE_INT:
13518 if (mode == VOIDmode)
13519 mode = GET_MODE (rtl);
13520
13521 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13522 {
13523 loc_result = new_loc_descr (DW_OP_implicit_value,
13524 GET_MODE_SIZE (mode), 0);
13525 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13526 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13527 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13528 }
13529 break;
13530
13531 case CONST_VECTOR:
13532 if (mode == VOIDmode)
13533 mode = GET_MODE (rtl);
13534
13535 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13536 {
13537 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13538 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13539 unsigned char *array
13540 = ggc_vec_alloc<unsigned char> (length * elt_size);
13541 unsigned int i;
13542 unsigned char *p;
13543 enum machine_mode imode = GET_MODE_INNER (mode);
13544
13545 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13546 switch (GET_MODE_CLASS (mode))
13547 {
13548 case MODE_VECTOR_INT:
13549 for (i = 0, p = array; i < length; i++, p += elt_size)
13550 {
13551 rtx elt = CONST_VECTOR_ELT (rtl, i);
13552 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13553 }
13554 break;
13555
13556 case MODE_VECTOR_FLOAT:
13557 for (i = 0, p = array; i < length; i++, p += elt_size)
13558 {
13559 rtx elt = CONST_VECTOR_ELT (rtl, i);
13560 insert_float (elt, p);
13561 }
13562 break;
13563
13564 default:
13565 gcc_unreachable ();
13566 }
13567
13568 loc_result = new_loc_descr (DW_OP_implicit_value,
13569 length * elt_size, 0);
13570 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13571 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13572 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13573 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13574 }
13575 break;
13576
13577 case CONST:
13578 if (mode == VOIDmode
13579 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13580 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13581 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13582 {
13583 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13584 break;
13585 }
13586 /* FALLTHROUGH */
13587 case SYMBOL_REF:
13588 if (!const_ok_for_output (rtl))
13589 break;
13590 case LABEL_REF:
13591 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13592 && (dwarf_version >= 4 || !dwarf_strict))
13593 {
13594 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13595 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13596 vec_safe_push (used_rtx_array, rtl);
13597 }
13598 break;
13599
13600 case DEBUG_IMPLICIT_PTR:
13601 loc_result = implicit_ptr_descriptor (rtl, 0);
13602 break;
13603
13604 case PLUS:
13605 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13606 && CONST_INT_P (XEXP (rtl, 1)))
13607 {
13608 loc_result
13609 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13610 break;
13611 }
13612 /* FALLTHRU */
13613 do_default:
13614 default:
13615 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13616 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13617 && dwarf_version >= 4)
13618 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13619 {
13620 /* Value expression. */
13621 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13622 if (loc_result)
13623 add_loc_descr (&loc_result,
13624 new_loc_descr (DW_OP_stack_value, 0, 0));
13625 }
13626 break;
13627 }
13628
13629 return loc_result;
13630 }
13631
13632 /* We need to figure out what section we should use as the base for the
13633 address ranges where a given location is valid.
13634 1. If this particular DECL has a section associated with it, use that.
13635 2. If this function has a section associated with it, use that.
13636 3. Otherwise, use the text section.
13637 XXX: If you split a variable across multiple sections, we won't notice. */
13638
13639 static const char *
13640 secname_for_decl (const_tree decl)
13641 {
13642 const char *secname;
13643
13644 if (VAR_OR_FUNCTION_DECL_P (decl)
13645 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13646 && DECL_SECTION_NAME (decl))
13647 secname = DECL_SECTION_NAME (decl);
13648 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13649 secname = DECL_SECTION_NAME (current_function_decl);
13650 else if (cfun && in_cold_section_p)
13651 secname = crtl->subsections.cold_section_label;
13652 else
13653 secname = text_section_label;
13654
13655 return secname;
13656 }
13657
13658 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13659
13660 static bool
13661 decl_by_reference_p (tree decl)
13662 {
13663 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13664 || TREE_CODE (decl) == VAR_DECL)
13665 && DECL_BY_REFERENCE (decl));
13666 }
13667
13668 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13669 for VARLOC. */
13670
13671 static dw_loc_descr_ref
13672 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13673 enum var_init_status initialized)
13674 {
13675 int have_address = 0;
13676 dw_loc_descr_ref descr;
13677 enum machine_mode mode;
13678
13679 if (want_address != 2)
13680 {
13681 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13682 /* Single part. */
13683 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13684 {
13685 varloc = PAT_VAR_LOCATION_LOC (varloc);
13686 if (GET_CODE (varloc) == EXPR_LIST)
13687 varloc = XEXP (varloc, 0);
13688 mode = GET_MODE (varloc);
13689 if (MEM_P (varloc))
13690 {
13691 rtx addr = XEXP (varloc, 0);
13692 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13693 mode, initialized);
13694 if (descr)
13695 have_address = 1;
13696 else
13697 {
13698 rtx x = avoid_constant_pool_reference (varloc);
13699 if (x != varloc)
13700 descr = mem_loc_descriptor (x, mode, VOIDmode,
13701 initialized);
13702 }
13703 }
13704 else
13705 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13706 }
13707 else
13708 return 0;
13709 }
13710 else
13711 {
13712 if (GET_CODE (varloc) == VAR_LOCATION)
13713 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13714 else
13715 mode = DECL_MODE (loc);
13716 descr = loc_descriptor (varloc, mode, initialized);
13717 have_address = 1;
13718 }
13719
13720 if (!descr)
13721 return 0;
13722
13723 if (want_address == 2 && !have_address
13724 && (dwarf_version >= 4 || !dwarf_strict))
13725 {
13726 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13727 {
13728 expansion_failed (loc, NULL_RTX,
13729 "DWARF address size mismatch");
13730 return 0;
13731 }
13732 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13733 have_address = 1;
13734 }
13735 /* Show if we can't fill the request for an address. */
13736 if (want_address && !have_address)
13737 {
13738 expansion_failed (loc, NULL_RTX,
13739 "Want address and only have value");
13740 return 0;
13741 }
13742
13743 /* If we've got an address and don't want one, dereference. */
13744 if (!want_address && have_address)
13745 {
13746 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13747 enum dwarf_location_atom op;
13748
13749 if (size > DWARF2_ADDR_SIZE || size == -1)
13750 {
13751 expansion_failed (loc, NULL_RTX,
13752 "DWARF address size mismatch");
13753 return 0;
13754 }
13755 else if (size == DWARF2_ADDR_SIZE)
13756 op = DW_OP_deref;
13757 else
13758 op = DW_OP_deref_size;
13759
13760 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13761 }
13762
13763 return descr;
13764 }
13765
13766 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13767 if it is not possible. */
13768
13769 static dw_loc_descr_ref
13770 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13771 {
13772 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13773 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13774 else if (dwarf_version >= 3 || !dwarf_strict)
13775 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13776 else
13777 return NULL;
13778 }
13779
13780 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13781 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13782
13783 static dw_loc_descr_ref
13784 dw_sra_loc_expr (tree decl, rtx loc)
13785 {
13786 rtx p;
13787 unsigned int padsize = 0;
13788 dw_loc_descr_ref descr, *descr_tail;
13789 unsigned HOST_WIDE_INT decl_size;
13790 rtx varloc;
13791 enum var_init_status initialized;
13792
13793 if (DECL_SIZE (decl) == NULL
13794 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13795 return NULL;
13796
13797 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13798 descr = NULL;
13799 descr_tail = &descr;
13800
13801 for (p = loc; p; p = XEXP (p, 1))
13802 {
13803 unsigned int bitsize = decl_piece_bitsize (p);
13804 rtx loc_note = *decl_piece_varloc_ptr (p);
13805 dw_loc_descr_ref cur_descr;
13806 dw_loc_descr_ref *tail, last = NULL;
13807 unsigned int opsize = 0;
13808
13809 if (loc_note == NULL_RTX
13810 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13811 {
13812 padsize += bitsize;
13813 continue;
13814 }
13815 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13816 varloc = NOTE_VAR_LOCATION (loc_note);
13817 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13818 if (cur_descr == NULL)
13819 {
13820 padsize += bitsize;
13821 continue;
13822 }
13823
13824 /* Check that cur_descr either doesn't use
13825 DW_OP_*piece operations, or their sum is equal
13826 to bitsize. Otherwise we can't embed it. */
13827 for (tail = &cur_descr; *tail != NULL;
13828 tail = &(*tail)->dw_loc_next)
13829 if ((*tail)->dw_loc_opc == DW_OP_piece)
13830 {
13831 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13832 * BITS_PER_UNIT;
13833 last = *tail;
13834 }
13835 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13836 {
13837 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13838 last = *tail;
13839 }
13840
13841 if (last != NULL && opsize != bitsize)
13842 {
13843 padsize += bitsize;
13844 /* Discard the current piece of the descriptor and release any
13845 addr_table entries it uses. */
13846 remove_loc_list_addr_table_entries (cur_descr);
13847 continue;
13848 }
13849
13850 /* If there is a hole, add DW_OP_*piece after empty DWARF
13851 expression, which means that those bits are optimized out. */
13852 if (padsize)
13853 {
13854 if (padsize > decl_size)
13855 {
13856 remove_loc_list_addr_table_entries (cur_descr);
13857 goto discard_descr;
13858 }
13859 decl_size -= padsize;
13860 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13861 if (*descr_tail == NULL)
13862 {
13863 remove_loc_list_addr_table_entries (cur_descr);
13864 goto discard_descr;
13865 }
13866 descr_tail = &(*descr_tail)->dw_loc_next;
13867 padsize = 0;
13868 }
13869 *descr_tail = cur_descr;
13870 descr_tail = tail;
13871 if (bitsize > decl_size)
13872 goto discard_descr;
13873 decl_size -= bitsize;
13874 if (last == NULL)
13875 {
13876 HOST_WIDE_INT offset = 0;
13877 if (GET_CODE (varloc) == VAR_LOCATION
13878 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13879 {
13880 varloc = PAT_VAR_LOCATION_LOC (varloc);
13881 if (GET_CODE (varloc) == EXPR_LIST)
13882 varloc = XEXP (varloc, 0);
13883 }
13884 do
13885 {
13886 if (GET_CODE (varloc) == CONST
13887 || GET_CODE (varloc) == SIGN_EXTEND
13888 || GET_CODE (varloc) == ZERO_EXTEND)
13889 varloc = XEXP (varloc, 0);
13890 else if (GET_CODE (varloc) == SUBREG)
13891 varloc = SUBREG_REG (varloc);
13892 else
13893 break;
13894 }
13895 while (1);
13896 /* DW_OP_bit_size offset should be zero for register
13897 or implicit location descriptions and empty location
13898 descriptions, but for memory addresses needs big endian
13899 adjustment. */
13900 if (MEM_P (varloc))
13901 {
13902 unsigned HOST_WIDE_INT memsize
13903 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13904 if (memsize != bitsize)
13905 {
13906 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13907 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13908 goto discard_descr;
13909 if (memsize < bitsize)
13910 goto discard_descr;
13911 if (BITS_BIG_ENDIAN)
13912 offset = memsize - bitsize;
13913 }
13914 }
13915
13916 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13917 if (*descr_tail == NULL)
13918 goto discard_descr;
13919 descr_tail = &(*descr_tail)->dw_loc_next;
13920 }
13921 }
13922
13923 /* If there were any non-empty expressions, add padding till the end of
13924 the decl. */
13925 if (descr != NULL && decl_size != 0)
13926 {
13927 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13928 if (*descr_tail == NULL)
13929 goto discard_descr;
13930 }
13931 return descr;
13932
13933 discard_descr:
13934 /* Discard the descriptor and release any addr_table entries it uses. */
13935 remove_loc_list_addr_table_entries (descr);
13936 return NULL;
13937 }
13938
13939 /* Return the dwarf representation of the location list LOC_LIST of
13940 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13941 function. */
13942
13943 static dw_loc_list_ref
13944 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13945 {
13946 const char *endname, *secname;
13947 rtx varloc;
13948 enum var_init_status initialized;
13949 struct var_loc_node *node;
13950 dw_loc_descr_ref descr;
13951 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13952 dw_loc_list_ref list = NULL;
13953 dw_loc_list_ref *listp = &list;
13954
13955 /* Now that we know what section we are using for a base,
13956 actually construct the list of locations.
13957 The first location information is what is passed to the
13958 function that creates the location list, and the remaining
13959 locations just get added on to that list.
13960 Note that we only know the start address for a location
13961 (IE location changes), so to build the range, we use
13962 the range [current location start, next location start].
13963 This means we have to special case the last node, and generate
13964 a range of [last location start, end of function label]. */
13965
13966 secname = secname_for_decl (decl);
13967
13968 for (node = loc_list->first; node; node = node->next)
13969 if (GET_CODE (node->loc) == EXPR_LIST
13970 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13971 {
13972 if (GET_CODE (node->loc) == EXPR_LIST)
13973 {
13974 /* This requires DW_OP_{,bit_}piece, which is not usable
13975 inside DWARF expressions. */
13976 if (want_address != 2)
13977 continue;
13978 descr = dw_sra_loc_expr (decl, node->loc);
13979 if (descr == NULL)
13980 continue;
13981 }
13982 else
13983 {
13984 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13985 varloc = NOTE_VAR_LOCATION (node->loc);
13986 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13987 }
13988 if (descr)
13989 {
13990 bool range_across_switch = false;
13991 /* If section switch happens in between node->label
13992 and node->next->label (or end of function) and
13993 we can't emit it as a single entry list,
13994 emit two ranges, first one ending at the end
13995 of first partition and second one starting at the
13996 beginning of second partition. */
13997 if (node == loc_list->last_before_switch
13998 && (node != loc_list->first || loc_list->first->next)
13999 && current_function_decl)
14000 {
14001 endname = cfun->fde->dw_fde_end;
14002 range_across_switch = true;
14003 }
14004 /* The variable has a location between NODE->LABEL and
14005 NODE->NEXT->LABEL. */
14006 else if (node->next)
14007 endname = node->next->label;
14008 /* If the variable has a location at the last label
14009 it keeps its location until the end of function. */
14010 else if (!current_function_decl)
14011 endname = text_end_label;
14012 else
14013 {
14014 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14015 current_function_funcdef_no);
14016 endname = ggc_strdup (label_id);
14017 }
14018
14019 *listp = new_loc_list (descr, node->label, endname, secname);
14020 if (TREE_CODE (decl) == PARM_DECL
14021 && node == loc_list->first
14022 && NOTE_P (node->loc)
14023 && strcmp (node->label, endname) == 0)
14024 (*listp)->force = true;
14025 listp = &(*listp)->dw_loc_next;
14026
14027 if (range_across_switch)
14028 {
14029 if (GET_CODE (node->loc) == EXPR_LIST)
14030 descr = dw_sra_loc_expr (decl, node->loc);
14031 else
14032 {
14033 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14034 varloc = NOTE_VAR_LOCATION (node->loc);
14035 descr = dw_loc_list_1 (decl, varloc, want_address,
14036 initialized);
14037 }
14038 gcc_assert (descr);
14039 /* The variable has a location between NODE->LABEL and
14040 NODE->NEXT->LABEL. */
14041 if (node->next)
14042 endname = node->next->label;
14043 else
14044 endname = cfun->fde->dw_fde_second_end;
14045 *listp = new_loc_list (descr,
14046 cfun->fde->dw_fde_second_begin,
14047 endname, secname);
14048 listp = &(*listp)->dw_loc_next;
14049 }
14050 }
14051 }
14052
14053 /* Try to avoid the overhead of a location list emitting a location
14054 expression instead, but only if we didn't have more than one
14055 location entry in the first place. If some entries were not
14056 representable, we don't want to pretend a single entry that was
14057 applies to the entire scope in which the variable is
14058 available. */
14059 if (list && loc_list->first->next)
14060 gen_llsym (list);
14061
14062 return list;
14063 }
14064
14065 /* Return if the loc_list has only single element and thus can be represented
14066 as location description. */
14067
14068 static bool
14069 single_element_loc_list_p (dw_loc_list_ref list)
14070 {
14071 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14072 return !list->ll_symbol;
14073 }
14074
14075 /* To each location in list LIST add loc descr REF. */
14076
14077 static void
14078 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14079 {
14080 dw_loc_descr_ref copy;
14081 add_loc_descr (&list->expr, ref);
14082 list = list->dw_loc_next;
14083 while (list)
14084 {
14085 copy = ggc_alloc<dw_loc_descr_node> ();
14086 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14087 add_loc_descr (&list->expr, copy);
14088 while (copy->dw_loc_next)
14089 {
14090 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14091 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14092 copy->dw_loc_next = new_copy;
14093 copy = new_copy;
14094 }
14095 list = list->dw_loc_next;
14096 }
14097 }
14098
14099 /* Given two lists RET and LIST
14100 produce location list that is result of adding expression in LIST
14101 to expression in RET on each position in program.
14102 Might be destructive on both RET and LIST.
14103
14104 TODO: We handle only simple cases of RET or LIST having at most one
14105 element. General case would inolve sorting the lists in program order
14106 and merging them that will need some additional work.
14107 Adding that will improve quality of debug info especially for SRA-ed
14108 structures. */
14109
14110 static void
14111 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14112 {
14113 if (!list)
14114 return;
14115 if (!*ret)
14116 {
14117 *ret = list;
14118 return;
14119 }
14120 if (!list->dw_loc_next)
14121 {
14122 add_loc_descr_to_each (*ret, list->expr);
14123 return;
14124 }
14125 if (!(*ret)->dw_loc_next)
14126 {
14127 add_loc_descr_to_each (list, (*ret)->expr);
14128 *ret = list;
14129 return;
14130 }
14131 expansion_failed (NULL_TREE, NULL_RTX,
14132 "Don't know how to merge two non-trivial"
14133 " location lists.\n");
14134 *ret = NULL;
14135 return;
14136 }
14137
14138 /* LOC is constant expression. Try a luck, look it up in constant
14139 pool and return its loc_descr of its address. */
14140
14141 static dw_loc_descr_ref
14142 cst_pool_loc_descr (tree loc)
14143 {
14144 /* Get an RTL for this, if something has been emitted. */
14145 rtx rtl = lookup_constant_def (loc);
14146
14147 if (!rtl || !MEM_P (rtl))
14148 {
14149 gcc_assert (!rtl);
14150 return 0;
14151 }
14152 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14153
14154 /* TODO: We might get more coverage if we was actually delaying expansion
14155 of all expressions till end of compilation when constant pools are fully
14156 populated. */
14157 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14158 {
14159 expansion_failed (loc, NULL_RTX,
14160 "CST value in contant pool but not marked.");
14161 return 0;
14162 }
14163 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14164 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14165 }
14166
14167 /* Return dw_loc_list representing address of addr_expr LOC
14168 by looking for inner INDIRECT_REF expression and turning
14169 it into simple arithmetics. */
14170
14171 static dw_loc_list_ref
14172 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14173 {
14174 tree obj, offset;
14175 HOST_WIDE_INT bitsize, bitpos, bytepos;
14176 enum machine_mode mode;
14177 int unsignedp, volatilep = 0;
14178 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14179
14180 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14181 &bitsize, &bitpos, &offset, &mode,
14182 &unsignedp, &volatilep, false);
14183 STRIP_NOPS (obj);
14184 if (bitpos % BITS_PER_UNIT)
14185 {
14186 expansion_failed (loc, NULL_RTX, "bitfield access");
14187 return 0;
14188 }
14189 if (!INDIRECT_REF_P (obj))
14190 {
14191 expansion_failed (obj,
14192 NULL_RTX, "no indirect ref in inner refrence");
14193 return 0;
14194 }
14195 if (!offset && !bitpos)
14196 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14197 else if (toplev
14198 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14199 && (dwarf_version >= 4 || !dwarf_strict))
14200 {
14201 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14202 if (!list_ret)
14203 return 0;
14204 if (offset)
14205 {
14206 /* Variable offset. */
14207 list_ret1 = loc_list_from_tree (offset, 0);
14208 if (list_ret1 == 0)
14209 return 0;
14210 add_loc_list (&list_ret, list_ret1);
14211 if (!list_ret)
14212 return 0;
14213 add_loc_descr_to_each (list_ret,
14214 new_loc_descr (DW_OP_plus, 0, 0));
14215 }
14216 bytepos = bitpos / BITS_PER_UNIT;
14217 if (bytepos > 0)
14218 add_loc_descr_to_each (list_ret,
14219 new_loc_descr (DW_OP_plus_uconst,
14220 bytepos, 0));
14221 else if (bytepos < 0)
14222 loc_list_plus_const (list_ret, bytepos);
14223 add_loc_descr_to_each (list_ret,
14224 new_loc_descr (DW_OP_stack_value, 0, 0));
14225 }
14226 return list_ret;
14227 }
14228
14229
14230 /* Generate Dwarf location list representing LOC.
14231 If WANT_ADDRESS is false, expression computing LOC will be computed
14232 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14233 if WANT_ADDRESS is 2, expression computing address useable in location
14234 will be returned (i.e. DW_OP_reg can be used
14235 to refer to register values). */
14236
14237 static dw_loc_list_ref
14238 loc_list_from_tree (tree loc, int want_address)
14239 {
14240 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14241 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14242 int have_address = 0;
14243 enum dwarf_location_atom op;
14244
14245 /* ??? Most of the time we do not take proper care for sign/zero
14246 extending the values properly. Hopefully this won't be a real
14247 problem... */
14248
14249 switch (TREE_CODE (loc))
14250 {
14251 case ERROR_MARK:
14252 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14253 return 0;
14254
14255 case PLACEHOLDER_EXPR:
14256 /* This case involves extracting fields from an object to determine the
14257 position of other fields. We don't try to encode this here. The
14258 only user of this is Ada, which encodes the needed information using
14259 the names of types. */
14260 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14261 return 0;
14262
14263 case CALL_EXPR:
14264 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14265 /* There are no opcodes for these operations. */
14266 return 0;
14267
14268 case PREINCREMENT_EXPR:
14269 case PREDECREMENT_EXPR:
14270 case POSTINCREMENT_EXPR:
14271 case POSTDECREMENT_EXPR:
14272 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14273 /* There are no opcodes for these operations. */
14274 return 0;
14275
14276 case ADDR_EXPR:
14277 /* If we already want an address, see if there is INDIRECT_REF inside
14278 e.g. for &this->field. */
14279 if (want_address)
14280 {
14281 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14282 (loc, want_address == 2);
14283 if (list_ret)
14284 have_address = 1;
14285 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14286 && (ret = cst_pool_loc_descr (loc)))
14287 have_address = 1;
14288 }
14289 /* Otherwise, process the argument and look for the address. */
14290 if (!list_ret && !ret)
14291 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14292 else
14293 {
14294 if (want_address)
14295 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14296 return NULL;
14297 }
14298 break;
14299
14300 case VAR_DECL:
14301 if (DECL_THREAD_LOCAL_P (loc))
14302 {
14303 rtx rtl;
14304 enum dwarf_location_atom tls_op;
14305 enum dtprel_bool dtprel = dtprel_false;
14306
14307 if (targetm.have_tls)
14308 {
14309 /* If this is not defined, we have no way to emit the
14310 data. */
14311 if (!targetm.asm_out.output_dwarf_dtprel)
14312 return 0;
14313
14314 /* The way DW_OP_GNU_push_tls_address is specified, we
14315 can only look up addresses of objects in the current
14316 module. We used DW_OP_addr as first op, but that's
14317 wrong, because DW_OP_addr is relocated by the debug
14318 info consumer, while DW_OP_GNU_push_tls_address
14319 operand shouldn't be. */
14320 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14321 return 0;
14322 dtprel = dtprel_true;
14323 tls_op = DW_OP_GNU_push_tls_address;
14324 }
14325 else
14326 {
14327 if (!targetm.emutls.debug_form_tls_address
14328 || !(dwarf_version >= 3 || !dwarf_strict))
14329 return 0;
14330 /* We stuffed the control variable into the DECL_VALUE_EXPR
14331 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14332 no longer appear in gimple code. We used the control
14333 variable in specific so that we could pick it up here. */
14334 loc = DECL_VALUE_EXPR (loc);
14335 tls_op = DW_OP_form_tls_address;
14336 }
14337
14338 rtl = rtl_for_decl_location (loc);
14339 if (rtl == NULL_RTX)
14340 return 0;
14341
14342 if (!MEM_P (rtl))
14343 return 0;
14344 rtl = XEXP (rtl, 0);
14345 if (! CONSTANT_P (rtl))
14346 return 0;
14347
14348 ret = new_addr_loc_descr (rtl, dtprel);
14349 ret1 = new_loc_descr (tls_op, 0, 0);
14350 add_loc_descr (&ret, ret1);
14351
14352 have_address = 1;
14353 break;
14354 }
14355 /* FALLTHRU */
14356
14357 case PARM_DECL:
14358 case RESULT_DECL:
14359 if (DECL_HAS_VALUE_EXPR_P (loc))
14360 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14361 want_address);
14362 /* FALLTHRU */
14363
14364 case FUNCTION_DECL:
14365 {
14366 rtx rtl;
14367 var_loc_list *loc_list = lookup_decl_loc (loc);
14368
14369 if (loc_list && loc_list->first)
14370 {
14371 list_ret = dw_loc_list (loc_list, loc, want_address);
14372 have_address = want_address != 0;
14373 break;
14374 }
14375 rtl = rtl_for_decl_location (loc);
14376 if (rtl == NULL_RTX)
14377 {
14378 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14379 return 0;
14380 }
14381 else if (CONST_INT_P (rtl))
14382 {
14383 HOST_WIDE_INT val = INTVAL (rtl);
14384 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14385 val &= GET_MODE_MASK (DECL_MODE (loc));
14386 ret = int_loc_descriptor (val);
14387 }
14388 else if (GET_CODE (rtl) == CONST_STRING)
14389 {
14390 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14391 return 0;
14392 }
14393 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14394 ret = new_addr_loc_descr (rtl, dtprel_false);
14395 else
14396 {
14397 enum machine_mode mode, mem_mode;
14398
14399 /* Certain constructs can only be represented at top-level. */
14400 if (want_address == 2)
14401 {
14402 ret = loc_descriptor (rtl, VOIDmode,
14403 VAR_INIT_STATUS_INITIALIZED);
14404 have_address = 1;
14405 }
14406 else
14407 {
14408 mode = GET_MODE (rtl);
14409 mem_mode = VOIDmode;
14410 if (MEM_P (rtl))
14411 {
14412 mem_mode = mode;
14413 mode = get_address_mode (rtl);
14414 rtl = XEXP (rtl, 0);
14415 have_address = 1;
14416 }
14417 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14418 VAR_INIT_STATUS_INITIALIZED);
14419 }
14420 if (!ret)
14421 expansion_failed (loc, rtl,
14422 "failed to produce loc descriptor for rtl");
14423 }
14424 }
14425 break;
14426
14427 case MEM_REF:
14428 /* ??? FIXME. */
14429 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14430 return 0;
14431 /* Fallthru. */
14432 case INDIRECT_REF:
14433 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14434 have_address = 1;
14435 break;
14436
14437 case COMPOUND_EXPR:
14438 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14439
14440 CASE_CONVERT:
14441 case VIEW_CONVERT_EXPR:
14442 case SAVE_EXPR:
14443 case MODIFY_EXPR:
14444 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14445
14446 case COMPONENT_REF:
14447 case BIT_FIELD_REF:
14448 case ARRAY_REF:
14449 case ARRAY_RANGE_REF:
14450 case REALPART_EXPR:
14451 case IMAGPART_EXPR:
14452 {
14453 tree obj, offset;
14454 HOST_WIDE_INT bitsize, bitpos, bytepos;
14455 enum machine_mode mode;
14456 int unsignedp, volatilep = 0;
14457
14458 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14459 &unsignedp, &volatilep, false);
14460
14461 gcc_assert (obj != loc);
14462
14463 list_ret = loc_list_from_tree (obj,
14464 want_address == 2
14465 && !bitpos && !offset ? 2 : 1);
14466 /* TODO: We can extract value of the small expression via shifting even
14467 for nonzero bitpos. */
14468 if (list_ret == 0)
14469 return 0;
14470 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14471 {
14472 expansion_failed (loc, NULL_RTX,
14473 "bitfield access");
14474 return 0;
14475 }
14476
14477 if (offset != NULL_TREE)
14478 {
14479 /* Variable offset. */
14480 list_ret1 = loc_list_from_tree (offset, 0);
14481 if (list_ret1 == 0)
14482 return 0;
14483 add_loc_list (&list_ret, list_ret1);
14484 if (!list_ret)
14485 return 0;
14486 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14487 }
14488
14489 bytepos = bitpos / BITS_PER_UNIT;
14490 if (bytepos > 0)
14491 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14492 else if (bytepos < 0)
14493 loc_list_plus_const (list_ret, bytepos);
14494
14495 have_address = 1;
14496 break;
14497 }
14498
14499 case INTEGER_CST:
14500 if ((want_address || !tree_fits_shwi_p (loc))
14501 && (ret = cst_pool_loc_descr (loc)))
14502 have_address = 1;
14503 else if (want_address == 2
14504 && tree_fits_shwi_p (loc)
14505 && (ret = address_of_int_loc_descriptor
14506 (int_size_in_bytes (TREE_TYPE (loc)),
14507 tree_to_shwi (loc))))
14508 have_address = 1;
14509 else if (tree_fits_shwi_p (loc))
14510 ret = int_loc_descriptor (tree_to_shwi (loc));
14511 else
14512 {
14513 expansion_failed (loc, NULL_RTX,
14514 "Integer operand is not host integer");
14515 return 0;
14516 }
14517 break;
14518
14519 case CONSTRUCTOR:
14520 case REAL_CST:
14521 case STRING_CST:
14522 case COMPLEX_CST:
14523 if ((ret = cst_pool_loc_descr (loc)))
14524 have_address = 1;
14525 else
14526 /* We can construct small constants here using int_loc_descriptor. */
14527 expansion_failed (loc, NULL_RTX,
14528 "constructor or constant not in constant pool");
14529 break;
14530
14531 case TRUTH_AND_EXPR:
14532 case TRUTH_ANDIF_EXPR:
14533 case BIT_AND_EXPR:
14534 op = DW_OP_and;
14535 goto do_binop;
14536
14537 case TRUTH_XOR_EXPR:
14538 case BIT_XOR_EXPR:
14539 op = DW_OP_xor;
14540 goto do_binop;
14541
14542 case TRUTH_OR_EXPR:
14543 case TRUTH_ORIF_EXPR:
14544 case BIT_IOR_EXPR:
14545 op = DW_OP_or;
14546 goto do_binop;
14547
14548 case FLOOR_DIV_EXPR:
14549 case CEIL_DIV_EXPR:
14550 case ROUND_DIV_EXPR:
14551 case TRUNC_DIV_EXPR:
14552 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14553 return 0;
14554 op = DW_OP_div;
14555 goto do_binop;
14556
14557 case MINUS_EXPR:
14558 op = DW_OP_minus;
14559 goto do_binop;
14560
14561 case FLOOR_MOD_EXPR:
14562 case CEIL_MOD_EXPR:
14563 case ROUND_MOD_EXPR:
14564 case TRUNC_MOD_EXPR:
14565 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14566 {
14567 op = DW_OP_mod;
14568 goto do_binop;
14569 }
14570 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14571 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14572 if (list_ret == 0 || list_ret1 == 0)
14573 return 0;
14574
14575 add_loc_list (&list_ret, list_ret1);
14576 if (list_ret == 0)
14577 return 0;
14578 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14579 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14580 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14581 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14582 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14583 break;
14584
14585 case MULT_EXPR:
14586 op = DW_OP_mul;
14587 goto do_binop;
14588
14589 case LSHIFT_EXPR:
14590 op = DW_OP_shl;
14591 goto do_binop;
14592
14593 case RSHIFT_EXPR:
14594 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14595 goto do_binop;
14596
14597 case POINTER_PLUS_EXPR:
14598 case PLUS_EXPR:
14599 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14600 {
14601 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14602 if (list_ret == 0)
14603 return 0;
14604
14605 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14606 break;
14607 }
14608
14609 op = DW_OP_plus;
14610 goto do_binop;
14611
14612 case LE_EXPR:
14613 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14614 return 0;
14615
14616 op = DW_OP_le;
14617 goto do_binop;
14618
14619 case GE_EXPR:
14620 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14621 return 0;
14622
14623 op = DW_OP_ge;
14624 goto do_binop;
14625
14626 case LT_EXPR:
14627 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14628 return 0;
14629
14630 op = DW_OP_lt;
14631 goto do_binop;
14632
14633 case GT_EXPR:
14634 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14635 return 0;
14636
14637 op = DW_OP_gt;
14638 goto do_binop;
14639
14640 case EQ_EXPR:
14641 op = DW_OP_eq;
14642 goto do_binop;
14643
14644 case NE_EXPR:
14645 op = DW_OP_ne;
14646 goto do_binop;
14647
14648 do_binop:
14649 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14650 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14651 if (list_ret == 0 || list_ret1 == 0)
14652 return 0;
14653
14654 add_loc_list (&list_ret, list_ret1);
14655 if (list_ret == 0)
14656 return 0;
14657 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14658 break;
14659
14660 case TRUTH_NOT_EXPR:
14661 case BIT_NOT_EXPR:
14662 op = DW_OP_not;
14663 goto do_unop;
14664
14665 case ABS_EXPR:
14666 op = DW_OP_abs;
14667 goto do_unop;
14668
14669 case NEGATE_EXPR:
14670 op = DW_OP_neg;
14671 goto do_unop;
14672
14673 do_unop:
14674 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14675 if (list_ret == 0)
14676 return 0;
14677
14678 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14679 break;
14680
14681 case MIN_EXPR:
14682 case MAX_EXPR:
14683 {
14684 const enum tree_code code =
14685 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14686
14687 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14688 build2 (code, integer_type_node,
14689 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14690 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14691 }
14692
14693 /* ... fall through ... */
14694
14695 case COND_EXPR:
14696 {
14697 dw_loc_descr_ref lhs
14698 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14699 dw_loc_list_ref rhs
14700 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14701 dw_loc_descr_ref bra_node, jump_node, tmp;
14702
14703 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14704 if (list_ret == 0 || lhs == 0 || rhs == 0)
14705 return 0;
14706
14707 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14708 add_loc_descr_to_each (list_ret, bra_node);
14709
14710 add_loc_list (&list_ret, rhs);
14711 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14712 add_loc_descr_to_each (list_ret, jump_node);
14713
14714 add_loc_descr_to_each (list_ret, lhs);
14715 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14716 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14717
14718 /* ??? Need a node to point the skip at. Use a nop. */
14719 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14720 add_loc_descr_to_each (list_ret, tmp);
14721 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14722 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14723 }
14724 break;
14725
14726 case FIX_TRUNC_EXPR:
14727 return 0;
14728
14729 default:
14730 /* Leave front-end specific codes as simply unknown. This comes
14731 up, for instance, with the C STMT_EXPR. */
14732 if ((unsigned int) TREE_CODE (loc)
14733 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14734 {
14735 expansion_failed (loc, NULL_RTX,
14736 "language specific tree node");
14737 return 0;
14738 }
14739
14740 #ifdef ENABLE_CHECKING
14741 /* Otherwise this is a generic code; we should just lists all of
14742 these explicitly. We forgot one. */
14743 gcc_unreachable ();
14744 #else
14745 /* In a release build, we want to degrade gracefully: better to
14746 generate incomplete debugging information than to crash. */
14747 return NULL;
14748 #endif
14749 }
14750
14751 if (!ret && !list_ret)
14752 return 0;
14753
14754 if (want_address == 2 && !have_address
14755 && (dwarf_version >= 4 || !dwarf_strict))
14756 {
14757 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14758 {
14759 expansion_failed (loc, NULL_RTX,
14760 "DWARF address size mismatch");
14761 return 0;
14762 }
14763 if (ret)
14764 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14765 else
14766 add_loc_descr_to_each (list_ret,
14767 new_loc_descr (DW_OP_stack_value, 0, 0));
14768 have_address = 1;
14769 }
14770 /* Show if we can't fill the request for an address. */
14771 if (want_address && !have_address)
14772 {
14773 expansion_failed (loc, NULL_RTX,
14774 "Want address and only have value");
14775 return 0;
14776 }
14777
14778 gcc_assert (!ret || !list_ret);
14779
14780 /* If we've got an address and don't want one, dereference. */
14781 if (!want_address && have_address)
14782 {
14783 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14784
14785 if (size > DWARF2_ADDR_SIZE || size == -1)
14786 {
14787 expansion_failed (loc, NULL_RTX,
14788 "DWARF address size mismatch");
14789 return 0;
14790 }
14791 else if (size == DWARF2_ADDR_SIZE)
14792 op = DW_OP_deref;
14793 else
14794 op = DW_OP_deref_size;
14795
14796 if (ret)
14797 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14798 else
14799 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14800 }
14801 if (ret)
14802 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14803
14804 return list_ret;
14805 }
14806
14807 /* Same as above but return only single location expression. */
14808 static dw_loc_descr_ref
14809 loc_descriptor_from_tree (tree loc, int want_address)
14810 {
14811 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14812 if (!ret)
14813 return NULL;
14814 if (ret->dw_loc_next)
14815 {
14816 expansion_failed (loc, NULL_RTX,
14817 "Location list where only loc descriptor needed");
14818 return NULL;
14819 }
14820 return ret->expr;
14821 }
14822
14823 /* Given a value, round it up to the lowest multiple of `boundary'
14824 which is not less than the value itself. */
14825
14826 static inline HOST_WIDE_INT
14827 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14828 {
14829 return (((value + boundary - 1) / boundary) * boundary);
14830 }
14831
14832 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14833 pointer to the declared type for the relevant field variable, or return
14834 `integer_type_node' if the given node turns out to be an
14835 ERROR_MARK node. */
14836
14837 static inline tree
14838 field_type (const_tree decl)
14839 {
14840 tree type;
14841
14842 if (TREE_CODE (decl) == ERROR_MARK)
14843 return integer_type_node;
14844
14845 type = DECL_BIT_FIELD_TYPE (decl);
14846 if (type == NULL_TREE)
14847 type = TREE_TYPE (decl);
14848
14849 return type;
14850 }
14851
14852 /* Given a pointer to a tree node, return the alignment in bits for
14853 it, or else return BITS_PER_WORD if the node actually turns out to
14854 be an ERROR_MARK node. */
14855
14856 static inline unsigned
14857 simple_type_align_in_bits (const_tree type)
14858 {
14859 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14860 }
14861
14862 static inline unsigned
14863 simple_decl_align_in_bits (const_tree decl)
14864 {
14865 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14866 }
14867
14868 /* Return the result of rounding T up to ALIGN. */
14869
14870 static inline offset_int
14871 round_up_to_align (const offset_int &t, unsigned int align)
14872 {
14873 return wi::udiv_trunc (t + align - 1, align) * align;
14874 }
14875
14876 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14877 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14878 or return 0 if we are unable to determine what that offset is, either
14879 because the argument turns out to be a pointer to an ERROR_MARK node, or
14880 because the offset is actually variable. (We can't handle the latter case
14881 just yet). */
14882
14883 static HOST_WIDE_INT
14884 field_byte_offset (const_tree decl)
14885 {
14886 offset_int object_offset_in_bits;
14887 offset_int object_offset_in_bytes;
14888 offset_int bitpos_int;
14889
14890 if (TREE_CODE (decl) == ERROR_MARK)
14891 return 0;
14892
14893 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14894
14895 /* We cannot yet cope with fields whose positions are variable, so
14896 for now, when we see such things, we simply return 0. Someday, we may
14897 be able to handle such cases, but it will be damn difficult. */
14898 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14899 return 0;
14900
14901 bitpos_int = wi::to_offset (bit_position (decl));
14902
14903 #ifdef PCC_BITFIELD_TYPE_MATTERS
14904 if (PCC_BITFIELD_TYPE_MATTERS)
14905 {
14906 tree type;
14907 tree field_size_tree;
14908 offset_int deepest_bitpos;
14909 offset_int field_size_in_bits;
14910 unsigned int type_align_in_bits;
14911 unsigned int decl_align_in_bits;
14912 offset_int type_size_in_bits;
14913
14914 type = field_type (decl);
14915 type_size_in_bits = offset_int_type_size_in_bits (type);
14916 type_align_in_bits = simple_type_align_in_bits (type);
14917
14918 field_size_tree = DECL_SIZE (decl);
14919
14920 /* The size could be unspecified if there was an error, or for
14921 a flexible array member. */
14922 if (!field_size_tree)
14923 field_size_tree = bitsize_zero_node;
14924
14925 /* If the size of the field is not constant, use the type size. */
14926 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14927 field_size_in_bits = wi::to_offset (field_size_tree);
14928 else
14929 field_size_in_bits = type_size_in_bits;
14930
14931 decl_align_in_bits = simple_decl_align_in_bits (decl);
14932
14933 /* The GCC front-end doesn't make any attempt to keep track of the
14934 starting bit offset (relative to the start of the containing
14935 structure type) of the hypothetical "containing object" for a
14936 bit-field. Thus, when computing the byte offset value for the
14937 start of the "containing object" of a bit-field, we must deduce
14938 this information on our own. This can be rather tricky to do in
14939 some cases. For example, handling the following structure type
14940 definition when compiling for an i386/i486 target (which only
14941 aligns long long's to 32-bit boundaries) can be very tricky:
14942
14943 struct S { int field1; long long field2:31; };
14944
14945 Fortunately, there is a simple rule-of-thumb which can be used
14946 in such cases. When compiling for an i386/i486, GCC will
14947 allocate 8 bytes for the structure shown above. It decides to
14948 do this based upon one simple rule for bit-field allocation.
14949 GCC allocates each "containing object" for each bit-field at
14950 the first (i.e. lowest addressed) legitimate alignment boundary
14951 (based upon the required minimum alignment for the declared
14952 type of the field) which it can possibly use, subject to the
14953 condition that there is still enough available space remaining
14954 in the containing object (when allocated at the selected point)
14955 to fully accommodate all of the bits of the bit-field itself.
14956
14957 This simple rule makes it obvious why GCC allocates 8 bytes for
14958 each object of the structure type shown above. When looking
14959 for a place to allocate the "containing object" for `field2',
14960 the compiler simply tries to allocate a 64-bit "containing
14961 object" at each successive 32-bit boundary (starting at zero)
14962 until it finds a place to allocate that 64- bit field such that
14963 at least 31 contiguous (and previously unallocated) bits remain
14964 within that selected 64 bit field. (As it turns out, for the
14965 example above, the compiler finds it is OK to allocate the
14966 "containing object" 64-bit field at bit-offset zero within the
14967 structure type.)
14968
14969 Here we attempt to work backwards from the limited set of facts
14970 we're given, and we try to deduce from those facts, where GCC
14971 must have believed that the containing object started (within
14972 the structure type). The value we deduce is then used (by the
14973 callers of this routine) to generate DW_AT_location and
14974 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14975 the case of DW_AT_location, regular fields as well). */
14976
14977 /* Figure out the bit-distance from the start of the structure to
14978 the "deepest" bit of the bit-field. */
14979 deepest_bitpos = bitpos_int + field_size_in_bits;
14980
14981 /* This is the tricky part. Use some fancy footwork to deduce
14982 where the lowest addressed bit of the containing object must
14983 be. */
14984 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14985
14986 /* Round up to type_align by default. This works best for
14987 bitfields. */
14988 object_offset_in_bits
14989 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14990
14991 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14992 {
14993 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14994
14995 /* Round up to decl_align instead. */
14996 object_offset_in_bits
14997 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14998 }
14999 }
15000 else
15001 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15002 object_offset_in_bits = bitpos_int;
15003
15004 object_offset_in_bytes
15005 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15006 return object_offset_in_bytes.to_shwi ();
15007 }
15008 \f
15009 /* The following routines define various Dwarf attributes and any data
15010 associated with them. */
15011
15012 /* Add a location description attribute value to a DIE.
15013
15014 This emits location attributes suitable for whole variables and
15015 whole parameters. Note that the location attributes for struct fields are
15016 generated by the routine `data_member_location_attribute' below. */
15017
15018 static inline void
15019 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15020 dw_loc_list_ref descr)
15021 {
15022 if (descr == 0)
15023 return;
15024 if (single_element_loc_list_p (descr))
15025 add_AT_loc (die, attr_kind, descr->expr);
15026 else
15027 add_AT_loc_list (die, attr_kind, descr);
15028 }
15029
15030 /* Add DW_AT_accessibility attribute to DIE if needed. */
15031
15032 static void
15033 add_accessibility_attribute (dw_die_ref die, tree decl)
15034 {
15035 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15036 children, otherwise the default is DW_ACCESS_public. In DWARF2
15037 the default has always been DW_ACCESS_public. */
15038 if (TREE_PROTECTED (decl))
15039 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15040 else if (TREE_PRIVATE (decl))
15041 {
15042 if (dwarf_version == 2
15043 || die->die_parent == NULL
15044 || die->die_parent->die_tag != DW_TAG_class_type)
15045 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15046 }
15047 else if (dwarf_version > 2
15048 && die->die_parent
15049 && die->die_parent->die_tag == DW_TAG_class_type)
15050 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15051 }
15052
15053 /* Attach the specialized form of location attribute used for data members of
15054 struct and union types. In the special case of a FIELD_DECL node which
15055 represents a bit-field, the "offset" part of this special location
15056 descriptor must indicate the distance in bytes from the lowest-addressed
15057 byte of the containing struct or union type to the lowest-addressed byte of
15058 the "containing object" for the bit-field. (See the `field_byte_offset'
15059 function above).
15060
15061 For any given bit-field, the "containing object" is a hypothetical object
15062 (of some integral or enum type) within which the given bit-field lives. The
15063 type of this hypothetical "containing object" is always the same as the
15064 declared type of the individual bit-field itself (for GCC anyway... the
15065 DWARF spec doesn't actually mandate this). Note that it is the size (in
15066 bytes) of the hypothetical "containing object" which will be given in the
15067 DW_AT_byte_size attribute for this bit-field. (See the
15068 `byte_size_attribute' function below.) It is also used when calculating the
15069 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15070 function below.) */
15071
15072 static void
15073 add_data_member_location_attribute (dw_die_ref die, tree decl)
15074 {
15075 HOST_WIDE_INT offset;
15076 dw_loc_descr_ref loc_descr = 0;
15077
15078 if (TREE_CODE (decl) == TREE_BINFO)
15079 {
15080 /* We're working on the TAG_inheritance for a base class. */
15081 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15082 {
15083 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15084 aren't at a fixed offset from all (sub)objects of the same
15085 type. We need to extract the appropriate offset from our
15086 vtable. The following dwarf expression means
15087
15088 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15089
15090 This is specific to the V3 ABI, of course. */
15091
15092 dw_loc_descr_ref tmp;
15093
15094 /* Make a copy of the object address. */
15095 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15096 add_loc_descr (&loc_descr, tmp);
15097
15098 /* Extract the vtable address. */
15099 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15100 add_loc_descr (&loc_descr, tmp);
15101
15102 /* Calculate the address of the offset. */
15103 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15104 gcc_assert (offset < 0);
15105
15106 tmp = int_loc_descriptor (-offset);
15107 add_loc_descr (&loc_descr, tmp);
15108 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15109 add_loc_descr (&loc_descr, tmp);
15110
15111 /* Extract the offset. */
15112 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15113 add_loc_descr (&loc_descr, tmp);
15114
15115 /* Add it to the object address. */
15116 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15117 add_loc_descr (&loc_descr, tmp);
15118 }
15119 else
15120 offset = tree_to_shwi (BINFO_OFFSET (decl));
15121 }
15122 else
15123 offset = field_byte_offset (decl);
15124
15125 if (! loc_descr)
15126 {
15127 if (dwarf_version > 2)
15128 {
15129 /* Don't need to output a location expression, just the constant. */
15130 if (offset < 0)
15131 add_AT_int (die, DW_AT_data_member_location, offset);
15132 else
15133 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15134 return;
15135 }
15136 else
15137 {
15138 enum dwarf_location_atom op;
15139
15140 /* The DWARF2 standard says that we should assume that the structure
15141 address is already on the stack, so we can specify a structure
15142 field address by using DW_OP_plus_uconst. */
15143 op = DW_OP_plus_uconst;
15144 loc_descr = new_loc_descr (op, offset, 0);
15145 }
15146 }
15147
15148 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15149 }
15150
15151 /* Writes integer values to dw_vec_const array. */
15152
15153 static void
15154 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15155 {
15156 while (size != 0)
15157 {
15158 *dest++ = val & 0xff;
15159 val >>= 8;
15160 --size;
15161 }
15162 }
15163
15164 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15165
15166 static HOST_WIDE_INT
15167 extract_int (const unsigned char *src, unsigned int size)
15168 {
15169 HOST_WIDE_INT val = 0;
15170
15171 src += size;
15172 while (size != 0)
15173 {
15174 val <<= 8;
15175 val |= *--src & 0xff;
15176 --size;
15177 }
15178 return val;
15179 }
15180
15181 /* Writes wide_int values to dw_vec_const array. */
15182
15183 static void
15184 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15185 {
15186 int i;
15187
15188 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15189 {
15190 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15191 return;
15192 }
15193
15194 /* We'd have to extend this code to support odd sizes. */
15195 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15196
15197 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15198
15199 if (WORDS_BIG_ENDIAN)
15200 for (i = n - 1; i >= 0; i--)
15201 {
15202 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15203 dest += sizeof (HOST_WIDE_INT);
15204 }
15205 else
15206 for (i = 0; i < n; i++)
15207 {
15208 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15209 dest += sizeof (HOST_WIDE_INT);
15210 }
15211 }
15212
15213 /* Writes floating point values to dw_vec_const array. */
15214
15215 static void
15216 insert_float (const_rtx rtl, unsigned char *array)
15217 {
15218 REAL_VALUE_TYPE rv;
15219 long val[4];
15220 int i;
15221
15222 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15223 real_to_target (val, &rv, GET_MODE (rtl));
15224
15225 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15226 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15227 {
15228 insert_int (val[i], 4, array);
15229 array += 4;
15230 }
15231 }
15232
15233 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15234 does not have a "location" either in memory or in a register. These
15235 things can arise in GNU C when a constant is passed as an actual parameter
15236 to an inlined function. They can also arise in C++ where declared
15237 constants do not necessarily get memory "homes". */
15238
15239 static bool
15240 add_const_value_attribute (dw_die_ref die, rtx rtl)
15241 {
15242 switch (GET_CODE (rtl))
15243 {
15244 case CONST_INT:
15245 {
15246 HOST_WIDE_INT val = INTVAL (rtl);
15247
15248 if (val < 0)
15249 add_AT_int (die, DW_AT_const_value, val);
15250 else
15251 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15252 }
15253 return true;
15254
15255 case CONST_WIDE_INT:
15256 add_AT_wide (die, DW_AT_const_value,
15257 std::make_pair (rtl, GET_MODE (rtl)));
15258 return true;
15259
15260 case CONST_DOUBLE:
15261 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15262 floating-point constant. A CONST_DOUBLE is used whenever the
15263 constant requires more than one word in order to be adequately
15264 represented. */
15265 {
15266 enum machine_mode mode = GET_MODE (rtl);
15267
15268 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15269 add_AT_double (die, DW_AT_const_value,
15270 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15271 else
15272 {
15273 unsigned int length = GET_MODE_SIZE (mode);
15274 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15275
15276 insert_float (rtl, array);
15277 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15278 }
15279 }
15280 return true;
15281
15282 case CONST_VECTOR:
15283 {
15284 enum machine_mode mode = GET_MODE (rtl);
15285 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15286 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15287 unsigned char *array
15288 = ggc_vec_alloc<unsigned char> (length * elt_size);
15289 unsigned int i;
15290 unsigned char *p;
15291 enum machine_mode imode = GET_MODE_INNER (mode);
15292
15293 switch (GET_MODE_CLASS (mode))
15294 {
15295 case MODE_VECTOR_INT:
15296 for (i = 0, p = array; i < length; i++, p += elt_size)
15297 {
15298 rtx elt = CONST_VECTOR_ELT (rtl, i);
15299 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15300 }
15301 break;
15302
15303 case MODE_VECTOR_FLOAT:
15304 for (i = 0, p = array; i < length; i++, p += elt_size)
15305 {
15306 rtx elt = CONST_VECTOR_ELT (rtl, i);
15307 insert_float (elt, p);
15308 }
15309 break;
15310
15311 default:
15312 gcc_unreachable ();
15313 }
15314
15315 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15316 }
15317 return true;
15318
15319 case CONST_STRING:
15320 if (dwarf_version >= 4 || !dwarf_strict)
15321 {
15322 dw_loc_descr_ref loc_result;
15323 resolve_one_addr (&rtl);
15324 rtl_addr:
15325 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15326 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15327 add_AT_loc (die, DW_AT_location, loc_result);
15328 vec_safe_push (used_rtx_array, rtl);
15329 return true;
15330 }
15331 return false;
15332
15333 case CONST:
15334 if (CONSTANT_P (XEXP (rtl, 0)))
15335 return add_const_value_attribute (die, XEXP (rtl, 0));
15336 /* FALLTHROUGH */
15337 case SYMBOL_REF:
15338 if (!const_ok_for_output (rtl))
15339 return false;
15340 case LABEL_REF:
15341 if (dwarf_version >= 4 || !dwarf_strict)
15342 goto rtl_addr;
15343 return false;
15344
15345 case PLUS:
15346 /* In cases where an inlined instance of an inline function is passed
15347 the address of an `auto' variable (which is local to the caller) we
15348 can get a situation where the DECL_RTL of the artificial local
15349 variable (for the inlining) which acts as a stand-in for the
15350 corresponding formal parameter (of the inline function) will look
15351 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15352 exactly a compile-time constant expression, but it isn't the address
15353 of the (artificial) local variable either. Rather, it represents the
15354 *value* which the artificial local variable always has during its
15355 lifetime. We currently have no way to represent such quasi-constant
15356 values in Dwarf, so for now we just punt and generate nothing. */
15357 return false;
15358
15359 case HIGH:
15360 case CONST_FIXED:
15361 return false;
15362
15363 case MEM:
15364 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15365 && MEM_READONLY_P (rtl)
15366 && GET_MODE (rtl) == BLKmode)
15367 {
15368 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15369 return true;
15370 }
15371 return false;
15372
15373 default:
15374 /* No other kinds of rtx should be possible here. */
15375 gcc_unreachable ();
15376 }
15377 return false;
15378 }
15379
15380 /* Determine whether the evaluation of EXPR references any variables
15381 or functions which aren't otherwise used (and therefore may not be
15382 output). */
15383 static tree
15384 reference_to_unused (tree * tp, int * walk_subtrees,
15385 void * data ATTRIBUTE_UNUSED)
15386 {
15387 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15388 *walk_subtrees = 0;
15389
15390 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15391 && ! TREE_ASM_WRITTEN (*tp))
15392 return *tp;
15393 /* ??? The C++ FE emits debug information for using decls, so
15394 putting gcc_unreachable here falls over. See PR31899. For now
15395 be conservative. */
15396 else if (!symtab->global_info_ready
15397 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15398 return *tp;
15399 else if (TREE_CODE (*tp) == VAR_DECL)
15400 {
15401 varpool_node *node = varpool_node::get (*tp);
15402 if (!node || !node->definition)
15403 return *tp;
15404 }
15405 else if (TREE_CODE (*tp) == FUNCTION_DECL
15406 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15407 {
15408 /* The call graph machinery must have finished analyzing,
15409 optimizing and gimplifying the CU by now.
15410 So if *TP has no call graph node associated
15411 to it, it means *TP will not be emitted. */
15412 if (!cgraph_node::get (*tp))
15413 return *tp;
15414 }
15415 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15416 return *tp;
15417
15418 return NULL_TREE;
15419 }
15420
15421 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15422 for use in a later add_const_value_attribute call. */
15423
15424 static rtx
15425 rtl_for_decl_init (tree init, tree type)
15426 {
15427 rtx rtl = NULL_RTX;
15428
15429 STRIP_NOPS (init);
15430
15431 /* If a variable is initialized with a string constant without embedded
15432 zeros, build CONST_STRING. */
15433 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15434 {
15435 tree enttype = TREE_TYPE (type);
15436 tree domain = TYPE_DOMAIN (type);
15437 enum machine_mode mode = TYPE_MODE (enttype);
15438
15439 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15440 && domain
15441 && integer_zerop (TYPE_MIN_VALUE (domain))
15442 && compare_tree_int (TYPE_MAX_VALUE (domain),
15443 TREE_STRING_LENGTH (init) - 1) == 0
15444 && ((size_t) TREE_STRING_LENGTH (init)
15445 == strlen (TREE_STRING_POINTER (init)) + 1))
15446 {
15447 rtl = gen_rtx_CONST_STRING (VOIDmode,
15448 ggc_strdup (TREE_STRING_POINTER (init)));
15449 rtl = gen_rtx_MEM (BLKmode, rtl);
15450 MEM_READONLY_P (rtl) = 1;
15451 }
15452 }
15453 /* Other aggregates, and complex values, could be represented using
15454 CONCAT: FIXME! */
15455 else if (AGGREGATE_TYPE_P (type)
15456 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15457 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15458 || TREE_CODE (type) == COMPLEX_TYPE)
15459 ;
15460 /* Vectors only work if their mode is supported by the target.
15461 FIXME: generic vectors ought to work too. */
15462 else if (TREE_CODE (type) == VECTOR_TYPE
15463 && !VECTOR_MODE_P (TYPE_MODE (type)))
15464 ;
15465 /* If the initializer is something that we know will expand into an
15466 immediate RTL constant, expand it now. We must be careful not to
15467 reference variables which won't be output. */
15468 else if (initializer_constant_valid_p (init, type)
15469 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15470 {
15471 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15472 possible. */
15473 if (TREE_CODE (type) == VECTOR_TYPE)
15474 switch (TREE_CODE (init))
15475 {
15476 case VECTOR_CST:
15477 break;
15478 case CONSTRUCTOR:
15479 if (TREE_CONSTANT (init))
15480 {
15481 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15482 bool constant_p = true;
15483 tree value;
15484 unsigned HOST_WIDE_INT ix;
15485
15486 /* Even when ctor is constant, it might contain non-*_CST
15487 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15488 belong into VECTOR_CST nodes. */
15489 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15490 if (!CONSTANT_CLASS_P (value))
15491 {
15492 constant_p = false;
15493 break;
15494 }
15495
15496 if (constant_p)
15497 {
15498 init = build_vector_from_ctor (type, elts);
15499 break;
15500 }
15501 }
15502 /* FALLTHRU */
15503
15504 default:
15505 return NULL;
15506 }
15507
15508 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15509
15510 /* If expand_expr returns a MEM, it wasn't immediate. */
15511 gcc_assert (!rtl || !MEM_P (rtl));
15512 }
15513
15514 return rtl;
15515 }
15516
15517 /* Generate RTL for the variable DECL to represent its location. */
15518
15519 static rtx
15520 rtl_for_decl_location (tree decl)
15521 {
15522 rtx rtl;
15523
15524 /* Here we have to decide where we are going to say the parameter "lives"
15525 (as far as the debugger is concerned). We only have a couple of
15526 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15527
15528 DECL_RTL normally indicates where the parameter lives during most of the
15529 activation of the function. If optimization is enabled however, this
15530 could be either NULL or else a pseudo-reg. Both of those cases indicate
15531 that the parameter doesn't really live anywhere (as far as the code
15532 generation parts of GCC are concerned) during most of the function's
15533 activation. That will happen (for example) if the parameter is never
15534 referenced within the function.
15535
15536 We could just generate a location descriptor here for all non-NULL
15537 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15538 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15539 where DECL_RTL is NULL or is a pseudo-reg.
15540
15541 Note however that we can only get away with using DECL_INCOMING_RTL as
15542 a backup substitute for DECL_RTL in certain limited cases. In cases
15543 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15544 we can be sure that the parameter was passed using the same type as it is
15545 declared to have within the function, and that its DECL_INCOMING_RTL
15546 points us to a place where a value of that type is passed.
15547
15548 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15549 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15550 because in these cases DECL_INCOMING_RTL points us to a value of some
15551 type which is *different* from the type of the parameter itself. Thus,
15552 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15553 such cases, the debugger would end up (for example) trying to fetch a
15554 `float' from a place which actually contains the first part of a
15555 `double'. That would lead to really incorrect and confusing
15556 output at debug-time.
15557
15558 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15559 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15560 are a couple of exceptions however. On little-endian machines we can
15561 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15562 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15563 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15564 when (on a little-endian machine) a non-prototyped function has a
15565 parameter declared to be of type `short' or `char'. In such cases,
15566 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15567 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15568 passed `int' value. If the debugger then uses that address to fetch
15569 a `short' or a `char' (on a little-endian machine) the result will be
15570 the correct data, so we allow for such exceptional cases below.
15571
15572 Note that our goal here is to describe the place where the given formal
15573 parameter lives during most of the function's activation (i.e. between the
15574 end of the prologue and the start of the epilogue). We'll do that as best
15575 as we can. Note however that if the given formal parameter is modified
15576 sometime during the execution of the function, then a stack backtrace (at
15577 debug-time) will show the function as having been called with the *new*
15578 value rather than the value which was originally passed in. This happens
15579 rarely enough that it is not a major problem, but it *is* a problem, and
15580 I'd like to fix it.
15581
15582 A future version of dwarf2out.c may generate two additional attributes for
15583 any given DW_TAG_formal_parameter DIE which will describe the "passed
15584 type" and the "passed location" for the given formal parameter in addition
15585 to the attributes we now generate to indicate the "declared type" and the
15586 "active location" for each parameter. This additional set of attributes
15587 could be used by debuggers for stack backtraces. Separately, note that
15588 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15589 This happens (for example) for inlined-instances of inline function formal
15590 parameters which are never referenced. This really shouldn't be
15591 happening. All PARM_DECL nodes should get valid non-NULL
15592 DECL_INCOMING_RTL values. FIXME. */
15593
15594 /* Use DECL_RTL as the "location" unless we find something better. */
15595 rtl = DECL_RTL_IF_SET (decl);
15596
15597 /* When generating abstract instances, ignore everything except
15598 constants, symbols living in memory, and symbols living in
15599 fixed registers. */
15600 if (! reload_completed)
15601 {
15602 if (rtl
15603 && (CONSTANT_P (rtl)
15604 || (MEM_P (rtl)
15605 && CONSTANT_P (XEXP (rtl, 0)))
15606 || (REG_P (rtl)
15607 && TREE_CODE (decl) == VAR_DECL
15608 && TREE_STATIC (decl))))
15609 {
15610 rtl = targetm.delegitimize_address (rtl);
15611 return rtl;
15612 }
15613 rtl = NULL_RTX;
15614 }
15615 else if (TREE_CODE (decl) == PARM_DECL)
15616 {
15617 if (rtl == NULL_RTX
15618 || is_pseudo_reg (rtl)
15619 || (MEM_P (rtl)
15620 && is_pseudo_reg (XEXP (rtl, 0))
15621 && DECL_INCOMING_RTL (decl)
15622 && MEM_P (DECL_INCOMING_RTL (decl))
15623 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15624 {
15625 tree declared_type = TREE_TYPE (decl);
15626 tree passed_type = DECL_ARG_TYPE (decl);
15627 enum machine_mode dmode = TYPE_MODE (declared_type);
15628 enum machine_mode pmode = TYPE_MODE (passed_type);
15629
15630 /* This decl represents a formal parameter which was optimized out.
15631 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15632 all cases where (rtl == NULL_RTX) just below. */
15633 if (dmode == pmode)
15634 rtl = DECL_INCOMING_RTL (decl);
15635 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15636 && SCALAR_INT_MODE_P (dmode)
15637 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15638 && DECL_INCOMING_RTL (decl))
15639 {
15640 rtx inc = DECL_INCOMING_RTL (decl);
15641 if (REG_P (inc))
15642 rtl = inc;
15643 else if (MEM_P (inc))
15644 {
15645 if (BYTES_BIG_ENDIAN)
15646 rtl = adjust_address_nv (inc, dmode,
15647 GET_MODE_SIZE (pmode)
15648 - GET_MODE_SIZE (dmode));
15649 else
15650 rtl = inc;
15651 }
15652 }
15653 }
15654
15655 /* If the parm was passed in registers, but lives on the stack, then
15656 make a big endian correction if the mode of the type of the
15657 parameter is not the same as the mode of the rtl. */
15658 /* ??? This is the same series of checks that are made in dbxout.c before
15659 we reach the big endian correction code there. It isn't clear if all
15660 of these checks are necessary here, but keeping them all is the safe
15661 thing to do. */
15662 else if (MEM_P (rtl)
15663 && XEXP (rtl, 0) != const0_rtx
15664 && ! CONSTANT_P (XEXP (rtl, 0))
15665 /* Not passed in memory. */
15666 && !MEM_P (DECL_INCOMING_RTL (decl))
15667 /* Not passed by invisible reference. */
15668 && (!REG_P (XEXP (rtl, 0))
15669 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15670 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15671 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15672 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15673 #endif
15674 )
15675 /* Big endian correction check. */
15676 && BYTES_BIG_ENDIAN
15677 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15678 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15679 < UNITS_PER_WORD))
15680 {
15681 enum machine_mode addr_mode = get_address_mode (rtl);
15682 int offset = (UNITS_PER_WORD
15683 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15684
15685 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15686 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15687 }
15688 }
15689 else if (TREE_CODE (decl) == VAR_DECL
15690 && rtl
15691 && MEM_P (rtl)
15692 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15693 && BYTES_BIG_ENDIAN)
15694 {
15695 enum machine_mode addr_mode = get_address_mode (rtl);
15696 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15697 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15698
15699 /* If a variable is declared "register" yet is smaller than
15700 a register, then if we store the variable to memory, it
15701 looks like we're storing a register-sized value, when in
15702 fact we are not. We need to adjust the offset of the
15703 storage location to reflect the actual value's bytes,
15704 else gdb will not be able to display it. */
15705 if (rsize > dsize)
15706 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15707 plus_constant (addr_mode, XEXP (rtl, 0),
15708 rsize - dsize));
15709 }
15710
15711 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15712 and will have been substituted directly into all expressions that use it.
15713 C does not have such a concept, but C++ and other languages do. */
15714 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15715 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15716
15717 if (rtl)
15718 rtl = targetm.delegitimize_address (rtl);
15719
15720 /* If we don't look past the constant pool, we risk emitting a
15721 reference to a constant pool entry that isn't referenced from
15722 code, and thus is not emitted. */
15723 if (rtl)
15724 rtl = avoid_constant_pool_reference (rtl);
15725
15726 /* Try harder to get a rtl. If this symbol ends up not being emitted
15727 in the current CU, resolve_addr will remove the expression referencing
15728 it. */
15729 if (rtl == NULL_RTX
15730 && TREE_CODE (decl) == VAR_DECL
15731 && !DECL_EXTERNAL (decl)
15732 && TREE_STATIC (decl)
15733 && DECL_NAME (decl)
15734 && !DECL_HARD_REGISTER (decl)
15735 && DECL_MODE (decl) != VOIDmode)
15736 {
15737 rtl = make_decl_rtl_for_debug (decl);
15738 if (!MEM_P (rtl)
15739 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15740 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15741 rtl = NULL_RTX;
15742 }
15743
15744 return rtl;
15745 }
15746
15747 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15748 returned. If so, the decl for the COMMON block is returned, and the
15749 value is the offset into the common block for the symbol. */
15750
15751 static tree
15752 fortran_common (tree decl, HOST_WIDE_INT *value)
15753 {
15754 tree val_expr, cvar;
15755 enum machine_mode mode;
15756 HOST_WIDE_INT bitsize, bitpos;
15757 tree offset;
15758 int unsignedp, volatilep = 0;
15759
15760 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15761 it does not have a value (the offset into the common area), or if it
15762 is thread local (as opposed to global) then it isn't common, and shouldn't
15763 be handled as such. */
15764 if (TREE_CODE (decl) != VAR_DECL
15765 || !TREE_STATIC (decl)
15766 || !DECL_HAS_VALUE_EXPR_P (decl)
15767 || !is_fortran ())
15768 return NULL_TREE;
15769
15770 val_expr = DECL_VALUE_EXPR (decl);
15771 if (TREE_CODE (val_expr) != COMPONENT_REF)
15772 return NULL_TREE;
15773
15774 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15775 &mode, &unsignedp, &volatilep, true);
15776
15777 if (cvar == NULL_TREE
15778 || TREE_CODE (cvar) != VAR_DECL
15779 || DECL_ARTIFICIAL (cvar)
15780 || !TREE_PUBLIC (cvar))
15781 return NULL_TREE;
15782
15783 *value = 0;
15784 if (offset != NULL)
15785 {
15786 if (!tree_fits_shwi_p (offset))
15787 return NULL_TREE;
15788 *value = tree_to_shwi (offset);
15789 }
15790 if (bitpos != 0)
15791 *value += bitpos / BITS_PER_UNIT;
15792
15793 return cvar;
15794 }
15795
15796 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15797 data attribute for a variable or a parameter. We generate the
15798 DW_AT_const_value attribute only in those cases where the given variable
15799 or parameter does not have a true "location" either in memory or in a
15800 register. This can happen (for example) when a constant is passed as an
15801 actual argument in a call to an inline function. (It's possible that
15802 these things can crop up in other ways also.) Note that one type of
15803 constant value which can be passed into an inlined function is a constant
15804 pointer. This can happen for example if an actual argument in an inlined
15805 function call evaluates to a compile-time constant address.
15806
15807 CACHE_P is true if it is worth caching the location list for DECL,
15808 so that future calls can reuse it rather than regenerate it from scratch.
15809 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15810 since we will need to refer to them each time the function is inlined. */
15811
15812 static bool
15813 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15814 enum dwarf_attribute attr)
15815 {
15816 rtx rtl;
15817 dw_loc_list_ref list;
15818 var_loc_list *loc_list;
15819 cached_dw_loc_list *cache;
15820 void **slot;
15821
15822 if (TREE_CODE (decl) == ERROR_MARK)
15823 return false;
15824
15825 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15826 || TREE_CODE (decl) == RESULT_DECL);
15827
15828 /* Try to get some constant RTL for this decl, and use that as the value of
15829 the location. */
15830
15831 rtl = rtl_for_decl_location (decl);
15832 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15833 && add_const_value_attribute (die, rtl))
15834 return true;
15835
15836 /* See if we have single element location list that is equivalent to
15837 a constant value. That way we are better to use add_const_value_attribute
15838 rather than expanding constant value equivalent. */
15839 loc_list = lookup_decl_loc (decl);
15840 if (loc_list
15841 && loc_list->first
15842 && loc_list->first->next == NULL
15843 && NOTE_P (loc_list->first->loc)
15844 && NOTE_VAR_LOCATION (loc_list->first->loc)
15845 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15846 {
15847 struct var_loc_node *node;
15848
15849 node = loc_list->first;
15850 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15851 if (GET_CODE (rtl) == EXPR_LIST)
15852 rtl = XEXP (rtl, 0);
15853 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15854 && add_const_value_attribute (die, rtl))
15855 return true;
15856 }
15857 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15858 list several times. See if we've already cached the contents. */
15859 list = NULL;
15860 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15861 cache_p = false;
15862 if (cache_p)
15863 {
15864 cache = (cached_dw_loc_list *)
15865 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15866 if (cache)
15867 list = cache->loc_list;
15868 }
15869 if (list == NULL)
15870 {
15871 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15872 /* It is usually worth caching this result if the decl is from
15873 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15874 if (cache_p && list && list->dw_loc_next)
15875 {
15876 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15877 DECL_UID (decl), INSERT);
15878 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15879 cache->decl_id = DECL_UID (decl);
15880 cache->loc_list = list;
15881 *slot = cache;
15882 }
15883 }
15884 if (list)
15885 {
15886 add_AT_location_description (die, attr, list);
15887 return true;
15888 }
15889 /* None of that worked, so it must not really have a location;
15890 try adding a constant value attribute from the DECL_INITIAL. */
15891 return tree_add_const_value_attribute_for_decl (die, decl);
15892 }
15893
15894 /* Add VARIABLE and DIE into deferred locations list. */
15895
15896 static void
15897 defer_location (tree variable, dw_die_ref die)
15898 {
15899 deferred_locations entry;
15900 entry.variable = variable;
15901 entry.die = die;
15902 vec_safe_push (deferred_locations_list, entry);
15903 }
15904
15905 /* Helper function for tree_add_const_value_attribute. Natively encode
15906 initializer INIT into an array. Return true if successful. */
15907
15908 static bool
15909 native_encode_initializer (tree init, unsigned char *array, int size)
15910 {
15911 tree type;
15912
15913 if (init == NULL_TREE)
15914 return false;
15915
15916 STRIP_NOPS (init);
15917 switch (TREE_CODE (init))
15918 {
15919 case STRING_CST:
15920 type = TREE_TYPE (init);
15921 if (TREE_CODE (type) == ARRAY_TYPE)
15922 {
15923 tree enttype = TREE_TYPE (type);
15924 enum machine_mode mode = TYPE_MODE (enttype);
15925
15926 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15927 return false;
15928 if (int_size_in_bytes (type) != size)
15929 return false;
15930 if (size > TREE_STRING_LENGTH (init))
15931 {
15932 memcpy (array, TREE_STRING_POINTER (init),
15933 TREE_STRING_LENGTH (init));
15934 memset (array + TREE_STRING_LENGTH (init),
15935 '\0', size - TREE_STRING_LENGTH (init));
15936 }
15937 else
15938 memcpy (array, TREE_STRING_POINTER (init), size);
15939 return true;
15940 }
15941 return false;
15942 case CONSTRUCTOR:
15943 type = TREE_TYPE (init);
15944 if (int_size_in_bytes (type) != size)
15945 return false;
15946 if (TREE_CODE (type) == ARRAY_TYPE)
15947 {
15948 HOST_WIDE_INT min_index;
15949 unsigned HOST_WIDE_INT cnt;
15950 int curpos = 0, fieldsize;
15951 constructor_elt *ce;
15952
15953 if (TYPE_DOMAIN (type) == NULL_TREE
15954 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15955 return false;
15956
15957 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15958 if (fieldsize <= 0)
15959 return false;
15960
15961 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15962 memset (array, '\0', size);
15963 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15964 {
15965 tree val = ce->value;
15966 tree index = ce->index;
15967 int pos = curpos;
15968 if (index && TREE_CODE (index) == RANGE_EXPR)
15969 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15970 * fieldsize;
15971 else if (index)
15972 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15973
15974 if (val)
15975 {
15976 STRIP_NOPS (val);
15977 if (!native_encode_initializer (val, array + pos, fieldsize))
15978 return false;
15979 }
15980 curpos = pos + fieldsize;
15981 if (index && TREE_CODE (index) == RANGE_EXPR)
15982 {
15983 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15984 - tree_to_shwi (TREE_OPERAND (index, 0));
15985 while (count-- > 0)
15986 {
15987 if (val)
15988 memcpy (array + curpos, array + pos, fieldsize);
15989 curpos += fieldsize;
15990 }
15991 }
15992 gcc_assert (curpos <= size);
15993 }
15994 return true;
15995 }
15996 else if (TREE_CODE (type) == RECORD_TYPE
15997 || TREE_CODE (type) == UNION_TYPE)
15998 {
15999 tree field = NULL_TREE;
16000 unsigned HOST_WIDE_INT cnt;
16001 constructor_elt *ce;
16002
16003 if (int_size_in_bytes (type) != size)
16004 return false;
16005
16006 if (TREE_CODE (type) == RECORD_TYPE)
16007 field = TYPE_FIELDS (type);
16008
16009 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16010 {
16011 tree val = ce->value;
16012 int pos, fieldsize;
16013
16014 if (ce->index != 0)
16015 field = ce->index;
16016
16017 if (val)
16018 STRIP_NOPS (val);
16019
16020 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16021 return false;
16022
16023 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16024 && TYPE_DOMAIN (TREE_TYPE (field))
16025 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16026 return false;
16027 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16028 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16029 return false;
16030 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16031 pos = int_byte_position (field);
16032 gcc_assert (pos + fieldsize <= size);
16033 if (val
16034 && !native_encode_initializer (val, array + pos, fieldsize))
16035 return false;
16036 }
16037 return true;
16038 }
16039 return false;
16040 case VIEW_CONVERT_EXPR:
16041 case NON_LVALUE_EXPR:
16042 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16043 default:
16044 return native_encode_expr (init, array, size) == size;
16045 }
16046 }
16047
16048 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16049 attribute is the const value T. */
16050
16051 static bool
16052 tree_add_const_value_attribute (dw_die_ref die, tree t)
16053 {
16054 tree init;
16055 tree type = TREE_TYPE (t);
16056 rtx rtl;
16057
16058 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16059 return false;
16060
16061 init = t;
16062 gcc_assert (!DECL_P (init));
16063
16064 rtl = rtl_for_decl_init (init, type);
16065 if (rtl)
16066 return add_const_value_attribute (die, rtl);
16067 /* If the host and target are sane, try harder. */
16068 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16069 && initializer_constant_valid_p (init, type))
16070 {
16071 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16072 if (size > 0 && (int) size == size)
16073 {
16074 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16075
16076 if (native_encode_initializer (init, array, size))
16077 {
16078 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16079 return true;
16080 }
16081 ggc_free (array);
16082 }
16083 }
16084 return false;
16085 }
16086
16087 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16088 attribute is the const value of T, where T is an integral constant
16089 variable with static storage duration
16090 (so it can't be a PARM_DECL or a RESULT_DECL). */
16091
16092 static bool
16093 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16094 {
16095
16096 if (!decl
16097 || (TREE_CODE (decl) != VAR_DECL
16098 && TREE_CODE (decl) != CONST_DECL)
16099 || (TREE_CODE (decl) == VAR_DECL
16100 && !TREE_STATIC (decl)))
16101 return false;
16102
16103 if (TREE_READONLY (decl)
16104 && ! TREE_THIS_VOLATILE (decl)
16105 && DECL_INITIAL (decl))
16106 /* OK */;
16107 else
16108 return false;
16109
16110 /* Don't add DW_AT_const_value if abstract origin already has one. */
16111 if (get_AT (var_die, DW_AT_const_value))
16112 return false;
16113
16114 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16115 }
16116
16117 /* Convert the CFI instructions for the current function into a
16118 location list. This is used for DW_AT_frame_base when we targeting
16119 a dwarf2 consumer that does not support the dwarf3
16120 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16121 expressions. */
16122
16123 static dw_loc_list_ref
16124 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16125 {
16126 int ix;
16127 dw_fde_ref fde;
16128 dw_loc_list_ref list, *list_tail;
16129 dw_cfi_ref cfi;
16130 dw_cfa_location last_cfa, next_cfa;
16131 const char *start_label, *last_label, *section;
16132 dw_cfa_location remember;
16133
16134 fde = cfun->fde;
16135 gcc_assert (fde != NULL);
16136
16137 section = secname_for_decl (current_function_decl);
16138 list_tail = &list;
16139 list = NULL;
16140
16141 memset (&next_cfa, 0, sizeof (next_cfa));
16142 next_cfa.reg = INVALID_REGNUM;
16143 remember = next_cfa;
16144
16145 start_label = fde->dw_fde_begin;
16146
16147 /* ??? Bald assumption that the CIE opcode list does not contain
16148 advance opcodes. */
16149 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16150 lookup_cfa_1 (cfi, &next_cfa, &remember);
16151
16152 last_cfa = next_cfa;
16153 last_label = start_label;
16154
16155 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16156 {
16157 /* If the first partition contained no CFI adjustments, the
16158 CIE opcodes apply to the whole first partition. */
16159 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16160 fde->dw_fde_begin, fde->dw_fde_end, section);
16161 list_tail =&(*list_tail)->dw_loc_next;
16162 start_label = last_label = fde->dw_fde_second_begin;
16163 }
16164
16165 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16166 {
16167 switch (cfi->dw_cfi_opc)
16168 {
16169 case DW_CFA_set_loc:
16170 case DW_CFA_advance_loc1:
16171 case DW_CFA_advance_loc2:
16172 case DW_CFA_advance_loc4:
16173 if (!cfa_equal_p (&last_cfa, &next_cfa))
16174 {
16175 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16176 start_label, last_label, section);
16177
16178 list_tail = &(*list_tail)->dw_loc_next;
16179 last_cfa = next_cfa;
16180 start_label = last_label;
16181 }
16182 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16183 break;
16184
16185 case DW_CFA_advance_loc:
16186 /* The encoding is complex enough that we should never emit this. */
16187 gcc_unreachable ();
16188
16189 default:
16190 lookup_cfa_1 (cfi, &next_cfa, &remember);
16191 break;
16192 }
16193 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16194 {
16195 if (!cfa_equal_p (&last_cfa, &next_cfa))
16196 {
16197 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16198 start_label, last_label, section);
16199
16200 list_tail = &(*list_tail)->dw_loc_next;
16201 last_cfa = next_cfa;
16202 start_label = last_label;
16203 }
16204 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16205 start_label, fde->dw_fde_end, section);
16206 list_tail = &(*list_tail)->dw_loc_next;
16207 start_label = last_label = fde->dw_fde_second_begin;
16208 }
16209 }
16210
16211 if (!cfa_equal_p (&last_cfa, &next_cfa))
16212 {
16213 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16214 start_label, last_label, section);
16215 list_tail = &(*list_tail)->dw_loc_next;
16216 start_label = last_label;
16217 }
16218
16219 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16220 start_label,
16221 fde->dw_fde_second_begin
16222 ? fde->dw_fde_second_end : fde->dw_fde_end,
16223 section);
16224
16225 if (list && list->dw_loc_next)
16226 gen_llsym (list);
16227
16228 return list;
16229 }
16230
16231 /* Compute a displacement from the "steady-state frame pointer" to the
16232 frame base (often the same as the CFA), and store it in
16233 frame_pointer_fb_offset. OFFSET is added to the displacement
16234 before the latter is negated. */
16235
16236 static void
16237 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16238 {
16239 rtx reg, elim;
16240
16241 #ifdef FRAME_POINTER_CFA_OFFSET
16242 reg = frame_pointer_rtx;
16243 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16244 #else
16245 reg = arg_pointer_rtx;
16246 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16247 #endif
16248
16249 elim = (ira_use_lra_p
16250 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16251 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16252 if (GET_CODE (elim) == PLUS)
16253 {
16254 offset += INTVAL (XEXP (elim, 1));
16255 elim = XEXP (elim, 0);
16256 }
16257
16258 frame_pointer_fb_offset = -offset;
16259
16260 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16261 in which to eliminate. This is because it's stack pointer isn't
16262 directly accessible as a register within the ISA. To work around
16263 this, assume that while we cannot provide a proper value for
16264 frame_pointer_fb_offset, we won't need one either. */
16265 frame_pointer_fb_offset_valid
16266 = ((SUPPORTS_STACK_ALIGNMENT
16267 && (elim == hard_frame_pointer_rtx
16268 || elim == stack_pointer_rtx))
16269 || elim == (frame_pointer_needed
16270 ? hard_frame_pointer_rtx
16271 : stack_pointer_rtx));
16272 }
16273
16274 /* Generate a DW_AT_name attribute given some string value to be included as
16275 the value of the attribute. */
16276
16277 static void
16278 add_name_attribute (dw_die_ref die, const char *name_string)
16279 {
16280 if (name_string != NULL && *name_string != 0)
16281 {
16282 if (demangle_name_func)
16283 name_string = (*demangle_name_func) (name_string);
16284
16285 add_AT_string (die, DW_AT_name, name_string);
16286 }
16287 }
16288
16289 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16290 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16291 of TYPE accordingly.
16292
16293 ??? This is a temporary measure until after we're able to generate
16294 regular DWARF for the complex Ada type system. */
16295
16296 static void
16297 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16298 dw_die_ref context_die)
16299 {
16300 tree dtype;
16301 dw_die_ref dtype_die;
16302
16303 if (!lang_hooks.types.descriptive_type)
16304 return;
16305
16306 dtype = lang_hooks.types.descriptive_type (type);
16307 if (!dtype)
16308 return;
16309
16310 dtype_die = lookup_type_die (dtype);
16311 if (!dtype_die)
16312 {
16313 gen_type_die (dtype, context_die);
16314 dtype_die = lookup_type_die (dtype);
16315 gcc_assert (dtype_die);
16316 }
16317
16318 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16319 }
16320
16321 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16322
16323 static const char *
16324 comp_dir_string (void)
16325 {
16326 const char *wd;
16327 char *wd1;
16328 static const char *cached_wd = NULL;
16329
16330 if (cached_wd != NULL)
16331 return cached_wd;
16332
16333 wd = get_src_pwd ();
16334 if (wd == NULL)
16335 return NULL;
16336
16337 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16338 {
16339 int wdlen;
16340
16341 wdlen = strlen (wd);
16342 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16343 strcpy (wd1, wd);
16344 wd1 [wdlen] = DIR_SEPARATOR;
16345 wd1 [wdlen + 1] = 0;
16346 wd = wd1;
16347 }
16348
16349 cached_wd = remap_debug_filename (wd);
16350 return cached_wd;
16351 }
16352
16353 /* Generate a DW_AT_comp_dir attribute for DIE. */
16354
16355 static void
16356 add_comp_dir_attribute (dw_die_ref die)
16357 {
16358 const char * wd = comp_dir_string ();
16359 if (wd != NULL)
16360 add_AT_string (die, DW_AT_comp_dir, wd);
16361 }
16362
16363 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16364 default. */
16365
16366 static int
16367 lower_bound_default (void)
16368 {
16369 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16370 {
16371 case DW_LANG_C:
16372 case DW_LANG_C89:
16373 case DW_LANG_C99:
16374 case DW_LANG_C_plus_plus:
16375 case DW_LANG_ObjC:
16376 case DW_LANG_ObjC_plus_plus:
16377 case DW_LANG_Java:
16378 return 0;
16379 case DW_LANG_Fortran77:
16380 case DW_LANG_Fortran90:
16381 case DW_LANG_Fortran95:
16382 return 1;
16383 case DW_LANG_UPC:
16384 case DW_LANG_D:
16385 case DW_LANG_Python:
16386 return dwarf_version >= 4 ? 0 : -1;
16387 case DW_LANG_Ada95:
16388 case DW_LANG_Ada83:
16389 case DW_LANG_Cobol74:
16390 case DW_LANG_Cobol85:
16391 case DW_LANG_Pascal83:
16392 case DW_LANG_Modula2:
16393 case DW_LANG_PLI:
16394 return dwarf_version >= 4 ? 1 : -1;
16395 default:
16396 return -1;
16397 }
16398 }
16399
16400 /* Given a tree node describing an array bound (either lower or upper) output
16401 a representation for that bound. */
16402
16403 static void
16404 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16405 {
16406 switch (TREE_CODE (bound))
16407 {
16408 case ERROR_MARK:
16409 return;
16410
16411 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16412 case INTEGER_CST:
16413 {
16414 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16415 int dflt;
16416
16417 /* Use the default if possible. */
16418 if (bound_attr == DW_AT_lower_bound
16419 && tree_fits_shwi_p (bound)
16420 && (dflt = lower_bound_default ()) != -1
16421 && tree_to_shwi (bound) == dflt)
16422 ;
16423
16424 /* If HOST_WIDE_INT is big enough then represent the bound as
16425 a constant value. We need to choose a form based on
16426 whether the type is signed or unsigned. We cannot just
16427 call add_AT_unsigned if the value itself is positive
16428 (add_AT_unsigned might add the unsigned value encoded as
16429 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16430 bounds type and then sign extend any unsigned values found
16431 for signed types. This is needed only for
16432 DW_AT_{lower,upper}_bound, since for most other attributes,
16433 consumers will treat DW_FORM_data[1248] as unsigned values,
16434 regardless of the underlying type. */
16435 else if (prec <= HOST_BITS_PER_WIDE_INT
16436 || tree_fits_uhwi_p (bound))
16437 {
16438 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16439 add_AT_unsigned (subrange_die, bound_attr,
16440 TREE_INT_CST_LOW (bound));
16441 else
16442 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16443 }
16444 else
16445 /* Otherwise represent the bound as an unsigned value with
16446 the precision of its type. The precision and signedness
16447 of the type will be necessary to re-interpret it
16448 unambiguously. */
16449 add_AT_wide (subrange_die, bound_attr, bound);
16450 }
16451 break;
16452
16453 CASE_CONVERT:
16454 case VIEW_CONVERT_EXPR:
16455 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16456 break;
16457
16458 case SAVE_EXPR:
16459 break;
16460
16461 case VAR_DECL:
16462 case PARM_DECL:
16463 case RESULT_DECL:
16464 {
16465 dw_die_ref decl_die = lookup_decl_die (bound);
16466
16467 /* ??? Can this happen, or should the variable have been bound
16468 first? Probably it can, since I imagine that we try to create
16469 the types of parameters in the order in which they exist in
16470 the list, and won't have created a forward reference to a
16471 later parameter. */
16472 if (decl_die != NULL)
16473 {
16474 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16475 break;
16476 }
16477 }
16478 /* FALLTHRU */
16479
16480 default:
16481 {
16482 /* Otherwise try to create a stack operation procedure to
16483 evaluate the value of the array bound. */
16484
16485 dw_die_ref ctx, decl_die;
16486 dw_loc_list_ref list;
16487
16488 list = loc_list_from_tree (bound, 2);
16489 if (list == NULL || single_element_loc_list_p (list))
16490 {
16491 /* If DW_AT_*bound is not a reference nor constant, it is
16492 a DWARF expression rather than location description.
16493 For that loc_list_from_tree (bound, 0) is needed.
16494 If that fails to give a single element list,
16495 fall back to outputting this as a reference anyway. */
16496 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16497 if (list2 && single_element_loc_list_p (list2))
16498 {
16499 add_AT_loc (subrange_die, bound_attr, list2->expr);
16500 break;
16501 }
16502 }
16503 if (list == NULL)
16504 break;
16505
16506 if (current_function_decl == 0)
16507 ctx = comp_unit_die ();
16508 else
16509 ctx = lookup_decl_die (current_function_decl);
16510
16511 decl_die = new_die (DW_TAG_variable, ctx, bound);
16512 add_AT_flag (decl_die, DW_AT_artificial, 1);
16513 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16514 add_AT_location_description (decl_die, DW_AT_location, list);
16515 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16516 break;
16517 }
16518 }
16519 }
16520
16521 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16522 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16523 Note that the block of subscript information for an array type also
16524 includes information about the element type of the given array type. */
16525
16526 static void
16527 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16528 {
16529 unsigned dimension_number;
16530 tree lower, upper;
16531 dw_die_ref subrange_die;
16532
16533 for (dimension_number = 0;
16534 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16535 type = TREE_TYPE (type), dimension_number++)
16536 {
16537 tree domain = TYPE_DOMAIN (type);
16538
16539 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16540 break;
16541
16542 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16543 and (in GNU C only) variable bounds. Handle all three forms
16544 here. */
16545 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16546 if (domain)
16547 {
16548 /* We have an array type with specified bounds. */
16549 lower = TYPE_MIN_VALUE (domain);
16550 upper = TYPE_MAX_VALUE (domain);
16551
16552 /* Define the index type. */
16553 if (TREE_TYPE (domain))
16554 {
16555 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16556 TREE_TYPE field. We can't emit debug info for this
16557 because it is an unnamed integral type. */
16558 if (TREE_CODE (domain) == INTEGER_TYPE
16559 && TYPE_NAME (domain) == NULL_TREE
16560 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16561 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16562 ;
16563 else
16564 add_type_attribute (subrange_die, TREE_TYPE (domain),
16565 TYPE_UNQUALIFIED, type_die);
16566 }
16567
16568 /* ??? If upper is NULL, the array has unspecified length,
16569 but it does have a lower bound. This happens with Fortran
16570 dimension arr(N:*)
16571 Since the debugger is definitely going to need to know N
16572 to produce useful results, go ahead and output the lower
16573 bound solo, and hope the debugger can cope. */
16574
16575 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16576 if (upper)
16577 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16578 }
16579
16580 /* Otherwise we have an array type with an unspecified length. The
16581 DWARF-2 spec does not say how to handle this; let's just leave out the
16582 bounds. */
16583 }
16584 }
16585
16586 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16587
16588 static void
16589 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16590 {
16591 dw_die_ref decl_die;
16592 HOST_WIDE_INT size;
16593
16594 switch (TREE_CODE (tree_node))
16595 {
16596 case ERROR_MARK:
16597 size = 0;
16598 break;
16599 case ENUMERAL_TYPE:
16600 case RECORD_TYPE:
16601 case UNION_TYPE:
16602 case QUAL_UNION_TYPE:
16603 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16604 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16605 {
16606 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16607 return;
16608 }
16609 size = int_size_in_bytes (tree_node);
16610 break;
16611 case FIELD_DECL:
16612 /* For a data member of a struct or union, the DW_AT_byte_size is
16613 generally given as the number of bytes normally allocated for an
16614 object of the *declared* type of the member itself. This is true
16615 even for bit-fields. */
16616 size = int_size_in_bytes (field_type (tree_node));
16617 break;
16618 default:
16619 gcc_unreachable ();
16620 }
16621
16622 /* Note that `size' might be -1 when we get to this point. If it is, that
16623 indicates that the byte size of the entity in question is variable. We
16624 have no good way of expressing this fact in Dwarf at the present time,
16625 when location description was not used by the caller code instead. */
16626 if (size >= 0)
16627 add_AT_unsigned (die, DW_AT_byte_size, size);
16628 }
16629
16630 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16631 which specifies the distance in bits from the highest order bit of the
16632 "containing object" for the bit-field to the highest order bit of the
16633 bit-field itself.
16634
16635 For any given bit-field, the "containing object" is a hypothetical object
16636 (of some integral or enum type) within which the given bit-field lives. The
16637 type of this hypothetical "containing object" is always the same as the
16638 declared type of the individual bit-field itself. The determination of the
16639 exact location of the "containing object" for a bit-field is rather
16640 complicated. It's handled by the `field_byte_offset' function (above).
16641
16642 Note that it is the size (in bytes) of the hypothetical "containing object"
16643 which will be given in the DW_AT_byte_size attribute for this bit-field.
16644 (See `byte_size_attribute' above). */
16645
16646 static inline void
16647 add_bit_offset_attribute (dw_die_ref die, tree decl)
16648 {
16649 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16650 tree type = DECL_BIT_FIELD_TYPE (decl);
16651 HOST_WIDE_INT bitpos_int;
16652 HOST_WIDE_INT highest_order_object_bit_offset;
16653 HOST_WIDE_INT highest_order_field_bit_offset;
16654 HOST_WIDE_INT bit_offset;
16655
16656 /* Must be a field and a bit field. */
16657 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16658
16659 /* We can't yet handle bit-fields whose offsets are variable, so if we
16660 encounter such things, just return without generating any attribute
16661 whatsoever. Likewise for variable or too large size. */
16662 if (! tree_fits_shwi_p (bit_position (decl))
16663 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16664 return;
16665
16666 bitpos_int = int_bit_position (decl);
16667
16668 /* Note that the bit offset is always the distance (in bits) from the
16669 highest-order bit of the "containing object" to the highest-order bit of
16670 the bit-field itself. Since the "high-order end" of any object or field
16671 is different on big-endian and little-endian machines, the computation
16672 below must take account of these differences. */
16673 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16674 highest_order_field_bit_offset = bitpos_int;
16675
16676 if (! BYTES_BIG_ENDIAN)
16677 {
16678 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16679 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16680 }
16681
16682 bit_offset
16683 = (! BYTES_BIG_ENDIAN
16684 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16685 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16686
16687 if (bit_offset < 0)
16688 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16689 else
16690 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16691 }
16692
16693 /* For a FIELD_DECL node which represents a bit field, output an attribute
16694 which specifies the length in bits of the given field. */
16695
16696 static inline void
16697 add_bit_size_attribute (dw_die_ref die, tree decl)
16698 {
16699 /* Must be a field and a bit field. */
16700 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16701 && DECL_BIT_FIELD_TYPE (decl));
16702
16703 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16704 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16705 }
16706
16707 /* If the compiled language is ANSI C, then add a 'prototyped'
16708 attribute, if arg types are given for the parameters of a function. */
16709
16710 static inline void
16711 add_prototyped_attribute (dw_die_ref die, tree func_type)
16712 {
16713 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16714 && prototype_p (func_type))
16715 add_AT_flag (die, DW_AT_prototyped, 1);
16716 }
16717
16718 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16719 by looking in either the type declaration or object declaration
16720 equate table. */
16721
16722 static inline dw_die_ref
16723 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16724 {
16725 dw_die_ref origin_die = NULL;
16726
16727 if (TREE_CODE (origin) != FUNCTION_DECL)
16728 {
16729 /* We may have gotten separated from the block for the inlined
16730 function, if we're in an exception handler or some such; make
16731 sure that the abstract function has been written out.
16732
16733 Doing this for nested functions is wrong, however; functions are
16734 distinct units, and our context might not even be inline. */
16735 tree fn = origin;
16736
16737 if (TYPE_P (fn))
16738 fn = TYPE_STUB_DECL (fn);
16739
16740 fn = decl_function_context (fn);
16741 if (fn)
16742 dwarf2out_abstract_function (fn);
16743 }
16744
16745 if (DECL_P (origin))
16746 origin_die = lookup_decl_die (origin);
16747 else if (TYPE_P (origin))
16748 origin_die = lookup_type_die (origin);
16749
16750 /* XXX: Functions that are never lowered don't always have correct block
16751 trees (in the case of java, they simply have no block tree, in some other
16752 languages). For these functions, there is nothing we can really do to
16753 output correct debug info for inlined functions in all cases. Rather
16754 than die, we'll just produce deficient debug info now, in that we will
16755 have variables without a proper abstract origin. In the future, when all
16756 functions are lowered, we should re-add a gcc_assert (origin_die)
16757 here. */
16758
16759 if (origin_die)
16760 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16761 return origin_die;
16762 }
16763
16764 /* We do not currently support the pure_virtual attribute. */
16765
16766 static inline void
16767 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16768 {
16769 if (DECL_VINDEX (func_decl))
16770 {
16771 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16772
16773 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16774 add_AT_loc (die, DW_AT_vtable_elem_location,
16775 new_loc_descr (DW_OP_constu,
16776 tree_to_shwi (DECL_VINDEX (func_decl)),
16777 0));
16778
16779 /* GNU extension: Record what type this method came from originally. */
16780 if (debug_info_level > DINFO_LEVEL_TERSE
16781 && DECL_CONTEXT (func_decl))
16782 add_AT_die_ref (die, DW_AT_containing_type,
16783 lookup_type_die (DECL_CONTEXT (func_decl)));
16784 }
16785 }
16786 \f
16787 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16788 given decl. This used to be a vendor extension until after DWARF 4
16789 standardized it. */
16790
16791 static void
16792 add_linkage_attr (dw_die_ref die, tree decl)
16793 {
16794 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16795
16796 /* Mimic what assemble_name_raw does with a leading '*'. */
16797 if (name[0] == '*')
16798 name = &name[1];
16799
16800 if (dwarf_version >= 4)
16801 add_AT_string (die, DW_AT_linkage_name, name);
16802 else
16803 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16804 }
16805
16806 /* Add source coordinate attributes for the given decl. */
16807
16808 static void
16809 add_src_coords_attributes (dw_die_ref die, tree decl)
16810 {
16811 expanded_location s;
16812
16813 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16814 return;
16815 s = expand_location (DECL_SOURCE_LOCATION (decl));
16816 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16817 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16818 }
16819
16820 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16821
16822 static void
16823 add_linkage_name (dw_die_ref die, tree decl)
16824 {
16825 if (debug_info_level > DINFO_LEVEL_NONE
16826 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16827 && TREE_PUBLIC (decl)
16828 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16829 && die->die_tag != DW_TAG_member)
16830 {
16831 /* Defer until we have an assembler name set. */
16832 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16833 {
16834 limbo_die_node *asm_name;
16835
16836 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16837 asm_name->die = die;
16838 asm_name->created_for = decl;
16839 asm_name->next = deferred_asm_name;
16840 deferred_asm_name = asm_name;
16841 }
16842 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16843 add_linkage_attr (die, decl);
16844 }
16845 }
16846
16847 /* Add a DW_AT_name attribute and source coordinate attribute for the
16848 given decl, but only if it actually has a name. */
16849
16850 static void
16851 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16852 {
16853 tree decl_name;
16854
16855 decl_name = DECL_NAME (decl);
16856 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16857 {
16858 const char *name = dwarf2_name (decl, 0);
16859 if (name)
16860 add_name_attribute (die, name);
16861 if (! DECL_ARTIFICIAL (decl))
16862 add_src_coords_attributes (die, decl);
16863
16864 add_linkage_name (die, decl);
16865 }
16866
16867 #ifdef VMS_DEBUGGING_INFO
16868 /* Get the function's name, as described by its RTL. This may be different
16869 from the DECL_NAME name used in the source file. */
16870 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16871 {
16872 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16873 XEXP (DECL_RTL (decl), 0), false);
16874 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16875 }
16876 #endif /* VMS_DEBUGGING_INFO */
16877 }
16878
16879 #ifdef VMS_DEBUGGING_INFO
16880 /* Output the debug main pointer die for VMS */
16881
16882 void
16883 dwarf2out_vms_debug_main_pointer (void)
16884 {
16885 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16886 dw_die_ref die;
16887
16888 /* Allocate the VMS debug main subprogram die. */
16889 die = ggc_cleared_alloc<die_node> ();
16890 die->die_tag = DW_TAG_subprogram;
16891 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16892 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16893 current_function_funcdef_no);
16894 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16895
16896 /* Make it the first child of comp_unit_die (). */
16897 die->die_parent = comp_unit_die ();
16898 if (comp_unit_die ()->die_child)
16899 {
16900 die->die_sib = comp_unit_die ()->die_child->die_sib;
16901 comp_unit_die ()->die_child->die_sib = die;
16902 }
16903 else
16904 {
16905 die->die_sib = die;
16906 comp_unit_die ()->die_child = die;
16907 }
16908 }
16909 #endif /* VMS_DEBUGGING_INFO */
16910
16911 /* Push a new declaration scope. */
16912
16913 static void
16914 push_decl_scope (tree scope)
16915 {
16916 vec_safe_push (decl_scope_table, scope);
16917 }
16918
16919 /* Pop a declaration scope. */
16920
16921 static inline void
16922 pop_decl_scope (void)
16923 {
16924 decl_scope_table->pop ();
16925 }
16926
16927 /* walk_tree helper function for uses_local_type, below. */
16928
16929 static tree
16930 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16931 {
16932 if (!TYPE_P (*tp))
16933 *walk_subtrees = 0;
16934 else
16935 {
16936 tree name = TYPE_NAME (*tp);
16937 if (name && DECL_P (name) && decl_function_context (name))
16938 return *tp;
16939 }
16940 return NULL_TREE;
16941 }
16942
16943 /* If TYPE involves a function-local type (including a local typedef to a
16944 non-local type), returns that type; otherwise returns NULL_TREE. */
16945
16946 static tree
16947 uses_local_type (tree type)
16948 {
16949 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16950 return used;
16951 }
16952
16953 /* Return the DIE for the scope that immediately contains this type.
16954 Non-named types that do not involve a function-local type get global
16955 scope. Named types nested in namespaces or other types get their
16956 containing scope. All other types (i.e. function-local named types) get
16957 the current active scope. */
16958
16959 static dw_die_ref
16960 scope_die_for (tree t, dw_die_ref context_die)
16961 {
16962 dw_die_ref scope_die = NULL;
16963 tree containing_scope;
16964
16965 /* Non-types always go in the current scope. */
16966 gcc_assert (TYPE_P (t));
16967
16968 /* Use the scope of the typedef, rather than the scope of the type
16969 it refers to. */
16970 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16971 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16972 else
16973 containing_scope = TYPE_CONTEXT (t);
16974
16975 /* Use the containing namespace if there is one. */
16976 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16977 {
16978 if (context_die == lookup_decl_die (containing_scope))
16979 /* OK */;
16980 else if (debug_info_level > DINFO_LEVEL_TERSE)
16981 context_die = get_context_die (containing_scope);
16982 else
16983 containing_scope = NULL_TREE;
16984 }
16985
16986 /* Ignore function type "scopes" from the C frontend. They mean that
16987 a tagged type is local to a parmlist of a function declarator, but
16988 that isn't useful to DWARF. */
16989 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16990 containing_scope = NULL_TREE;
16991
16992 if (SCOPE_FILE_SCOPE_P (containing_scope))
16993 {
16994 /* If T uses a local type keep it local as well, to avoid references
16995 to function-local DIEs from outside the function. */
16996 if (current_function_decl && uses_local_type (t))
16997 scope_die = context_die;
16998 else
16999 scope_die = comp_unit_die ();
17000 }
17001 else if (TYPE_P (containing_scope))
17002 {
17003 /* For types, we can just look up the appropriate DIE. */
17004 if (debug_info_level > DINFO_LEVEL_TERSE)
17005 scope_die = get_context_die (containing_scope);
17006 else
17007 {
17008 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17009 if (scope_die == NULL)
17010 scope_die = comp_unit_die ();
17011 }
17012 }
17013 else
17014 scope_die = context_die;
17015
17016 return scope_die;
17017 }
17018
17019 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17020
17021 static inline int
17022 local_scope_p (dw_die_ref context_die)
17023 {
17024 for (; context_die; context_die = context_die->die_parent)
17025 if (context_die->die_tag == DW_TAG_inlined_subroutine
17026 || context_die->die_tag == DW_TAG_subprogram)
17027 return 1;
17028
17029 return 0;
17030 }
17031
17032 /* Returns nonzero if CONTEXT_DIE is a class. */
17033
17034 static inline int
17035 class_scope_p (dw_die_ref context_die)
17036 {
17037 return (context_die
17038 && (context_die->die_tag == DW_TAG_structure_type
17039 || context_die->die_tag == DW_TAG_class_type
17040 || context_die->die_tag == DW_TAG_interface_type
17041 || context_die->die_tag == DW_TAG_union_type));
17042 }
17043
17044 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17045 whether or not to treat a DIE in this context as a declaration. */
17046
17047 static inline int
17048 class_or_namespace_scope_p (dw_die_ref context_die)
17049 {
17050 return (class_scope_p (context_die)
17051 || (context_die && context_die->die_tag == DW_TAG_namespace));
17052 }
17053
17054 /* Many forms of DIEs require a "type description" attribute. This
17055 routine locates the proper "type descriptor" die for the type given
17056 by 'type' plus any additional qualifiers given by 'cv_quals', and
17057 adds a DW_AT_type attribute below the given die. */
17058
17059 static void
17060 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17061 dw_die_ref context_die)
17062 {
17063 enum tree_code code = TREE_CODE (type);
17064 dw_die_ref type_die = NULL;
17065
17066 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17067 or fixed-point type, use the inner type. This is because we have no
17068 support for unnamed types in base_type_die. This can happen if this is
17069 an Ada subrange type. Correct solution is emit a subrange type die. */
17070 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17071 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17072 type = TREE_TYPE (type), code = TREE_CODE (type);
17073
17074 if (code == ERROR_MARK
17075 /* Handle a special case. For functions whose return type is void, we
17076 generate *no* type attribute. (Note that no object may have type
17077 `void', so this only applies to function return types). */
17078 || code == VOID_TYPE)
17079 return;
17080
17081 type_die = modified_type_die (type,
17082 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17083 context_die);
17084
17085 if (type_die != NULL)
17086 add_AT_die_ref (object_die, DW_AT_type, type_die);
17087 }
17088
17089 /* Given an object die, add the calling convention attribute for the
17090 function call type. */
17091 static void
17092 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17093 {
17094 enum dwarf_calling_convention value = DW_CC_normal;
17095
17096 value = ((enum dwarf_calling_convention)
17097 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17098
17099 if (is_fortran ()
17100 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17101 {
17102 /* DWARF 2 doesn't provide a way to identify a program's source-level
17103 entry point. DW_AT_calling_convention attributes are only meant
17104 to describe functions' calling conventions. However, lacking a
17105 better way to signal the Fortran main program, we used this for
17106 a long time, following existing custom. Now, DWARF 4 has
17107 DW_AT_main_subprogram, which we add below, but some tools still
17108 rely on the old way, which we thus keep. */
17109 value = DW_CC_program;
17110
17111 if (dwarf_version >= 4 || !dwarf_strict)
17112 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17113 }
17114
17115 /* Only add the attribute if the backend requests it, and
17116 is not DW_CC_normal. */
17117 if (value && (value != DW_CC_normal))
17118 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17119 }
17120
17121 /* Given a tree pointer to a struct, class, union, or enum type node, return
17122 a pointer to the (string) tag name for the given type, or zero if the type
17123 was declared without a tag. */
17124
17125 static const char *
17126 type_tag (const_tree type)
17127 {
17128 const char *name = 0;
17129
17130 if (TYPE_NAME (type) != 0)
17131 {
17132 tree t = 0;
17133
17134 /* Find the IDENTIFIER_NODE for the type name. */
17135 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17136 && !TYPE_NAMELESS (type))
17137 t = TYPE_NAME (type);
17138
17139 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17140 a TYPE_DECL node, regardless of whether or not a `typedef' was
17141 involved. */
17142 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17143 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17144 {
17145 /* We want to be extra verbose. Don't call dwarf_name if
17146 DECL_NAME isn't set. The default hook for decl_printable_name
17147 doesn't like that, and in this context it's correct to return
17148 0, instead of "<anonymous>" or the like. */
17149 if (DECL_NAME (TYPE_NAME (type))
17150 && !DECL_NAMELESS (TYPE_NAME (type)))
17151 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17152 }
17153
17154 /* Now get the name as a string, or invent one. */
17155 if (!name && t != 0)
17156 name = IDENTIFIER_POINTER (t);
17157 }
17158
17159 return (name == 0 || *name == '\0') ? 0 : name;
17160 }
17161
17162 /* Return the type associated with a data member, make a special check
17163 for bit field types. */
17164
17165 static inline tree
17166 member_declared_type (const_tree member)
17167 {
17168 return (DECL_BIT_FIELD_TYPE (member)
17169 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17170 }
17171
17172 /* Get the decl's label, as described by its RTL. This may be different
17173 from the DECL_NAME name used in the source file. */
17174
17175 #if 0
17176 static const char *
17177 decl_start_label (tree decl)
17178 {
17179 rtx x;
17180 const char *fnname;
17181
17182 x = DECL_RTL (decl);
17183 gcc_assert (MEM_P (x));
17184
17185 x = XEXP (x, 0);
17186 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17187
17188 fnname = XSTR (x, 0);
17189 return fnname;
17190 }
17191 #endif
17192 \f
17193 /* These routines generate the internal representation of the DIE's for
17194 the compilation unit. Debugging information is collected by walking
17195 the declaration trees passed in from dwarf2out_decl(). */
17196
17197 static void
17198 gen_array_type_die (tree type, dw_die_ref context_die)
17199 {
17200 dw_die_ref scope_die = scope_die_for (type, context_die);
17201 dw_die_ref array_die;
17202
17203 /* GNU compilers represent multidimensional array types as sequences of one
17204 dimensional array types whose element types are themselves array types.
17205 We sometimes squish that down to a single array_type DIE with multiple
17206 subscripts in the Dwarf debugging info. The draft Dwarf specification
17207 say that we are allowed to do this kind of compression in C, because
17208 there is no difference between an array of arrays and a multidimensional
17209 array. We don't do this for Ada to remain as close as possible to the
17210 actual representation, which is especially important against the language
17211 flexibilty wrt arrays of variable size. */
17212
17213 bool collapse_nested_arrays = !is_ada ();
17214 tree element_type;
17215
17216 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17217 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17218 if (TYPE_STRING_FLAG (type)
17219 && TREE_CODE (type) == ARRAY_TYPE
17220 && is_fortran ()
17221 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17222 {
17223 HOST_WIDE_INT size;
17224
17225 array_die = new_die (DW_TAG_string_type, scope_die, type);
17226 add_name_attribute (array_die, type_tag (type));
17227 equate_type_number_to_die (type, array_die);
17228 size = int_size_in_bytes (type);
17229 if (size >= 0)
17230 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17231 else if (TYPE_DOMAIN (type) != NULL_TREE
17232 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17233 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17234 {
17235 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17236 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17237
17238 size = int_size_in_bytes (TREE_TYPE (szdecl));
17239 if (loc && size > 0)
17240 {
17241 add_AT_location_description (array_die, DW_AT_string_length, loc);
17242 if (size != DWARF2_ADDR_SIZE)
17243 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17244 }
17245 }
17246 return;
17247 }
17248
17249 array_die = new_die (DW_TAG_array_type, scope_die, type);
17250 add_name_attribute (array_die, type_tag (type));
17251 equate_type_number_to_die (type, array_die);
17252
17253 if (TREE_CODE (type) == VECTOR_TYPE)
17254 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17255
17256 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17257 if (is_fortran ()
17258 && TREE_CODE (type) == ARRAY_TYPE
17259 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17260 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17261 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17262
17263 #if 0
17264 /* We default the array ordering. SDB will probably do
17265 the right things even if DW_AT_ordering is not present. It's not even
17266 an issue until we start to get into multidimensional arrays anyway. If
17267 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17268 then we'll have to put the DW_AT_ordering attribute back in. (But if
17269 and when we find out that we need to put these in, we will only do so
17270 for multidimensional arrays. */
17271 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17272 #endif
17273
17274 if (TREE_CODE (type) == VECTOR_TYPE)
17275 {
17276 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17277 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17278 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17279 add_bound_info (subrange_die, DW_AT_upper_bound,
17280 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17281 }
17282 else
17283 add_subscript_info (array_die, type, collapse_nested_arrays);
17284
17285 /* Add representation of the type of the elements of this array type and
17286 emit the corresponding DIE if we haven't done it already. */
17287 element_type = TREE_TYPE (type);
17288 if (collapse_nested_arrays)
17289 while (TREE_CODE (element_type) == ARRAY_TYPE)
17290 {
17291 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17292 break;
17293 element_type = TREE_TYPE (element_type);
17294 }
17295
17296 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17297
17298 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17299 if (TYPE_ARTIFICIAL (type))
17300 add_AT_flag (array_die, DW_AT_artificial, 1);
17301
17302 if (get_AT (array_die, DW_AT_name))
17303 add_pubtype (type, array_die);
17304 }
17305
17306 static dw_loc_descr_ref
17307 descr_info_loc (tree val, tree base_decl)
17308 {
17309 HOST_WIDE_INT size;
17310 dw_loc_descr_ref loc, loc2;
17311 enum dwarf_location_atom op;
17312
17313 if (val == base_decl)
17314 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17315
17316 switch (TREE_CODE (val))
17317 {
17318 CASE_CONVERT:
17319 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17320 case VAR_DECL:
17321 return loc_descriptor_from_tree (val, 0);
17322 case INTEGER_CST:
17323 if (tree_fits_shwi_p (val))
17324 return int_loc_descriptor (tree_to_shwi (val));
17325 break;
17326 case INDIRECT_REF:
17327 size = int_size_in_bytes (TREE_TYPE (val));
17328 if (size < 0)
17329 break;
17330 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17331 if (!loc)
17332 break;
17333 if (size == DWARF2_ADDR_SIZE)
17334 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17335 else
17336 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17337 return loc;
17338 case POINTER_PLUS_EXPR:
17339 case PLUS_EXPR:
17340 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17341 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17342 {
17343 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17344 if (!loc)
17345 break;
17346 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17347 }
17348 else
17349 {
17350 op = DW_OP_plus;
17351 do_binop:
17352 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17353 if (!loc)
17354 break;
17355 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17356 if (!loc2)
17357 break;
17358 add_loc_descr (&loc, loc2);
17359 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17360 }
17361 return loc;
17362 case MINUS_EXPR:
17363 op = DW_OP_minus;
17364 goto do_binop;
17365 case MULT_EXPR:
17366 op = DW_OP_mul;
17367 goto do_binop;
17368 case EQ_EXPR:
17369 op = DW_OP_eq;
17370 goto do_binop;
17371 case NE_EXPR:
17372 op = DW_OP_ne;
17373 goto do_binop;
17374 default:
17375 break;
17376 }
17377 return NULL;
17378 }
17379
17380 static void
17381 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17382 tree val, tree base_decl)
17383 {
17384 dw_loc_descr_ref loc;
17385
17386 if (tree_fits_shwi_p (val))
17387 {
17388 add_AT_unsigned (die, attr, tree_to_shwi (val));
17389 return;
17390 }
17391
17392 loc = descr_info_loc (val, base_decl);
17393 if (!loc)
17394 return;
17395
17396 add_AT_loc (die, attr, loc);
17397 }
17398
17399 /* This routine generates DIE for array with hidden descriptor, details
17400 are filled into *info by a langhook. */
17401
17402 static void
17403 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17404 dw_die_ref context_die)
17405 {
17406 dw_die_ref scope_die = scope_die_for (type, context_die);
17407 dw_die_ref array_die;
17408 int dim;
17409
17410 array_die = new_die (DW_TAG_array_type, scope_die, type);
17411 add_name_attribute (array_die, type_tag (type));
17412 equate_type_number_to_die (type, array_die);
17413
17414 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17415 if (is_fortran ()
17416 && info->ndimensions >= 2)
17417 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17418
17419 if (info->data_location)
17420 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17421 info->base_decl);
17422 if (info->associated)
17423 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17424 info->base_decl);
17425 if (info->allocated)
17426 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17427 info->base_decl);
17428
17429 for (dim = 0; dim < info->ndimensions; dim++)
17430 {
17431 dw_die_ref subrange_die
17432 = new_die (DW_TAG_subrange_type, array_die, NULL);
17433
17434 if (info->dimen[dim].lower_bound)
17435 {
17436 /* If it is the default value, omit it. */
17437 int dflt;
17438
17439 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17440 && (dflt = lower_bound_default ()) != -1
17441 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17442 ;
17443 else
17444 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17445 info->dimen[dim].lower_bound,
17446 info->base_decl);
17447 }
17448 if (info->dimen[dim].upper_bound)
17449 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17450 info->dimen[dim].upper_bound,
17451 info->base_decl);
17452 if (info->dimen[dim].stride)
17453 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17454 info->dimen[dim].stride,
17455 info->base_decl);
17456 }
17457
17458 gen_type_die (info->element_type, context_die);
17459 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17460 context_die);
17461
17462 if (get_AT (array_die, DW_AT_name))
17463 add_pubtype (type, array_die);
17464 }
17465
17466 #if 0
17467 static void
17468 gen_entry_point_die (tree decl, dw_die_ref context_die)
17469 {
17470 tree origin = decl_ultimate_origin (decl);
17471 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17472
17473 if (origin != NULL)
17474 add_abstract_origin_attribute (decl_die, origin);
17475 else
17476 {
17477 add_name_and_src_coords_attributes (decl_die, decl);
17478 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17479 TYPE_UNQUALIFIED, context_die);
17480 }
17481
17482 if (DECL_ABSTRACT (decl))
17483 equate_decl_number_to_die (decl, decl_die);
17484 else
17485 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17486 }
17487 #endif
17488
17489 /* Walk through the list of incomplete types again, trying once more to
17490 emit full debugging info for them. */
17491
17492 static void
17493 retry_incomplete_types (void)
17494 {
17495 int i;
17496
17497 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17498 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17499 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17500 }
17501
17502 /* Determine what tag to use for a record type. */
17503
17504 static enum dwarf_tag
17505 record_type_tag (tree type)
17506 {
17507 if (! lang_hooks.types.classify_record)
17508 return DW_TAG_structure_type;
17509
17510 switch (lang_hooks.types.classify_record (type))
17511 {
17512 case RECORD_IS_STRUCT:
17513 return DW_TAG_structure_type;
17514
17515 case RECORD_IS_CLASS:
17516 return DW_TAG_class_type;
17517
17518 case RECORD_IS_INTERFACE:
17519 if (dwarf_version >= 3 || !dwarf_strict)
17520 return DW_TAG_interface_type;
17521 return DW_TAG_structure_type;
17522
17523 default:
17524 gcc_unreachable ();
17525 }
17526 }
17527
17528 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17529 include all of the information about the enumeration values also. Each
17530 enumerated type name/value is listed as a child of the enumerated type
17531 DIE. */
17532
17533 static dw_die_ref
17534 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17535 {
17536 dw_die_ref type_die = lookup_type_die (type);
17537
17538 if (type_die == NULL)
17539 {
17540 type_die = new_die (DW_TAG_enumeration_type,
17541 scope_die_for (type, context_die), type);
17542 equate_type_number_to_die (type, type_die);
17543 add_name_attribute (type_die, type_tag (type));
17544 if (dwarf_version >= 4 || !dwarf_strict)
17545 {
17546 if (ENUM_IS_SCOPED (type))
17547 add_AT_flag (type_die, DW_AT_enum_class, 1);
17548 if (ENUM_IS_OPAQUE (type))
17549 add_AT_flag (type_die, DW_AT_declaration, 1);
17550 }
17551 }
17552 else if (! TYPE_SIZE (type))
17553 return type_die;
17554 else
17555 remove_AT (type_die, DW_AT_declaration);
17556
17557 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17558 given enum type is incomplete, do not generate the DW_AT_byte_size
17559 attribute or the DW_AT_element_list attribute. */
17560 if (TYPE_SIZE (type))
17561 {
17562 tree link;
17563
17564 TREE_ASM_WRITTEN (type) = 1;
17565 add_byte_size_attribute (type_die, type);
17566 if (dwarf_version >= 3 || !dwarf_strict)
17567 {
17568 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17569 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17570 context_die);
17571 }
17572 if (TYPE_STUB_DECL (type) != NULL_TREE)
17573 {
17574 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17575 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17576 }
17577
17578 /* If the first reference to this type was as the return type of an
17579 inline function, then it may not have a parent. Fix this now. */
17580 if (type_die->die_parent == NULL)
17581 add_child_die (scope_die_for (type, context_die), type_die);
17582
17583 for (link = TYPE_VALUES (type);
17584 link != NULL; link = TREE_CHAIN (link))
17585 {
17586 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17587 tree value = TREE_VALUE (link);
17588
17589 add_name_attribute (enum_die,
17590 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17591
17592 if (TREE_CODE (value) == CONST_DECL)
17593 value = DECL_INITIAL (value);
17594
17595 if (simple_type_size_in_bits (TREE_TYPE (value))
17596 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17597 {
17598 /* For constant forms created by add_AT_unsigned DWARF
17599 consumers (GDB, elfutils, etc.) always zero extend
17600 the value. Only when the actual value is negative
17601 do we need to use add_AT_int to generate a constant
17602 form that can represent negative values. */
17603 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17604 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17605 add_AT_unsigned (enum_die, DW_AT_const_value,
17606 (unsigned HOST_WIDE_INT) val);
17607 else
17608 add_AT_int (enum_die, DW_AT_const_value, val);
17609 }
17610 else
17611 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17612 that here. TODO: This should be re-worked to use correct
17613 signed/unsigned double tags for all cases. */
17614 add_AT_wide (enum_die, DW_AT_const_value, value);
17615 }
17616
17617 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17618 if (TYPE_ARTIFICIAL (type))
17619 add_AT_flag (type_die, DW_AT_artificial, 1);
17620 }
17621 else
17622 add_AT_flag (type_die, DW_AT_declaration, 1);
17623
17624 add_pubtype (type, type_die);
17625
17626 return type_die;
17627 }
17628
17629 /* Generate a DIE to represent either a real live formal parameter decl or to
17630 represent just the type of some formal parameter position in some function
17631 type.
17632
17633 Note that this routine is a bit unusual because its argument may be a
17634 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17635 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17636 node. If it's the former then this function is being called to output a
17637 DIE to represent a formal parameter object (or some inlining thereof). If
17638 it's the latter, then this function is only being called to output a
17639 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17640 argument type of some subprogram type.
17641 If EMIT_NAME_P is true, name and source coordinate attributes
17642 are emitted. */
17643
17644 static dw_die_ref
17645 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17646 dw_die_ref context_die)
17647 {
17648 tree node_or_origin = node ? node : origin;
17649 tree ultimate_origin;
17650 dw_die_ref parm_die;
17651
17652 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
17653 {
17654 parm_die = lookup_decl_die (node);
17655
17656 if (parm_die && parm_die->die_parent == NULL)
17657 {
17658 add_child_die (context_die, parm_die);
17659 /* XXX check that parm_die already has all the right attributes
17660 that we would add below? */
17661 return parm_die;
17662 }
17663 }
17664
17665 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
17666
17667 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17668 {
17669 case tcc_declaration:
17670 ultimate_origin = decl_ultimate_origin (node_or_origin);
17671 if (node || ultimate_origin)
17672 origin = ultimate_origin;
17673 if (origin != NULL && node != origin)
17674 add_abstract_origin_attribute (parm_die, origin);
17675 else if (emit_name_p)
17676 add_name_and_src_coords_attributes (parm_die, node);
17677 if (origin == NULL
17678 || (! DECL_ABSTRACT (node_or_origin)
17679 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17680 decl_function_context
17681 (node_or_origin))))
17682 {
17683 tree type = TREE_TYPE (node_or_origin);
17684 if (decl_by_reference_p (node_or_origin))
17685 add_type_attribute (parm_die, TREE_TYPE (type),
17686 TYPE_UNQUALIFIED, context_die);
17687 else
17688 add_type_attribute (parm_die, type,
17689 decl_quals (node_or_origin),
17690 context_die);
17691 }
17692 if (origin == NULL && DECL_ARTIFICIAL (node))
17693 add_AT_flag (parm_die, DW_AT_artificial, 1);
17694
17695 if (node && node != origin)
17696 equate_decl_number_to_die (node, parm_die);
17697 if (! DECL_ABSTRACT (node_or_origin))
17698 add_location_or_const_value_attribute (parm_die, node_or_origin,
17699 node == NULL, DW_AT_location);
17700
17701 break;
17702
17703 case tcc_type:
17704 /* We were called with some kind of a ..._TYPE node. */
17705 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17706 context_die);
17707 break;
17708
17709 default:
17710 gcc_unreachable ();
17711 }
17712
17713 return parm_die;
17714 }
17715
17716 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17717 children DW_TAG_formal_parameter DIEs representing the arguments of the
17718 parameter pack.
17719
17720 PARM_PACK must be a function parameter pack.
17721 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17722 must point to the subsequent arguments of the function PACK_ARG belongs to.
17723 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17724 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17725 following the last one for which a DIE was generated. */
17726
17727 static dw_die_ref
17728 gen_formal_parameter_pack_die (tree parm_pack,
17729 tree pack_arg,
17730 dw_die_ref subr_die,
17731 tree *next_arg)
17732 {
17733 tree arg;
17734 dw_die_ref parm_pack_die;
17735
17736 gcc_assert (parm_pack
17737 && lang_hooks.function_parameter_pack_p (parm_pack)
17738 && subr_die);
17739
17740 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17741 add_src_coords_attributes (parm_pack_die, parm_pack);
17742
17743 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17744 {
17745 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17746 parm_pack))
17747 break;
17748 gen_formal_parameter_die (arg, NULL,
17749 false /* Don't emit name attribute. */,
17750 parm_pack_die);
17751 }
17752 if (next_arg)
17753 *next_arg = arg;
17754 return parm_pack_die;
17755 }
17756
17757 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17758 at the end of an (ANSI prototyped) formal parameters list. */
17759
17760 static void
17761 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17762 {
17763 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17764 }
17765
17766 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17767 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17768 parameters as specified in some function type specification (except for
17769 those which appear as part of a function *definition*). */
17770
17771 static void
17772 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17773 {
17774 tree link;
17775 tree formal_type = NULL;
17776 tree first_parm_type;
17777 tree arg;
17778
17779 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17780 {
17781 arg = DECL_ARGUMENTS (function_or_method_type);
17782 function_or_method_type = TREE_TYPE (function_or_method_type);
17783 }
17784 else
17785 arg = NULL_TREE;
17786
17787 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17788
17789 /* Make our first pass over the list of formal parameter types and output a
17790 DW_TAG_formal_parameter DIE for each one. */
17791 for (link = first_parm_type; link; )
17792 {
17793 dw_die_ref parm_die;
17794
17795 formal_type = TREE_VALUE (link);
17796 if (formal_type == void_type_node)
17797 break;
17798
17799 /* Output a (nameless) DIE to represent the formal parameter itself. */
17800 parm_die = gen_formal_parameter_die (formal_type, NULL,
17801 true /* Emit name attribute. */,
17802 context_die);
17803 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17804 && link == first_parm_type)
17805 {
17806 add_AT_flag (parm_die, DW_AT_artificial, 1);
17807 if (dwarf_version >= 3 || !dwarf_strict)
17808 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17809 }
17810 else if (arg && DECL_ARTIFICIAL (arg))
17811 add_AT_flag (parm_die, DW_AT_artificial, 1);
17812
17813 link = TREE_CHAIN (link);
17814 if (arg)
17815 arg = DECL_CHAIN (arg);
17816 }
17817
17818 /* If this function type has an ellipsis, add a
17819 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17820 if (formal_type != void_type_node)
17821 gen_unspecified_parameters_die (function_or_method_type, context_die);
17822
17823 /* Make our second (and final) pass over the list of formal parameter types
17824 and output DIEs to represent those types (as necessary). */
17825 for (link = TYPE_ARG_TYPES (function_or_method_type);
17826 link && TREE_VALUE (link);
17827 link = TREE_CHAIN (link))
17828 gen_type_die (TREE_VALUE (link), context_die);
17829 }
17830
17831 /* We want to generate the DIE for TYPE so that we can generate the
17832 die for MEMBER, which has been defined; we will need to refer back
17833 to the member declaration nested within TYPE. If we're trying to
17834 generate minimal debug info for TYPE, processing TYPE won't do the
17835 trick; we need to attach the member declaration by hand. */
17836
17837 static void
17838 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17839 {
17840 gen_type_die (type, context_die);
17841
17842 /* If we're trying to avoid duplicate debug info, we may not have
17843 emitted the member decl for this function. Emit it now. */
17844 if (TYPE_STUB_DECL (type)
17845 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17846 && ! lookup_decl_die (member))
17847 {
17848 dw_die_ref type_die;
17849 gcc_assert (!decl_ultimate_origin (member));
17850
17851 push_decl_scope (type);
17852 type_die = lookup_type_die_strip_naming_typedef (type);
17853 if (TREE_CODE (member) == FUNCTION_DECL)
17854 gen_subprogram_die (member, type_die);
17855 else if (TREE_CODE (member) == FIELD_DECL)
17856 {
17857 /* Ignore the nameless fields that are used to skip bits but handle
17858 C++ anonymous unions and structs. */
17859 if (DECL_NAME (member) != NULL_TREE
17860 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17861 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17862 {
17863 gen_type_die (member_declared_type (member), type_die);
17864 gen_field_die (member, type_die);
17865 }
17866 }
17867 else
17868 gen_variable_die (member, NULL_TREE, type_die);
17869
17870 pop_decl_scope ();
17871 }
17872 }
17873 \f
17874 /* Forward declare these functions, because they are mutually recursive
17875 with their set_block_* pairing functions. */
17876 static void set_decl_origin_self (tree);
17877 static void set_decl_abstract_flags (tree, int);
17878
17879 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17880 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17881 that it points to the node itself, thus indicating that the node is its
17882 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17883 the given node is NULL, recursively descend the decl/block tree which
17884 it is the root of, and for each other ..._DECL or BLOCK node contained
17885 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17886 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17887 values to point to themselves. */
17888
17889 static void
17890 set_block_origin_self (tree stmt)
17891 {
17892 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17893 {
17894 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17895
17896 {
17897 tree local_decl;
17898
17899 for (local_decl = BLOCK_VARS (stmt);
17900 local_decl != NULL_TREE;
17901 local_decl = DECL_CHAIN (local_decl))
17902 if (! DECL_EXTERNAL (local_decl))
17903 set_decl_origin_self (local_decl); /* Potential recursion. */
17904 }
17905
17906 {
17907 tree subblock;
17908
17909 for (subblock = BLOCK_SUBBLOCKS (stmt);
17910 subblock != NULL_TREE;
17911 subblock = BLOCK_CHAIN (subblock))
17912 set_block_origin_self (subblock); /* Recurse. */
17913 }
17914 }
17915 }
17916
17917 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17918 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17919 node to so that it points to the node itself, thus indicating that the
17920 node represents its own (abstract) origin. Additionally, if the
17921 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17922 the decl/block tree of which the given node is the root of, and for
17923 each other ..._DECL or BLOCK node contained therein whose
17924 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17925 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17926 point to themselves. */
17927
17928 static void
17929 set_decl_origin_self (tree decl)
17930 {
17931 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17932 {
17933 DECL_ABSTRACT_ORIGIN (decl) = decl;
17934 if (TREE_CODE (decl) == FUNCTION_DECL)
17935 {
17936 tree arg;
17937
17938 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17939 DECL_ABSTRACT_ORIGIN (arg) = arg;
17940 if (DECL_INITIAL (decl) != NULL_TREE
17941 && DECL_INITIAL (decl) != error_mark_node)
17942 set_block_origin_self (DECL_INITIAL (decl));
17943 }
17944 }
17945 }
17946 \f
17947 /* Given a pointer to some BLOCK node, and a boolean value to set the
17948 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17949 the given block, and for all local decls and all local sub-blocks
17950 (recursively) which are contained therein. */
17951
17952 static void
17953 set_block_abstract_flags (tree stmt, int setting)
17954 {
17955 tree local_decl;
17956 tree subblock;
17957 unsigned int i;
17958
17959 BLOCK_ABSTRACT (stmt) = setting;
17960
17961 for (local_decl = BLOCK_VARS (stmt);
17962 local_decl != NULL_TREE;
17963 local_decl = DECL_CHAIN (local_decl))
17964 if (! DECL_EXTERNAL (local_decl))
17965 set_decl_abstract_flags (local_decl, setting);
17966
17967 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17968 {
17969 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17970 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17971 || TREE_CODE (local_decl) == PARM_DECL)
17972 set_decl_abstract_flags (local_decl, setting);
17973 }
17974
17975 for (subblock = BLOCK_SUBBLOCKS (stmt);
17976 subblock != NULL_TREE;
17977 subblock = BLOCK_CHAIN (subblock))
17978 set_block_abstract_flags (subblock, setting);
17979 }
17980
17981 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17982 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17983 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17984 set the abstract flags for all of the parameters, local vars, local
17985 blocks and sub-blocks (recursively) to the same setting. */
17986
17987 static void
17988 set_decl_abstract_flags (tree decl, int setting)
17989 {
17990 DECL_ABSTRACT (decl) = setting;
17991 if (TREE_CODE (decl) == FUNCTION_DECL)
17992 {
17993 tree arg;
17994
17995 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17996 DECL_ABSTRACT (arg) = setting;
17997 if (DECL_INITIAL (decl) != NULL_TREE
17998 && DECL_INITIAL (decl) != error_mark_node)
17999 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18000 }
18001 }
18002
18003 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18004 may later generate inlined and/or out-of-line instances of. */
18005
18006 static void
18007 dwarf2out_abstract_function (tree decl)
18008 {
18009 dw_die_ref old_die;
18010 tree save_fn;
18011 tree context;
18012 int was_abstract;
18013 htab_t old_decl_loc_table;
18014 htab_t old_cached_dw_loc_list_table;
18015 int old_call_site_count, old_tail_call_site_count;
18016 struct call_arg_loc_node *old_call_arg_locations;
18017
18018 /* Make sure we have the actual abstract inline, not a clone. */
18019 decl = DECL_ORIGIN (decl);
18020
18021 old_die = lookup_decl_die (decl);
18022 if (old_die && get_AT (old_die, DW_AT_inline))
18023 /* We've already generated the abstract instance. */
18024 return;
18025
18026 /* We can be called while recursively when seeing block defining inlined subroutine
18027 DIE. Be sure to not clobber the outer location table nor use it or we would
18028 get locations in abstract instantces. */
18029 old_decl_loc_table = decl_loc_table;
18030 decl_loc_table = NULL;
18031 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18032 cached_dw_loc_list_table = NULL;
18033 old_call_arg_locations = call_arg_locations;
18034 call_arg_locations = NULL;
18035 old_call_site_count = call_site_count;
18036 call_site_count = -1;
18037 old_tail_call_site_count = tail_call_site_count;
18038 tail_call_site_count = -1;
18039
18040 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18041 we don't get confused by DECL_ABSTRACT. */
18042 if (debug_info_level > DINFO_LEVEL_TERSE)
18043 {
18044 context = decl_class_context (decl);
18045 if (context)
18046 gen_type_die_for_member
18047 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18048 }
18049
18050 /* Pretend we've just finished compiling this function. */
18051 save_fn = current_function_decl;
18052 current_function_decl = decl;
18053
18054 was_abstract = DECL_ABSTRACT (decl);
18055 set_decl_abstract_flags (decl, 1);
18056 dwarf2out_decl (decl);
18057 if (! was_abstract)
18058 set_decl_abstract_flags (decl, 0);
18059
18060 current_function_decl = save_fn;
18061 decl_loc_table = old_decl_loc_table;
18062 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18063 call_arg_locations = old_call_arg_locations;
18064 call_site_count = old_call_site_count;
18065 tail_call_site_count = old_tail_call_site_count;
18066 }
18067
18068 /* Helper function of premark_used_types() which gets called through
18069 htab_traverse.
18070
18071 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18072 marked as unused by prune_unused_types. */
18073
18074 bool
18075 premark_used_types_helper (tree const &type, void *)
18076 {
18077 dw_die_ref die;
18078
18079 die = lookup_type_die (type);
18080 if (die != NULL)
18081 die->die_perennial_p = 1;
18082 return true;
18083 }
18084
18085 /* Helper function of premark_types_used_by_global_vars which gets called
18086 through htab_traverse.
18087
18088 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18089 marked as unused by prune_unused_types. The DIE of the type is marked
18090 only if the global variable using the type will actually be emitted. */
18091
18092 static int
18093 premark_types_used_by_global_vars_helper (void **slot,
18094 void *data ATTRIBUTE_UNUSED)
18095 {
18096 struct types_used_by_vars_entry *entry;
18097 dw_die_ref die;
18098
18099 entry = (struct types_used_by_vars_entry *) *slot;
18100 gcc_assert (entry->type != NULL
18101 && entry->var_decl != NULL);
18102 die = lookup_type_die (entry->type);
18103 if (die)
18104 {
18105 /* Ask cgraph if the global variable really is to be emitted.
18106 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18107 varpool_node *node = varpool_node::get (entry->var_decl);
18108 if (node && node->definition)
18109 {
18110 die->die_perennial_p = 1;
18111 /* Keep the parent DIEs as well. */
18112 while ((die = die->die_parent) && die->die_perennial_p == 0)
18113 die->die_perennial_p = 1;
18114 }
18115 }
18116 return 1;
18117 }
18118
18119 /* Mark all members of used_types_hash as perennial. */
18120
18121 static void
18122 premark_used_types (struct function *fun)
18123 {
18124 if (fun && fun->used_types_hash)
18125 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18126 }
18127
18128 /* Mark all members of types_used_by_vars_entry as perennial. */
18129
18130 static void
18131 premark_types_used_by_global_vars (void)
18132 {
18133 if (types_used_by_vars_hash)
18134 htab_traverse (types_used_by_vars_hash,
18135 premark_types_used_by_global_vars_helper, NULL);
18136 }
18137
18138 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18139 for CA_LOC call arg loc node. */
18140
18141 static dw_die_ref
18142 gen_call_site_die (tree decl, dw_die_ref subr_die,
18143 struct call_arg_loc_node *ca_loc)
18144 {
18145 dw_die_ref stmt_die = NULL, die;
18146 tree block = ca_loc->block;
18147
18148 while (block
18149 && block != DECL_INITIAL (decl)
18150 && TREE_CODE (block) == BLOCK)
18151 {
18152 if (block_map.length () > BLOCK_NUMBER (block))
18153 stmt_die = block_map[BLOCK_NUMBER (block)];
18154 if (stmt_die)
18155 break;
18156 block = BLOCK_SUPERCONTEXT (block);
18157 }
18158 if (stmt_die == NULL)
18159 stmt_die = subr_die;
18160 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18161 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18162 if (ca_loc->tail_call_p)
18163 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18164 if (ca_loc->symbol_ref)
18165 {
18166 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18167 if (tdie)
18168 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18169 else
18170 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18171 }
18172 return die;
18173 }
18174
18175 /* Generate a DIE to represent a declared function (either file-scope or
18176 block-local). */
18177
18178 static void
18179 gen_subprogram_die (tree decl, dw_die_ref context_die)
18180 {
18181 tree origin = decl_ultimate_origin (decl);
18182 dw_die_ref subr_die;
18183 tree outer_scope;
18184 dw_die_ref old_die = lookup_decl_die (decl);
18185 int declaration = (current_function_decl != decl
18186 || class_or_namespace_scope_p (context_die));
18187
18188 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18189
18190 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18191 started to generate the abstract instance of an inline, decided to output
18192 its containing class, and proceeded to emit the declaration of the inline
18193 from the member list for the class. If so, DECLARATION takes priority;
18194 we'll get back to the abstract instance when done with the class. */
18195
18196 /* The class-scope declaration DIE must be the primary DIE. */
18197 if (origin && declaration && class_or_namespace_scope_p (context_die))
18198 {
18199 origin = NULL;
18200 gcc_assert (!old_die);
18201 }
18202
18203 /* Now that the C++ front end lazily declares artificial member fns, we
18204 might need to retrofit the declaration into its class. */
18205 if (!declaration && !origin && !old_die
18206 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18207 && !class_or_namespace_scope_p (context_die)
18208 && debug_info_level > DINFO_LEVEL_TERSE)
18209 old_die = force_decl_die (decl);
18210
18211 if (origin != NULL && origin != decl)
18212 {
18213 gcc_assert (!declaration || local_scope_p (context_die));
18214
18215 /* Fixup die_parent for the abstract instance of a nested
18216 inline function. */
18217 if (old_die && old_die->die_parent == NULL)
18218 add_child_die (context_die, old_die);
18219
18220 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18221 add_abstract_origin_attribute (subr_die, origin);
18222 /* This is where the actual code for a cloned function is.
18223 Let's emit linkage name attribute for it. This helps
18224 debuggers to e.g, set breakpoints into
18225 constructors/destructors when the user asks "break
18226 K::K". */
18227 add_linkage_name (subr_die, decl);
18228 }
18229 else if (old_die)
18230 {
18231 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18232 struct dwarf_file_data * file_index = lookup_filename (s.file);
18233
18234 if (!get_AT_flag (old_die, DW_AT_declaration)
18235 /* We can have a normal definition following an inline one in the
18236 case of redefinition of GNU C extern inlines.
18237 It seems reasonable to use AT_specification in this case. */
18238 && !get_AT (old_die, DW_AT_inline))
18239 {
18240 /* Detect and ignore this case, where we are trying to output
18241 something we have already output. */
18242 return;
18243 }
18244
18245 /* If the definition comes from the same place as the declaration,
18246 maybe use the old DIE. We always want the DIE for this function
18247 that has the *_pc attributes to be under comp_unit_die so the
18248 debugger can find it. We also need to do this for abstract
18249 instances of inlines, since the spec requires the out-of-line copy
18250 to have the same parent. For local class methods, this doesn't
18251 apply; we just use the old DIE. */
18252 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18253 && (DECL_ARTIFICIAL (decl)
18254 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18255 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18256 == (unsigned) s.line))))
18257 {
18258 subr_die = old_die;
18259
18260 /* Clear out the declaration attribute and the formal parameters.
18261 Do not remove all children, because it is possible that this
18262 declaration die was forced using force_decl_die(). In such
18263 cases die that forced declaration die (e.g. TAG_imported_module)
18264 is one of the children that we do not want to remove. */
18265 remove_AT (subr_die, DW_AT_declaration);
18266 remove_AT (subr_die, DW_AT_object_pointer);
18267 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18268 }
18269 else
18270 {
18271 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18272 add_AT_specification (subr_die, old_die);
18273 add_pubname (decl, subr_die);
18274 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18275 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18276 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18277 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18278
18279 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18280 emit the real type on the definition die. */
18281 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18282 {
18283 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18284 if (die == auto_die || die == decltype_auto_die)
18285 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18286 TYPE_UNQUALIFIED, context_die);
18287 }
18288 }
18289 }
18290 else
18291 {
18292 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18293
18294 if (TREE_PUBLIC (decl))
18295 add_AT_flag (subr_die, DW_AT_external, 1);
18296
18297 add_name_and_src_coords_attributes (subr_die, decl);
18298 add_pubname (decl, subr_die);
18299 if (debug_info_level > DINFO_LEVEL_TERSE)
18300 {
18301 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18302 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18303 TYPE_UNQUALIFIED, context_die);
18304 }
18305
18306 add_pure_or_virtual_attribute (subr_die, decl);
18307 if (DECL_ARTIFICIAL (decl))
18308 add_AT_flag (subr_die, DW_AT_artificial, 1);
18309
18310 add_accessibility_attribute (subr_die, decl);
18311 }
18312
18313 if (declaration)
18314 {
18315 if (!old_die || !get_AT (old_die, DW_AT_inline))
18316 {
18317 add_AT_flag (subr_die, DW_AT_declaration, 1);
18318
18319 /* If this is an explicit function declaration then generate
18320 a DW_AT_explicit attribute. */
18321 if (lang_hooks.decls.function_decl_explicit_p (decl)
18322 && (dwarf_version >= 3 || !dwarf_strict))
18323 add_AT_flag (subr_die, DW_AT_explicit, 1);
18324
18325 /* The first time we see a member function, it is in the context of
18326 the class to which it belongs. We make sure of this by emitting
18327 the class first. The next time is the definition, which is
18328 handled above. The two may come from the same source text.
18329
18330 Note that force_decl_die() forces function declaration die. It is
18331 later reused to represent definition. */
18332 equate_decl_number_to_die (decl, subr_die);
18333 }
18334 }
18335 else if (DECL_ABSTRACT (decl))
18336 {
18337 if (DECL_DECLARED_INLINE_P (decl))
18338 {
18339 if (cgraph_function_possibly_inlined_p (decl))
18340 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18341 else
18342 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18343 }
18344 else
18345 {
18346 if (cgraph_function_possibly_inlined_p (decl))
18347 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18348 else
18349 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18350 }
18351
18352 if (DECL_DECLARED_INLINE_P (decl)
18353 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18354 add_AT_flag (subr_die, DW_AT_artificial, 1);
18355
18356 equate_decl_number_to_die (decl, subr_die);
18357 }
18358 else if (!DECL_EXTERNAL (decl)
18359 && (!DECL_STRUCT_FUNCTION (decl)
18360 || DECL_STRUCT_FUNCTION (decl)->gimple_df))
18361 {
18362 HOST_WIDE_INT cfa_fb_offset;
18363
18364 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18365
18366 if (!old_die || !get_AT (old_die, DW_AT_inline))
18367 equate_decl_number_to_die (decl, subr_die);
18368
18369 gcc_checking_assert (fun);
18370 if (!flag_reorder_blocks_and_partition)
18371 {
18372 dw_fde_ref fde = fun->fde;
18373 if (fde->dw_fde_begin)
18374 {
18375 /* We have already generated the labels. */
18376 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18377 fde->dw_fde_end, false);
18378 }
18379 else
18380 {
18381 /* Create start/end labels and add the range. */
18382 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18383 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18384 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18385 current_function_funcdef_no);
18386 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18387 current_function_funcdef_no);
18388 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18389 false);
18390 }
18391
18392 #if VMS_DEBUGGING_INFO
18393 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18394 Section 2.3 Prologue and Epilogue Attributes:
18395 When a breakpoint is set on entry to a function, it is generally
18396 desirable for execution to be suspended, not on the very first
18397 instruction of the function, but rather at a point after the
18398 function's frame has been set up, after any language defined local
18399 declaration processing has been completed, and before execution of
18400 the first statement of the function begins. Debuggers generally
18401 cannot properly determine where this point is. Similarly for a
18402 breakpoint set on exit from a function. The prologue and epilogue
18403 attributes allow a compiler to communicate the location(s) to use. */
18404
18405 {
18406 if (fde->dw_fde_vms_end_prologue)
18407 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18408 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18409
18410 if (fde->dw_fde_vms_begin_epilogue)
18411 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18412 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18413 }
18414 #endif
18415
18416 }
18417 else
18418 {
18419 /* Generate pubnames entries for the split function code ranges. */
18420 dw_fde_ref fde = fun->fde;
18421
18422 if (fde->dw_fde_second_begin)
18423 {
18424 if (dwarf_version >= 3 || !dwarf_strict)
18425 {
18426 /* We should use ranges for non-contiguous code section
18427 addresses. Use the actual code range for the initial
18428 section, since the HOT/COLD labels might precede an
18429 alignment offset. */
18430 bool range_list_added = false;
18431 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18432 fde->dw_fde_end, &range_list_added,
18433 false);
18434 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18435 fde->dw_fde_second_end,
18436 &range_list_added, false);
18437 if (range_list_added)
18438 add_ranges (NULL);
18439 }
18440 else
18441 {
18442 /* There is no real support in DW2 for this .. so we make
18443 a work-around. First, emit the pub name for the segment
18444 containing the function label. Then make and emit a
18445 simplified subprogram DIE for the second segment with the
18446 name pre-fixed by __hot/cold_sect_of_. We use the same
18447 linkage name for the second die so that gdb will find both
18448 sections when given "b foo". */
18449 const char *name = NULL;
18450 tree decl_name = DECL_NAME (decl);
18451 dw_die_ref seg_die;
18452
18453 /* Do the 'primary' section. */
18454 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18455 fde->dw_fde_end, false);
18456
18457 /* Build a minimal DIE for the secondary section. */
18458 seg_die = new_die (DW_TAG_subprogram,
18459 subr_die->die_parent, decl);
18460
18461 if (TREE_PUBLIC (decl))
18462 add_AT_flag (seg_die, DW_AT_external, 1);
18463
18464 if (decl_name != NULL
18465 && IDENTIFIER_POINTER (decl_name) != NULL)
18466 {
18467 name = dwarf2_name (decl, 1);
18468 if (! DECL_ARTIFICIAL (decl))
18469 add_src_coords_attributes (seg_die, decl);
18470
18471 add_linkage_name (seg_die, decl);
18472 }
18473 gcc_assert (name != NULL);
18474 add_pure_or_virtual_attribute (seg_die, decl);
18475 if (DECL_ARTIFICIAL (decl))
18476 add_AT_flag (seg_die, DW_AT_artificial, 1);
18477
18478 name = concat ("__second_sect_of_", name, NULL);
18479 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18480 fde->dw_fde_second_end, false);
18481 add_name_attribute (seg_die, name);
18482 if (want_pubnames ())
18483 add_pubname_string (name, seg_die);
18484 }
18485 }
18486 else
18487 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18488 false);
18489 }
18490
18491 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18492
18493 /* We define the "frame base" as the function's CFA. This is more
18494 convenient for several reasons: (1) It's stable across the prologue
18495 and epilogue, which makes it better than just a frame pointer,
18496 (2) With dwarf3, there exists a one-byte encoding that allows us
18497 to reference the .debug_frame data by proxy, but failing that,
18498 (3) We can at least reuse the code inspection and interpretation
18499 code that determines the CFA position at various points in the
18500 function. */
18501 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18502 {
18503 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18504 add_AT_loc (subr_die, DW_AT_frame_base, op);
18505 }
18506 else
18507 {
18508 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18509 if (list->dw_loc_next)
18510 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18511 else
18512 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18513 }
18514
18515 /* Compute a displacement from the "steady-state frame pointer" to
18516 the CFA. The former is what all stack slots and argument slots
18517 will reference in the rtl; the latter is what we've told the
18518 debugger about. We'll need to adjust all frame_base references
18519 by this displacement. */
18520 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18521
18522 if (fun->static_chain_decl)
18523 add_AT_location_description (subr_die, DW_AT_static_link,
18524 loc_list_from_tree (fun->static_chain_decl, 2));
18525 }
18526 else if (!DECL_EXTERNAL (decl))
18527 {
18528 if (!old_die || !get_AT (old_die, DW_AT_inline))
18529 equate_decl_number_to_die (decl, subr_die);
18530 }
18531
18532 /* Generate child dies for template paramaters. */
18533 if (debug_info_level > DINFO_LEVEL_TERSE)
18534 {
18535 /* XXX */
18536 if (!lookup_decl_die (decl))
18537 equate_decl_number_to_die (decl, subr_die);
18538 gen_generic_params_dies (decl);
18539 }
18540
18541 /* Now output descriptions of the arguments for this function. This gets
18542 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18543 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18544 `...' at the end of the formal parameter list. In order to find out if
18545 there was a trailing ellipsis or not, we must instead look at the type
18546 associated with the FUNCTION_DECL. This will be a node of type
18547 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18548 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18549 an ellipsis at the end. */
18550
18551 /* In the case where we are describing a mere function declaration, all we
18552 need to do here (and all we *can* do here) is to describe the *types* of
18553 its formal parameters. */
18554 if (debug_info_level <= DINFO_LEVEL_TERSE)
18555 ;
18556 else if (declaration)
18557 gen_formal_types_die (decl, subr_die);
18558 else
18559 {
18560 /* Generate DIEs to represent all known formal parameters. */
18561 tree parm = DECL_ARGUMENTS (decl);
18562 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18563 tree generic_decl_parm = generic_decl
18564 ? DECL_ARGUMENTS (generic_decl)
18565 : NULL;
18566
18567 /* Now we want to walk the list of parameters of the function and
18568 emit their relevant DIEs.
18569
18570 We consider the case of DECL being an instance of a generic function
18571 as well as it being a normal function.
18572
18573 If DECL is an instance of a generic function we walk the
18574 parameters of the generic function declaration _and_ the parameters of
18575 DECL itself. This is useful because we want to emit specific DIEs for
18576 function parameter packs and those are declared as part of the
18577 generic function declaration. In that particular case,
18578 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18579 That DIE has children DIEs representing the set of arguments
18580 of the pack. Note that the set of pack arguments can be empty.
18581 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18582 children DIE.
18583
18584 Otherwise, we just consider the parameters of DECL. */
18585 while (generic_decl_parm || parm)
18586 {
18587 if (generic_decl_parm
18588 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18589 gen_formal_parameter_pack_die (generic_decl_parm,
18590 parm, subr_die,
18591 &parm);
18592 else if (parm)
18593 {
18594 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18595
18596 if (parm == DECL_ARGUMENTS (decl)
18597 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18598 && parm_die
18599 && (dwarf_version >= 3 || !dwarf_strict))
18600 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18601
18602 parm = DECL_CHAIN (parm);
18603 }
18604
18605 if (generic_decl_parm)
18606 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18607 }
18608
18609 /* Decide whether we need an unspecified_parameters DIE at the end.
18610 There are 2 more cases to do this for: 1) the ansi ... declaration -
18611 this is detectable when the end of the arg list is not a
18612 void_type_node 2) an unprototyped function declaration (not a
18613 definition). This just means that we have no info about the
18614 parameters at all. */
18615 if (prototype_p (TREE_TYPE (decl)))
18616 {
18617 /* This is the prototyped case, check for.... */
18618 if (stdarg_p (TREE_TYPE (decl)))
18619 gen_unspecified_parameters_die (decl, subr_die);
18620 }
18621 else if (DECL_INITIAL (decl) == NULL_TREE)
18622 gen_unspecified_parameters_die (decl, subr_die);
18623 }
18624
18625 /* Output Dwarf info for all of the stuff within the body of the function
18626 (if it has one - it may be just a declaration). */
18627 outer_scope = DECL_INITIAL (decl);
18628
18629 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18630 a function. This BLOCK actually represents the outermost binding contour
18631 for the function, i.e. the contour in which the function's formal
18632 parameters and labels get declared. Curiously, it appears that the front
18633 end doesn't actually put the PARM_DECL nodes for the current function onto
18634 the BLOCK_VARS list for this outer scope, but are strung off of the
18635 DECL_ARGUMENTS list for the function instead.
18636
18637 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18638 the LABEL_DECL nodes for the function however, and we output DWARF info
18639 for those in decls_for_scope. Just within the `outer_scope' there will be
18640 a BLOCK node representing the function's outermost pair of curly braces,
18641 and any blocks used for the base and member initializers of a C++
18642 constructor function. */
18643 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK
18644 && (!DECL_STRUCT_FUNCTION (decl)
18645 || DECL_STRUCT_FUNCTION (decl)->gimple_df))
18646 {
18647 int call_site_note_count = 0;
18648 int tail_call_site_note_count = 0;
18649
18650 /* Emit a DW_TAG_variable DIE for a named return value. */
18651 if (DECL_NAME (DECL_RESULT (decl)))
18652 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18653
18654 current_function_has_inlines = 0;
18655 decls_for_scope (outer_scope, subr_die, 0);
18656
18657 if (call_arg_locations && !dwarf_strict)
18658 {
18659 struct call_arg_loc_node *ca_loc;
18660 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18661 {
18662 dw_die_ref die = NULL;
18663 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18664 rtx arg, next_arg;
18665
18666 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18667 arg; arg = next_arg)
18668 {
18669 dw_loc_descr_ref reg, val;
18670 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18671 dw_die_ref cdie, tdie = NULL;
18672
18673 next_arg = XEXP (arg, 1);
18674 if (REG_P (XEXP (XEXP (arg, 0), 0))
18675 && next_arg
18676 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18677 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18678 && REGNO (XEXP (XEXP (arg, 0), 0))
18679 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18680 next_arg = XEXP (next_arg, 1);
18681 if (mode == VOIDmode)
18682 {
18683 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18684 if (mode == VOIDmode)
18685 mode = GET_MODE (XEXP (arg, 0));
18686 }
18687 if (mode == VOIDmode || mode == BLKmode)
18688 continue;
18689 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18690 {
18691 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18692 tloc = XEXP (XEXP (arg, 0), 1);
18693 continue;
18694 }
18695 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18696 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18697 {
18698 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18699 tlocc = XEXP (XEXP (arg, 0), 1);
18700 continue;
18701 }
18702 reg = NULL;
18703 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18704 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18705 VAR_INIT_STATUS_INITIALIZED);
18706 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18707 {
18708 rtx mem = XEXP (XEXP (arg, 0), 0);
18709 reg = mem_loc_descriptor (XEXP (mem, 0),
18710 get_address_mode (mem),
18711 GET_MODE (mem),
18712 VAR_INIT_STATUS_INITIALIZED);
18713 }
18714 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18715 == DEBUG_PARAMETER_REF)
18716 {
18717 tree tdecl
18718 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18719 tdie = lookup_decl_die (tdecl);
18720 if (tdie == NULL)
18721 continue;
18722 }
18723 else
18724 continue;
18725 if (reg == NULL
18726 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18727 != DEBUG_PARAMETER_REF)
18728 continue;
18729 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18730 VOIDmode,
18731 VAR_INIT_STATUS_INITIALIZED);
18732 if (val == NULL)
18733 continue;
18734 if (die == NULL)
18735 die = gen_call_site_die (decl, subr_die, ca_loc);
18736 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18737 NULL_TREE);
18738 if (reg != NULL)
18739 add_AT_loc (cdie, DW_AT_location, reg);
18740 else if (tdie != NULL)
18741 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18742 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18743 if (next_arg != XEXP (arg, 1))
18744 {
18745 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18746 if (mode == VOIDmode)
18747 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18748 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18749 0), 1),
18750 mode, VOIDmode,
18751 VAR_INIT_STATUS_INITIALIZED);
18752 if (val != NULL)
18753 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18754 }
18755 }
18756 if (die == NULL
18757 && (ca_loc->symbol_ref || tloc))
18758 die = gen_call_site_die (decl, subr_die, ca_loc);
18759 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18760 {
18761 dw_loc_descr_ref tval = NULL;
18762
18763 if (tloc != NULL_RTX)
18764 tval = mem_loc_descriptor (tloc,
18765 GET_MODE (tloc) == VOIDmode
18766 ? Pmode : GET_MODE (tloc),
18767 VOIDmode,
18768 VAR_INIT_STATUS_INITIALIZED);
18769 if (tval)
18770 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18771 else if (tlocc != NULL_RTX)
18772 {
18773 tval = mem_loc_descriptor (tlocc,
18774 GET_MODE (tlocc) == VOIDmode
18775 ? Pmode : GET_MODE (tlocc),
18776 VOIDmode,
18777 VAR_INIT_STATUS_INITIALIZED);
18778 if (tval)
18779 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18780 tval);
18781 }
18782 }
18783 if (die != NULL)
18784 {
18785 call_site_note_count++;
18786 if (ca_loc->tail_call_p)
18787 tail_call_site_note_count++;
18788 }
18789 }
18790 }
18791 call_arg_locations = NULL;
18792 call_arg_loc_last = NULL;
18793 if (tail_call_site_count >= 0
18794 && tail_call_site_count == tail_call_site_note_count
18795 && !dwarf_strict)
18796 {
18797 if (call_site_count >= 0
18798 && call_site_count == call_site_note_count)
18799 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18800 else
18801 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18802 }
18803 call_site_count = -1;
18804 tail_call_site_count = -1;
18805 }
18806
18807 if (subr_die != old_die)
18808 /* Add the calling convention attribute if requested. */
18809 add_calling_convention_attribute (subr_die, decl);
18810 }
18811
18812 /* Returns a hash value for X (which really is a die_struct). */
18813
18814 static hashval_t
18815 common_block_die_table_hash (const void *x)
18816 {
18817 const_dw_die_ref d = (const_dw_die_ref) x;
18818 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18819 }
18820
18821 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18822 as decl_id and die_parent of die_struct Y. */
18823
18824 static int
18825 common_block_die_table_eq (const void *x, const void *y)
18826 {
18827 const_dw_die_ref d = (const_dw_die_ref) x;
18828 const_dw_die_ref e = (const_dw_die_ref) y;
18829 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18830 }
18831
18832 /* Generate a DIE to represent a declared data object.
18833 Either DECL or ORIGIN must be non-null. */
18834
18835 static void
18836 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18837 {
18838 HOST_WIDE_INT off = 0;
18839 tree com_decl;
18840 tree decl_or_origin = decl ? decl : origin;
18841 tree ultimate_origin;
18842 dw_die_ref var_die;
18843 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18844 dw_die_ref origin_die;
18845 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18846 || class_or_namespace_scope_p (context_die));
18847 bool specialization_p = false;
18848
18849 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18850 if (decl || ultimate_origin)
18851 origin = ultimate_origin;
18852 com_decl = fortran_common (decl_or_origin, &off);
18853
18854 /* Symbol in common gets emitted as a child of the common block, in the form
18855 of a data member. */
18856 if (com_decl)
18857 {
18858 dw_die_ref com_die;
18859 dw_loc_list_ref loc;
18860 die_node com_die_arg;
18861
18862 var_die = lookup_decl_die (decl_or_origin);
18863 if (var_die)
18864 {
18865 if (get_AT (var_die, DW_AT_location) == NULL)
18866 {
18867 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18868 if (loc)
18869 {
18870 if (off)
18871 {
18872 /* Optimize the common case. */
18873 if (single_element_loc_list_p (loc)
18874 && loc->expr->dw_loc_opc == DW_OP_addr
18875 && loc->expr->dw_loc_next == NULL
18876 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18877 == SYMBOL_REF)
18878 {
18879 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18880 loc->expr->dw_loc_oprnd1.v.val_addr
18881 = plus_constant (GET_MODE (x), x , off);
18882 }
18883 else
18884 loc_list_plus_const (loc, off);
18885 }
18886 add_AT_location_description (var_die, DW_AT_location, loc);
18887 remove_AT (var_die, DW_AT_declaration);
18888 }
18889 }
18890 return;
18891 }
18892
18893 if (common_block_die_table == NULL)
18894 common_block_die_table
18895 = htab_create_ggc (10, common_block_die_table_hash,
18896 common_block_die_table_eq, NULL);
18897
18898 com_die_arg.decl_id = DECL_UID (com_decl);
18899 com_die_arg.die_parent = context_die;
18900 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18901 loc = loc_list_from_tree (com_decl, 2);
18902 if (com_die == NULL)
18903 {
18904 const char *cnam
18905 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18906 void **slot;
18907
18908 com_die = new_die (DW_TAG_common_block, context_die, decl);
18909 add_name_and_src_coords_attributes (com_die, com_decl);
18910 if (loc)
18911 {
18912 add_AT_location_description (com_die, DW_AT_location, loc);
18913 /* Avoid sharing the same loc descriptor between
18914 DW_TAG_common_block and DW_TAG_variable. */
18915 loc = loc_list_from_tree (com_decl, 2);
18916 }
18917 else if (DECL_EXTERNAL (decl))
18918 add_AT_flag (com_die, DW_AT_declaration, 1);
18919 if (want_pubnames ())
18920 add_pubname_string (cnam, com_die); /* ??? needed? */
18921 com_die->decl_id = DECL_UID (com_decl);
18922 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18923 *slot = (void *) com_die;
18924 }
18925 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18926 {
18927 add_AT_location_description (com_die, DW_AT_location, loc);
18928 loc = loc_list_from_tree (com_decl, 2);
18929 remove_AT (com_die, DW_AT_declaration);
18930 }
18931 var_die = new_die (DW_TAG_variable, com_die, decl);
18932 add_name_and_src_coords_attributes (var_die, decl);
18933 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18934 context_die);
18935 add_AT_flag (var_die, DW_AT_external, 1);
18936 if (loc)
18937 {
18938 if (off)
18939 {
18940 /* Optimize the common case. */
18941 if (single_element_loc_list_p (loc)
18942 && loc->expr->dw_loc_opc == DW_OP_addr
18943 && loc->expr->dw_loc_next == NULL
18944 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18945 {
18946 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18947 loc->expr->dw_loc_oprnd1.v.val_addr
18948 = plus_constant (GET_MODE (x), x, off);
18949 }
18950 else
18951 loc_list_plus_const (loc, off);
18952 }
18953 add_AT_location_description (var_die, DW_AT_location, loc);
18954 }
18955 else if (DECL_EXTERNAL (decl))
18956 add_AT_flag (var_die, DW_AT_declaration, 1);
18957 equate_decl_number_to_die (decl, var_die);
18958 return;
18959 }
18960
18961 /* If the compiler emitted a definition for the DECL declaration
18962 and if we already emitted a DIE for it, don't emit a second
18963 DIE for it again. Allow re-declarations of DECLs that are
18964 inside functions, though. */
18965 if (old_die && !declaration && !local_scope_p (context_die))
18966 return;
18967
18968 /* For static data members, the declaration in the class is supposed
18969 to have DW_TAG_member tag; the specification should still be
18970 DW_TAG_variable referencing the DW_TAG_member DIE. */
18971 if (declaration && class_scope_p (context_die))
18972 var_die = new_die (DW_TAG_member, context_die, decl);
18973 else
18974 var_die = new_die (DW_TAG_variable, context_die, decl);
18975
18976 origin_die = NULL;
18977 if (origin != NULL)
18978 origin_die = add_abstract_origin_attribute (var_die, origin);
18979
18980 /* Loop unrolling can create multiple blocks that refer to the same
18981 static variable, so we must test for the DW_AT_declaration flag.
18982
18983 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18984 copy decls and set the DECL_ABSTRACT flag on them instead of
18985 sharing them.
18986
18987 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18988
18989 ??? The declare_in_namespace support causes us to get two DIEs for one
18990 variable, both of which are declarations. We want to avoid considering
18991 one to be a specification, so we must test that this DIE is not a
18992 declaration. */
18993 else if (old_die && TREE_STATIC (decl) && ! declaration
18994 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18995 {
18996 /* This is a definition of a C++ class level static. */
18997 add_AT_specification (var_die, old_die);
18998 specialization_p = true;
18999 if (DECL_NAME (decl))
19000 {
19001 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19002 struct dwarf_file_data * file_index = lookup_filename (s.file);
19003
19004 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19005 add_AT_file (var_die, DW_AT_decl_file, file_index);
19006
19007 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19008 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19009
19010 if (old_die->die_tag == DW_TAG_member)
19011 add_linkage_name (var_die, decl);
19012 }
19013 }
19014 else
19015 add_name_and_src_coords_attributes (var_die, decl);
19016
19017 if ((origin == NULL && !specialization_p)
19018 || (origin != NULL
19019 && !DECL_ABSTRACT (decl_or_origin)
19020 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19021 decl_function_context
19022 (decl_or_origin))))
19023 {
19024 tree type = TREE_TYPE (decl_or_origin);
19025
19026 if (decl_by_reference_p (decl_or_origin))
19027 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19028 context_die);
19029 else
19030 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19031 context_die);
19032 }
19033
19034 if (origin == NULL && !specialization_p)
19035 {
19036 if (TREE_PUBLIC (decl))
19037 add_AT_flag (var_die, DW_AT_external, 1);
19038
19039 if (DECL_ARTIFICIAL (decl))
19040 add_AT_flag (var_die, DW_AT_artificial, 1);
19041
19042 add_accessibility_attribute (var_die, decl);
19043 }
19044
19045 if (declaration)
19046 add_AT_flag (var_die, DW_AT_declaration, 1);
19047
19048 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19049 equate_decl_number_to_die (decl, var_die);
19050
19051 if (! declaration
19052 && (! DECL_ABSTRACT (decl_or_origin)
19053 /* Local static vars are shared between all clones/inlines,
19054 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19055 already set. */
19056 || (TREE_CODE (decl_or_origin) == VAR_DECL
19057 && TREE_STATIC (decl_or_origin)
19058 && DECL_RTL_SET_P (decl_or_origin)))
19059 /* When abstract origin already has DW_AT_location attribute, no need
19060 to add it again. */
19061 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19062 {
19063 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19064 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19065 defer_location (decl_or_origin, var_die);
19066 else
19067 add_location_or_const_value_attribute (var_die, decl_or_origin,
19068 decl == NULL, DW_AT_location);
19069 add_pubname (decl_or_origin, var_die);
19070 }
19071 else
19072 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19073 }
19074
19075 /* Generate a DIE to represent a named constant. */
19076
19077 static void
19078 gen_const_die (tree decl, dw_die_ref context_die)
19079 {
19080 dw_die_ref const_die;
19081 tree type = TREE_TYPE (decl);
19082
19083 const_die = new_die (DW_TAG_constant, context_die, decl);
19084 add_name_and_src_coords_attributes (const_die, decl);
19085 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19086 if (TREE_PUBLIC (decl))
19087 add_AT_flag (const_die, DW_AT_external, 1);
19088 if (DECL_ARTIFICIAL (decl))
19089 add_AT_flag (const_die, DW_AT_artificial, 1);
19090 tree_add_const_value_attribute_for_decl (const_die, decl);
19091 }
19092
19093 /* Generate a DIE to represent a label identifier. */
19094
19095 static void
19096 gen_label_die (tree decl, dw_die_ref context_die)
19097 {
19098 tree origin = decl_ultimate_origin (decl);
19099 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19100 rtx insn;
19101 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19102
19103 if (origin != NULL)
19104 add_abstract_origin_attribute (lbl_die, origin);
19105 else
19106 add_name_and_src_coords_attributes (lbl_die, decl);
19107
19108 if (DECL_ABSTRACT (decl))
19109 equate_decl_number_to_die (decl, lbl_die);
19110 else
19111 {
19112 insn = DECL_RTL_IF_SET (decl);
19113
19114 /* Deleted labels are programmer specified labels which have been
19115 eliminated because of various optimizations. We still emit them
19116 here so that it is possible to put breakpoints on them. */
19117 if (insn
19118 && (LABEL_P (insn)
19119 || ((NOTE_P (insn)
19120 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19121 {
19122 /* When optimization is enabled (via -O) some parts of the compiler
19123 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19124 represent source-level labels which were explicitly declared by
19125 the user. This really shouldn't be happening though, so catch
19126 it if it ever does happen. */
19127 gcc_assert (!INSN_DELETED_P (insn));
19128
19129 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19130 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19131 }
19132 else if (insn
19133 && NOTE_P (insn)
19134 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19135 && CODE_LABEL_NUMBER (insn) != -1)
19136 {
19137 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19138 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19139 }
19140 }
19141 }
19142
19143 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19144 attributes to the DIE for a block STMT, to describe where the inlined
19145 function was called from. This is similar to add_src_coords_attributes. */
19146
19147 static inline void
19148 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19149 {
19150 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19151
19152 if (dwarf_version >= 3 || !dwarf_strict)
19153 {
19154 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19155 add_AT_unsigned (die, DW_AT_call_line, s.line);
19156 }
19157 }
19158
19159
19160 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19161 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19162
19163 static inline void
19164 add_high_low_attributes (tree stmt, dw_die_ref die)
19165 {
19166 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19167
19168 if (BLOCK_FRAGMENT_CHAIN (stmt)
19169 && (dwarf_version >= 3 || !dwarf_strict))
19170 {
19171 tree chain, superblock = NULL_TREE;
19172 dw_die_ref pdie;
19173 dw_attr_ref attr = NULL;
19174
19175 if (inlined_function_outer_scope_p (stmt))
19176 {
19177 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19178 BLOCK_NUMBER (stmt));
19179 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19180 }
19181
19182 /* Optimize duplicate .debug_ranges lists or even tails of
19183 lists. If this BLOCK has same ranges as its supercontext,
19184 lookup DW_AT_ranges attribute in the supercontext (and
19185 recursively so), verify that the ranges_table contains the
19186 right values and use it instead of adding a new .debug_range. */
19187 for (chain = stmt, pdie = die;
19188 BLOCK_SAME_RANGE (chain);
19189 chain = BLOCK_SUPERCONTEXT (chain))
19190 {
19191 dw_attr_ref new_attr;
19192
19193 pdie = pdie->die_parent;
19194 if (pdie == NULL)
19195 break;
19196 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19197 break;
19198 new_attr = get_AT (pdie, DW_AT_ranges);
19199 if (new_attr == NULL
19200 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19201 break;
19202 attr = new_attr;
19203 superblock = BLOCK_SUPERCONTEXT (chain);
19204 }
19205 if (attr != NULL
19206 && (ranges_table[attr->dw_attr_val.v.val_offset
19207 / 2 / DWARF2_ADDR_SIZE].num
19208 == BLOCK_NUMBER (superblock))
19209 && BLOCK_FRAGMENT_CHAIN (superblock))
19210 {
19211 unsigned long off = attr->dw_attr_val.v.val_offset
19212 / 2 / DWARF2_ADDR_SIZE;
19213 unsigned long supercnt = 0, thiscnt = 0;
19214 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19215 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19216 {
19217 ++supercnt;
19218 gcc_checking_assert (ranges_table[off + supercnt].num
19219 == BLOCK_NUMBER (chain));
19220 }
19221 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19222 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19223 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19224 ++thiscnt;
19225 gcc_assert (supercnt >= thiscnt);
19226 add_AT_range_list (die, DW_AT_ranges,
19227 ((off + supercnt - thiscnt)
19228 * 2 * DWARF2_ADDR_SIZE),
19229 false);
19230 return;
19231 }
19232
19233 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19234
19235 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19236 do
19237 {
19238 add_ranges (chain);
19239 chain = BLOCK_FRAGMENT_CHAIN (chain);
19240 }
19241 while (chain);
19242 add_ranges (NULL);
19243 }
19244 else
19245 {
19246 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19247 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19248 BLOCK_NUMBER (stmt));
19249 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19250 BLOCK_NUMBER (stmt));
19251 add_AT_low_high_pc (die, label, label_high, false);
19252 }
19253 }
19254
19255 /* Generate a DIE for a lexical block. */
19256
19257 static void
19258 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19259 {
19260 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19261
19262 if (call_arg_locations)
19263 {
19264 if (block_map.length () <= BLOCK_NUMBER (stmt))
19265 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19266 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19267 }
19268
19269 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19270 add_high_low_attributes (stmt, stmt_die);
19271
19272 decls_for_scope (stmt, stmt_die, depth);
19273 }
19274
19275 /* Generate a DIE for an inlined subprogram. */
19276
19277 static void
19278 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19279 {
19280 tree decl;
19281
19282 /* The instance of function that is effectively being inlined shall not
19283 be abstract. */
19284 gcc_assert (! BLOCK_ABSTRACT (stmt));
19285
19286 decl = block_ultimate_origin (stmt);
19287
19288 /* Emit info for the abstract instance first, if we haven't yet. We
19289 must emit this even if the block is abstract, otherwise when we
19290 emit the block below (or elsewhere), we may end up trying to emit
19291 a die whose origin die hasn't been emitted, and crashing. */
19292 dwarf2out_abstract_function (decl);
19293
19294 if (! BLOCK_ABSTRACT (stmt))
19295 {
19296 dw_die_ref subr_die
19297 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19298
19299 if (call_arg_locations)
19300 {
19301 if (block_map.length () <= BLOCK_NUMBER (stmt))
19302 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19303 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19304 }
19305 add_abstract_origin_attribute (subr_die, decl);
19306 if (TREE_ASM_WRITTEN (stmt))
19307 add_high_low_attributes (stmt, subr_die);
19308 add_call_src_coords_attributes (stmt, subr_die);
19309
19310 decls_for_scope (stmt, subr_die, depth);
19311 current_function_has_inlines = 1;
19312 }
19313 }
19314
19315 /* Generate a DIE for a field in a record, or structure. */
19316
19317 static void
19318 gen_field_die (tree decl, dw_die_ref context_die)
19319 {
19320 dw_die_ref decl_die;
19321
19322 if (TREE_TYPE (decl) == error_mark_node)
19323 return;
19324
19325 decl_die = new_die (DW_TAG_member, context_die, decl);
19326 add_name_and_src_coords_attributes (decl_die, decl);
19327 add_type_attribute (decl_die, member_declared_type (decl),
19328 decl_quals (decl), context_die);
19329
19330 if (DECL_BIT_FIELD_TYPE (decl))
19331 {
19332 add_byte_size_attribute (decl_die, decl);
19333 add_bit_size_attribute (decl_die, decl);
19334 add_bit_offset_attribute (decl_die, decl);
19335 }
19336
19337 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19338 add_data_member_location_attribute (decl_die, decl);
19339
19340 if (DECL_ARTIFICIAL (decl))
19341 add_AT_flag (decl_die, DW_AT_artificial, 1);
19342
19343 add_accessibility_attribute (decl_die, decl);
19344
19345 /* Equate decl number to die, so that we can look up this decl later on. */
19346 equate_decl_number_to_die (decl, decl_die);
19347 }
19348
19349 #if 0
19350 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19351 Use modified_type_die instead.
19352 We keep this code here just in case these types of DIEs may be needed to
19353 represent certain things in other languages (e.g. Pascal) someday. */
19354
19355 static void
19356 gen_pointer_type_die (tree type, dw_die_ref context_die)
19357 {
19358 dw_die_ref ptr_die
19359 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19360
19361 equate_type_number_to_die (type, ptr_die);
19362 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19363 context_die);
19364 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19365 }
19366
19367 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19368 Use modified_type_die instead.
19369 We keep this code here just in case these types of DIEs may be needed to
19370 represent certain things in other languages (e.g. Pascal) someday. */
19371
19372 static void
19373 gen_reference_type_die (tree type, dw_die_ref context_die)
19374 {
19375 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19376
19377 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19378 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19379 else
19380 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19381
19382 equate_type_number_to_die (type, ref_die);
19383 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19384 context_die);
19385 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19386 }
19387 #endif
19388
19389 /* Generate a DIE for a pointer to a member type. */
19390
19391 static void
19392 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19393 {
19394 dw_die_ref ptr_die
19395 = new_die (DW_TAG_ptr_to_member_type,
19396 scope_die_for (type, context_die), type);
19397
19398 equate_type_number_to_die (type, ptr_die);
19399 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19400 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19401 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19402 context_die);
19403 }
19404
19405 typedef const char *dchar_p; /* For DEF_VEC_P. */
19406
19407 static char *producer_string;
19408
19409 /* Return a heap allocated producer string including command line options
19410 if -grecord-gcc-switches. */
19411
19412 static char *
19413 gen_producer_string (void)
19414 {
19415 size_t j;
19416 auto_vec<dchar_p> switches;
19417 const char *language_string = lang_hooks.name;
19418 char *producer, *tail;
19419 const char *p;
19420 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19421 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19422
19423 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19424 switch (save_decoded_options[j].opt_index)
19425 {
19426 case OPT_o:
19427 case OPT_d:
19428 case OPT_dumpbase:
19429 case OPT_dumpdir:
19430 case OPT_auxbase:
19431 case OPT_auxbase_strip:
19432 case OPT_quiet:
19433 case OPT_version:
19434 case OPT_v:
19435 case OPT_w:
19436 case OPT_L:
19437 case OPT_D:
19438 case OPT_I:
19439 case OPT_U:
19440 case OPT_SPECIAL_unknown:
19441 case OPT_SPECIAL_ignore:
19442 case OPT_SPECIAL_program_name:
19443 case OPT_SPECIAL_input_file:
19444 case OPT_grecord_gcc_switches:
19445 case OPT_gno_record_gcc_switches:
19446 case OPT__output_pch_:
19447 case OPT_fdiagnostics_show_location_:
19448 case OPT_fdiagnostics_show_option:
19449 case OPT_fdiagnostics_show_caret:
19450 case OPT_fdiagnostics_color_:
19451 case OPT_fverbose_asm:
19452 case OPT____:
19453 case OPT__sysroot_:
19454 case OPT_nostdinc:
19455 case OPT_nostdinc__:
19456 /* Ignore these. */
19457 continue;
19458 default:
19459 if (cl_options[save_decoded_options[j].opt_index].flags
19460 & CL_NO_DWARF_RECORD)
19461 continue;
19462 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19463 == '-');
19464 switch (save_decoded_options[j].canonical_option[0][1])
19465 {
19466 case 'M':
19467 case 'i':
19468 case 'W':
19469 continue;
19470 case 'f':
19471 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19472 "dump", 4) == 0)
19473 continue;
19474 break;
19475 default:
19476 break;
19477 }
19478 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19479 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19480 break;
19481 }
19482
19483 producer = XNEWVEC (char, plen + 1 + len + 1);
19484 tail = producer;
19485 sprintf (tail, "%s %s", language_string, version_string);
19486 tail += plen;
19487
19488 FOR_EACH_VEC_ELT (switches, j, p)
19489 {
19490 len = strlen (p);
19491 *tail = ' ';
19492 memcpy (tail + 1, p, len);
19493 tail += len + 1;
19494 }
19495
19496 *tail = '\0';
19497 return producer;
19498 }
19499
19500 /* Generate the DIE for the compilation unit. */
19501
19502 static dw_die_ref
19503 gen_compile_unit_die (const char *filename)
19504 {
19505 dw_die_ref die;
19506 const char *language_string = lang_hooks.name;
19507 int language;
19508
19509 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19510
19511 if (filename)
19512 {
19513 add_name_attribute (die, filename);
19514 /* Don't add cwd for <built-in>. */
19515 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19516 add_comp_dir_attribute (die);
19517 }
19518
19519 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19520
19521 /* If our producer is LTO try to figure out a common language to use
19522 from the global list of translation units. */
19523 if (strcmp (language_string, "GNU GIMPLE") == 0)
19524 {
19525 unsigned i;
19526 tree t;
19527 const char *common_lang = NULL;
19528
19529 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19530 {
19531 if (!TRANSLATION_UNIT_LANGUAGE (t))
19532 continue;
19533 if (!common_lang)
19534 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19535 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19536 ;
19537 else if (strncmp (common_lang, "GNU C", 5) == 0
19538 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19539 /* Mixing C and C++ is ok, use C++ in that case. */
19540 common_lang = "GNU C++";
19541 else
19542 {
19543 /* Fall back to C. */
19544 common_lang = NULL;
19545 break;
19546 }
19547 }
19548
19549 if (common_lang)
19550 language_string = common_lang;
19551 }
19552
19553 language = DW_LANG_C89;
19554 if (strcmp (language_string, "GNU C++") == 0)
19555 language = DW_LANG_C_plus_plus;
19556 else if (strcmp (language_string, "GNU F77") == 0)
19557 language = DW_LANG_Fortran77;
19558 else if (strcmp (language_string, "GNU Pascal") == 0)
19559 language = DW_LANG_Pascal83;
19560 else if (dwarf_version >= 3 || !dwarf_strict)
19561 {
19562 if (strcmp (language_string, "GNU Ada") == 0)
19563 language = DW_LANG_Ada95;
19564 else if (strcmp (language_string, "GNU Fortran") == 0)
19565 language = DW_LANG_Fortran95;
19566 else if (strcmp (language_string, "GNU Java") == 0)
19567 language = DW_LANG_Java;
19568 else if (strcmp (language_string, "GNU Objective-C") == 0)
19569 language = DW_LANG_ObjC;
19570 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19571 language = DW_LANG_ObjC_plus_plus;
19572 else if (dwarf_version >= 5 || !dwarf_strict)
19573 {
19574 if (strcmp (language_string, "GNU Go") == 0)
19575 language = DW_LANG_Go;
19576 }
19577 }
19578 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19579 else if (strcmp (language_string, "GNU Fortran") == 0)
19580 language = DW_LANG_Fortran90;
19581
19582 add_AT_unsigned (die, DW_AT_language, language);
19583
19584 switch (language)
19585 {
19586 case DW_LANG_Fortran77:
19587 case DW_LANG_Fortran90:
19588 case DW_LANG_Fortran95:
19589 /* Fortran has case insensitive identifiers and the front-end
19590 lowercases everything. */
19591 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19592 break;
19593 default:
19594 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19595 break;
19596 }
19597 return die;
19598 }
19599
19600 /* Generate the DIE for a base class. */
19601
19602 static void
19603 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19604 {
19605 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19606
19607 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19608 add_data_member_location_attribute (die, binfo);
19609
19610 if (BINFO_VIRTUAL_P (binfo))
19611 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19612
19613 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19614 children, otherwise the default is DW_ACCESS_public. In DWARF2
19615 the default has always been DW_ACCESS_private. */
19616 if (access == access_public_node)
19617 {
19618 if (dwarf_version == 2
19619 || context_die->die_tag == DW_TAG_class_type)
19620 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19621 }
19622 else if (access == access_protected_node)
19623 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19624 else if (dwarf_version > 2
19625 && context_die->die_tag != DW_TAG_class_type)
19626 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19627 }
19628
19629 /* Generate a DIE for a class member. */
19630
19631 static void
19632 gen_member_die (tree type, dw_die_ref context_die)
19633 {
19634 tree member;
19635 tree binfo = TYPE_BINFO (type);
19636 dw_die_ref child;
19637
19638 /* If this is not an incomplete type, output descriptions of each of its
19639 members. Note that as we output the DIEs necessary to represent the
19640 members of this record or union type, we will also be trying to output
19641 DIEs to represent the *types* of those members. However the `type'
19642 function (above) will specifically avoid generating type DIEs for member
19643 types *within* the list of member DIEs for this (containing) type except
19644 for those types (of members) which are explicitly marked as also being
19645 members of this (containing) type themselves. The g++ front- end can
19646 force any given type to be treated as a member of some other (containing)
19647 type by setting the TYPE_CONTEXT of the given (member) type to point to
19648 the TREE node representing the appropriate (containing) type. */
19649
19650 /* First output info about the base classes. */
19651 if (binfo)
19652 {
19653 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19654 int i;
19655 tree base;
19656
19657 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19658 gen_inheritance_die (base,
19659 (accesses ? (*accesses)[i] : access_public_node),
19660 context_die);
19661 }
19662
19663 /* Now output info about the data members and type members. */
19664 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19665 {
19666 /* If we thought we were generating minimal debug info for TYPE
19667 and then changed our minds, some of the member declarations
19668 may have already been defined. Don't define them again, but
19669 do put them in the right order. */
19670
19671 child = lookup_decl_die (member);
19672 if (child)
19673 splice_child_die (context_die, child);
19674 else
19675 gen_decl_die (member, NULL, context_die);
19676 }
19677
19678 /* Now output info about the function members (if any). */
19679 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19680 {
19681 /* Don't include clones in the member list. */
19682 if (DECL_ABSTRACT_ORIGIN (member))
19683 continue;
19684
19685 child = lookup_decl_die (member);
19686 if (child)
19687 splice_child_die (context_die, child);
19688 else
19689 gen_decl_die (member, NULL, context_die);
19690 }
19691 }
19692
19693 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19694 is set, we pretend that the type was never defined, so we only get the
19695 member DIEs needed by later specification DIEs. */
19696
19697 static void
19698 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19699 enum debug_info_usage usage)
19700 {
19701 dw_die_ref type_die = lookup_type_die (type);
19702 dw_die_ref scope_die = 0;
19703 int nested = 0;
19704 int complete = (TYPE_SIZE (type)
19705 && (! TYPE_STUB_DECL (type)
19706 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19707 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19708 complete = complete && should_emit_struct_debug (type, usage);
19709
19710 if (type_die && ! complete)
19711 return;
19712
19713 if (TYPE_CONTEXT (type) != NULL_TREE
19714 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19715 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19716 nested = 1;
19717
19718 scope_die = scope_die_for (type, context_die);
19719
19720 /* Generate child dies for template paramaters. */
19721 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19722 schedule_generic_params_dies_gen (type);
19723
19724 if (! type_die || (nested && is_cu_die (scope_die)))
19725 /* First occurrence of type or toplevel definition of nested class. */
19726 {
19727 dw_die_ref old_die = type_die;
19728
19729 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19730 ? record_type_tag (type) : DW_TAG_union_type,
19731 scope_die, type);
19732 equate_type_number_to_die (type, type_die);
19733 if (old_die)
19734 add_AT_specification (type_die, old_die);
19735 else
19736 add_name_attribute (type_die, type_tag (type));
19737 }
19738 else
19739 remove_AT (type_die, DW_AT_declaration);
19740
19741 /* If this type has been completed, then give it a byte_size attribute and
19742 then give a list of members. */
19743 if (complete && !ns_decl)
19744 {
19745 /* Prevent infinite recursion in cases where the type of some member of
19746 this type is expressed in terms of this type itself. */
19747 TREE_ASM_WRITTEN (type) = 1;
19748 add_byte_size_attribute (type_die, type);
19749 if (TYPE_STUB_DECL (type) != NULL_TREE)
19750 {
19751 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19752 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19753 }
19754
19755 /* If the first reference to this type was as the return type of an
19756 inline function, then it may not have a parent. Fix this now. */
19757 if (type_die->die_parent == NULL)
19758 add_child_die (scope_die, type_die);
19759
19760 push_decl_scope (type);
19761 gen_member_die (type, type_die);
19762 pop_decl_scope ();
19763
19764 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19765 if (TYPE_ARTIFICIAL (type))
19766 add_AT_flag (type_die, DW_AT_artificial, 1);
19767
19768 /* GNU extension: Record what type our vtable lives in. */
19769 if (TYPE_VFIELD (type))
19770 {
19771 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19772
19773 gen_type_die (vtype, context_die);
19774 add_AT_die_ref (type_die, DW_AT_containing_type,
19775 lookup_type_die (vtype));
19776 }
19777 }
19778 else
19779 {
19780 add_AT_flag (type_die, DW_AT_declaration, 1);
19781
19782 /* We don't need to do this for function-local types. */
19783 if (TYPE_STUB_DECL (type)
19784 && ! decl_function_context (TYPE_STUB_DECL (type)))
19785 vec_safe_push (incomplete_types, type);
19786 }
19787
19788 if (get_AT (type_die, DW_AT_name))
19789 add_pubtype (type, type_die);
19790 }
19791
19792 /* Generate a DIE for a subroutine _type_. */
19793
19794 static void
19795 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19796 {
19797 tree return_type = TREE_TYPE (type);
19798 dw_die_ref subr_die
19799 = new_die (DW_TAG_subroutine_type,
19800 scope_die_for (type, context_die), type);
19801
19802 equate_type_number_to_die (type, subr_die);
19803 add_prototyped_attribute (subr_die, type);
19804 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19805 gen_formal_types_die (type, subr_die);
19806
19807 if (get_AT (subr_die, DW_AT_name))
19808 add_pubtype (type, subr_die);
19809 }
19810
19811 /* Generate a DIE for a type definition. */
19812
19813 static void
19814 gen_typedef_die (tree decl, dw_die_ref context_die)
19815 {
19816 dw_die_ref type_die;
19817 tree origin;
19818
19819 if (TREE_ASM_WRITTEN (decl))
19820 return;
19821
19822 TREE_ASM_WRITTEN (decl) = 1;
19823 type_die = new_die (DW_TAG_typedef, context_die, decl);
19824 origin = decl_ultimate_origin (decl);
19825 if (origin != NULL)
19826 add_abstract_origin_attribute (type_die, origin);
19827 else
19828 {
19829 tree type;
19830
19831 add_name_and_src_coords_attributes (type_die, decl);
19832 if (DECL_ORIGINAL_TYPE (decl))
19833 {
19834 type = DECL_ORIGINAL_TYPE (decl);
19835
19836 gcc_assert (type != TREE_TYPE (decl));
19837 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19838 }
19839 else
19840 {
19841 type = TREE_TYPE (decl);
19842
19843 if (is_naming_typedef_decl (TYPE_NAME (type)))
19844 {
19845 /* Here, we are in the case of decl being a typedef naming
19846 an anonymous type, e.g:
19847 typedef struct {...} foo;
19848 In that case TREE_TYPE (decl) is not a typedef variant
19849 type and TYPE_NAME of the anonymous type is set to the
19850 TYPE_DECL of the typedef. This construct is emitted by
19851 the C++ FE.
19852
19853 TYPE is the anonymous struct named by the typedef
19854 DECL. As we need the DW_AT_type attribute of the
19855 DW_TAG_typedef to point to the DIE of TYPE, let's
19856 generate that DIE right away. add_type_attribute
19857 called below will then pick (via lookup_type_die) that
19858 anonymous struct DIE. */
19859 if (!TREE_ASM_WRITTEN (type))
19860 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19861
19862 /* This is a GNU Extension. We are adding a
19863 DW_AT_linkage_name attribute to the DIE of the
19864 anonymous struct TYPE. The value of that attribute
19865 is the name of the typedef decl naming the anonymous
19866 struct. This greatly eases the work of consumers of
19867 this debug info. */
19868 add_linkage_attr (lookup_type_die (type), decl);
19869 }
19870 }
19871
19872 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19873
19874 if (is_naming_typedef_decl (decl))
19875 /* We want that all subsequent calls to lookup_type_die with
19876 TYPE in argument yield the DW_TAG_typedef we have just
19877 created. */
19878 equate_type_number_to_die (type, type_die);
19879
19880 add_accessibility_attribute (type_die, decl);
19881 }
19882
19883 if (DECL_ABSTRACT (decl))
19884 equate_decl_number_to_die (decl, type_die);
19885
19886 if (get_AT (type_die, DW_AT_name))
19887 add_pubtype (decl, type_die);
19888 }
19889
19890 /* Generate a DIE for a struct, class, enum or union type. */
19891
19892 static void
19893 gen_tagged_type_die (tree type,
19894 dw_die_ref context_die,
19895 enum debug_info_usage usage)
19896 {
19897 int need_pop;
19898
19899 if (type == NULL_TREE
19900 || !is_tagged_type (type))
19901 return;
19902
19903 /* If this is a nested type whose containing class hasn't been written
19904 out yet, writing it out will cover this one, too. This does not apply
19905 to instantiations of member class templates; they need to be added to
19906 the containing class as they are generated. FIXME: This hurts the
19907 idea of combining type decls from multiple TUs, since we can't predict
19908 what set of template instantiations we'll get. */
19909 if (TYPE_CONTEXT (type)
19910 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19911 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19912 {
19913 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19914
19915 if (TREE_ASM_WRITTEN (type))
19916 return;
19917
19918 /* If that failed, attach ourselves to the stub. */
19919 push_decl_scope (TYPE_CONTEXT (type));
19920 context_die = lookup_type_die (TYPE_CONTEXT (type));
19921 need_pop = 1;
19922 }
19923 else if (TYPE_CONTEXT (type) != NULL_TREE
19924 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19925 {
19926 /* If this type is local to a function that hasn't been written
19927 out yet, use a NULL context for now; it will be fixed up in
19928 decls_for_scope. */
19929 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19930 /* A declaration DIE doesn't count; nested types need to go in the
19931 specification. */
19932 if (context_die && is_declaration_die (context_die))
19933 context_die = NULL;
19934 need_pop = 0;
19935 }
19936 else
19937 {
19938 context_die = declare_in_namespace (type, context_die);
19939 need_pop = 0;
19940 }
19941
19942 if (TREE_CODE (type) == ENUMERAL_TYPE)
19943 {
19944 /* This might have been written out by the call to
19945 declare_in_namespace. */
19946 if (!TREE_ASM_WRITTEN (type))
19947 gen_enumeration_type_die (type, context_die);
19948 }
19949 else
19950 gen_struct_or_union_type_die (type, context_die, usage);
19951
19952 if (need_pop)
19953 pop_decl_scope ();
19954
19955 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19956 it up if it is ever completed. gen_*_type_die will set it for us
19957 when appropriate. */
19958 }
19959
19960 /* Generate a type description DIE. */
19961
19962 static void
19963 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19964 enum debug_info_usage usage)
19965 {
19966 struct array_descr_info info;
19967
19968 if (type == NULL_TREE || type == error_mark_node)
19969 return;
19970
19971 if (TYPE_NAME (type) != NULL_TREE
19972 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19973 && is_redundant_typedef (TYPE_NAME (type))
19974 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19975 /* The DECL of this type is a typedef we don't want to emit debug
19976 info for but we want debug info for its underlying typedef.
19977 This can happen for e.g, the injected-class-name of a C++
19978 type. */
19979 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19980
19981 /* If TYPE is a typedef type variant, let's generate debug info
19982 for the parent typedef which TYPE is a type of. */
19983 if (typedef_variant_p (type))
19984 {
19985 if (TREE_ASM_WRITTEN (type))
19986 return;
19987
19988 /* Prevent broken recursion; we can't hand off to the same type. */
19989 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19990
19991 /* Give typedefs the right scope. */
19992 context_die = scope_die_for (type, context_die);
19993
19994 TREE_ASM_WRITTEN (type) = 1;
19995
19996 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19997 return;
19998 }
19999
20000 /* If type is an anonymous tagged type named by a typedef, let's
20001 generate debug info for the typedef. */
20002 if (is_naming_typedef_decl (TYPE_NAME (type)))
20003 {
20004 /* Use the DIE of the containing namespace as the parent DIE of
20005 the type description DIE we want to generate. */
20006 if (DECL_CONTEXT (TYPE_NAME (type))
20007 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20008 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20009
20010 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20011 return;
20012 }
20013
20014 /* If this is an array type with hidden descriptor, handle it first. */
20015 if (!TREE_ASM_WRITTEN (type)
20016 && lang_hooks.types.get_array_descr_info
20017 && lang_hooks.types.get_array_descr_info (type, &info)
20018 && (dwarf_version >= 3 || !dwarf_strict))
20019 {
20020 gen_descr_array_type_die (type, &info, context_die);
20021 TREE_ASM_WRITTEN (type) = 1;
20022 return;
20023 }
20024
20025 /* We are going to output a DIE to represent the unqualified version
20026 of this type (i.e. without any const or volatile qualifiers) so
20027 get the main variant (i.e. the unqualified version) of this type
20028 now. (Vectors are special because the debugging info is in the
20029 cloned type itself). */
20030 if (TREE_CODE (type) != VECTOR_TYPE)
20031 type = type_main_variant (type);
20032
20033 if (TREE_ASM_WRITTEN (type))
20034 return;
20035
20036 switch (TREE_CODE (type))
20037 {
20038 case ERROR_MARK:
20039 break;
20040
20041 case POINTER_TYPE:
20042 case REFERENCE_TYPE:
20043 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20044 ensures that the gen_type_die recursion will terminate even if the
20045 type is recursive. Recursive types are possible in Ada. */
20046 /* ??? We could perhaps do this for all types before the switch
20047 statement. */
20048 TREE_ASM_WRITTEN (type) = 1;
20049
20050 /* For these types, all that is required is that we output a DIE (or a
20051 set of DIEs) to represent the "basis" type. */
20052 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20053 DINFO_USAGE_IND_USE);
20054 break;
20055
20056 case OFFSET_TYPE:
20057 /* This code is used for C++ pointer-to-data-member types.
20058 Output a description of the relevant class type. */
20059 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20060 DINFO_USAGE_IND_USE);
20061
20062 /* Output a description of the type of the object pointed to. */
20063 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20064 DINFO_USAGE_IND_USE);
20065
20066 /* Now output a DIE to represent this pointer-to-data-member type
20067 itself. */
20068 gen_ptr_to_mbr_type_die (type, context_die);
20069 break;
20070
20071 case FUNCTION_TYPE:
20072 /* Force out return type (in case it wasn't forced out already). */
20073 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20074 DINFO_USAGE_DIR_USE);
20075 gen_subroutine_type_die (type, context_die);
20076 break;
20077
20078 case METHOD_TYPE:
20079 /* Force out return type (in case it wasn't forced out already). */
20080 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20081 DINFO_USAGE_DIR_USE);
20082 gen_subroutine_type_die (type, context_die);
20083 break;
20084
20085 case ARRAY_TYPE:
20086 gen_array_type_die (type, context_die);
20087 break;
20088
20089 case VECTOR_TYPE:
20090 gen_array_type_die (type, context_die);
20091 break;
20092
20093 case ENUMERAL_TYPE:
20094 case RECORD_TYPE:
20095 case UNION_TYPE:
20096 case QUAL_UNION_TYPE:
20097 gen_tagged_type_die (type, context_die, usage);
20098 return;
20099
20100 case VOID_TYPE:
20101 case INTEGER_TYPE:
20102 case REAL_TYPE:
20103 case FIXED_POINT_TYPE:
20104 case COMPLEX_TYPE:
20105 case BOOLEAN_TYPE:
20106 /* No DIEs needed for fundamental types. */
20107 break;
20108
20109 case NULLPTR_TYPE:
20110 case LANG_TYPE:
20111 /* Just use DW_TAG_unspecified_type. */
20112 {
20113 dw_die_ref type_die = lookup_type_die (type);
20114 if (type_die == NULL)
20115 {
20116 tree name = TYPE_IDENTIFIER (type);
20117 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20118 type);
20119 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20120 equate_type_number_to_die (type, type_die);
20121 }
20122 }
20123 break;
20124
20125 default:
20126 if (is_cxx_auto (type))
20127 {
20128 tree name = TYPE_IDENTIFIER (type);
20129 dw_die_ref *die = (name == get_identifier ("auto")
20130 ? &auto_die : &decltype_auto_die);
20131 if (!*die)
20132 {
20133 *die = new_die (DW_TAG_unspecified_type,
20134 comp_unit_die (), NULL_TREE);
20135 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20136 }
20137 equate_type_number_to_die (type, *die);
20138 break;
20139 }
20140 gcc_unreachable ();
20141 }
20142
20143 TREE_ASM_WRITTEN (type) = 1;
20144 }
20145
20146 static void
20147 gen_type_die (tree type, dw_die_ref context_die)
20148 {
20149 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20150 }
20151
20152 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20153 things which are local to the given block. */
20154
20155 static void
20156 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20157 {
20158 int must_output_die = 0;
20159 bool inlined_func;
20160
20161 /* Ignore blocks that are NULL. */
20162 if (stmt == NULL_TREE)
20163 return;
20164
20165 inlined_func = inlined_function_outer_scope_p (stmt);
20166
20167 /* If the block is one fragment of a non-contiguous block, do not
20168 process the variables, since they will have been done by the
20169 origin block. Do process subblocks. */
20170 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20171 {
20172 tree sub;
20173
20174 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20175 gen_block_die (sub, context_die, depth + 1);
20176
20177 return;
20178 }
20179
20180 /* Determine if we need to output any Dwarf DIEs at all to represent this
20181 block. */
20182 if (inlined_func)
20183 /* The outer scopes for inlinings *must* always be represented. We
20184 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20185 must_output_die = 1;
20186 else
20187 {
20188 /* Determine if this block directly contains any "significant"
20189 local declarations which we will need to output DIEs for. */
20190 if (debug_info_level > DINFO_LEVEL_TERSE)
20191 /* We are not in terse mode so *any* local declaration counts
20192 as being a "significant" one. */
20193 must_output_die = ((BLOCK_VARS (stmt) != NULL
20194 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20195 && (TREE_USED (stmt)
20196 || TREE_ASM_WRITTEN (stmt)
20197 || BLOCK_ABSTRACT (stmt)));
20198 else if ((TREE_USED (stmt)
20199 || TREE_ASM_WRITTEN (stmt)
20200 || BLOCK_ABSTRACT (stmt))
20201 && !dwarf2out_ignore_block (stmt))
20202 must_output_die = 1;
20203 }
20204
20205 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20206 DIE for any block which contains no significant local declarations at
20207 all. Rather, in such cases we just call `decls_for_scope' so that any
20208 needed Dwarf info for any sub-blocks will get properly generated. Note
20209 that in terse mode, our definition of what constitutes a "significant"
20210 local declaration gets restricted to include only inlined function
20211 instances and local (nested) function definitions. */
20212 if (must_output_die)
20213 {
20214 if (inlined_func)
20215 {
20216 /* If STMT block is abstract, that means we have been called
20217 indirectly from dwarf2out_abstract_function.
20218 That function rightfully marks the descendent blocks (of
20219 the abstract function it is dealing with) as being abstract,
20220 precisely to prevent us from emitting any
20221 DW_TAG_inlined_subroutine DIE as a descendent
20222 of an abstract function instance. So in that case, we should
20223 not call gen_inlined_subroutine_die.
20224
20225 Later though, when cgraph asks dwarf2out to emit info
20226 for the concrete instance of the function decl into which
20227 the concrete instance of STMT got inlined, the later will lead
20228 to the generation of a DW_TAG_inlined_subroutine DIE. */
20229 if (! BLOCK_ABSTRACT (stmt))
20230 gen_inlined_subroutine_die (stmt, context_die, depth);
20231 }
20232 else
20233 gen_lexical_block_die (stmt, context_die, depth);
20234 }
20235 else
20236 decls_for_scope (stmt, context_die, depth);
20237 }
20238
20239 /* Process variable DECL (or variable with origin ORIGIN) within
20240 block STMT and add it to CONTEXT_DIE. */
20241 static void
20242 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20243 {
20244 dw_die_ref die;
20245 tree decl_or_origin = decl ? decl : origin;
20246
20247 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20248 die = lookup_decl_die (decl_or_origin);
20249 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20250 && TYPE_DECL_IS_STUB (decl_or_origin))
20251 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20252 else
20253 die = NULL;
20254
20255 if (die != NULL && die->die_parent == NULL)
20256 add_child_die (context_die, die);
20257 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20258 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20259 stmt, context_die);
20260 else
20261 gen_decl_die (decl, origin, context_die);
20262 }
20263
20264 /* Generate all of the decls declared within a given scope and (recursively)
20265 all of its sub-blocks. */
20266
20267 static void
20268 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20269 {
20270 tree decl;
20271 unsigned int i;
20272 tree subblocks;
20273
20274 /* Ignore NULL blocks. */
20275 if (stmt == NULL_TREE)
20276 return;
20277
20278 /* Output the DIEs to represent all of the data objects and typedefs
20279 declared directly within this block but not within any nested
20280 sub-blocks. Also, nested function and tag DIEs have been
20281 generated with a parent of NULL; fix that up now. We don't
20282 have to do this if we're at -g1. */
20283 if (debug_info_level > DINFO_LEVEL_TERSE)
20284 {
20285 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20286 process_scope_var (stmt, decl, NULL_TREE, context_die);
20287 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20288 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20289 context_die);
20290 }
20291
20292 /* Even if we're at -g1, we need to process the subblocks in order to get
20293 inlined call information. */
20294
20295 /* Output the DIEs to represent all sub-blocks (and the items declared
20296 therein) of this block. */
20297 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20298 subblocks != NULL;
20299 subblocks = BLOCK_CHAIN (subblocks))
20300 gen_block_die (subblocks, context_die, depth + 1);
20301 }
20302
20303 /* Is this a typedef we can avoid emitting? */
20304
20305 static inline int
20306 is_redundant_typedef (const_tree decl)
20307 {
20308 if (TYPE_DECL_IS_STUB (decl))
20309 return 1;
20310
20311 if (DECL_ARTIFICIAL (decl)
20312 && DECL_CONTEXT (decl)
20313 && is_tagged_type (DECL_CONTEXT (decl))
20314 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20315 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20316 /* Also ignore the artificial member typedef for the class name. */
20317 return 1;
20318
20319 return 0;
20320 }
20321
20322 /* Return TRUE if TYPE is a typedef that names a type for linkage
20323 purposes. This kind of typedefs is produced by the C++ FE for
20324 constructs like:
20325
20326 typedef struct {...} foo;
20327
20328 In that case, there is no typedef variant type produced for foo.
20329 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20330 struct type. */
20331
20332 static bool
20333 is_naming_typedef_decl (const_tree decl)
20334 {
20335 if (decl == NULL_TREE
20336 || TREE_CODE (decl) != TYPE_DECL
20337 || !is_tagged_type (TREE_TYPE (decl))
20338 || DECL_IS_BUILTIN (decl)
20339 || is_redundant_typedef (decl)
20340 /* It looks like Ada produces TYPE_DECLs that are very similar
20341 to C++ naming typedefs but that have different
20342 semantics. Let's be specific to c++ for now. */
20343 || !is_cxx ())
20344 return FALSE;
20345
20346 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20347 && TYPE_NAME (TREE_TYPE (decl)) == decl
20348 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20349 != TYPE_NAME (TREE_TYPE (decl))));
20350 }
20351
20352 /* Returns the DIE for a context. */
20353
20354 static inline dw_die_ref
20355 get_context_die (tree context)
20356 {
20357 if (context)
20358 {
20359 /* Find die that represents this context. */
20360 if (TYPE_P (context))
20361 {
20362 context = TYPE_MAIN_VARIANT (context);
20363 return strip_naming_typedef (context, force_type_die (context));
20364 }
20365 else
20366 return force_decl_die (context);
20367 }
20368 return comp_unit_die ();
20369 }
20370
20371 /* Returns the DIE for decl. A DIE will always be returned. */
20372
20373 static dw_die_ref
20374 force_decl_die (tree decl)
20375 {
20376 dw_die_ref decl_die;
20377 unsigned saved_external_flag;
20378 tree save_fn = NULL_TREE;
20379 decl_die = lookup_decl_die (decl);
20380 if (!decl_die)
20381 {
20382 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20383
20384 decl_die = lookup_decl_die (decl);
20385 if (decl_die)
20386 return decl_die;
20387
20388 switch (TREE_CODE (decl))
20389 {
20390 case FUNCTION_DECL:
20391 /* Clear current_function_decl, so that gen_subprogram_die thinks
20392 that this is a declaration. At this point, we just want to force
20393 declaration die. */
20394 save_fn = current_function_decl;
20395 current_function_decl = NULL_TREE;
20396 gen_subprogram_die (decl, context_die);
20397 current_function_decl = save_fn;
20398 break;
20399
20400 case VAR_DECL:
20401 /* Set external flag to force declaration die. Restore it after
20402 gen_decl_die() call. */
20403 saved_external_flag = DECL_EXTERNAL (decl);
20404 DECL_EXTERNAL (decl) = 1;
20405 gen_decl_die (decl, NULL, context_die);
20406 DECL_EXTERNAL (decl) = saved_external_flag;
20407 break;
20408
20409 case NAMESPACE_DECL:
20410 if (dwarf_version >= 3 || !dwarf_strict)
20411 dwarf2out_decl (decl);
20412 else
20413 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20414 decl_die = comp_unit_die ();
20415 break;
20416
20417 case TRANSLATION_UNIT_DECL:
20418 decl_die = comp_unit_die ();
20419 break;
20420
20421 default:
20422 gcc_unreachable ();
20423 }
20424
20425 /* We should be able to find the DIE now. */
20426 if (!decl_die)
20427 decl_die = lookup_decl_die (decl);
20428 gcc_assert (decl_die);
20429 }
20430
20431 return decl_die;
20432 }
20433
20434 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20435 always returned. */
20436
20437 static dw_die_ref
20438 force_type_die (tree type)
20439 {
20440 dw_die_ref type_die;
20441
20442 type_die = lookup_type_die (type);
20443 if (!type_die)
20444 {
20445 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20446
20447 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20448 context_die);
20449 gcc_assert (type_die);
20450 }
20451 return type_die;
20452 }
20453
20454 /* Force out any required namespaces to be able to output DECL,
20455 and return the new context_die for it, if it's changed. */
20456
20457 static dw_die_ref
20458 setup_namespace_context (tree thing, dw_die_ref context_die)
20459 {
20460 tree context = (DECL_P (thing)
20461 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20462 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20463 /* Force out the namespace. */
20464 context_die = force_decl_die (context);
20465
20466 return context_die;
20467 }
20468
20469 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20470 type) within its namespace, if appropriate.
20471
20472 For compatibility with older debuggers, namespace DIEs only contain
20473 declarations; all definitions are emitted at CU scope. */
20474
20475 static dw_die_ref
20476 declare_in_namespace (tree thing, dw_die_ref context_die)
20477 {
20478 dw_die_ref ns_context;
20479
20480 if (debug_info_level <= DINFO_LEVEL_TERSE)
20481 return context_die;
20482
20483 /* If this decl is from an inlined function, then don't try to emit it in its
20484 namespace, as we will get confused. It would have already been emitted
20485 when the abstract instance of the inline function was emitted anyways. */
20486 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20487 return context_die;
20488
20489 ns_context = setup_namespace_context (thing, context_die);
20490
20491 if (ns_context != context_die)
20492 {
20493 if (is_fortran ())
20494 return ns_context;
20495 if (DECL_P (thing))
20496 gen_decl_die (thing, NULL, ns_context);
20497 else
20498 gen_type_die (thing, ns_context);
20499 }
20500 return context_die;
20501 }
20502
20503 /* Generate a DIE for a namespace or namespace alias. */
20504
20505 static void
20506 gen_namespace_die (tree decl, dw_die_ref context_die)
20507 {
20508 dw_die_ref namespace_die;
20509
20510 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20511 they are an alias of. */
20512 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20513 {
20514 /* Output a real namespace or module. */
20515 context_die = setup_namespace_context (decl, comp_unit_die ());
20516 namespace_die = new_die (is_fortran ()
20517 ? DW_TAG_module : DW_TAG_namespace,
20518 context_die, decl);
20519 /* For Fortran modules defined in different CU don't add src coords. */
20520 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20521 {
20522 const char *name = dwarf2_name (decl, 0);
20523 if (name)
20524 add_name_attribute (namespace_die, name);
20525 }
20526 else
20527 add_name_and_src_coords_attributes (namespace_die, decl);
20528 if (DECL_EXTERNAL (decl))
20529 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20530 equate_decl_number_to_die (decl, namespace_die);
20531 }
20532 else
20533 {
20534 /* Output a namespace alias. */
20535
20536 /* Force out the namespace we are an alias of, if necessary. */
20537 dw_die_ref origin_die
20538 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20539
20540 if (DECL_FILE_SCOPE_P (decl)
20541 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20542 context_die = setup_namespace_context (decl, comp_unit_die ());
20543 /* Now create the namespace alias DIE. */
20544 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20545 add_name_and_src_coords_attributes (namespace_die, decl);
20546 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20547 equate_decl_number_to_die (decl, namespace_die);
20548 }
20549 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20550 if (want_pubnames ())
20551 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20552 }
20553
20554 /* Generate Dwarf debug information for a decl described by DECL.
20555 The return value is currently only meaningful for PARM_DECLs,
20556 for all other decls it returns NULL. */
20557
20558 static dw_die_ref
20559 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20560 {
20561 tree decl_or_origin = decl ? decl : origin;
20562 tree class_origin = NULL, ultimate_origin;
20563
20564 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20565 return NULL;
20566
20567 switch (TREE_CODE (decl_or_origin))
20568 {
20569 case ERROR_MARK:
20570 break;
20571
20572 case CONST_DECL:
20573 if (!is_fortran () && !is_ada ())
20574 {
20575 /* The individual enumerators of an enum type get output when we output
20576 the Dwarf representation of the relevant enum type itself. */
20577 break;
20578 }
20579
20580 /* Emit its type. */
20581 gen_type_die (TREE_TYPE (decl), context_die);
20582
20583 /* And its containing namespace. */
20584 context_die = declare_in_namespace (decl, context_die);
20585
20586 gen_const_die (decl, context_die);
20587 break;
20588
20589 case FUNCTION_DECL:
20590 /* Don't output any DIEs to represent mere function declarations,
20591 unless they are class members or explicit block externs. */
20592 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20593 && DECL_FILE_SCOPE_P (decl_or_origin)
20594 && (current_function_decl == NULL_TREE
20595 || DECL_ARTIFICIAL (decl_or_origin)))
20596 break;
20597
20598 #if 0
20599 /* FIXME */
20600 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20601 on local redeclarations of global functions. That seems broken. */
20602 if (current_function_decl != decl)
20603 /* This is only a declaration. */;
20604 #endif
20605
20606 /* If we're emitting a clone, emit info for the abstract instance. */
20607 if (origin || DECL_ORIGIN (decl) != decl)
20608 dwarf2out_abstract_function (origin
20609 ? DECL_ORIGIN (origin)
20610 : DECL_ABSTRACT_ORIGIN (decl));
20611
20612 /* If we're emitting an out-of-line copy of an inline function,
20613 emit info for the abstract instance and set up to refer to it. */
20614 else if (cgraph_function_possibly_inlined_p (decl)
20615 && ! DECL_ABSTRACT (decl)
20616 && ! class_or_namespace_scope_p (context_die)
20617 /* dwarf2out_abstract_function won't emit a die if this is just
20618 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20619 that case, because that works only if we have a die. */
20620 && DECL_INITIAL (decl) != NULL_TREE)
20621 {
20622 dwarf2out_abstract_function (decl);
20623 set_decl_origin_self (decl);
20624 }
20625
20626 /* Otherwise we're emitting the primary DIE for this decl. */
20627 else if (debug_info_level > DINFO_LEVEL_TERSE)
20628 {
20629 /* Before we describe the FUNCTION_DECL itself, make sure that we
20630 have its containing type. */
20631 if (!origin)
20632 origin = decl_class_context (decl);
20633 if (origin != NULL_TREE)
20634 gen_type_die (origin, context_die);
20635
20636 /* And its return type. */
20637 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20638
20639 /* And its virtual context. */
20640 if (DECL_VINDEX (decl) != NULL_TREE)
20641 gen_type_die (DECL_CONTEXT (decl), context_die);
20642
20643 /* Make sure we have a member DIE for decl. */
20644 if (origin != NULL_TREE)
20645 gen_type_die_for_member (origin, decl, context_die);
20646
20647 /* And its containing namespace. */
20648 context_die = declare_in_namespace (decl, context_die);
20649 }
20650
20651 /* Now output a DIE to represent the function itself. */
20652 if (decl)
20653 gen_subprogram_die (decl, context_die);
20654 break;
20655
20656 case TYPE_DECL:
20657 /* If we are in terse mode, don't generate any DIEs to represent any
20658 actual typedefs. */
20659 if (debug_info_level <= DINFO_LEVEL_TERSE)
20660 break;
20661
20662 /* In the special case of a TYPE_DECL node representing the declaration
20663 of some type tag, if the given TYPE_DECL is marked as having been
20664 instantiated from some other (original) TYPE_DECL node (e.g. one which
20665 was generated within the original definition of an inline function) we
20666 used to generate a special (abbreviated) DW_TAG_structure_type,
20667 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20668 should be actually referencing those DIEs, as variable DIEs with that
20669 type would be emitted already in the abstract origin, so it was always
20670 removed during unused type prunning. Don't add anything in this
20671 case. */
20672 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20673 break;
20674
20675 if (is_redundant_typedef (decl))
20676 gen_type_die (TREE_TYPE (decl), context_die);
20677 else
20678 /* Output a DIE to represent the typedef itself. */
20679 gen_typedef_die (decl, context_die);
20680 break;
20681
20682 case LABEL_DECL:
20683 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20684 gen_label_die (decl, context_die);
20685 break;
20686
20687 case VAR_DECL:
20688 case RESULT_DECL:
20689 /* If we are in terse mode, don't generate any DIEs to represent any
20690 variable declarations or definitions. */
20691 if (debug_info_level <= DINFO_LEVEL_TERSE)
20692 break;
20693
20694 /* Output any DIEs that are needed to specify the type of this data
20695 object. */
20696 if (decl_by_reference_p (decl_or_origin))
20697 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20698 else
20699 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20700
20701 /* And its containing type. */
20702 class_origin = decl_class_context (decl_or_origin);
20703 if (class_origin != NULL_TREE)
20704 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20705
20706 /* And its containing namespace. */
20707 context_die = declare_in_namespace (decl_or_origin, context_die);
20708
20709 /* Now output the DIE to represent the data object itself. This gets
20710 complicated because of the possibility that the VAR_DECL really
20711 represents an inlined instance of a formal parameter for an inline
20712 function. */
20713 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20714 if (ultimate_origin != NULL_TREE
20715 && TREE_CODE (ultimate_origin) == PARM_DECL)
20716 gen_formal_parameter_die (decl, origin,
20717 true /* Emit name attribute. */,
20718 context_die);
20719 else
20720 gen_variable_die (decl, origin, context_die);
20721 break;
20722
20723 case FIELD_DECL:
20724 /* Ignore the nameless fields that are used to skip bits but handle C++
20725 anonymous unions and structs. */
20726 if (DECL_NAME (decl) != NULL_TREE
20727 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20728 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20729 {
20730 gen_type_die (member_declared_type (decl), context_die);
20731 gen_field_die (decl, context_die);
20732 }
20733 break;
20734
20735 case PARM_DECL:
20736 if (DECL_BY_REFERENCE (decl_or_origin))
20737 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20738 else
20739 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20740 return gen_formal_parameter_die (decl, origin,
20741 true /* Emit name attribute. */,
20742 context_die);
20743
20744 case NAMESPACE_DECL:
20745 case IMPORTED_DECL:
20746 if (dwarf_version >= 3 || !dwarf_strict)
20747 gen_namespace_die (decl, context_die);
20748 break;
20749
20750 case NAMELIST_DECL:
20751 gen_namelist_decl (DECL_NAME (decl), context_die,
20752 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20753 break;
20754
20755 default:
20756 /* Probably some frontend-internal decl. Assume we don't care. */
20757 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20758 break;
20759 }
20760
20761 return NULL;
20762 }
20763 \f
20764 /* Output debug information for global decl DECL. Called from toplev.c after
20765 compilation proper has finished. */
20766
20767 static void
20768 dwarf2out_global_decl (tree decl)
20769 {
20770 /* Output DWARF2 information for file-scope tentative data object
20771 declarations, file-scope (extern) function declarations (which
20772 had no corresponding body) and file-scope tagged type declarations
20773 and definitions which have not yet been forced out. */
20774 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20775 dwarf2out_decl (decl);
20776 }
20777
20778 /* Output debug information for type decl DECL. Called from toplev.c
20779 and from language front ends (to record built-in types). */
20780 static void
20781 dwarf2out_type_decl (tree decl, int local)
20782 {
20783 if (!local)
20784 dwarf2out_decl (decl);
20785 }
20786
20787 /* Output debug information for imported module or decl DECL.
20788 NAME is non-NULL name in the lexical block if the decl has been renamed.
20789 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20790 that DECL belongs to.
20791 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20792 static void
20793 dwarf2out_imported_module_or_decl_1 (tree decl,
20794 tree name,
20795 tree lexical_block,
20796 dw_die_ref lexical_block_die)
20797 {
20798 expanded_location xloc;
20799 dw_die_ref imported_die = NULL;
20800 dw_die_ref at_import_die;
20801
20802 if (TREE_CODE (decl) == IMPORTED_DECL)
20803 {
20804 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20805 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20806 gcc_assert (decl);
20807 }
20808 else
20809 xloc = expand_location (input_location);
20810
20811 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20812 {
20813 at_import_die = force_type_die (TREE_TYPE (decl));
20814 /* For namespace N { typedef void T; } using N::T; base_type_die
20815 returns NULL, but DW_TAG_imported_declaration requires
20816 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20817 if (!at_import_die)
20818 {
20819 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20820 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20821 at_import_die = lookup_type_die (TREE_TYPE (decl));
20822 gcc_assert (at_import_die);
20823 }
20824 }
20825 else
20826 {
20827 at_import_die = lookup_decl_die (decl);
20828 if (!at_import_die)
20829 {
20830 /* If we're trying to avoid duplicate debug info, we may not have
20831 emitted the member decl for this field. Emit it now. */
20832 if (TREE_CODE (decl) == FIELD_DECL)
20833 {
20834 tree type = DECL_CONTEXT (decl);
20835
20836 if (TYPE_CONTEXT (type)
20837 && TYPE_P (TYPE_CONTEXT (type))
20838 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20839 DINFO_USAGE_DIR_USE))
20840 return;
20841 gen_type_die_for_member (type, decl,
20842 get_context_die (TYPE_CONTEXT (type)));
20843 }
20844 if (TREE_CODE (decl) == NAMELIST_DECL)
20845 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20846 get_context_die (DECL_CONTEXT (decl)),
20847 NULL_TREE);
20848 else
20849 at_import_die = force_decl_die (decl);
20850 }
20851 }
20852
20853 if (TREE_CODE (decl) == NAMESPACE_DECL)
20854 {
20855 if (dwarf_version >= 3 || !dwarf_strict)
20856 imported_die = new_die (DW_TAG_imported_module,
20857 lexical_block_die,
20858 lexical_block);
20859 else
20860 return;
20861 }
20862 else
20863 imported_die = new_die (DW_TAG_imported_declaration,
20864 lexical_block_die,
20865 lexical_block);
20866
20867 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20868 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20869 if (name)
20870 add_AT_string (imported_die, DW_AT_name,
20871 IDENTIFIER_POINTER (name));
20872 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20873 }
20874
20875 /* Output debug information for imported module or decl DECL.
20876 NAME is non-NULL name in context if the decl has been renamed.
20877 CHILD is true if decl is one of the renamed decls as part of
20878 importing whole module. */
20879
20880 static void
20881 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20882 bool child)
20883 {
20884 /* dw_die_ref at_import_die; */
20885 dw_die_ref scope_die;
20886
20887 if (debug_info_level <= DINFO_LEVEL_TERSE)
20888 return;
20889
20890 gcc_assert (decl);
20891
20892 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20893 We need decl DIE for reference and scope die. First, get DIE for the decl
20894 itself. */
20895
20896 /* Get the scope die for decl context. Use comp_unit_die for global module
20897 or decl. If die is not found for non globals, force new die. */
20898 if (context
20899 && TYPE_P (context)
20900 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20901 return;
20902
20903 if (!(dwarf_version >= 3 || !dwarf_strict))
20904 return;
20905
20906 scope_die = get_context_die (context);
20907
20908 if (child)
20909 {
20910 gcc_assert (scope_die->die_child);
20911 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20912 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20913 scope_die = scope_die->die_child;
20914 }
20915
20916 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20917 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20918
20919 }
20920
20921 /* Output debug information for namelists. */
20922
20923 static dw_die_ref
20924 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20925 {
20926 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20927 tree value;
20928 unsigned i;
20929
20930 if (debug_info_level <= DINFO_LEVEL_TERSE)
20931 return NULL;
20932
20933 gcc_assert (scope_die != NULL);
20934 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20935 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20936
20937 /* If there are no item_decls, we have a nondefining namelist, e.g.
20938 with USE association; hence, set DW_AT_declaration. */
20939 if (item_decls == NULL_TREE)
20940 {
20941 add_AT_flag (nml_die, DW_AT_declaration, 1);
20942 return nml_die;
20943 }
20944
20945 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20946 {
20947 nml_item_ref_die = lookup_decl_die (value);
20948 if (!nml_item_ref_die)
20949 nml_item_ref_die = force_decl_die (value);
20950
20951 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20952 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20953 }
20954 return nml_die;
20955 }
20956
20957
20958 /* Write the debugging output for DECL. */
20959
20960 static void
20961 dwarf2out_decl (tree decl)
20962 {
20963 dw_die_ref context_die = comp_unit_die ();
20964
20965 switch (TREE_CODE (decl))
20966 {
20967 case ERROR_MARK:
20968 return;
20969
20970 case FUNCTION_DECL:
20971 /* What we would really like to do here is to filter out all mere
20972 file-scope declarations of file-scope functions which are never
20973 referenced later within this translation unit (and keep all of ones
20974 that *are* referenced later on) but we aren't clairvoyant, so we have
20975 no idea which functions will be referenced in the future (i.e. later
20976 on within the current translation unit). So here we just ignore all
20977 file-scope function declarations which are not also definitions. If
20978 and when the debugger needs to know something about these functions,
20979 it will have to hunt around and find the DWARF information associated
20980 with the definition of the function.
20981
20982 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20983 nodes represent definitions and which ones represent mere
20984 declarations. We have to check DECL_INITIAL instead. That's because
20985 the C front-end supports some weird semantics for "extern inline"
20986 function definitions. These can get inlined within the current
20987 translation unit (and thus, we need to generate Dwarf info for their
20988 abstract instances so that the Dwarf info for the concrete inlined
20989 instances can have something to refer to) but the compiler never
20990 generates any out-of-lines instances of such things (despite the fact
20991 that they *are* definitions).
20992
20993 The important point is that the C front-end marks these "extern
20994 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20995 them anyway. Note that the C++ front-end also plays some similar games
20996 for inline function definitions appearing within include files which
20997 also contain `#pragma interface' pragmas.
20998
20999 If we are called from dwarf2out_abstract_function output a DIE
21000 anyway. We can end up here this way with early inlining and LTO
21001 where the inlined function is output in a different LTRANS unit
21002 or not at all. */
21003 if (DECL_INITIAL (decl) == NULL_TREE
21004 && ! DECL_ABSTRACT (decl))
21005 return;
21006
21007 /* If we're a nested function, initially use a parent of NULL; if we're
21008 a plain function, this will be fixed up in decls_for_scope. If
21009 we're a method, it will be ignored, since we already have a DIE. */
21010 if (decl_function_context (decl)
21011 /* But if we're in terse mode, we don't care about scope. */
21012 && debug_info_level > DINFO_LEVEL_TERSE)
21013 context_die = NULL;
21014 break;
21015
21016 case VAR_DECL:
21017 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21018 declaration and if the declaration was never even referenced from
21019 within this entire compilation unit. We suppress these DIEs in
21020 order to save space in the .debug section (by eliminating entries
21021 which are probably useless). Note that we must not suppress
21022 block-local extern declarations (whether used or not) because that
21023 would screw-up the debugger's name lookup mechanism and cause it to
21024 miss things which really ought to be in scope at a given point. */
21025 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21026 return;
21027
21028 /* For local statics lookup proper context die. */
21029 if (TREE_STATIC (decl)
21030 && DECL_CONTEXT (decl)
21031 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21032 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21033
21034 /* If we are in terse mode, don't generate any DIEs to represent any
21035 variable declarations or definitions. */
21036 if (debug_info_level <= DINFO_LEVEL_TERSE)
21037 return;
21038 break;
21039
21040 case CONST_DECL:
21041 if (debug_info_level <= DINFO_LEVEL_TERSE)
21042 return;
21043 if (!is_fortran () && !is_ada ())
21044 return;
21045 if (TREE_STATIC (decl) && decl_function_context (decl))
21046 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21047 break;
21048
21049 case NAMESPACE_DECL:
21050 case IMPORTED_DECL:
21051 if (debug_info_level <= DINFO_LEVEL_TERSE)
21052 return;
21053 if (lookup_decl_die (decl) != NULL)
21054 return;
21055 break;
21056
21057 case TYPE_DECL:
21058 /* Don't emit stubs for types unless they are needed by other DIEs. */
21059 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21060 return;
21061
21062 /* Don't bother trying to generate any DIEs to represent any of the
21063 normal built-in types for the language we are compiling. */
21064 if (DECL_IS_BUILTIN (decl))
21065 return;
21066
21067 /* If we are in terse mode, don't generate any DIEs for types. */
21068 if (debug_info_level <= DINFO_LEVEL_TERSE)
21069 return;
21070
21071 /* If we're a function-scope tag, initially use a parent of NULL;
21072 this will be fixed up in decls_for_scope. */
21073 if (decl_function_context (decl))
21074 context_die = NULL;
21075
21076 break;
21077
21078 case NAMELIST_DECL:
21079 break;
21080
21081 default:
21082 return;
21083 }
21084
21085 gen_decl_die (decl, NULL, context_die);
21086
21087 dw_die_ref die = lookup_decl_die (decl);
21088 if (die)
21089 check_die (die, 0);
21090 }
21091
21092 /* Early dumping of DECLs before we lose language data. */
21093
21094 void
21095 dwarf2out_early_decl (tree decl)
21096 {
21097 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21098 other DECLs and they can point to template types or other things
21099 that dwarf2out can't handle when done via dwarf2out_decl. */
21100 if (TREE_CODE (decl) != TYPE_DECL
21101 && TREE_CODE (decl) != PARM_DECL)
21102 {
21103 if (TREE_CODE (decl) == FUNCTION_DECL)
21104 {
21105 push_cfun (DECL_STRUCT_FUNCTION (decl));
21106 current_function_decl = decl;
21107 }
21108 dwarf2out_decl (decl);
21109 if (TREE_CODE (decl) == FUNCTION_DECL)
21110 {
21111 pop_cfun ();
21112 current_function_decl = NULL;
21113 }
21114 }
21115 }
21116
21117 /* Write the debugging output for DECL. */
21118
21119 static void
21120 dwarf2out_function_decl (tree decl)
21121 {
21122 dwarf2out_decl (decl);
21123 call_arg_locations = NULL;
21124 call_arg_loc_last = NULL;
21125 call_site_count = -1;
21126 tail_call_site_count = -1;
21127 block_map.release ();
21128 htab_empty (decl_loc_table);
21129 htab_empty (cached_dw_loc_list_table);
21130 }
21131
21132 /* Output a marker (i.e. a label) for the beginning of the generated code for
21133 a lexical block. */
21134
21135 static void
21136 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21137 unsigned int blocknum)
21138 {
21139 switch_to_section (current_function_section ());
21140 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21141 }
21142
21143 /* Output a marker (i.e. a label) for the end of the generated code for a
21144 lexical block. */
21145
21146 static void
21147 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21148 {
21149 switch_to_section (current_function_section ());
21150 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21151 }
21152
21153 /* Returns nonzero if it is appropriate not to emit any debugging
21154 information for BLOCK, because it doesn't contain any instructions.
21155
21156 Don't allow this for blocks with nested functions or local classes
21157 as we would end up with orphans, and in the presence of scheduling
21158 we may end up calling them anyway. */
21159
21160 static bool
21161 dwarf2out_ignore_block (const_tree block)
21162 {
21163 tree decl;
21164 unsigned int i;
21165
21166 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21167 if (TREE_CODE (decl) == FUNCTION_DECL
21168 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21169 return 0;
21170 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21171 {
21172 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21173 if (TREE_CODE (decl) == FUNCTION_DECL
21174 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21175 return 0;
21176 }
21177
21178 return 1;
21179 }
21180
21181 /* Hash table routines for file_hash. */
21182
21183 static int
21184 file_table_eq (const void *p1_p, const void *p2_p)
21185 {
21186 const struct dwarf_file_data *const p1 =
21187 (const struct dwarf_file_data *) p1_p;
21188 const char *const p2 = (const char *) p2_p;
21189 return filename_cmp (p1->filename, p2) == 0;
21190 }
21191
21192 static hashval_t
21193 file_table_hash (const void *p_p)
21194 {
21195 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21196 return htab_hash_string (p->filename);
21197 }
21198
21199 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21200 dwarf2out.c) and return its "index". The index of each (known) filename is
21201 just a unique number which is associated with only that one filename. We
21202 need such numbers for the sake of generating labels (in the .debug_sfnames
21203 section) and references to those files numbers (in the .debug_srcinfo
21204 and.debug_macinfo sections). If the filename given as an argument is not
21205 found in our current list, add it to the list and assign it the next
21206 available unique index number. In order to speed up searches, we remember
21207 the index of the filename was looked up last. This handles the majority of
21208 all searches. */
21209
21210 static struct dwarf_file_data *
21211 lookup_filename (const char *file_name)
21212 {
21213 void ** slot;
21214 struct dwarf_file_data * created;
21215
21216 /* Check to see if the file name that was searched on the previous
21217 call matches this file name. If so, return the index. */
21218 if (file_table_last_lookup
21219 && (file_name == file_table_last_lookup->filename
21220 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21221 return file_table_last_lookup;
21222
21223 /* Didn't match the previous lookup, search the table. */
21224 slot = htab_find_slot_with_hash (file_table, file_name,
21225 htab_hash_string (file_name), INSERT);
21226 if (*slot)
21227 return (struct dwarf_file_data *) *slot;
21228
21229 created = ggc_alloc<dwarf_file_data> ();
21230 created->filename = file_name;
21231 created->emitted_number = 0;
21232 *slot = created;
21233 return created;
21234 }
21235
21236 /* If the assembler will construct the file table, then translate the compiler
21237 internal file table number into the assembler file table number, and emit
21238 a .file directive if we haven't already emitted one yet. The file table
21239 numbers are different because we prune debug info for unused variables and
21240 types, which may include filenames. */
21241
21242 static int
21243 maybe_emit_file (struct dwarf_file_data * fd)
21244 {
21245 if (! fd->emitted_number)
21246 {
21247 if (last_emitted_file)
21248 fd->emitted_number = last_emitted_file->emitted_number + 1;
21249 else
21250 fd->emitted_number = 1;
21251 last_emitted_file = fd;
21252
21253 if (DWARF2_ASM_LINE_DEBUG_INFO)
21254 {
21255 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21256 output_quoted_string (asm_out_file,
21257 remap_debug_filename (fd->filename));
21258 fputc ('\n', asm_out_file);
21259 }
21260 }
21261
21262 return fd->emitted_number;
21263 }
21264
21265 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21266 That generation should happen after function debug info has been
21267 generated. The value of the attribute is the constant value of ARG. */
21268
21269 static void
21270 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21271 {
21272 die_arg_entry entry;
21273
21274 if (!die || !arg)
21275 return;
21276
21277 if (!tmpl_value_parm_die_table)
21278 vec_alloc (tmpl_value_parm_die_table, 32);
21279
21280 entry.die = die;
21281 entry.arg = arg;
21282 vec_safe_push (tmpl_value_parm_die_table, entry);
21283 }
21284
21285 /* Return TRUE if T is an instance of generic type, FALSE
21286 otherwise. */
21287
21288 static bool
21289 generic_type_p (tree t)
21290 {
21291 if (t == NULL_TREE || !TYPE_P (t))
21292 return false;
21293 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21294 }
21295
21296 /* Schedule the generation of the generic parameter dies for the
21297 instance of generic type T. The proper generation itself is later
21298 done by gen_scheduled_generic_parms_dies. */
21299
21300 static void
21301 schedule_generic_params_dies_gen (tree t)
21302 {
21303 if (!generic_type_p (t))
21304 return;
21305
21306 if (!generic_type_instances)
21307 vec_alloc (generic_type_instances, 256);
21308
21309 vec_safe_push (generic_type_instances, t);
21310 }
21311
21312 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21313 by append_entry_to_tmpl_value_parm_die_table. This function must
21314 be called after function DIEs have been generated. */
21315
21316 static void
21317 gen_remaining_tmpl_value_param_die_attribute (void)
21318 {
21319 if (tmpl_value_parm_die_table)
21320 {
21321 unsigned i;
21322 die_arg_entry *e;
21323
21324 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21325 tree_add_const_value_attribute (e->die, e->arg);
21326 }
21327 }
21328
21329 /* Generate generic parameters DIEs for instances of generic types
21330 that have been previously scheduled by
21331 schedule_generic_params_dies_gen. This function must be called
21332 after all the types of the CU have been laid out. */
21333
21334 static void
21335 gen_scheduled_generic_parms_dies (void)
21336 {
21337 unsigned i;
21338 tree t;
21339
21340 if (!generic_type_instances)
21341 return;
21342
21343 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21344 if (COMPLETE_TYPE_P (t))
21345 gen_generic_params_dies (t);
21346 }
21347
21348
21349 /* Replace DW_AT_name for the decl with name. */
21350
21351 static void
21352 dwarf2out_set_name (tree decl, tree name)
21353 {
21354 dw_die_ref die;
21355 dw_attr_ref attr;
21356 const char *dname;
21357
21358 die = TYPE_SYMTAB_DIE (decl);
21359 if (!die)
21360 return;
21361
21362 dname = dwarf2_name (name, 0);
21363 if (!dname)
21364 return;
21365
21366 attr = get_AT (die, DW_AT_name);
21367 if (attr)
21368 {
21369 struct indirect_string_node *node;
21370
21371 node = find_AT_string (dname);
21372 /* replace the string. */
21373 attr->dw_attr_val.v.val_str = node;
21374 }
21375
21376 else
21377 add_name_attribute (die, dname);
21378 }
21379
21380 /* True if before or during processing of the first function being emitted. */
21381 static bool in_first_function_p = true;
21382 /* True if loc_note during dwarf2out_var_location call might still be
21383 before first real instruction at address equal to .Ltext0. */
21384 static bool maybe_at_text_label_p = true;
21385 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21386 static unsigned int first_loclabel_num_not_at_text_label;
21387
21388 /* Called by the final INSN scan whenever we see a var location. We
21389 use it to drop labels in the right places, and throw the location in
21390 our lookup table. */
21391
21392 static void
21393 dwarf2out_var_location (rtx_insn *loc_note)
21394 {
21395 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21396 struct var_loc_node *newloc;
21397 rtx_insn *next_real, *next_note;
21398 static const char *last_label;
21399 static const char *last_postcall_label;
21400 static bool last_in_cold_section_p;
21401 static rtx_insn *expected_next_loc_note;
21402 tree decl;
21403 bool var_loc_p;
21404
21405 if (!NOTE_P (loc_note))
21406 {
21407 if (CALL_P (loc_note))
21408 {
21409 call_site_count++;
21410 if (SIBLING_CALL_P (loc_note))
21411 tail_call_site_count++;
21412 }
21413 return;
21414 }
21415
21416 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21417 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21418 return;
21419
21420 /* Optimize processing a large consecutive sequence of location
21421 notes so we don't spend too much time in next_real_insn. If the
21422 next insn is another location note, remember the next_real_insn
21423 calculation for next time. */
21424 next_real = cached_next_real_insn;
21425 if (next_real)
21426 {
21427 if (expected_next_loc_note != loc_note)
21428 next_real = NULL;
21429 }
21430
21431 next_note = NEXT_INSN (loc_note);
21432 if (! next_note
21433 || INSN_DELETED_P (next_note)
21434 || ! NOTE_P (next_note)
21435 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21436 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21437 next_note = NULL;
21438
21439 if (! next_real)
21440 next_real = next_real_insn (loc_note);
21441
21442 if (next_note)
21443 {
21444 expected_next_loc_note = next_note;
21445 cached_next_real_insn = next_real;
21446 }
21447 else
21448 cached_next_real_insn = NULL;
21449
21450 /* If there are no instructions which would be affected by this note,
21451 don't do anything. */
21452 if (var_loc_p
21453 && next_real == NULL_RTX
21454 && !NOTE_DURING_CALL_P (loc_note))
21455 return;
21456
21457 if (next_real == NULL_RTX)
21458 next_real = get_last_insn ();
21459
21460 /* If there were any real insns between note we processed last time
21461 and this note (or if it is the first note), clear
21462 last_{,postcall_}label so that they are not reused this time. */
21463 if (last_var_location_insn == NULL_RTX
21464 || last_var_location_insn != next_real
21465 || last_in_cold_section_p != in_cold_section_p)
21466 {
21467 last_label = NULL;
21468 last_postcall_label = NULL;
21469 }
21470
21471 if (var_loc_p)
21472 {
21473 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21474 newloc = add_var_loc_to_decl (decl, loc_note,
21475 NOTE_DURING_CALL_P (loc_note)
21476 ? last_postcall_label : last_label);
21477 if (newloc == NULL)
21478 return;
21479 }
21480 else
21481 {
21482 decl = NULL_TREE;
21483 newloc = NULL;
21484 }
21485
21486 /* If there were no real insns between note we processed last time
21487 and this note, use the label we emitted last time. Otherwise
21488 create a new label and emit it. */
21489 if (last_label == NULL)
21490 {
21491 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21492 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21493 loclabel_num++;
21494 last_label = ggc_strdup (loclabel);
21495 /* See if loclabel might be equal to .Ltext0. If yes,
21496 bump first_loclabel_num_not_at_text_label. */
21497 if (!have_multiple_function_sections
21498 && in_first_function_p
21499 && maybe_at_text_label_p)
21500 {
21501 static rtx_insn *last_start;
21502 rtx_insn *insn;
21503 for (insn = loc_note; insn; insn = previous_insn (insn))
21504 if (insn == last_start)
21505 break;
21506 else if (!NONDEBUG_INSN_P (insn))
21507 continue;
21508 else
21509 {
21510 rtx body = PATTERN (insn);
21511 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21512 continue;
21513 /* Inline asm could occupy zero bytes. */
21514 else if (GET_CODE (body) == ASM_INPUT
21515 || asm_noperands (body) >= 0)
21516 continue;
21517 #ifdef HAVE_attr_length
21518 else if (get_attr_min_length (insn) == 0)
21519 continue;
21520 #endif
21521 else
21522 {
21523 /* Assume insn has non-zero length. */
21524 maybe_at_text_label_p = false;
21525 break;
21526 }
21527 }
21528 if (maybe_at_text_label_p)
21529 {
21530 last_start = loc_note;
21531 first_loclabel_num_not_at_text_label = loclabel_num;
21532 }
21533 }
21534 }
21535
21536 if (!var_loc_p)
21537 {
21538 struct call_arg_loc_node *ca_loc
21539 = ggc_cleared_alloc<call_arg_loc_node> ();
21540 rtx prev = prev_real_insn (loc_note), x;
21541 ca_loc->call_arg_loc_note = loc_note;
21542 ca_loc->next = NULL;
21543 ca_loc->label = last_label;
21544 gcc_assert (prev
21545 && (CALL_P (prev)
21546 || (NONJUMP_INSN_P (prev)
21547 && GET_CODE (PATTERN (prev)) == SEQUENCE
21548 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21549 if (!CALL_P (prev))
21550 prev = XVECEXP (PATTERN (prev), 0, 0);
21551 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21552 x = get_call_rtx_from (PATTERN (prev));
21553 if (x)
21554 {
21555 x = XEXP (XEXP (x, 0), 0);
21556 if (GET_CODE (x) == SYMBOL_REF
21557 && SYMBOL_REF_DECL (x)
21558 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21559 ca_loc->symbol_ref = x;
21560 }
21561 ca_loc->block = insn_scope (prev);
21562 if (call_arg_locations)
21563 call_arg_loc_last->next = ca_loc;
21564 else
21565 call_arg_locations = ca_loc;
21566 call_arg_loc_last = ca_loc;
21567 }
21568 else if (!NOTE_DURING_CALL_P (loc_note))
21569 newloc->label = last_label;
21570 else
21571 {
21572 if (!last_postcall_label)
21573 {
21574 sprintf (loclabel, "%s-1", last_label);
21575 last_postcall_label = ggc_strdup (loclabel);
21576 }
21577 newloc->label = last_postcall_label;
21578 }
21579
21580 last_var_location_insn = next_real;
21581 last_in_cold_section_p = in_cold_section_p;
21582 }
21583
21584 /* Note in one location list that text section has changed. */
21585
21586 static int
21587 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21588 {
21589 var_loc_list *list = (var_loc_list *) *slot;
21590 if (list->first)
21591 list->last_before_switch
21592 = list->last->next ? list->last->next : list->last;
21593 return 1;
21594 }
21595
21596 /* Note in all location lists that text section has changed. */
21597
21598 static void
21599 var_location_switch_text_section (void)
21600 {
21601 if (decl_loc_table == NULL)
21602 return;
21603
21604 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21605 }
21606
21607 /* Create a new line number table. */
21608
21609 static dw_line_info_table *
21610 new_line_info_table (void)
21611 {
21612 dw_line_info_table *table;
21613
21614 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21615 table->file_num = 1;
21616 table->line_num = 1;
21617 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21618
21619 return table;
21620 }
21621
21622 /* Lookup the "current" table into which we emit line info, so
21623 that we don't have to do it for every source line. */
21624
21625 static void
21626 set_cur_line_info_table (section *sec)
21627 {
21628 dw_line_info_table *table;
21629
21630 if (sec == text_section)
21631 table = text_section_line_info;
21632 else if (sec == cold_text_section)
21633 {
21634 table = cold_text_section_line_info;
21635 if (!table)
21636 {
21637 cold_text_section_line_info = table = new_line_info_table ();
21638 table->end_label = cold_end_label;
21639 }
21640 }
21641 else
21642 {
21643 const char *end_label;
21644
21645 if (flag_reorder_blocks_and_partition)
21646 {
21647 if (in_cold_section_p)
21648 end_label = crtl->subsections.cold_section_end_label;
21649 else
21650 end_label = crtl->subsections.hot_section_end_label;
21651 }
21652 else
21653 {
21654 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21655 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21656 current_function_funcdef_no);
21657 end_label = ggc_strdup (label);
21658 }
21659
21660 table = new_line_info_table ();
21661 table->end_label = end_label;
21662
21663 vec_safe_push (separate_line_info, table);
21664 }
21665
21666 if (DWARF2_ASM_LINE_DEBUG_INFO)
21667 table->is_stmt = (cur_line_info_table
21668 ? cur_line_info_table->is_stmt
21669 : DWARF_LINE_DEFAULT_IS_STMT_START);
21670 cur_line_info_table = table;
21671 }
21672
21673
21674 /* We need to reset the locations at the beginning of each
21675 function. We can't do this in the end_function hook, because the
21676 declarations that use the locations won't have been output when
21677 that hook is called. Also compute have_multiple_function_sections here. */
21678
21679 static void
21680 dwarf2out_begin_function (tree fun)
21681 {
21682 section *sec = function_section (fun);
21683
21684 if (sec != text_section)
21685 have_multiple_function_sections = true;
21686
21687 if (flag_reorder_blocks_and_partition && !cold_text_section)
21688 {
21689 gcc_assert (current_function_decl == fun);
21690 cold_text_section = unlikely_text_section ();
21691 switch_to_section (cold_text_section);
21692 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21693 switch_to_section (sec);
21694 }
21695
21696 dwarf2out_note_section_used ();
21697 call_site_count = 0;
21698 tail_call_site_count = 0;
21699
21700 set_cur_line_info_table (sec);
21701 }
21702
21703 /* Helper function of dwarf2out_end_function, called only after emitting
21704 the very first function into assembly. Check if some .debug_loc range
21705 might end with a .LVL* label that could be equal to .Ltext0.
21706 In that case we must force using absolute addresses in .debug_loc ranges,
21707 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21708 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21709 list terminator.
21710 Set have_multiple_function_sections to true in that case and
21711 terminate htab traversal. */
21712
21713 static int
21714 find_empty_loc_ranges_at_text_label (void **slot, void *)
21715 {
21716 var_loc_list *entry;
21717 struct var_loc_node *node;
21718
21719 entry = (var_loc_list *) *slot;
21720 node = entry->first;
21721 if (node && node->next && node->next->label)
21722 {
21723 unsigned int i;
21724 const char *label = node->next->label;
21725 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21726
21727 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21728 {
21729 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21730 if (strcmp (label, loclabel) == 0)
21731 {
21732 have_multiple_function_sections = true;
21733 return 0;
21734 }
21735 }
21736 }
21737 return 1;
21738 }
21739
21740 /* Hook called after emitting a function into assembly.
21741 This does something only for the very first function emitted. */
21742
21743 static void
21744 dwarf2out_end_function (unsigned int)
21745 {
21746 if (in_first_function_p
21747 && !have_multiple_function_sections
21748 && first_loclabel_num_not_at_text_label
21749 && decl_loc_table)
21750 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21751 NULL);
21752 in_first_function_p = false;
21753 maybe_at_text_label_p = false;
21754 }
21755
21756 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21757
21758 static void
21759 push_dw_line_info_entry (dw_line_info_table *table,
21760 enum dw_line_info_opcode opcode, unsigned int val)
21761 {
21762 dw_line_info_entry e;
21763 e.opcode = opcode;
21764 e.val = val;
21765 vec_safe_push (table->entries, e);
21766 }
21767
21768 /* Output a label to mark the beginning of a source code line entry
21769 and record information relating to this source line, in
21770 'line_info_table' for later output of the .debug_line section. */
21771 /* ??? The discriminator parameter ought to be unsigned. */
21772
21773 static void
21774 dwarf2out_source_line (unsigned int line, const char *filename,
21775 int discriminator, bool is_stmt)
21776 {
21777 unsigned int file_num;
21778 dw_line_info_table *table;
21779
21780 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21781 return;
21782
21783 /* The discriminator column was added in dwarf4. Simplify the below
21784 by simply removing it if we're not supposed to output it. */
21785 if (dwarf_version < 4 && dwarf_strict)
21786 discriminator = 0;
21787
21788 table = cur_line_info_table;
21789 file_num = maybe_emit_file (lookup_filename (filename));
21790
21791 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21792 the debugger has used the second (possibly duplicate) line number
21793 at the beginning of the function to mark the end of the prologue.
21794 We could eliminate any other duplicates within the function. For
21795 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21796 that second line number entry. */
21797 /* Recall that this end-of-prologue indication is *not* the same thing
21798 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21799 to which the hook corresponds, follows the last insn that was
21800 emitted by gen_prologue. What we need is to precede the first insn
21801 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21802 insn that corresponds to something the user wrote. These may be
21803 very different locations once scheduling is enabled. */
21804
21805 if (0 && file_num == table->file_num
21806 && line == table->line_num
21807 && discriminator == table->discrim_num
21808 && is_stmt == table->is_stmt)
21809 return;
21810
21811 switch_to_section (current_function_section ());
21812
21813 /* If requested, emit something human-readable. */
21814 if (flag_debug_asm)
21815 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21816
21817 if (DWARF2_ASM_LINE_DEBUG_INFO)
21818 {
21819 /* Emit the .loc directive understood by GNU as. */
21820 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21821 file_num, line, is_stmt, discriminator */
21822 fputs ("\t.loc ", asm_out_file);
21823 fprint_ul (asm_out_file, file_num);
21824 putc (' ', asm_out_file);
21825 fprint_ul (asm_out_file, line);
21826 putc (' ', asm_out_file);
21827 putc ('0', asm_out_file);
21828
21829 if (is_stmt != table->is_stmt)
21830 {
21831 fputs (" is_stmt ", asm_out_file);
21832 putc (is_stmt ? '1' : '0', asm_out_file);
21833 }
21834 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21835 {
21836 gcc_assert (discriminator > 0);
21837 fputs (" discriminator ", asm_out_file);
21838 fprint_ul (asm_out_file, (unsigned long) discriminator);
21839 }
21840 putc ('\n', asm_out_file);
21841 }
21842 else
21843 {
21844 unsigned int label_num = ++line_info_label_num;
21845
21846 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21847
21848 push_dw_line_info_entry (table, LI_set_address, label_num);
21849 if (file_num != table->file_num)
21850 push_dw_line_info_entry (table, LI_set_file, file_num);
21851 if (discriminator != table->discrim_num)
21852 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21853 if (is_stmt != table->is_stmt)
21854 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21855 push_dw_line_info_entry (table, LI_set_line, line);
21856 }
21857
21858 table->file_num = file_num;
21859 table->line_num = line;
21860 table->discrim_num = discriminator;
21861 table->is_stmt = is_stmt;
21862 table->in_use = true;
21863 }
21864
21865 /* Record the beginning of a new source file. */
21866
21867 static void
21868 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21869 {
21870 if (flag_eliminate_dwarf2_dups)
21871 {
21872 /* Record the beginning of the file for break_out_includes. */
21873 dw_die_ref bincl_die;
21874
21875 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21876 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21877 }
21878
21879 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21880 {
21881 macinfo_entry e;
21882 e.code = DW_MACINFO_start_file;
21883 e.lineno = lineno;
21884 e.info = ggc_strdup (filename);
21885 vec_safe_push (macinfo_table, e);
21886 }
21887 }
21888
21889 /* Record the end of a source file. */
21890
21891 static void
21892 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21893 {
21894 if (flag_eliminate_dwarf2_dups)
21895 /* Record the end of the file for break_out_includes. */
21896 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21897
21898 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21899 {
21900 macinfo_entry e;
21901 e.code = DW_MACINFO_end_file;
21902 e.lineno = lineno;
21903 e.info = NULL;
21904 vec_safe_push (macinfo_table, e);
21905 }
21906 }
21907
21908 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21909 the tail part of the directive line, i.e. the part which is past the
21910 initial whitespace, #, whitespace, directive-name, whitespace part. */
21911
21912 static void
21913 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21914 const char *buffer ATTRIBUTE_UNUSED)
21915 {
21916 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21917 {
21918 macinfo_entry e;
21919 /* Insert a dummy first entry to be able to optimize the whole
21920 predefined macro block using DW_MACRO_GNU_transparent_include. */
21921 if (macinfo_table->is_empty () && lineno <= 1)
21922 {
21923 e.code = 0;
21924 e.lineno = 0;
21925 e.info = NULL;
21926 vec_safe_push (macinfo_table, e);
21927 }
21928 e.code = DW_MACINFO_define;
21929 e.lineno = lineno;
21930 e.info = ggc_strdup (buffer);
21931 vec_safe_push (macinfo_table, e);
21932 }
21933 }
21934
21935 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21936 the tail part of the directive line, i.e. the part which is past the
21937 initial whitespace, #, whitespace, directive-name, whitespace part. */
21938
21939 static void
21940 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21941 const char *buffer ATTRIBUTE_UNUSED)
21942 {
21943 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21944 {
21945 macinfo_entry e;
21946 /* Insert a dummy first entry to be able to optimize the whole
21947 predefined macro block using DW_MACRO_GNU_transparent_include. */
21948 if (macinfo_table->is_empty () && lineno <= 1)
21949 {
21950 e.code = 0;
21951 e.lineno = 0;
21952 e.info = NULL;
21953 vec_safe_push (macinfo_table, e);
21954 }
21955 e.code = DW_MACINFO_undef;
21956 e.lineno = lineno;
21957 e.info = ggc_strdup (buffer);
21958 vec_safe_push (macinfo_table, e);
21959 }
21960 }
21961
21962 /* Helpers to manipulate hash table of CUs. */
21963
21964 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21965 {
21966 typedef macinfo_entry value_type;
21967 typedef macinfo_entry compare_type;
21968 static inline hashval_t hash (const value_type *);
21969 static inline bool equal (const value_type *, const compare_type *);
21970 };
21971
21972 inline hashval_t
21973 macinfo_entry_hasher::hash (const value_type *entry)
21974 {
21975 return htab_hash_string (entry->info);
21976 }
21977
21978 inline bool
21979 macinfo_entry_hasher::equal (const value_type *entry1,
21980 const compare_type *entry2)
21981 {
21982 return !strcmp (entry1->info, entry2->info);
21983 }
21984
21985 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21986
21987 /* Output a single .debug_macinfo entry. */
21988
21989 static void
21990 output_macinfo_op (macinfo_entry *ref)
21991 {
21992 int file_num;
21993 size_t len;
21994 struct indirect_string_node *node;
21995 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21996 struct dwarf_file_data *fd;
21997
21998 switch (ref->code)
21999 {
22000 case DW_MACINFO_start_file:
22001 fd = lookup_filename (ref->info);
22002 file_num = maybe_emit_file (fd);
22003 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22004 dw2_asm_output_data_uleb128 (ref->lineno,
22005 "Included from line number %lu",
22006 (unsigned long) ref->lineno);
22007 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22008 break;
22009 case DW_MACINFO_end_file:
22010 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22011 break;
22012 case DW_MACINFO_define:
22013 case DW_MACINFO_undef:
22014 len = strlen (ref->info) + 1;
22015 if (!dwarf_strict
22016 && len > DWARF_OFFSET_SIZE
22017 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22018 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22019 {
22020 ref->code = ref->code == DW_MACINFO_define
22021 ? DW_MACRO_GNU_define_indirect
22022 : DW_MACRO_GNU_undef_indirect;
22023 output_macinfo_op (ref);
22024 return;
22025 }
22026 dw2_asm_output_data (1, ref->code,
22027 ref->code == DW_MACINFO_define
22028 ? "Define macro" : "Undefine macro");
22029 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22030 (unsigned long) ref->lineno);
22031 dw2_asm_output_nstring (ref->info, -1, "The macro");
22032 break;
22033 case DW_MACRO_GNU_define_indirect:
22034 case DW_MACRO_GNU_undef_indirect:
22035 node = find_AT_string (ref->info);
22036 gcc_assert (node
22037 && ((node->form == DW_FORM_strp)
22038 || (node->form == DW_FORM_GNU_str_index)));
22039 dw2_asm_output_data (1, ref->code,
22040 ref->code == DW_MACRO_GNU_define_indirect
22041 ? "Define macro indirect"
22042 : "Undefine macro indirect");
22043 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22044 (unsigned long) ref->lineno);
22045 if (node->form == DW_FORM_strp)
22046 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22047 debug_str_section, "The macro: \"%s\"",
22048 ref->info);
22049 else
22050 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22051 ref->info);
22052 break;
22053 case DW_MACRO_GNU_transparent_include:
22054 dw2_asm_output_data (1, ref->code, "Transparent include");
22055 ASM_GENERATE_INTERNAL_LABEL (label,
22056 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22057 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22058 break;
22059 default:
22060 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22061 ASM_COMMENT_START, (unsigned long) ref->code);
22062 break;
22063 }
22064 }
22065
22066 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22067 other compilation unit .debug_macinfo sections. IDX is the first
22068 index of a define/undef, return the number of ops that should be
22069 emitted in a comdat .debug_macinfo section and emit
22070 a DW_MACRO_GNU_transparent_include entry referencing it.
22071 If the define/undef entry should be emitted normally, return 0. */
22072
22073 static unsigned
22074 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22075 macinfo_hash_type **macinfo_htab)
22076 {
22077 macinfo_entry *first, *second, *cur, *inc;
22078 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22079 unsigned char checksum[16];
22080 struct md5_ctx ctx;
22081 char *grp_name, *tail;
22082 const char *base;
22083 unsigned int i, count, encoded_filename_len, linebuf_len;
22084 macinfo_entry **slot;
22085
22086 first = &(*macinfo_table)[idx];
22087 second = &(*macinfo_table)[idx + 1];
22088
22089 /* Optimize only if there are at least two consecutive define/undef ops,
22090 and either all of them are before first DW_MACINFO_start_file
22091 with lineno {0,1} (i.e. predefined macro block), or all of them are
22092 in some included header file. */
22093 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22094 return 0;
22095 if (vec_safe_is_empty (files))
22096 {
22097 if (first->lineno > 1 || second->lineno > 1)
22098 return 0;
22099 }
22100 else if (first->lineno == 0)
22101 return 0;
22102
22103 /* Find the last define/undef entry that can be grouped together
22104 with first and at the same time compute md5 checksum of their
22105 codes, linenumbers and strings. */
22106 md5_init_ctx (&ctx);
22107 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22108 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22109 break;
22110 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22111 break;
22112 else
22113 {
22114 unsigned char code = cur->code;
22115 md5_process_bytes (&code, 1, &ctx);
22116 checksum_uleb128 (cur->lineno, &ctx);
22117 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22118 }
22119 md5_finish_ctx (&ctx, checksum);
22120 count = i - idx;
22121
22122 /* From the containing include filename (if any) pick up just
22123 usable characters from its basename. */
22124 if (vec_safe_is_empty (files))
22125 base = "";
22126 else
22127 base = lbasename (files->last ().info);
22128 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22129 if (ISIDNUM (base[i]) || base[i] == '.')
22130 encoded_filename_len++;
22131 /* Count . at the end. */
22132 if (encoded_filename_len)
22133 encoded_filename_len++;
22134
22135 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22136 linebuf_len = strlen (linebuf);
22137
22138 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22139 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22140 + 16 * 2 + 1);
22141 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22142 tail = grp_name + 4;
22143 if (encoded_filename_len)
22144 {
22145 for (i = 0; base[i]; i++)
22146 if (ISIDNUM (base[i]) || base[i] == '.')
22147 *tail++ = base[i];
22148 *tail++ = '.';
22149 }
22150 memcpy (tail, linebuf, linebuf_len);
22151 tail += linebuf_len;
22152 *tail++ = '.';
22153 for (i = 0; i < 16; i++)
22154 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22155
22156 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22157 in the empty vector entry before the first define/undef. */
22158 inc = &(*macinfo_table)[idx - 1];
22159 inc->code = DW_MACRO_GNU_transparent_include;
22160 inc->lineno = 0;
22161 inc->info = ggc_strdup (grp_name);
22162 if (!*macinfo_htab)
22163 *macinfo_htab = new macinfo_hash_type (10);
22164 /* Avoid emitting duplicates. */
22165 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22166 if (*slot != NULL)
22167 {
22168 inc->code = 0;
22169 inc->info = NULL;
22170 /* If such an entry has been used before, just emit
22171 a DW_MACRO_GNU_transparent_include op. */
22172 inc = *slot;
22173 output_macinfo_op (inc);
22174 /* And clear all macinfo_entry in the range to avoid emitting them
22175 in the second pass. */
22176 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22177 {
22178 cur->code = 0;
22179 cur->info = NULL;
22180 }
22181 }
22182 else
22183 {
22184 *slot = inc;
22185 inc->lineno = (*macinfo_htab)->elements ();
22186 output_macinfo_op (inc);
22187 }
22188 return count;
22189 }
22190
22191 /* Save any strings needed by the macinfo table in the debug str
22192 table. All strings must be collected into the table by the time
22193 index_string is called. */
22194
22195 static void
22196 save_macinfo_strings (void)
22197 {
22198 unsigned len;
22199 unsigned i;
22200 macinfo_entry *ref;
22201
22202 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22203 {
22204 switch (ref->code)
22205 {
22206 /* Match the logic in output_macinfo_op to decide on
22207 indirect strings. */
22208 case DW_MACINFO_define:
22209 case DW_MACINFO_undef:
22210 len = strlen (ref->info) + 1;
22211 if (!dwarf_strict
22212 && len > DWARF_OFFSET_SIZE
22213 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22214 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22215 set_indirect_string (find_AT_string (ref->info));
22216 break;
22217 case DW_MACRO_GNU_define_indirect:
22218 case DW_MACRO_GNU_undef_indirect:
22219 set_indirect_string (find_AT_string (ref->info));
22220 break;
22221 default:
22222 break;
22223 }
22224 }
22225 }
22226
22227 /* Output macinfo section(s). */
22228
22229 static void
22230 output_macinfo (void)
22231 {
22232 unsigned i;
22233 unsigned long length = vec_safe_length (macinfo_table);
22234 macinfo_entry *ref;
22235 vec<macinfo_entry, va_gc> *files = NULL;
22236 macinfo_hash_type *macinfo_htab = NULL;
22237
22238 if (! length)
22239 return;
22240
22241 /* output_macinfo* uses these interchangeably. */
22242 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22243 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22244 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22245 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22246
22247 /* For .debug_macro emit the section header. */
22248 if (!dwarf_strict)
22249 {
22250 dw2_asm_output_data (2, 4, "DWARF macro version number");
22251 if (DWARF_OFFSET_SIZE == 8)
22252 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22253 else
22254 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22255 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22256 (!dwarf_split_debug_info ? debug_line_section_label
22257 : debug_skeleton_line_section_label),
22258 debug_line_section, NULL);
22259 }
22260
22261 /* In the first loop, it emits the primary .debug_macinfo section
22262 and after each emitted op the macinfo_entry is cleared.
22263 If a longer range of define/undef ops can be optimized using
22264 DW_MACRO_GNU_transparent_include, the
22265 DW_MACRO_GNU_transparent_include op is emitted and kept in
22266 the vector before the first define/undef in the range and the
22267 whole range of define/undef ops is not emitted and kept. */
22268 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22269 {
22270 switch (ref->code)
22271 {
22272 case DW_MACINFO_start_file:
22273 vec_safe_push (files, *ref);
22274 break;
22275 case DW_MACINFO_end_file:
22276 if (!vec_safe_is_empty (files))
22277 files->pop ();
22278 break;
22279 case DW_MACINFO_define:
22280 case DW_MACINFO_undef:
22281 if (!dwarf_strict
22282 && HAVE_COMDAT_GROUP
22283 && vec_safe_length (files) != 1
22284 && i > 0
22285 && i + 1 < length
22286 && (*macinfo_table)[i - 1].code == 0)
22287 {
22288 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22289 if (count)
22290 {
22291 i += count - 1;
22292 continue;
22293 }
22294 }
22295 break;
22296 case 0:
22297 /* A dummy entry may be inserted at the beginning to be able
22298 to optimize the whole block of predefined macros. */
22299 if (i == 0)
22300 continue;
22301 default:
22302 break;
22303 }
22304 output_macinfo_op (ref);
22305 ref->info = NULL;
22306 ref->code = 0;
22307 }
22308
22309 if (!macinfo_htab)
22310 return;
22311
22312 delete macinfo_htab;
22313 macinfo_htab = NULL;
22314
22315 /* If any DW_MACRO_GNU_transparent_include were used, on those
22316 DW_MACRO_GNU_transparent_include entries terminate the
22317 current chain and switch to a new comdat .debug_macinfo
22318 section and emit the define/undef entries within it. */
22319 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22320 switch (ref->code)
22321 {
22322 case 0:
22323 continue;
22324 case DW_MACRO_GNU_transparent_include:
22325 {
22326 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22327 tree comdat_key = get_identifier (ref->info);
22328 /* Terminate the previous .debug_macinfo section. */
22329 dw2_asm_output_data (1, 0, "End compilation unit");
22330 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22331 SECTION_DEBUG
22332 | SECTION_LINKONCE,
22333 comdat_key);
22334 ASM_GENERATE_INTERNAL_LABEL (label,
22335 DEBUG_MACRO_SECTION_LABEL,
22336 ref->lineno);
22337 ASM_OUTPUT_LABEL (asm_out_file, label);
22338 ref->code = 0;
22339 ref->info = NULL;
22340 dw2_asm_output_data (2, 4, "DWARF macro version number");
22341 if (DWARF_OFFSET_SIZE == 8)
22342 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22343 else
22344 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22345 }
22346 break;
22347 case DW_MACINFO_define:
22348 case DW_MACINFO_undef:
22349 output_macinfo_op (ref);
22350 ref->code = 0;
22351 ref->info = NULL;
22352 break;
22353 default:
22354 gcc_unreachable ();
22355 }
22356 }
22357
22358 /* Set up for Dwarf output at the start of compilation. */
22359
22360 static void
22361 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22362 {
22363 /* Allocate the file_table. */
22364 file_table = htab_create_ggc (50, file_table_hash,
22365 file_table_eq, NULL);
22366
22367 /* Allocate the decl_die_table. */
22368 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22369 decl_die_table_eq, NULL);
22370
22371 /* Allocate the decl_loc_table. */
22372 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22373 decl_loc_table_eq, NULL);
22374
22375 /* Allocate the cached_dw_loc_list_table. */
22376 cached_dw_loc_list_table
22377 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22378 cached_dw_loc_list_table_eq, NULL);
22379
22380 /* Allocate the initial hunk of the decl_scope_table. */
22381 vec_alloc (decl_scope_table, 256);
22382
22383 /* Allocate the initial hunk of the abbrev_die_table. */
22384 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22385 (ABBREV_DIE_TABLE_INCREMENT);
22386 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22387 /* Zero-th entry is allocated, but unused. */
22388 abbrev_die_table_in_use = 1;
22389
22390 /* Allocate the pubtypes and pubnames vectors. */
22391 vec_alloc (pubname_table, 32);
22392 vec_alloc (pubtype_table, 32);
22393
22394 vec_alloc (incomplete_types, 64);
22395
22396 vec_alloc (used_rtx_array, 32);
22397
22398 if (!dwarf_split_debug_info)
22399 {
22400 debug_info_section = get_section (DEBUG_INFO_SECTION,
22401 SECTION_DEBUG, NULL);
22402 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22403 SECTION_DEBUG, NULL);
22404 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22405 SECTION_DEBUG, NULL);
22406 }
22407 else
22408 {
22409 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22410 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22411 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22412 SECTION_DEBUG | SECTION_EXCLUDE,
22413 NULL);
22414 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22415 SECTION_DEBUG, NULL);
22416 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22417 SECTION_DEBUG, NULL);
22418 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22419 SECTION_DEBUG, NULL);
22420 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22421 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22422
22423 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22424 the main .o, but the skeleton_line goes into the split off dwo. */
22425 debug_skeleton_line_section
22426 = get_section (DEBUG_DWO_LINE_SECTION,
22427 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22428 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22429 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22430 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22431 SECTION_DEBUG | SECTION_EXCLUDE,
22432 NULL);
22433 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22434 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22435 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22436 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22437 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22438 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22439 }
22440 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22441 SECTION_DEBUG, NULL);
22442 debug_macinfo_section = get_section (dwarf_strict
22443 ? DEBUG_MACINFO_SECTION
22444 : DEBUG_MACRO_SECTION,
22445 DEBUG_MACRO_SECTION_FLAGS, NULL);
22446 debug_line_section = get_section (DEBUG_LINE_SECTION,
22447 SECTION_DEBUG, NULL);
22448 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22449 SECTION_DEBUG, NULL);
22450 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22451 SECTION_DEBUG, NULL);
22452 debug_str_section = get_section (DEBUG_STR_SECTION,
22453 DEBUG_STR_SECTION_FLAGS, NULL);
22454 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22455 SECTION_DEBUG, NULL);
22456 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22457 SECTION_DEBUG, NULL);
22458
22459 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22460 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22461 DEBUG_ABBREV_SECTION_LABEL, 0);
22462 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22463 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22464 COLD_TEXT_SECTION_LABEL, 0);
22465 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22466
22467 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22468 DEBUG_INFO_SECTION_LABEL, 0);
22469 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22470 DEBUG_LINE_SECTION_LABEL, 0);
22471 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22472 DEBUG_RANGES_SECTION_LABEL, 0);
22473 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22474 DEBUG_ADDR_SECTION_LABEL, 0);
22475 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22476 dwarf_strict
22477 ? DEBUG_MACINFO_SECTION_LABEL
22478 : DEBUG_MACRO_SECTION_LABEL, 0);
22479 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22480
22481 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22482 vec_alloc (macinfo_table, 64);
22483
22484 switch_to_section (text_section);
22485 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22486
22487 /* Make sure the line number table for .text always exists. */
22488 text_section_line_info = new_line_info_table ();
22489 text_section_line_info->end_label = text_end_label;
22490 }
22491
22492 /* Called before compile () starts outputtting functions, variables
22493 and toplevel asms into assembly. */
22494
22495 static void
22496 dwarf2out_assembly_start (void)
22497 {
22498 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22499 && dwarf2out_do_cfi_asm ()
22500 && (!(flag_unwind_tables || flag_exceptions)
22501 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22502 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22503 }
22504
22505 /* A helper function for dwarf2out_finish called through
22506 htab_traverse. Assign a string its index. All strings must be
22507 collected into the table by the time index_string is called,
22508 because the indexing code relies on htab_traverse to traverse nodes
22509 in the same order for each run. */
22510
22511 static int
22512 index_string (void **h, void *v)
22513 {
22514 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22515 unsigned int *index = (unsigned int *) v;
22516
22517 find_string_form (node);
22518 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22519 {
22520 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22521 node->index = *index;
22522 *index += 1;
22523 }
22524 return 1;
22525 }
22526
22527 /* A helper function for output_indirect_strings called through
22528 htab_traverse. Output the offset to a string and update the
22529 current offset. */
22530
22531 static int
22532 output_index_string_offset (void **h, void *v)
22533 {
22534 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22535 unsigned int *offset = (unsigned int *) v;
22536
22537 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22538 {
22539 /* Assert that this node has been assigned an index. */
22540 gcc_assert (node->index != NO_INDEX_ASSIGNED
22541 && node->index != NOT_INDEXED);
22542 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22543 "indexed string 0x%x: %s", node->index, node->str);
22544 *offset += strlen (node->str) + 1;
22545 }
22546 return 1;
22547 }
22548
22549 /* A helper function for dwarf2out_finish called through
22550 htab_traverse. Output the indexed string. */
22551
22552 static int
22553 output_index_string (void **h, void *v)
22554 {
22555 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22556 unsigned int *cur_idx = (unsigned int *) v;
22557
22558 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22559 {
22560 /* Assert that the strings are output in the same order as their
22561 indexes were assigned. */
22562 gcc_assert (*cur_idx == node->index);
22563 assemble_string (node->str, strlen (node->str) + 1);
22564 *cur_idx += 1;
22565 }
22566 return 1;
22567 }
22568
22569 /* A helper function for dwarf2out_finish called through
22570 htab_traverse. Emit one queued .debug_str string. */
22571
22572 static int
22573 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22574 {
22575 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22576
22577 node->form = find_string_form (node);
22578 if (node->form == DW_FORM_strp && node->refcount > 0)
22579 {
22580 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22581 assemble_string (node->str, strlen (node->str) + 1);
22582 }
22583
22584 return 1;
22585 }
22586
22587 /* Output the indexed string table. */
22588
22589 static void
22590 output_indirect_strings (void)
22591 {
22592 switch_to_section (debug_str_section);
22593 if (!dwarf_split_debug_info)
22594 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22595 else
22596 {
22597 unsigned int offset = 0;
22598 unsigned int cur_idx = 0;
22599
22600 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22601
22602 switch_to_section (debug_str_offsets_section);
22603 htab_traverse_noresize (debug_str_hash,
22604 output_index_string_offset,
22605 &offset);
22606 switch_to_section (debug_str_dwo_section);
22607 htab_traverse_noresize (debug_str_hash,
22608 output_index_string,
22609 &cur_idx);
22610 }
22611 }
22612
22613 /* Callback for htab_traverse to assign an index to an entry in the
22614 table, and to write that entry to the .debug_addr section. */
22615
22616 static int
22617 output_addr_table_entry (void **slot, void *data)
22618 {
22619 addr_table_entry *entry = (addr_table_entry *) *slot;
22620 unsigned int *cur_index = (unsigned int *)data;
22621
22622 if (entry->refcount == 0)
22623 {
22624 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22625 || entry->index == NOT_INDEXED);
22626 return 1;
22627 }
22628
22629 gcc_assert (entry->index == *cur_index);
22630 (*cur_index)++;
22631
22632 switch (entry->kind)
22633 {
22634 case ate_kind_rtx:
22635 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22636 "0x%x", entry->index);
22637 break;
22638 case ate_kind_rtx_dtprel:
22639 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22640 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22641 DWARF2_ADDR_SIZE,
22642 entry->addr.rtl);
22643 fputc ('\n', asm_out_file);
22644 break;
22645 case ate_kind_label:
22646 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22647 "0x%x", entry->index);
22648 break;
22649 default:
22650 gcc_unreachable ();
22651 }
22652 return 1;
22653 }
22654
22655 /* Produce the .debug_addr section. */
22656
22657 static void
22658 output_addr_table (void)
22659 {
22660 unsigned int index = 0;
22661 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22662 return;
22663
22664 switch_to_section (debug_addr_section);
22665 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22666 }
22667
22668 #if ENABLE_ASSERT_CHECKING
22669 /* Verify that all marks are clear. */
22670
22671 static void
22672 verify_marks_clear (dw_die_ref die)
22673 {
22674 dw_die_ref c;
22675
22676 gcc_assert (! die->die_mark);
22677 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22678 }
22679 #endif /* ENABLE_ASSERT_CHECKING */
22680
22681 /* Clear the marks for a die and its children.
22682 Be cool if the mark isn't set. */
22683
22684 static void
22685 prune_unmark_dies (dw_die_ref die)
22686 {
22687 dw_die_ref c;
22688
22689 if (die->die_mark)
22690 die->die_mark = 0;
22691 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22692 }
22693
22694 /* Given DIE that we're marking as used, find any other dies
22695 it references as attributes and mark them as used. */
22696
22697 static void
22698 prune_unused_types_walk_attribs (dw_die_ref die)
22699 {
22700 dw_attr_ref a;
22701 unsigned ix;
22702
22703 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22704 {
22705 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22706 {
22707 /* A reference to another DIE.
22708 Make sure that it will get emitted.
22709 If it was broken out into a comdat group, don't follow it. */
22710 if (! AT_ref (a)->comdat_type_p
22711 || a->dw_attr == DW_AT_specification)
22712 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22713 }
22714 /* Set the string's refcount to 0 so that prune_unused_types_mark
22715 accounts properly for it. */
22716 if (AT_class (a) == dw_val_class_str)
22717 a->dw_attr_val.v.val_str->refcount = 0;
22718 }
22719 }
22720
22721 /* Mark the generic parameters and arguments children DIEs of DIE. */
22722
22723 static void
22724 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22725 {
22726 dw_die_ref c;
22727
22728 if (die == NULL || die->die_child == NULL)
22729 return;
22730 c = die->die_child;
22731 do
22732 {
22733 if (is_template_parameter (c))
22734 prune_unused_types_mark (c, 1);
22735 c = c->die_sib;
22736 } while (c && c != die->die_child);
22737 }
22738
22739 /* Mark DIE as being used. If DOKIDS is true, then walk down
22740 to DIE's children. */
22741
22742 static void
22743 prune_unused_types_mark (dw_die_ref die, int dokids)
22744 {
22745 dw_die_ref c;
22746
22747 if (die->die_mark == 0)
22748 {
22749 /* We haven't done this node yet. Mark it as used. */
22750 die->die_mark = 1;
22751 /* If this is the DIE of a generic type instantiation,
22752 mark the children DIEs that describe its generic parms and
22753 args. */
22754 prune_unused_types_mark_generic_parms_dies (die);
22755
22756 /* We also have to mark its parents as used.
22757 (But we don't want to mark our parent's kids due to this,
22758 unless it is a class.) */
22759 if (die->die_parent)
22760 prune_unused_types_mark (die->die_parent,
22761 class_scope_p (die->die_parent));
22762
22763 /* Mark any referenced nodes. */
22764 prune_unused_types_walk_attribs (die);
22765
22766 /* If this node is a specification,
22767 also mark the definition, if it exists. */
22768 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22769 prune_unused_types_mark (die->die_definition, 1);
22770 }
22771
22772 if (dokids && die->die_mark != 2)
22773 {
22774 /* We need to walk the children, but haven't done so yet.
22775 Remember that we've walked the kids. */
22776 die->die_mark = 2;
22777
22778 /* If this is an array type, we need to make sure our
22779 kids get marked, even if they're types. If we're
22780 breaking out types into comdat sections, do this
22781 for all type definitions. */
22782 if (die->die_tag == DW_TAG_array_type
22783 || (use_debug_types
22784 && is_type_die (die) && ! is_declaration_die (die)))
22785 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22786 else
22787 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22788 }
22789 }
22790
22791 /* For local classes, look if any static member functions were emitted
22792 and if so, mark them. */
22793
22794 static void
22795 prune_unused_types_walk_local_classes (dw_die_ref die)
22796 {
22797 dw_die_ref c;
22798
22799 if (die->die_mark == 2)
22800 return;
22801
22802 switch (die->die_tag)
22803 {
22804 case DW_TAG_structure_type:
22805 case DW_TAG_union_type:
22806 case DW_TAG_class_type:
22807 break;
22808
22809 case DW_TAG_subprogram:
22810 if (!get_AT_flag (die, DW_AT_declaration)
22811 || die->die_definition != NULL)
22812 prune_unused_types_mark (die, 1);
22813 return;
22814
22815 default:
22816 return;
22817 }
22818
22819 /* Mark children. */
22820 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22821 }
22822
22823 /* Walk the tree DIE and mark types that we actually use. */
22824
22825 static void
22826 prune_unused_types_walk (dw_die_ref die)
22827 {
22828 dw_die_ref c;
22829
22830 /* Don't do anything if this node is already marked and
22831 children have been marked as well. */
22832 if (die->die_mark == 2)
22833 return;
22834
22835 switch (die->die_tag)
22836 {
22837 case DW_TAG_structure_type:
22838 case DW_TAG_union_type:
22839 case DW_TAG_class_type:
22840 if (die->die_perennial_p)
22841 break;
22842
22843 for (c = die->die_parent; c; c = c->die_parent)
22844 if (c->die_tag == DW_TAG_subprogram)
22845 break;
22846
22847 /* Finding used static member functions inside of classes
22848 is needed just for local classes, because for other classes
22849 static member function DIEs with DW_AT_specification
22850 are emitted outside of the DW_TAG_*_type. If we ever change
22851 it, we'd need to call this even for non-local classes. */
22852 if (c)
22853 prune_unused_types_walk_local_classes (die);
22854
22855 /* It's a type node --- don't mark it. */
22856 return;
22857
22858 case DW_TAG_const_type:
22859 case DW_TAG_packed_type:
22860 case DW_TAG_pointer_type:
22861 case DW_TAG_reference_type:
22862 case DW_TAG_rvalue_reference_type:
22863 case DW_TAG_volatile_type:
22864 case DW_TAG_typedef:
22865 case DW_TAG_array_type:
22866 case DW_TAG_interface_type:
22867 case DW_TAG_friend:
22868 case DW_TAG_variant_part:
22869 case DW_TAG_enumeration_type:
22870 case DW_TAG_subroutine_type:
22871 case DW_TAG_string_type:
22872 case DW_TAG_set_type:
22873 case DW_TAG_subrange_type:
22874 case DW_TAG_ptr_to_member_type:
22875 case DW_TAG_file_type:
22876 if (die->die_perennial_p)
22877 break;
22878
22879 /* It's a type node --- don't mark it. */
22880 return;
22881
22882 default:
22883 /* Mark everything else. */
22884 break;
22885 }
22886
22887 if (die->die_mark == 0)
22888 {
22889 die->die_mark = 1;
22890
22891 /* Now, mark any dies referenced from here. */
22892 prune_unused_types_walk_attribs (die);
22893 }
22894
22895 die->die_mark = 2;
22896
22897 /* Mark children. */
22898 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22899 }
22900
22901 /* Increment the string counts on strings referred to from DIE's
22902 attributes. */
22903
22904 static void
22905 prune_unused_types_update_strings (dw_die_ref die)
22906 {
22907 dw_attr_ref a;
22908 unsigned ix;
22909
22910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22911 if (AT_class (a) == dw_val_class_str)
22912 {
22913 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22914 s->refcount++;
22915 /* Avoid unnecessarily putting strings that are used less than
22916 twice in the hash table. */
22917 if (s->refcount
22918 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22919 {
22920 void ** slot;
22921 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22922 htab_hash_string (s->str),
22923 INSERT);
22924 gcc_assert (*slot == NULL);
22925 *slot = s;
22926 }
22927 }
22928 }
22929
22930 /* Remove from the tree DIE any dies that aren't marked. */
22931
22932 static void
22933 prune_unused_types_prune (dw_die_ref die)
22934 {
22935 dw_die_ref c;
22936
22937 gcc_assert (die->die_mark);
22938 prune_unused_types_update_strings (die);
22939
22940 if (! die->die_child)
22941 return;
22942
22943 c = die->die_child;
22944 do {
22945 dw_die_ref prev = c;
22946 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22947 if (c == die->die_child)
22948 {
22949 /* No marked children between 'prev' and the end of the list. */
22950 if (prev == c)
22951 /* No marked children at all. */
22952 die->die_child = NULL;
22953 else
22954 {
22955 prev->die_sib = c->die_sib;
22956 die->die_child = prev;
22957 }
22958 return;
22959 }
22960
22961 if (c != prev->die_sib)
22962 prev->die_sib = c;
22963 prune_unused_types_prune (c);
22964 } while (c != die->die_child);
22965 }
22966
22967 /* Remove dies representing declarations that we never use. */
22968
22969 static void
22970 prune_unused_types (void)
22971 {
22972 unsigned int i;
22973 limbo_die_node *node;
22974 comdat_type_node *ctnode;
22975 pubname_ref pub;
22976 dw_die_ref base_type;
22977
22978 #if ENABLE_ASSERT_CHECKING
22979 /* All the marks should already be clear. */
22980 verify_marks_clear (comp_unit_die ());
22981 for (node = limbo_die_list; node; node = node->next)
22982 verify_marks_clear (node->die);
22983 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22984 verify_marks_clear (ctnode->root_die);
22985 #endif /* ENABLE_ASSERT_CHECKING */
22986
22987 /* Mark types that are used in global variables. */
22988 premark_types_used_by_global_vars ();
22989
22990 /* Set the mark on nodes that are actually used. */
22991 prune_unused_types_walk (comp_unit_die ());
22992 for (node = limbo_die_list; node; node = node->next)
22993 prune_unused_types_walk (node->die);
22994 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22995 {
22996 prune_unused_types_walk (ctnode->root_die);
22997 prune_unused_types_mark (ctnode->type_die, 1);
22998 }
22999
23000 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23001 are unusual in that they are pubnames that are the children of pubtypes.
23002 They should only be marked via their parent DW_TAG_enumeration_type die,
23003 not as roots in themselves. */
23004 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23005 if (pub->die->die_tag != DW_TAG_enumerator)
23006 prune_unused_types_mark (pub->die, 1);
23007 for (i = 0; base_types.iterate (i, &base_type); i++)
23008 prune_unused_types_mark (base_type, 1);
23009
23010 if (debug_str_hash)
23011 htab_empty (debug_str_hash);
23012 if (skeleton_debug_str_hash)
23013 htab_empty (skeleton_debug_str_hash);
23014 prune_unused_types_prune (comp_unit_die ());
23015 for (node = limbo_die_list; node; node = node->next)
23016 prune_unused_types_prune (node->die);
23017 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23018 prune_unused_types_prune (ctnode->root_die);
23019
23020 /* Leave the marks clear. */
23021 prune_unmark_dies (comp_unit_die ());
23022 for (node = limbo_die_list; node; node = node->next)
23023 prune_unmark_dies (node->die);
23024 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23025 prune_unmark_dies (ctnode->root_die);
23026 }
23027
23028 /* Set the parameter to true if there are any relative pathnames in
23029 the file table. */
23030 static int
23031 file_table_relative_p (void ** slot, void *param)
23032 {
23033 bool *p = (bool *) param;
23034 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23035 if (!IS_ABSOLUTE_PATH (d->filename))
23036 {
23037 *p = true;
23038 return 0;
23039 }
23040 return 1;
23041 }
23042
23043 /* Helpers to manipulate hash table of comdat type units. */
23044
23045 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23046 {
23047 typedef comdat_type_node value_type;
23048 typedef comdat_type_node compare_type;
23049 static inline hashval_t hash (const value_type *);
23050 static inline bool equal (const value_type *, const compare_type *);
23051 };
23052
23053 inline hashval_t
23054 comdat_type_hasher::hash (const value_type *type_node)
23055 {
23056 hashval_t h;
23057 memcpy (&h, type_node->signature, sizeof (h));
23058 return h;
23059 }
23060
23061 inline bool
23062 comdat_type_hasher::equal (const value_type *type_node_1,
23063 const compare_type *type_node_2)
23064 {
23065 return (! memcmp (type_node_1->signature, type_node_2->signature,
23066 DWARF_TYPE_SIGNATURE_SIZE));
23067 }
23068
23069 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23070 to the location it would have been added, should we know its
23071 DECL_ASSEMBLER_NAME when we added other attributes. This will
23072 probably improve compactness of debug info, removing equivalent
23073 abbrevs, and hide any differences caused by deferring the
23074 computation of the assembler name, triggered by e.g. PCH. */
23075
23076 static inline void
23077 move_linkage_attr (dw_die_ref die)
23078 {
23079 unsigned ix = vec_safe_length (die->die_attr);
23080 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23081
23082 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23083 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23084
23085 while (--ix > 0)
23086 {
23087 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23088
23089 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23090 break;
23091 }
23092
23093 if (ix != vec_safe_length (die->die_attr) - 1)
23094 {
23095 die->die_attr->pop ();
23096 die->die_attr->quick_insert (ix, linkage);
23097 }
23098 }
23099
23100 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23101 referenced from typed stack ops and count how often they are used. */
23102
23103 static void
23104 mark_base_types (dw_loc_descr_ref loc)
23105 {
23106 dw_die_ref base_type = NULL;
23107
23108 for (; loc; loc = loc->dw_loc_next)
23109 {
23110 switch (loc->dw_loc_opc)
23111 {
23112 case DW_OP_GNU_regval_type:
23113 case DW_OP_GNU_deref_type:
23114 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23115 break;
23116 case DW_OP_GNU_convert:
23117 case DW_OP_GNU_reinterpret:
23118 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23119 continue;
23120 /* FALLTHRU */
23121 case DW_OP_GNU_const_type:
23122 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23123 break;
23124 case DW_OP_GNU_entry_value:
23125 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23126 continue;
23127 default:
23128 continue;
23129 }
23130 gcc_assert (base_type->die_parent == comp_unit_die ());
23131 if (base_type->die_mark)
23132 base_type->die_mark++;
23133 else
23134 {
23135 base_types.safe_push (base_type);
23136 base_type->die_mark = 1;
23137 }
23138 }
23139 }
23140
23141 /* Comparison function for sorting marked base types. */
23142
23143 static int
23144 base_type_cmp (const void *x, const void *y)
23145 {
23146 dw_die_ref dx = *(const dw_die_ref *) x;
23147 dw_die_ref dy = *(const dw_die_ref *) y;
23148 unsigned int byte_size1, byte_size2;
23149 unsigned int encoding1, encoding2;
23150 if (dx->die_mark > dy->die_mark)
23151 return -1;
23152 if (dx->die_mark < dy->die_mark)
23153 return 1;
23154 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23155 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23156 if (byte_size1 < byte_size2)
23157 return 1;
23158 if (byte_size1 > byte_size2)
23159 return -1;
23160 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23161 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23162 if (encoding1 < encoding2)
23163 return 1;
23164 if (encoding1 > encoding2)
23165 return -1;
23166 return 0;
23167 }
23168
23169 /* Move base types marked by mark_base_types as early as possible
23170 in the CU, sorted by decreasing usage count both to make the
23171 uleb128 references as small as possible and to make sure they
23172 will have die_offset already computed by calc_die_sizes when
23173 sizes of typed stack loc ops is computed. */
23174
23175 static void
23176 move_marked_base_types (void)
23177 {
23178 unsigned int i;
23179 dw_die_ref base_type, die, c;
23180
23181 if (base_types.is_empty ())
23182 return;
23183
23184 /* Sort by decreasing usage count, they will be added again in that
23185 order later on. */
23186 base_types.qsort (base_type_cmp);
23187 die = comp_unit_die ();
23188 c = die->die_child;
23189 do
23190 {
23191 dw_die_ref prev = c;
23192 c = c->die_sib;
23193 while (c->die_mark)
23194 {
23195 remove_child_with_prev (c, prev);
23196 /* As base types got marked, there must be at least
23197 one node other than DW_TAG_base_type. */
23198 gcc_assert (c != c->die_sib);
23199 c = c->die_sib;
23200 }
23201 }
23202 while (c != die->die_child);
23203 gcc_assert (die->die_child);
23204 c = die->die_child;
23205 for (i = 0; base_types.iterate (i, &base_type); i++)
23206 {
23207 base_type->die_mark = 0;
23208 base_type->die_sib = c->die_sib;
23209 c->die_sib = base_type;
23210 c = base_type;
23211 }
23212 }
23213
23214 /* Helper function for resolve_addr, attempt to resolve
23215 one CONST_STRING, return true if successful. Similarly verify that
23216 SYMBOL_REFs refer to variables emitted in the current CU. */
23217
23218 static bool
23219 resolve_one_addr (rtx *addr)
23220 {
23221 rtx rtl = *addr;
23222
23223 if (GET_CODE (rtl) == CONST_STRING)
23224 {
23225 size_t len = strlen (XSTR (rtl, 0)) + 1;
23226 tree t = build_string (len, XSTR (rtl, 0));
23227 tree tlen = size_int (len - 1);
23228 TREE_TYPE (t)
23229 = build_array_type (char_type_node, build_index_type (tlen));
23230 rtl = lookup_constant_def (t);
23231 if (!rtl || !MEM_P (rtl))
23232 return false;
23233 rtl = XEXP (rtl, 0);
23234 if (GET_CODE (rtl) == SYMBOL_REF
23235 && SYMBOL_REF_DECL (rtl)
23236 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23237 return false;
23238 vec_safe_push (used_rtx_array, rtl);
23239 *addr = rtl;
23240 return true;
23241 }
23242
23243 if (GET_CODE (rtl) == SYMBOL_REF
23244 && SYMBOL_REF_DECL (rtl))
23245 {
23246 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23247 {
23248 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23249 return false;
23250 }
23251 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23252 return false;
23253 }
23254
23255 if (GET_CODE (rtl) == CONST)
23256 {
23257 subrtx_ptr_iterator::array_type array;
23258 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23259 if (!resolve_one_addr (*iter))
23260 return false;
23261 }
23262
23263 return true;
23264 }
23265
23266 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23267 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23268 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23269
23270 static rtx
23271 string_cst_pool_decl (tree t)
23272 {
23273 rtx rtl = output_constant_def (t, 1);
23274 unsigned char *array;
23275 dw_loc_descr_ref l;
23276 tree decl;
23277 size_t len;
23278 dw_die_ref ref;
23279
23280 if (!rtl || !MEM_P (rtl))
23281 return NULL_RTX;
23282 rtl = XEXP (rtl, 0);
23283 if (GET_CODE (rtl) != SYMBOL_REF
23284 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23285 return NULL_RTX;
23286
23287 decl = SYMBOL_REF_DECL (rtl);
23288 if (!lookup_decl_die (decl))
23289 {
23290 len = TREE_STRING_LENGTH (t);
23291 vec_safe_push (used_rtx_array, rtl);
23292 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23293 array = ggc_vec_alloc<unsigned char> (len);
23294 memcpy (array, TREE_STRING_POINTER (t), len);
23295 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23296 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23297 l->dw_loc_oprnd2.v.val_vec.length = len;
23298 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23299 l->dw_loc_oprnd2.v.val_vec.array = array;
23300 add_AT_loc (ref, DW_AT_location, l);
23301 equate_decl_number_to_die (decl, ref);
23302 }
23303 return rtl;
23304 }
23305
23306 /* Helper function of resolve_addr_in_expr. LOC is
23307 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23308 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23309 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23310 with DW_OP_GNU_implicit_pointer if possible
23311 and return true, if unsuccessful, return false. */
23312
23313 static bool
23314 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23315 {
23316 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23317 HOST_WIDE_INT offset = 0;
23318 dw_die_ref ref = NULL;
23319 tree decl;
23320
23321 if (GET_CODE (rtl) == CONST
23322 && GET_CODE (XEXP (rtl, 0)) == PLUS
23323 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23324 {
23325 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23326 rtl = XEXP (XEXP (rtl, 0), 0);
23327 }
23328 if (GET_CODE (rtl) == CONST_STRING)
23329 {
23330 size_t len = strlen (XSTR (rtl, 0)) + 1;
23331 tree t = build_string (len, XSTR (rtl, 0));
23332 tree tlen = size_int (len - 1);
23333
23334 TREE_TYPE (t)
23335 = build_array_type (char_type_node, build_index_type (tlen));
23336 rtl = string_cst_pool_decl (t);
23337 if (!rtl)
23338 return false;
23339 }
23340 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23341 {
23342 decl = SYMBOL_REF_DECL (rtl);
23343 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23344 {
23345 ref = lookup_decl_die (decl);
23346 if (ref && (get_AT (ref, DW_AT_location)
23347 || get_AT (ref, DW_AT_const_value)))
23348 {
23349 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23350 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23351 loc->dw_loc_oprnd1.val_entry = NULL;
23352 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23353 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23354 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23355 loc->dw_loc_oprnd2.v.val_int = offset;
23356 return true;
23357 }
23358 }
23359 }
23360 return false;
23361 }
23362
23363 /* Helper function for resolve_addr, handle one location
23364 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23365 the location list couldn't be resolved. */
23366
23367 static bool
23368 resolve_addr_in_expr (dw_loc_descr_ref loc)
23369 {
23370 dw_loc_descr_ref keep = NULL;
23371 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23372 switch (loc->dw_loc_opc)
23373 {
23374 case DW_OP_addr:
23375 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23376 {
23377 if ((prev == NULL
23378 || prev->dw_loc_opc == DW_OP_piece
23379 || prev->dw_loc_opc == DW_OP_bit_piece)
23380 && loc->dw_loc_next
23381 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23382 && !dwarf_strict
23383 && optimize_one_addr_into_implicit_ptr (loc))
23384 break;
23385 return false;
23386 }
23387 break;
23388 case DW_OP_GNU_addr_index:
23389 case DW_OP_GNU_const_index:
23390 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23391 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23392 {
23393 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23394 if (!resolve_one_addr (&rtl))
23395 return false;
23396 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23397 loc->dw_loc_oprnd1.val_entry =
23398 add_addr_table_entry (rtl, ate_kind_rtx);
23399 }
23400 break;
23401 case DW_OP_const4u:
23402 case DW_OP_const8u:
23403 if (loc->dtprel
23404 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23405 return false;
23406 break;
23407 case DW_OP_plus_uconst:
23408 if (size_of_loc_descr (loc)
23409 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23410 + 1
23411 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23412 {
23413 dw_loc_descr_ref repl
23414 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23415 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23416 add_loc_descr (&repl, loc->dw_loc_next);
23417 *loc = *repl;
23418 }
23419 break;
23420 case DW_OP_implicit_value:
23421 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23422 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23423 return false;
23424 break;
23425 case DW_OP_GNU_implicit_pointer:
23426 case DW_OP_GNU_parameter_ref:
23427 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23428 {
23429 dw_die_ref ref
23430 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23431 if (ref == NULL)
23432 return false;
23433 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23434 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23435 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23436 }
23437 break;
23438 case DW_OP_GNU_const_type:
23439 case DW_OP_GNU_regval_type:
23440 case DW_OP_GNU_deref_type:
23441 case DW_OP_GNU_convert:
23442 case DW_OP_GNU_reinterpret:
23443 while (loc->dw_loc_next
23444 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23445 {
23446 dw_die_ref base1, base2;
23447 unsigned enc1, enc2, size1, size2;
23448 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23449 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23450 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23451 else if (loc->dw_loc_oprnd1.val_class
23452 == dw_val_class_unsigned_const)
23453 break;
23454 else
23455 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23456 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23457 == dw_val_class_unsigned_const)
23458 break;
23459 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23460 gcc_assert (base1->die_tag == DW_TAG_base_type
23461 && base2->die_tag == DW_TAG_base_type);
23462 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23463 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23464 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23465 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23466 if (size1 == size2
23467 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23468 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23469 && loc != keep)
23470 || enc1 == enc2))
23471 {
23472 /* Optimize away next DW_OP_GNU_convert after
23473 adjusting LOC's base type die reference. */
23474 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23475 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23476 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23477 else
23478 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23479 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23480 continue;
23481 }
23482 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23483 point typed stack entry. */
23484 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23485 keep = loc->dw_loc_next;
23486 break;
23487 }
23488 break;
23489 default:
23490 break;
23491 }
23492 return true;
23493 }
23494
23495 /* Helper function of resolve_addr. DIE had DW_AT_location of
23496 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23497 and DW_OP_addr couldn't be resolved. resolve_addr has already
23498 removed the DW_AT_location attribute. This function attempts to
23499 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23500 to it or DW_AT_const_value attribute, if possible. */
23501
23502 static void
23503 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23504 {
23505 if (TREE_CODE (decl) != VAR_DECL
23506 || lookup_decl_die (decl) != die
23507 || DECL_EXTERNAL (decl)
23508 || !TREE_STATIC (decl)
23509 || DECL_INITIAL (decl) == NULL_TREE
23510 || DECL_P (DECL_INITIAL (decl))
23511 || get_AT (die, DW_AT_const_value))
23512 return;
23513
23514 tree init = DECL_INITIAL (decl);
23515 HOST_WIDE_INT offset = 0;
23516 /* For variables that have been optimized away and thus
23517 don't have a memory location, see if we can emit
23518 DW_AT_const_value instead. */
23519 if (tree_add_const_value_attribute (die, init))
23520 return;
23521 if (dwarf_strict)
23522 return;
23523 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23524 and ADDR_EXPR refers to a decl that has DW_AT_location or
23525 DW_AT_const_value (but isn't addressable, otherwise
23526 resolving the original DW_OP_addr wouldn't fail), see if
23527 we can add DW_OP_GNU_implicit_pointer. */
23528 STRIP_NOPS (init);
23529 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23530 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23531 {
23532 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23533 init = TREE_OPERAND (init, 0);
23534 STRIP_NOPS (init);
23535 }
23536 if (TREE_CODE (init) != ADDR_EXPR)
23537 return;
23538 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23539 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23540 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23541 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23542 && TREE_OPERAND (init, 0) != decl))
23543 {
23544 dw_die_ref ref;
23545 dw_loc_descr_ref l;
23546
23547 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23548 {
23549 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23550 if (!rtl)
23551 return;
23552 decl = SYMBOL_REF_DECL (rtl);
23553 }
23554 else
23555 decl = TREE_OPERAND (init, 0);
23556 ref = lookup_decl_die (decl);
23557 if (ref == NULL
23558 || (!get_AT (ref, DW_AT_location)
23559 && !get_AT (ref, DW_AT_const_value)))
23560 return;
23561 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23562 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23563 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23564 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23565 add_AT_loc (die, DW_AT_location, l);
23566 }
23567 }
23568
23569 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23570 an address in .rodata section if the string literal is emitted there,
23571 or remove the containing location list or replace DW_AT_const_value
23572 with DW_AT_location and empty location expression, if it isn't found
23573 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23574 to something that has been emitted in the current CU. */
23575
23576 static void
23577 resolve_addr (dw_die_ref die)
23578 {
23579 dw_die_ref c;
23580 dw_attr_ref a;
23581 dw_loc_list_ref *curr, *start, loc;
23582 unsigned ix;
23583
23584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23585 switch (AT_class (a))
23586 {
23587 case dw_val_class_loc_list:
23588 start = curr = AT_loc_list_ptr (a);
23589 loc = *curr;
23590 gcc_assert (loc);
23591 /* The same list can be referenced more than once. See if we have
23592 already recorded the result from a previous pass. */
23593 if (loc->replaced)
23594 *curr = loc->dw_loc_next;
23595 else if (!loc->resolved_addr)
23596 {
23597 /* As things stand, we do not expect or allow one die to
23598 reference a suffix of another die's location list chain.
23599 References must be identical or completely separate.
23600 There is therefore no need to cache the result of this
23601 pass on any list other than the first; doing so
23602 would lead to unnecessary writes. */
23603 while (*curr)
23604 {
23605 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23606 if (!resolve_addr_in_expr ((*curr)->expr))
23607 {
23608 dw_loc_list_ref next = (*curr)->dw_loc_next;
23609 dw_loc_descr_ref l = (*curr)->expr;
23610
23611 if (next && (*curr)->ll_symbol)
23612 {
23613 gcc_assert (!next->ll_symbol);
23614 next->ll_symbol = (*curr)->ll_symbol;
23615 }
23616 if (dwarf_split_debug_info)
23617 remove_loc_list_addr_table_entries (l);
23618 *curr = next;
23619 }
23620 else
23621 {
23622 mark_base_types ((*curr)->expr);
23623 curr = &(*curr)->dw_loc_next;
23624 }
23625 }
23626 if (loc == *start)
23627 loc->resolved_addr = 1;
23628 else
23629 {
23630 loc->replaced = 1;
23631 loc->dw_loc_next = *start;
23632 }
23633 }
23634 if (!*start)
23635 {
23636 remove_AT (die, a->dw_attr);
23637 ix--;
23638 }
23639 break;
23640 case dw_val_class_loc:
23641 {
23642 dw_loc_descr_ref l = AT_loc (a);
23643 /* For -gdwarf-2 don't attempt to optimize
23644 DW_AT_data_member_location containing
23645 DW_OP_plus_uconst - older consumers might
23646 rely on it being that op instead of a more complex,
23647 but shorter, location description. */
23648 if ((dwarf_version > 2
23649 || a->dw_attr != DW_AT_data_member_location
23650 || l == NULL
23651 || l->dw_loc_opc != DW_OP_plus_uconst
23652 || l->dw_loc_next != NULL)
23653 && !resolve_addr_in_expr (l))
23654 {
23655 if (dwarf_split_debug_info)
23656 remove_loc_list_addr_table_entries (l);
23657 if (l != NULL
23658 && l->dw_loc_next == NULL
23659 && l->dw_loc_opc == DW_OP_addr
23660 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23661 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23662 && a->dw_attr == DW_AT_location)
23663 {
23664 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23665 remove_AT (die, a->dw_attr);
23666 ix--;
23667 optimize_location_into_implicit_ptr (die, decl);
23668 break;
23669 }
23670 remove_AT (die, a->dw_attr);
23671 ix--;
23672 }
23673 else
23674 mark_base_types (l);
23675 }
23676 break;
23677 case dw_val_class_addr:
23678 if (a->dw_attr == DW_AT_const_value
23679 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23680 {
23681 if (AT_index (a) != NOT_INDEXED)
23682 remove_addr_table_entry (a->dw_attr_val.val_entry);
23683 remove_AT (die, a->dw_attr);
23684 ix--;
23685 }
23686 if (die->die_tag == DW_TAG_GNU_call_site
23687 && a->dw_attr == DW_AT_abstract_origin)
23688 {
23689 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23690 dw_die_ref tdie = lookup_decl_die (tdecl);
23691 if (tdie == NULL
23692 && DECL_EXTERNAL (tdecl)
23693 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23694 {
23695 force_decl_die (tdecl);
23696 tdie = lookup_decl_die (tdecl);
23697 }
23698 if (tdie)
23699 {
23700 a->dw_attr_val.val_class = dw_val_class_die_ref;
23701 a->dw_attr_val.v.val_die_ref.die = tdie;
23702 a->dw_attr_val.v.val_die_ref.external = 0;
23703 }
23704 else
23705 {
23706 if (AT_index (a) != NOT_INDEXED)
23707 remove_addr_table_entry (a->dw_attr_val.val_entry);
23708 remove_AT (die, a->dw_attr);
23709 ix--;
23710 }
23711 }
23712 break;
23713 default:
23714 break;
23715 }
23716
23717 FOR_EACH_CHILD (die, c, resolve_addr (c));
23718 }
23719 \f
23720 /* Helper routines for optimize_location_lists.
23721 This pass tries to share identical local lists in .debug_loc
23722 section. */
23723
23724 /* Iteratively hash operands of LOC opcode into HSTATE. */
23725
23726 static void
23727 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23728 {
23729 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23730 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23731
23732 switch (loc->dw_loc_opc)
23733 {
23734 case DW_OP_const4u:
23735 case DW_OP_const8u:
23736 if (loc->dtprel)
23737 goto hash_addr;
23738 /* FALLTHRU */
23739 case DW_OP_const1u:
23740 case DW_OP_const1s:
23741 case DW_OP_const2u:
23742 case DW_OP_const2s:
23743 case DW_OP_const4s:
23744 case DW_OP_const8s:
23745 case DW_OP_constu:
23746 case DW_OP_consts:
23747 case DW_OP_pick:
23748 case DW_OP_plus_uconst:
23749 case DW_OP_breg0:
23750 case DW_OP_breg1:
23751 case DW_OP_breg2:
23752 case DW_OP_breg3:
23753 case DW_OP_breg4:
23754 case DW_OP_breg5:
23755 case DW_OP_breg6:
23756 case DW_OP_breg7:
23757 case DW_OP_breg8:
23758 case DW_OP_breg9:
23759 case DW_OP_breg10:
23760 case DW_OP_breg11:
23761 case DW_OP_breg12:
23762 case DW_OP_breg13:
23763 case DW_OP_breg14:
23764 case DW_OP_breg15:
23765 case DW_OP_breg16:
23766 case DW_OP_breg17:
23767 case DW_OP_breg18:
23768 case DW_OP_breg19:
23769 case DW_OP_breg20:
23770 case DW_OP_breg21:
23771 case DW_OP_breg22:
23772 case DW_OP_breg23:
23773 case DW_OP_breg24:
23774 case DW_OP_breg25:
23775 case DW_OP_breg26:
23776 case DW_OP_breg27:
23777 case DW_OP_breg28:
23778 case DW_OP_breg29:
23779 case DW_OP_breg30:
23780 case DW_OP_breg31:
23781 case DW_OP_regx:
23782 case DW_OP_fbreg:
23783 case DW_OP_piece:
23784 case DW_OP_deref_size:
23785 case DW_OP_xderef_size:
23786 hstate.add_object (val1->v.val_int);
23787 break;
23788 case DW_OP_skip:
23789 case DW_OP_bra:
23790 {
23791 int offset;
23792
23793 gcc_assert (val1->val_class == dw_val_class_loc);
23794 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23795 hstate.add_object (offset);
23796 }
23797 break;
23798 case DW_OP_implicit_value:
23799 hstate.add_object (val1->v.val_unsigned);
23800 switch (val2->val_class)
23801 {
23802 case dw_val_class_const:
23803 hstate.add_object (val2->v.val_int);
23804 break;
23805 case dw_val_class_vec:
23806 {
23807 unsigned int elt_size = val2->v.val_vec.elt_size;
23808 unsigned int len = val2->v.val_vec.length;
23809
23810 hstate.add_int (elt_size);
23811 hstate.add_int (len);
23812 hstate.add (val2->v.val_vec.array, len * elt_size);
23813 }
23814 break;
23815 case dw_val_class_const_double:
23816 hstate.add_object (val2->v.val_double.low);
23817 hstate.add_object (val2->v.val_double.high);
23818 break;
23819 case dw_val_class_wide_int:
23820 hstate.add_object (*val2->v.val_wide);
23821 break;
23822 case dw_val_class_addr:
23823 inchash::add_rtx (val2->v.val_addr, hstate);
23824 break;
23825 default:
23826 gcc_unreachable ();
23827 }
23828 break;
23829 case DW_OP_bregx:
23830 case DW_OP_bit_piece:
23831 hstate.add_object (val1->v.val_int);
23832 hstate.add_object (val2->v.val_int);
23833 break;
23834 case DW_OP_addr:
23835 hash_addr:
23836 if (loc->dtprel)
23837 {
23838 unsigned char dtprel = 0xd1;
23839 hstate.add_object (dtprel);
23840 }
23841 inchash::add_rtx (val1->v.val_addr, hstate);
23842 break;
23843 case DW_OP_GNU_addr_index:
23844 case DW_OP_GNU_const_index:
23845 {
23846 if (loc->dtprel)
23847 {
23848 unsigned char dtprel = 0xd1;
23849 hstate.add_object (dtprel);
23850 }
23851 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23852 }
23853 break;
23854 case DW_OP_GNU_implicit_pointer:
23855 hstate.add_int (val2->v.val_int);
23856 break;
23857 case DW_OP_GNU_entry_value:
23858 hstate.add_object (val1->v.val_loc);
23859 break;
23860 case DW_OP_GNU_regval_type:
23861 case DW_OP_GNU_deref_type:
23862 {
23863 unsigned int byte_size
23864 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23865 unsigned int encoding
23866 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23867 hstate.add_object (val1->v.val_int);
23868 hstate.add_object (byte_size);
23869 hstate.add_object (encoding);
23870 }
23871 break;
23872 case DW_OP_GNU_convert:
23873 case DW_OP_GNU_reinterpret:
23874 if (val1->val_class == dw_val_class_unsigned_const)
23875 {
23876 hstate.add_object (val1->v.val_unsigned);
23877 break;
23878 }
23879 /* FALLTHRU */
23880 case DW_OP_GNU_const_type:
23881 {
23882 unsigned int byte_size
23883 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23884 unsigned int encoding
23885 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23886 hstate.add_object (byte_size);
23887 hstate.add_object (encoding);
23888 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23889 break;
23890 hstate.add_object (val2->val_class);
23891 switch (val2->val_class)
23892 {
23893 case dw_val_class_const:
23894 hstate.add_object (val2->v.val_int);
23895 break;
23896 case dw_val_class_vec:
23897 {
23898 unsigned int elt_size = val2->v.val_vec.elt_size;
23899 unsigned int len = val2->v.val_vec.length;
23900
23901 hstate.add_object (elt_size);
23902 hstate.add_object (len);
23903 hstate.add (val2->v.val_vec.array, len * elt_size);
23904 }
23905 break;
23906 case dw_val_class_const_double:
23907 hstate.add_object (val2->v.val_double.low);
23908 hstate.add_object (val2->v.val_double.high);
23909 break;
23910 case dw_val_class_wide_int:
23911 hstate.add_object (*val2->v.val_wide);
23912 break;
23913 default:
23914 gcc_unreachable ();
23915 }
23916 }
23917 break;
23918
23919 default:
23920 /* Other codes have no operands. */
23921 break;
23922 }
23923 }
23924
23925 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23926
23927 static inline void
23928 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23929 {
23930 dw_loc_descr_ref l;
23931 bool sizes_computed = false;
23932 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23933 size_of_locs (loc);
23934
23935 for (l = loc; l != NULL; l = l->dw_loc_next)
23936 {
23937 enum dwarf_location_atom opc = l->dw_loc_opc;
23938 hstate.add_object (opc);
23939 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23940 {
23941 size_of_locs (loc);
23942 sizes_computed = true;
23943 }
23944 hash_loc_operands (l, hstate);
23945 }
23946 }
23947
23948 /* Compute hash of the whole location list LIST_HEAD. */
23949
23950 static inline void
23951 hash_loc_list (dw_loc_list_ref list_head)
23952 {
23953 dw_loc_list_ref curr = list_head;
23954 inchash::hash hstate;
23955
23956 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23957 {
23958 hstate.add (curr->begin, strlen (curr->begin) + 1);
23959 hstate.add (curr->end, strlen (curr->end) + 1);
23960 if (curr->section)
23961 hstate.add (curr->section, strlen (curr->section) + 1);
23962 hash_locs (curr->expr, hstate);
23963 }
23964 list_head->hash = hstate.end ();
23965 }
23966
23967 /* Return true if X and Y opcodes have the same operands. */
23968
23969 static inline bool
23970 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23971 {
23972 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23973 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23974 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23975 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23976
23977 switch (x->dw_loc_opc)
23978 {
23979 case DW_OP_const4u:
23980 case DW_OP_const8u:
23981 if (x->dtprel)
23982 goto hash_addr;
23983 /* FALLTHRU */
23984 case DW_OP_const1u:
23985 case DW_OP_const1s:
23986 case DW_OP_const2u:
23987 case DW_OP_const2s:
23988 case DW_OP_const4s:
23989 case DW_OP_const8s:
23990 case DW_OP_constu:
23991 case DW_OP_consts:
23992 case DW_OP_pick:
23993 case DW_OP_plus_uconst:
23994 case DW_OP_breg0:
23995 case DW_OP_breg1:
23996 case DW_OP_breg2:
23997 case DW_OP_breg3:
23998 case DW_OP_breg4:
23999 case DW_OP_breg5:
24000 case DW_OP_breg6:
24001 case DW_OP_breg7:
24002 case DW_OP_breg8:
24003 case DW_OP_breg9:
24004 case DW_OP_breg10:
24005 case DW_OP_breg11:
24006 case DW_OP_breg12:
24007 case DW_OP_breg13:
24008 case DW_OP_breg14:
24009 case DW_OP_breg15:
24010 case DW_OP_breg16:
24011 case DW_OP_breg17:
24012 case DW_OP_breg18:
24013 case DW_OP_breg19:
24014 case DW_OP_breg20:
24015 case DW_OP_breg21:
24016 case DW_OP_breg22:
24017 case DW_OP_breg23:
24018 case DW_OP_breg24:
24019 case DW_OP_breg25:
24020 case DW_OP_breg26:
24021 case DW_OP_breg27:
24022 case DW_OP_breg28:
24023 case DW_OP_breg29:
24024 case DW_OP_breg30:
24025 case DW_OP_breg31:
24026 case DW_OP_regx:
24027 case DW_OP_fbreg:
24028 case DW_OP_piece:
24029 case DW_OP_deref_size:
24030 case DW_OP_xderef_size:
24031 return valx1->v.val_int == valy1->v.val_int;
24032 case DW_OP_skip:
24033 case DW_OP_bra:
24034 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24035 can cause irrelevant differences in dw_loc_addr. */
24036 gcc_assert (valx1->val_class == dw_val_class_loc
24037 && valy1->val_class == dw_val_class_loc
24038 && (dwarf_split_debug_info
24039 || x->dw_loc_addr == y->dw_loc_addr));
24040 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24041 case DW_OP_implicit_value:
24042 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24043 || valx2->val_class != valy2->val_class)
24044 return false;
24045 switch (valx2->val_class)
24046 {
24047 case dw_val_class_const:
24048 return valx2->v.val_int == valy2->v.val_int;
24049 case dw_val_class_vec:
24050 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24051 && valx2->v.val_vec.length == valy2->v.val_vec.length
24052 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24053 valx2->v.val_vec.elt_size
24054 * valx2->v.val_vec.length) == 0;
24055 case dw_val_class_const_double:
24056 return valx2->v.val_double.low == valy2->v.val_double.low
24057 && valx2->v.val_double.high == valy2->v.val_double.high;
24058 case dw_val_class_wide_int:
24059 return *valx2->v.val_wide == *valy2->v.val_wide;
24060 case dw_val_class_addr:
24061 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24062 default:
24063 gcc_unreachable ();
24064 }
24065 case DW_OP_bregx:
24066 case DW_OP_bit_piece:
24067 return valx1->v.val_int == valy1->v.val_int
24068 && valx2->v.val_int == valy2->v.val_int;
24069 case DW_OP_addr:
24070 hash_addr:
24071 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24072 case DW_OP_GNU_addr_index:
24073 case DW_OP_GNU_const_index:
24074 {
24075 rtx ax1 = valx1->val_entry->addr.rtl;
24076 rtx ay1 = valy1->val_entry->addr.rtl;
24077 return rtx_equal_p (ax1, ay1);
24078 }
24079 case DW_OP_GNU_implicit_pointer:
24080 return valx1->val_class == dw_val_class_die_ref
24081 && valx1->val_class == valy1->val_class
24082 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24083 && valx2->v.val_int == valy2->v.val_int;
24084 case DW_OP_GNU_entry_value:
24085 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24086 case DW_OP_GNU_const_type:
24087 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24088 || valx2->val_class != valy2->val_class)
24089 return false;
24090 switch (valx2->val_class)
24091 {
24092 case dw_val_class_const:
24093 return valx2->v.val_int == valy2->v.val_int;
24094 case dw_val_class_vec:
24095 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24096 && valx2->v.val_vec.length == valy2->v.val_vec.length
24097 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24098 valx2->v.val_vec.elt_size
24099 * valx2->v.val_vec.length) == 0;
24100 case dw_val_class_const_double:
24101 return valx2->v.val_double.low == valy2->v.val_double.low
24102 && valx2->v.val_double.high == valy2->v.val_double.high;
24103 case dw_val_class_wide_int:
24104 return *valx2->v.val_wide == *valy2->v.val_wide;
24105 default:
24106 gcc_unreachable ();
24107 }
24108 case DW_OP_GNU_regval_type:
24109 case DW_OP_GNU_deref_type:
24110 return valx1->v.val_int == valy1->v.val_int
24111 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24112 case DW_OP_GNU_convert:
24113 case DW_OP_GNU_reinterpret:
24114 if (valx1->val_class != valy1->val_class)
24115 return false;
24116 if (valx1->val_class == dw_val_class_unsigned_const)
24117 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24118 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24119 case DW_OP_GNU_parameter_ref:
24120 return valx1->val_class == dw_val_class_die_ref
24121 && valx1->val_class == valy1->val_class
24122 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24123 default:
24124 /* Other codes have no operands. */
24125 return true;
24126 }
24127 }
24128
24129 /* Return true if DWARF location expressions X and Y are the same. */
24130
24131 static inline bool
24132 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24133 {
24134 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24135 if (x->dw_loc_opc != y->dw_loc_opc
24136 || x->dtprel != y->dtprel
24137 || !compare_loc_operands (x, y))
24138 break;
24139 return x == NULL && y == NULL;
24140 }
24141
24142 /* Hashtable helpers. */
24143
24144 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24145 {
24146 typedef dw_loc_list_struct value_type;
24147 typedef dw_loc_list_struct compare_type;
24148 static inline hashval_t hash (const value_type *);
24149 static inline bool equal (const value_type *, const compare_type *);
24150 };
24151
24152 /* Return precomputed hash of location list X. */
24153
24154 inline hashval_t
24155 loc_list_hasher::hash (const value_type *x)
24156 {
24157 return x->hash;
24158 }
24159
24160 /* Return true if location lists A and B are the same. */
24161
24162 inline bool
24163 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24164 {
24165 if (a == b)
24166 return 1;
24167 if (a->hash != b->hash)
24168 return 0;
24169 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24170 if (strcmp (a->begin, b->begin) != 0
24171 || strcmp (a->end, b->end) != 0
24172 || (a->section == NULL) != (b->section == NULL)
24173 || (a->section && strcmp (a->section, b->section) != 0)
24174 || !compare_locs (a->expr, b->expr))
24175 break;
24176 return a == NULL && b == NULL;
24177 }
24178
24179 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24180
24181
24182 /* Recursively optimize location lists referenced from DIE
24183 children and share them whenever possible. */
24184
24185 static void
24186 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24187 {
24188 dw_die_ref c;
24189 dw_attr_ref a;
24190 unsigned ix;
24191 dw_loc_list_struct **slot;
24192
24193 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24194 if (AT_class (a) == dw_val_class_loc_list)
24195 {
24196 dw_loc_list_ref list = AT_loc_list (a);
24197 /* TODO: perform some optimizations here, before hashing
24198 it and storing into the hash table. */
24199 hash_loc_list (list);
24200 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24201 if (*slot == NULL)
24202 *slot = list;
24203 else
24204 a->dw_attr_val.v.val_loc_list = *slot;
24205 }
24206
24207 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24208 }
24209
24210
24211 /* Recursively assign each location list a unique index into the debug_addr
24212 section. */
24213
24214 static void
24215 index_location_lists (dw_die_ref die)
24216 {
24217 dw_die_ref c;
24218 dw_attr_ref a;
24219 unsigned ix;
24220
24221 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24222 if (AT_class (a) == dw_val_class_loc_list)
24223 {
24224 dw_loc_list_ref list = AT_loc_list (a);
24225 dw_loc_list_ref curr;
24226 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24227 {
24228 /* Don't index an entry that has already been indexed
24229 or won't be output. */
24230 if (curr->begin_entry != NULL
24231 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24232 continue;
24233
24234 curr->begin_entry
24235 = add_addr_table_entry (xstrdup (curr->begin),
24236 ate_kind_label);
24237 }
24238 }
24239
24240 FOR_EACH_CHILD (die, c, index_location_lists (c));
24241 }
24242
24243 /* Optimize location lists referenced from DIE
24244 children and share them whenever possible. */
24245
24246 static void
24247 optimize_location_lists (dw_die_ref die)
24248 {
24249 loc_list_hash_type htab (500);
24250 optimize_location_lists_1 (die, &htab);
24251 }
24252 \f
24253 /* Output stuff that dwarf requires at the end of every file,
24254 and generate the DWARF-2 debugging info. */
24255
24256 static void
24257 dwarf2out_finish (const char *filename)
24258 {
24259 limbo_die_node *node, *next_node;
24260 comdat_type_node *ctnode;
24261 unsigned int i;
24262 dw_die_ref main_comp_unit_die;
24263
24264 /* PCH might result in DW_AT_producer string being restored from the
24265 header compilation, so always fill it with empty string initially
24266 and overwrite only here. */
24267 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24268 producer_string = gen_producer_string ();
24269 producer->dw_attr_val.v.val_str->refcount--;
24270 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24271
24272 gen_scheduled_generic_parms_dies ();
24273 gen_remaining_tmpl_value_param_die_attribute ();
24274
24275 /* Add the name for the main input file now. We delayed this from
24276 dwarf2out_init to avoid complications with PCH. */
24277 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24278 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24279 add_comp_dir_attribute (comp_unit_die ());
24280 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24281 {
24282 bool p = false;
24283 htab_traverse (file_table, file_table_relative_p, &p);
24284 if (p)
24285 add_comp_dir_attribute (comp_unit_die ());
24286 }
24287
24288 if (deferred_locations_list)
24289 for (i = 0; i < deferred_locations_list->length (); i++)
24290 {
24291 add_location_or_const_value_attribute (
24292 (*deferred_locations_list)[i].die,
24293 (*deferred_locations_list)[i].variable,
24294 false,
24295 DW_AT_location);
24296 }
24297
24298 /* Traverse the limbo die list, and add parent/child links. The only
24299 dies without parents that should be here are concrete instances of
24300 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24301 For concrete instances, we can get the parent die from the abstract
24302 instance. */
24303 for (node = limbo_die_list; node; node = next_node)
24304 {
24305 dw_die_ref die = node->die;
24306 next_node = node->next;
24307
24308 if (die->die_parent == NULL)
24309 {
24310 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24311
24312 if (origin && origin->die_parent)
24313 add_child_die (origin->die_parent, die);
24314 else if (is_cu_die (die))
24315 ;
24316 else if (seen_error ())
24317 /* It's OK to be confused by errors in the input. */
24318 add_child_die (comp_unit_die (), die);
24319 else
24320 {
24321 /* In certain situations, the lexical block containing a
24322 nested function can be optimized away, which results
24323 in the nested function die being orphaned. Likewise
24324 with the return type of that nested function. Force
24325 this to be a child of the containing function.
24326
24327 It may happen that even the containing function got fully
24328 inlined and optimized out. In that case we are lost and
24329 assign the empty child. This should not be big issue as
24330 the function is likely unreachable too. */
24331 gcc_assert (node->created_for);
24332
24333 if (DECL_P (node->created_for))
24334 origin = get_context_die (DECL_CONTEXT (node->created_for));
24335 else if (TYPE_P (node->created_for))
24336 origin = scope_die_for (node->created_for, comp_unit_die ());
24337 else
24338 origin = comp_unit_die ();
24339
24340 add_child_die (origin, die);
24341 }
24342 }
24343 }
24344
24345 limbo_die_list = NULL;
24346
24347 #if ENABLE_ASSERT_CHECKING
24348 {
24349 dw_die_ref die = comp_unit_die (), c;
24350 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24351 }
24352 #endif
24353 resolve_addr (comp_unit_die ());
24354 move_marked_base_types ();
24355
24356 for (node = deferred_asm_name; node; node = node->next)
24357 {
24358 tree decl = node->created_for;
24359 /* When generating LTO bytecode we can not generate new assembler
24360 names at this point and all important decls got theirs via
24361 free-lang-data. */
24362 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24363 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24364 {
24365 add_linkage_attr (node->die, decl);
24366 move_linkage_attr (node->die);
24367 }
24368 }
24369
24370 deferred_asm_name = NULL;
24371
24372 /* Walk through the list of incomplete types again, trying once more to
24373 emit full debugging info for them. */
24374 retry_incomplete_types ();
24375
24376 if (flag_eliminate_unused_debug_types)
24377 prune_unused_types ();
24378
24379 /* Generate separate COMDAT sections for type DIEs. */
24380 if (use_debug_types)
24381 {
24382 break_out_comdat_types (comp_unit_die ());
24383
24384 /* Each new type_unit DIE was added to the limbo die list when created.
24385 Since these have all been added to comdat_type_list, clear the
24386 limbo die list. */
24387 limbo_die_list = NULL;
24388
24389 /* For each new comdat type unit, copy declarations for incomplete
24390 types to make the new unit self-contained (i.e., no direct
24391 references to the main compile unit). */
24392 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24393 copy_decls_for_unworthy_types (ctnode->root_die);
24394 copy_decls_for_unworthy_types (comp_unit_die ());
24395
24396 /* In the process of copying declarations from one unit to another,
24397 we may have left some declarations behind that are no longer
24398 referenced. Prune them. */
24399 prune_unused_types ();
24400 }
24401
24402 /* Generate separate CUs for each of the include files we've seen.
24403 They will go into limbo_die_list. */
24404 if (flag_eliminate_dwarf2_dups)
24405 break_out_includes (comp_unit_die ());
24406
24407 /* Traverse the DIE's and add add sibling attributes to those DIE's
24408 that have children. */
24409 add_sibling_attributes (comp_unit_die ());
24410 for (node = limbo_die_list; node; node = node->next)
24411 add_sibling_attributes (node->die);
24412 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24413 add_sibling_attributes (ctnode->root_die);
24414
24415 /* When splitting DWARF info, we put some attributes in the
24416 skeleton compile_unit DIE that remains in the .o, while
24417 most attributes go in the DWO compile_unit_die. */
24418 if (dwarf_split_debug_info)
24419 main_comp_unit_die = gen_compile_unit_die (NULL);
24420 else
24421 main_comp_unit_die = comp_unit_die ();
24422
24423 /* Output a terminator label for the .text section. */
24424 switch_to_section (text_section);
24425 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24426 if (cold_text_section)
24427 {
24428 switch_to_section (cold_text_section);
24429 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24430 }
24431
24432 /* We can only use the low/high_pc attributes if all of the code was
24433 in .text. */
24434 if (!have_multiple_function_sections
24435 || (dwarf_version < 3 && dwarf_strict))
24436 {
24437 /* Don't add if the CU has no associated code. */
24438 if (text_section_used)
24439 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24440 text_end_label, true);
24441 }
24442 else
24443 {
24444 unsigned fde_idx;
24445 dw_fde_ref fde;
24446 bool range_list_added = false;
24447
24448 if (text_section_used)
24449 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24450 text_end_label, &range_list_added, true);
24451 if (cold_text_section_used)
24452 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24453 cold_end_label, &range_list_added, true);
24454
24455 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24456 {
24457 if (DECL_IGNORED_P (fde->decl))
24458 continue;
24459 if (!fde->in_std_section)
24460 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24461 fde->dw_fde_end, &range_list_added,
24462 true);
24463 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24464 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24465 fde->dw_fde_second_end, &range_list_added,
24466 true);
24467 }
24468
24469 if (range_list_added)
24470 {
24471 /* We need to give .debug_loc and .debug_ranges an appropriate
24472 "base address". Use zero so that these addresses become
24473 absolute. Historically, we've emitted the unexpected
24474 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24475 Emit both to give time for other tools to adapt. */
24476 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24477 if (! dwarf_strict && dwarf_version < 4)
24478 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24479
24480 add_ranges (NULL);
24481 }
24482 }
24483
24484 if (debug_info_level >= DINFO_LEVEL_TERSE)
24485 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24486 debug_line_section_label);
24487
24488 if (have_macinfo)
24489 add_AT_macptr (comp_unit_die (),
24490 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24491 macinfo_section_label);
24492
24493 if (dwarf_split_debug_info)
24494 {
24495 /* optimize_location_lists calculates the size of the lists,
24496 so index them first, and assign indices to the entries.
24497 Although optimize_location_lists will remove entries from
24498 the table, it only does so for duplicates, and therefore
24499 only reduces ref_counts to 1. */
24500 index_location_lists (comp_unit_die ());
24501
24502 if (addr_index_table != NULL)
24503 {
24504 unsigned int index = 0;
24505 htab_traverse_noresize (addr_index_table,
24506 index_addr_table_entry, &index);
24507 }
24508 }
24509
24510 if (have_location_lists)
24511 optimize_location_lists (comp_unit_die ());
24512
24513 save_macinfo_strings ();
24514
24515 if (dwarf_split_debug_info)
24516 {
24517 unsigned int index = 0;
24518
24519 /* Add attributes common to skeleton compile_units and
24520 type_units. Because these attributes include strings, it
24521 must be done before freezing the string table. Top-level
24522 skeleton die attrs are added when the skeleton type unit is
24523 created, so ensure it is created by this point. */
24524 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24525 htab_traverse_noresize (debug_str_hash, index_string, &index);
24526 }
24527
24528 /* Output all of the compilation units. We put the main one last so that
24529 the offsets are available to output_pubnames. */
24530 for (node = limbo_die_list; node; node = node->next)
24531 output_comp_unit (node->die, 0);
24532
24533 hash_table<comdat_type_hasher> comdat_type_table (100);
24534 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24535 {
24536 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24537
24538 /* Don't output duplicate types. */
24539 if (*slot != HTAB_EMPTY_ENTRY)
24540 continue;
24541
24542 /* Add a pointer to the line table for the main compilation unit
24543 so that the debugger can make sense of DW_AT_decl_file
24544 attributes. */
24545 if (debug_info_level >= DINFO_LEVEL_TERSE)
24546 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24547 (!dwarf_split_debug_info
24548 ? debug_line_section_label
24549 : debug_skeleton_line_section_label));
24550
24551 output_comdat_type_unit (ctnode);
24552 *slot = ctnode;
24553 }
24554
24555 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24556 both the main_cu and all skeleton TUs. Making this call unconditional
24557 would end up either adding a second copy of the AT_pubnames attribute, or
24558 requiring a special case in add_top_level_skeleton_die_attrs. */
24559 if (!dwarf_split_debug_info)
24560 add_AT_pubnames (comp_unit_die ());
24561
24562 if (dwarf_split_debug_info)
24563 {
24564 int mark;
24565 unsigned char checksum[16];
24566 struct md5_ctx ctx;
24567
24568 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24569 md5_init_ctx (&ctx);
24570 mark = 0;
24571 die_checksum (comp_unit_die (), &ctx, &mark);
24572 unmark_all_dies (comp_unit_die ());
24573 md5_finish_ctx (&ctx, checksum);
24574
24575 /* Use the first 8 bytes of the checksum as the dwo_id,
24576 and add it to both comp-unit DIEs. */
24577 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24578 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24579
24580 /* Add the base offset of the ranges table to the skeleton
24581 comp-unit DIE. */
24582 if (ranges_table_in_use)
24583 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24584 ranges_section_label);
24585
24586 switch_to_section (debug_addr_section);
24587 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24588 output_addr_table ();
24589 }
24590
24591 /* Output the main compilation unit if non-empty or if .debug_macinfo
24592 or .debug_macro will be emitted. */
24593 output_comp_unit (comp_unit_die (), have_macinfo);
24594
24595 if (dwarf_split_debug_info && info_section_emitted)
24596 output_skeleton_debug_sections (main_comp_unit_die);
24597
24598 /* Output the abbreviation table. */
24599 if (abbrev_die_table_in_use != 1)
24600 {
24601 switch_to_section (debug_abbrev_section);
24602 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24603 output_abbrev_section ();
24604 }
24605
24606 /* Output location list section if necessary. */
24607 if (have_location_lists)
24608 {
24609 /* Output the location lists info. */
24610 switch_to_section (debug_loc_section);
24611 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24612 output_location_lists (comp_unit_die ());
24613 }
24614
24615 output_pubtables ();
24616
24617 /* Output the address range information if a CU (.debug_info section)
24618 was emitted. We output an empty table even if we had no functions
24619 to put in it. This because the consumer has no way to tell the
24620 difference between an empty table that we omitted and failure to
24621 generate a table that would have contained data. */
24622 if (info_section_emitted)
24623 {
24624 unsigned long aranges_length = size_of_aranges ();
24625
24626 switch_to_section (debug_aranges_section);
24627 output_aranges (aranges_length);
24628 }
24629
24630 /* Output ranges section if necessary. */
24631 if (ranges_table_in_use)
24632 {
24633 switch_to_section (debug_ranges_section);
24634 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24635 output_ranges ();
24636 }
24637
24638 /* Have to end the macro section. */
24639 if (have_macinfo)
24640 {
24641 switch_to_section (debug_macinfo_section);
24642 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24643 output_macinfo ();
24644 dw2_asm_output_data (1, 0, "End compilation unit");
24645 }
24646
24647 /* Output the source line correspondence table. We must do this
24648 even if there is no line information. Otherwise, on an empty
24649 translation unit, we will generate a present, but empty,
24650 .debug_info section. IRIX 6.5 `nm' will then complain when
24651 examining the file. This is done late so that any filenames
24652 used by the debug_info section are marked as 'used'. */
24653 switch_to_section (debug_line_section);
24654 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24655 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24656 output_line_info (false);
24657
24658 if (dwarf_split_debug_info && info_section_emitted)
24659 {
24660 switch_to_section (debug_skeleton_line_section);
24661 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24662 output_line_info (true);
24663 }
24664
24665 /* If we emitted any indirect strings, output the string table too. */
24666 if (debug_str_hash || skeleton_debug_str_hash)
24667 output_indirect_strings ();
24668 }
24669
24670 #include "gt-dwarf2out.h"