cond.md (stzx_16): Use register_operand for operand 0.
[gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 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 "gimple.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "ggc.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 "hash-table.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
102
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx last_var_location_insn;
105 static rtx cached_next_real_insn;
106
107 #ifdef VMS_DEBUGGING_INFO
108 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
109
110 /* Define this macro to be a nonzero value if the directory specifications
111 which are output in the debug info should end with a separator. */
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
113 /* Define this macro to evaluate to a nonzero value if GCC should refrain
114 from generating indirect strings in DWARF2 debug information, for instance
115 if your target is stuck with an old version of GDB that is unable to
116 process them properly or uses VMS Debug. */
117 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
118 #else
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
121 #endif
122
123 /* ??? Poison these here until it can be done generically. They've been
124 totally replaced in this file; make sure it stays that way. */
125 #undef DWARF2_UNWIND_INFO
126 #undef DWARF2_FRAME_INFO
127 #if (GCC_VERSION >= 3000)
128 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
129 #endif
130
131 /* The size of the target's pointer type. */
132 #ifndef PTR_SIZE
133 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
134 #endif
135
136 /* Array of RTXes referenced by the debugging information, which therefore
137 must be kept around forever. */
138 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
139
140 /* A pointer to the base of a list of incomplete types which might be
141 completed at some later time. incomplete_types_list needs to be a
142 vec<tree, va_gc> *because we want to tell the garbage collector about
143 it. */
144 static GTY(()) vec<tree, va_gc> *incomplete_types;
145
146 /* A pointer to the base of a table of references to declaration
147 scopes. This table is a display which tracks the nesting
148 of declaration scopes at the current scope and containing
149 scopes. This table is used to find the proper place to
150 define type declaration DIE's. */
151 static GTY(()) vec<tree, va_gc> *decl_scope_table;
152
153 /* Pointers to various DWARF2 sections. */
154 static GTY(()) section *debug_info_section;
155 static GTY(()) section *debug_skeleton_info_section;
156 static GTY(()) section *debug_abbrev_section;
157 static GTY(()) section *debug_skeleton_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_addr_section;
160 static GTY(()) section *debug_macinfo_section;
161 static GTY(()) section *debug_line_section;
162 static GTY(()) section *debug_skeleton_line_section;
163 static GTY(()) section *debug_loc_section;
164 static GTY(()) section *debug_pubnames_section;
165 static GTY(()) section *debug_pubtypes_section;
166 static GTY(()) section *debug_str_section;
167 static GTY(()) section *debug_str_dwo_section;
168 static GTY(()) section *debug_str_offsets_section;
169 static GTY(()) section *debug_ranges_section;
170 static GTY(()) section *debug_frame_section;
171
172 /* Maximum size (in bytes) of an artificially generated label. */
173 #define MAX_ARTIFICIAL_LABEL_BYTES 30
174
175 /* According to the (draft) DWARF 3 specification, the initial length
176 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
177 bytes are 0xffffffff, followed by the length stored in the next 8
178 bytes.
179
180 However, the SGI/MIPS ABI uses an initial length which is equal to
181 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
182
183 #ifndef DWARF_INITIAL_LENGTH_SIZE
184 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
185 #endif
186
187 /* Round SIZE up to the nearest BOUNDARY. */
188 #define DWARF_ROUND(SIZE,BOUNDARY) \
189 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
190
191 /* CIE identifier. */
192 #if HOST_BITS_PER_WIDE_INT >= 64
193 #define DWARF_CIE_ID \
194 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
195 #else
196 #define DWARF_CIE_ID DW_CIE_ID
197 #endif
198
199
200 /* A vector for a table that contains frame description
201 information for each routine. */
202 #define NOT_INDEXED (-1U)
203 #define NO_INDEX_ASSIGNED (-2U)
204
205 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
206
207 struct GTY(()) indirect_string_node {
208 const char *str;
209 unsigned int refcount;
210 enum dwarf_form form;
211 char *label;
212 unsigned int index;
213 };
214
215 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
216
217 /* With split_debug_info, both the comp_dir and dwo_name go in the
218 main object file, rather than the dwo, similar to the force_direct
219 parameter elsewhere but with additional complications:
220
221 1) The string is needed in both the main object file and the dwo.
222 That is, the comp_dir and dwo_name will appear in both places.
223
224 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
225 DW_FORM_GNU_str_index.
226
227 3) GCC chooses the form to use late, depending on the size and
228 reference count.
229
230 Rather than forcing the all debug string handling functions and
231 callers to deal with these complications, simply use a separate,
232 special-cased string table for any attribute that should go in the
233 main object file. This limits the complexity to just the places
234 that need it. */
235
236 static GTY ((param_is (struct indirect_string_node)))
237 htab_t skeleton_debug_str_hash;
238
239 static GTY(()) int dw2_string_counter;
240
241 /* True if the compilation unit places functions in more than one section. */
242 static GTY(()) bool have_multiple_function_sections = false;
243
244 /* Whether the default text and cold text sections have been used at all. */
245
246 static GTY(()) bool text_section_used = false;
247 static GTY(()) bool cold_text_section_used = false;
248
249 /* The default cold text section. */
250 static GTY(()) section *cold_text_section;
251
252 /* Forward declarations for functions defined in this file. */
253
254 static char *stripattributes (const char *);
255 static void output_call_frame_info (int);
256 static void dwarf2out_note_section_used (void);
257
258 /* Personality decl of current unit. Used only when assembler does not support
259 personality CFI. */
260 static GTY(()) rtx current_unit_personality;
261
262 /* Data and reference forms for relocatable data. */
263 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
264 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
265
266 #ifndef DEBUG_FRAME_SECTION
267 #define DEBUG_FRAME_SECTION ".debug_frame"
268 #endif
269
270 #ifndef FUNC_BEGIN_LABEL
271 #define FUNC_BEGIN_LABEL "LFB"
272 #endif
273
274 #ifndef FUNC_END_LABEL
275 #define FUNC_END_LABEL "LFE"
276 #endif
277
278 #ifndef PROLOGUE_END_LABEL
279 #define PROLOGUE_END_LABEL "LPE"
280 #endif
281
282 #ifndef EPILOGUE_BEGIN_LABEL
283 #define EPILOGUE_BEGIN_LABEL "LEB"
284 #endif
285
286 #ifndef FRAME_BEGIN_LABEL
287 #define FRAME_BEGIN_LABEL "Lframe"
288 #endif
289 #define CIE_AFTER_SIZE_LABEL "LSCIE"
290 #define CIE_END_LABEL "LECIE"
291 #define FDE_LABEL "LSFDE"
292 #define FDE_AFTER_SIZE_LABEL "LASFDE"
293 #define FDE_END_LABEL "LEFDE"
294 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
295 #define LINE_NUMBER_END_LABEL "LELT"
296 #define LN_PROLOG_AS_LABEL "LASLTP"
297 #define LN_PROLOG_END_LABEL "LELTP"
298 #define DIE_LABEL_PREFIX "DW"
299 \f
300 /* Match the base name of a file to the base name of a compilation unit. */
301
302 static int
303 matches_main_base (const char *path)
304 {
305 /* Cache the last query. */
306 static const char *last_path = NULL;
307 static int last_match = 0;
308 if (path != last_path)
309 {
310 const char *base;
311 int length = base_of_path (path, &base);
312 last_path = path;
313 last_match = (length == main_input_baselength
314 && memcmp (base, main_input_basename, length) == 0);
315 }
316 return last_match;
317 }
318
319 #ifdef DEBUG_DEBUG_STRUCT
320
321 static int
322 dump_struct_debug (tree type, enum debug_info_usage usage,
323 enum debug_struct_file criterion, int generic,
324 int matches, int result)
325 {
326 /* Find the type name. */
327 tree type_decl = TYPE_STUB_DECL (type);
328 tree t = type_decl;
329 const char *name = 0;
330 if (TREE_CODE (t) == TYPE_DECL)
331 t = DECL_NAME (t);
332 if (t)
333 name = IDENTIFIER_POINTER (t);
334
335 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
336 criterion,
337 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
338 matches ? "bas" : "hdr",
339 generic ? "gen" : "ord",
340 usage == DINFO_USAGE_DFN ? ";" :
341 usage == DINFO_USAGE_DIR_USE ? "." : "*",
342 result,
343 (void*) type_decl, name);
344 return result;
345 }
346 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
347 dump_struct_debug (type, usage, criterion, generic, matches, result)
348
349 #else
350
351 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
352 (result)
353
354 #endif
355
356 static bool
357 should_emit_struct_debug (tree type, enum debug_info_usage usage)
358 {
359 enum debug_struct_file criterion;
360 tree type_decl;
361 bool generic = lang_hooks.types.generic_p (type);
362
363 if (generic)
364 criterion = debug_struct_generic[usage];
365 else
366 criterion = debug_struct_ordinary[usage];
367
368 if (criterion == DINFO_STRUCT_FILE_NONE)
369 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
370 if (criterion == DINFO_STRUCT_FILE_ANY)
371 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
372
373 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
374
375 if (type_decl != NULL)
376 {
377 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
378 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
379
380 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
381 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
382 }
383
384 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
385 }
386 \f
387 /* Return a pointer to a copy of the section string name S with all
388 attributes stripped off, and an asterisk prepended (for assemble_name). */
389
390 static inline char *
391 stripattributes (const char *s)
392 {
393 char *stripped = XNEWVEC (char, strlen (s) + 2);
394 char *p = stripped;
395
396 *p++ = '*';
397
398 while (*s && *s != ',')
399 *p++ = *s++;
400
401 *p = '\0';
402 return stripped;
403 }
404
405 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
406 switch to the data section instead, and write out a synthetic start label
407 for collect2 the first time around. */
408
409 static void
410 switch_to_eh_frame_section (bool back)
411 {
412 tree label;
413
414 #ifdef EH_FRAME_SECTION_NAME
415 if (eh_frame_section == 0)
416 {
417 int flags;
418
419 if (EH_TABLES_CAN_BE_READ_ONLY)
420 {
421 int fde_encoding;
422 int per_encoding;
423 int lsda_encoding;
424
425 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
426 /*global=*/0);
427 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
428 /*global=*/1);
429 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
430 /*global=*/0);
431 flags = ((! flag_pic
432 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
433 && (fde_encoding & 0x70) != DW_EH_PE_aligned
434 && (per_encoding & 0x70) != DW_EH_PE_absptr
435 && (per_encoding & 0x70) != DW_EH_PE_aligned
436 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
437 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
438 ? 0 : SECTION_WRITE);
439 }
440 else
441 flags = SECTION_WRITE;
442 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
443 }
444 #endif /* EH_FRAME_SECTION_NAME */
445
446 if (eh_frame_section)
447 switch_to_section (eh_frame_section);
448 else
449 {
450 /* We have no special eh_frame section. Put the information in
451 the data section and emit special labels to guide collect2. */
452 switch_to_section (data_section);
453
454 if (!back)
455 {
456 label = get_file_function_name ("F");
457 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
458 targetm.asm_out.globalize_label (asm_out_file,
459 IDENTIFIER_POINTER (label));
460 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
461 }
462 }
463 }
464
465 /* Switch [BACK] to the eh or debug frame table section, depending on
466 FOR_EH. */
467
468 static void
469 switch_to_frame_table_section (int for_eh, bool back)
470 {
471 if (for_eh)
472 switch_to_eh_frame_section (back);
473 else
474 {
475 if (!debug_frame_section)
476 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
477 SECTION_DEBUG, NULL);
478 switch_to_section (debug_frame_section);
479 }
480 }
481
482 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
483
484 enum dw_cfi_oprnd_type
485 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
486 {
487 switch (cfi)
488 {
489 case DW_CFA_nop:
490 case DW_CFA_GNU_window_save:
491 case DW_CFA_remember_state:
492 case DW_CFA_restore_state:
493 return dw_cfi_oprnd_unused;
494
495 case DW_CFA_set_loc:
496 case DW_CFA_advance_loc1:
497 case DW_CFA_advance_loc2:
498 case DW_CFA_advance_loc4:
499 case DW_CFA_MIPS_advance_loc8:
500 return dw_cfi_oprnd_addr;
501
502 case DW_CFA_offset:
503 case DW_CFA_offset_extended:
504 case DW_CFA_def_cfa:
505 case DW_CFA_offset_extended_sf:
506 case DW_CFA_def_cfa_sf:
507 case DW_CFA_restore:
508 case DW_CFA_restore_extended:
509 case DW_CFA_undefined:
510 case DW_CFA_same_value:
511 case DW_CFA_def_cfa_register:
512 case DW_CFA_register:
513 case DW_CFA_expression:
514 return dw_cfi_oprnd_reg_num;
515
516 case DW_CFA_def_cfa_offset:
517 case DW_CFA_GNU_args_size:
518 case DW_CFA_def_cfa_offset_sf:
519 return dw_cfi_oprnd_offset;
520
521 case DW_CFA_def_cfa_expression:
522 return dw_cfi_oprnd_loc;
523
524 default:
525 gcc_unreachable ();
526 }
527 }
528
529 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
530
531 enum dw_cfi_oprnd_type
532 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
533 {
534 switch (cfi)
535 {
536 case DW_CFA_def_cfa:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_offset:
539 case DW_CFA_offset_extended_sf:
540 case DW_CFA_offset_extended:
541 return dw_cfi_oprnd_offset;
542
543 case DW_CFA_register:
544 return dw_cfi_oprnd_reg_num;
545
546 case DW_CFA_expression:
547 return dw_cfi_oprnd_loc;
548
549 default:
550 return dw_cfi_oprnd_unused;
551 }
552 }
553
554 /* Output one FDE. */
555
556 static void
557 output_fde (dw_fde_ref fde, bool for_eh, bool second,
558 char *section_start_label, int fde_encoding, char *augmentation,
559 bool any_lsda_needed, int lsda_encoding)
560 {
561 const char *begin, *end;
562 static unsigned int j;
563 char l1[20], l2[20];
564
565 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
566 /* empty */ 0);
567 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
568 for_eh + j);
569 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
570 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
571 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
572 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
573 " indicating 64-bit DWARF extension");
574 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
575 "FDE Length");
576 ASM_OUTPUT_LABEL (asm_out_file, l1);
577
578 if (for_eh)
579 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
580 else
581 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
582 debug_frame_section, "FDE CIE offset");
583
584 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
585 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
586
587 if (for_eh)
588 {
589 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
590 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
591 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
592 "FDE initial location");
593 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
594 end, begin, "FDE address range");
595 }
596 else
597 {
598 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
599 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
600 }
601
602 if (augmentation[0])
603 {
604 if (any_lsda_needed)
605 {
606 int size = size_of_encoded_value (lsda_encoding);
607
608 if (lsda_encoding == DW_EH_PE_aligned)
609 {
610 int offset = ( 4 /* Length */
611 + 4 /* CIE offset */
612 + 2 * size_of_encoded_value (fde_encoding)
613 + 1 /* Augmentation size */ );
614 int pad = -offset & (PTR_SIZE - 1);
615
616 size += pad;
617 gcc_assert (size_of_uleb128 (size) == 1);
618 }
619
620 dw2_asm_output_data_uleb128 (size, "Augmentation size");
621
622 if (fde->uses_eh_lsda)
623 {
624 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
625 fde->funcdef_number);
626 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
627 gen_rtx_SYMBOL_REF (Pmode, l1),
628 false,
629 "Language Specific Data Area");
630 }
631 else
632 {
633 if (lsda_encoding == DW_EH_PE_aligned)
634 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
635 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
636 "Language Specific Data Area (none)");
637 }
638 }
639 else
640 dw2_asm_output_data_uleb128 (0, "Augmentation size");
641 }
642
643 /* Loop through the Call Frame Instructions associated with this FDE. */
644 fde->dw_fde_current_label = begin;
645 {
646 size_t from, until, i;
647
648 from = 0;
649 until = vec_safe_length (fde->dw_fde_cfi);
650
651 if (fde->dw_fde_second_begin == NULL)
652 ;
653 else if (!second)
654 until = fde->dw_fde_switch_cfi_index;
655 else
656 from = fde->dw_fde_switch_cfi_index;
657
658 for (i = from; i < until; i++)
659 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
660 }
661
662 /* If we are to emit a ref/link from function bodies to their frame tables,
663 do it now. This is typically performed to make sure that tables
664 associated with functions are dragged with them and not discarded in
665 garbage collecting links. We need to do this on a per function basis to
666 cope with -ffunction-sections. */
667
668 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
669 /* Switch to the function section, emit the ref to the tables, and
670 switch *back* into the table section. */
671 switch_to_section (function_section (fde->decl));
672 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
673 switch_to_frame_table_section (for_eh, true);
674 #endif
675
676 /* Pad the FDE out to an address sized boundary. */
677 ASM_OUTPUT_ALIGN (asm_out_file,
678 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
679 ASM_OUTPUT_LABEL (asm_out_file, l2);
680
681 j += 2;
682 }
683
684 /* Return true if frame description entry FDE is needed for EH. */
685
686 static bool
687 fde_needed_for_eh_p (dw_fde_ref fde)
688 {
689 if (flag_asynchronous_unwind_tables)
690 return true;
691
692 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
693 return true;
694
695 if (fde->uses_eh_lsda)
696 return true;
697
698 /* If exceptions are enabled, we have collected nothrow info. */
699 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
700 return false;
701
702 return true;
703 }
704
705 /* Output the call frame information used to record information
706 that relates to calculating the frame pointer, and records the
707 location of saved registers. */
708
709 static void
710 output_call_frame_info (int for_eh)
711 {
712 unsigned int i;
713 dw_fde_ref fde;
714 dw_cfi_ref cfi;
715 char l1[20], l2[20], section_start_label[20];
716 bool any_lsda_needed = false;
717 char augmentation[6];
718 int augmentation_size;
719 int fde_encoding = DW_EH_PE_absptr;
720 int per_encoding = DW_EH_PE_absptr;
721 int lsda_encoding = DW_EH_PE_absptr;
722 int return_reg;
723 rtx personality = NULL;
724 int dw_cie_version;
725
726 /* Don't emit a CIE if there won't be any FDEs. */
727 if (!fde_vec)
728 return;
729
730 /* Nothing to do if the assembler's doing it all. */
731 if (dwarf2out_do_cfi_asm ())
732 return;
733
734 /* If we don't have any functions we'll want to unwind out of, don't emit
735 any EH unwind information. If we make FDEs linkonce, we may have to
736 emit an empty label for an FDE that wouldn't otherwise be emitted. We
737 want to avoid having an FDE kept around when the function it refers to
738 is discarded. Example where this matters: a primary function template
739 in C++ requires EH information, an explicit specialization doesn't. */
740 if (for_eh)
741 {
742 bool any_eh_needed = false;
743
744 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
745 {
746 if (fde->uses_eh_lsda)
747 any_eh_needed = any_lsda_needed = true;
748 else if (fde_needed_for_eh_p (fde))
749 any_eh_needed = true;
750 else if (TARGET_USES_WEAK_UNWIND_INFO)
751 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
752 }
753
754 if (!any_eh_needed)
755 return;
756 }
757
758 /* We're going to be generating comments, so turn on app. */
759 if (flag_debug_asm)
760 app_enable ();
761
762 /* Switch to the proper frame section, first time. */
763 switch_to_frame_table_section (for_eh, false);
764
765 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
766 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
767
768 /* Output the CIE. */
769 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
770 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
771 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
772 dw2_asm_output_data (4, 0xffffffff,
773 "Initial length escape value indicating 64-bit DWARF extension");
774 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
775 "Length of Common Information Entry");
776 ASM_OUTPUT_LABEL (asm_out_file, l1);
777
778 /* Now that the CIE pointer is PC-relative for EH,
779 use 0 to identify the CIE. */
780 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
781 (for_eh ? 0 : DWARF_CIE_ID),
782 "CIE Identifier Tag");
783
784 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
785 use CIE version 1, unless that would produce incorrect results
786 due to overflowing the return register column. */
787 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
788 dw_cie_version = 1;
789 if (return_reg >= 256 || dwarf_version > 2)
790 dw_cie_version = 3;
791 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
792
793 augmentation[0] = 0;
794 augmentation_size = 0;
795
796 personality = current_unit_personality;
797 if (for_eh)
798 {
799 char *p;
800
801 /* Augmentation:
802 z Indicates that a uleb128 is present to size the
803 augmentation section.
804 L Indicates the encoding (and thus presence) of
805 an LSDA pointer in the FDE augmentation.
806 R Indicates a non-default pointer encoding for
807 FDE code pointers.
808 P Indicates the presence of an encoding + language
809 personality routine in the CIE augmentation. */
810
811 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
812 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
813 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
814
815 p = augmentation + 1;
816 if (personality)
817 {
818 *p++ = 'P';
819 augmentation_size += 1 + size_of_encoded_value (per_encoding);
820 assemble_external_libcall (personality);
821 }
822 if (any_lsda_needed)
823 {
824 *p++ = 'L';
825 augmentation_size += 1;
826 }
827 if (fde_encoding != DW_EH_PE_absptr)
828 {
829 *p++ = 'R';
830 augmentation_size += 1;
831 }
832 if (p > augmentation + 1)
833 {
834 augmentation[0] = 'z';
835 *p = '\0';
836 }
837
838 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
839 if (personality && per_encoding == DW_EH_PE_aligned)
840 {
841 int offset = ( 4 /* Length */
842 + 4 /* CIE Id */
843 + 1 /* CIE version */
844 + strlen (augmentation) + 1 /* Augmentation */
845 + size_of_uleb128 (1) /* Code alignment */
846 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
847 + 1 /* RA column */
848 + 1 /* Augmentation size */
849 + 1 /* Personality encoding */ );
850 int pad = -offset & (PTR_SIZE - 1);
851
852 augmentation_size += pad;
853
854 /* Augmentations should be small, so there's scarce need to
855 iterate for a solution. Die if we exceed one uleb128 byte. */
856 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
857 }
858 }
859
860 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
861 if (dw_cie_version >= 4)
862 {
863 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
864 dw2_asm_output_data (1, 0, "CIE Segment Size");
865 }
866 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
867 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
868 "CIE Data Alignment Factor");
869
870 if (dw_cie_version == 1)
871 dw2_asm_output_data (1, return_reg, "CIE RA Column");
872 else
873 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
874
875 if (augmentation[0])
876 {
877 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
878 if (personality)
879 {
880 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
881 eh_data_format_name (per_encoding));
882 dw2_asm_output_encoded_addr_rtx (per_encoding,
883 personality,
884 true, NULL);
885 }
886
887 if (any_lsda_needed)
888 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
889 eh_data_format_name (lsda_encoding));
890
891 if (fde_encoding != DW_EH_PE_absptr)
892 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
893 eh_data_format_name (fde_encoding));
894 }
895
896 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
897 output_cfi (cfi, NULL, for_eh);
898
899 /* Pad the CIE out to an address sized boundary. */
900 ASM_OUTPUT_ALIGN (asm_out_file,
901 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
902 ASM_OUTPUT_LABEL (asm_out_file, l2);
903
904 /* Loop through all of the FDE's. */
905 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
906 {
907 unsigned int k;
908
909 /* Don't emit EH unwind info for leaf functions that don't need it. */
910 if (for_eh && !fde_needed_for_eh_p (fde))
911 continue;
912
913 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
914 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
915 augmentation, any_lsda_needed, lsda_encoding);
916 }
917
918 if (for_eh && targetm.terminate_dw2_eh_frame_info)
919 dw2_asm_output_data (4, 0, "End of Table");
920
921 /* Turn off app to make assembly quicker. */
922 if (flag_debug_asm)
923 app_disable ();
924 }
925
926 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
927
928 static void
929 dwarf2out_do_cfi_startproc (bool second)
930 {
931 int enc;
932 rtx ref;
933 rtx personality = get_personality_function (current_function_decl);
934
935 fprintf (asm_out_file, "\t.cfi_startproc\n");
936
937 if (personality)
938 {
939 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
940 ref = personality;
941
942 /* ??? The GAS support isn't entirely consistent. We have to
943 handle indirect support ourselves, but PC-relative is done
944 in the assembler. Further, the assembler can't handle any
945 of the weirder relocation types. */
946 if (enc & DW_EH_PE_indirect)
947 ref = dw2_force_const_mem (ref, true);
948
949 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
950 output_addr_const (asm_out_file, ref);
951 fputc ('\n', asm_out_file);
952 }
953
954 if (crtl->uses_eh_lsda)
955 {
956 char lab[20];
957
958 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
959 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
960 current_function_funcdef_no);
961 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
962 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
963
964 if (enc & DW_EH_PE_indirect)
965 ref = dw2_force_const_mem (ref, true);
966
967 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
968 output_addr_const (asm_out_file, ref);
969 fputc ('\n', asm_out_file);
970 }
971 }
972
973 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
974 this allocation may be done before pass_final. */
975
976 dw_fde_ref
977 dwarf2out_alloc_current_fde (void)
978 {
979 dw_fde_ref fde;
980
981 fde = ggc_alloc_cleared_dw_fde_node ();
982 fde->decl = current_function_decl;
983 fde->funcdef_number = current_function_funcdef_no;
984 fde->fde_index = vec_safe_length (fde_vec);
985 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
986 fde->uses_eh_lsda = crtl->uses_eh_lsda;
987 fde->nothrow = crtl->nothrow;
988 fde->drap_reg = INVALID_REGNUM;
989 fde->vdrap_reg = INVALID_REGNUM;
990
991 /* Record the FDE associated with this function. */
992 cfun->fde = fde;
993 vec_safe_push (fde_vec, fde);
994
995 return fde;
996 }
997
998 /* Output a marker (i.e. a label) for the beginning of a function, before
999 the prologue. */
1000
1001 void
1002 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1003 const char *file ATTRIBUTE_UNUSED)
1004 {
1005 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1006 char * dup_label;
1007 dw_fde_ref fde;
1008 section *fnsec;
1009 bool do_frame;
1010
1011 current_function_func_begin_label = NULL;
1012
1013 do_frame = dwarf2out_do_frame ();
1014
1015 /* ??? current_function_func_begin_label is also used by except.c for
1016 call-site information. We must emit this label if it might be used. */
1017 if (!do_frame
1018 && (!flag_exceptions
1019 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1020 return;
1021
1022 fnsec = function_section (current_function_decl);
1023 switch_to_section (fnsec);
1024 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1025 current_function_funcdef_no);
1026 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1027 current_function_funcdef_no);
1028 dup_label = xstrdup (label);
1029 current_function_func_begin_label = dup_label;
1030
1031 /* We can elide the fde allocation if we're not emitting debug info. */
1032 if (!do_frame)
1033 return;
1034
1035 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1036 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1037 would include pass_dwarf2_frame. If we've not created the FDE yet,
1038 do so now. */
1039 fde = cfun->fde;
1040 if (fde == NULL)
1041 fde = dwarf2out_alloc_current_fde ();
1042
1043 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1044 fde->dw_fde_begin = dup_label;
1045 fde->dw_fde_current_label = dup_label;
1046 fde->in_std_section = (fnsec == text_section
1047 || (cold_text_section && fnsec == cold_text_section));
1048
1049 /* We only want to output line number information for the genuine dwarf2
1050 prologue case, not the eh frame case. */
1051 #ifdef DWARF2_DEBUGGING_INFO
1052 if (file)
1053 dwarf2out_source_line (line, file, 0, true);
1054 #endif
1055
1056 if (dwarf2out_do_cfi_asm ())
1057 dwarf2out_do_cfi_startproc (false);
1058 else
1059 {
1060 rtx personality = get_personality_function (current_function_decl);
1061 if (!current_unit_personality)
1062 current_unit_personality = personality;
1063
1064 /* We cannot keep a current personality per function as without CFI
1065 asm, at the point where we emit the CFI data, there is no current
1066 function anymore. */
1067 if (personality && current_unit_personality != personality)
1068 sorry ("multiple EH personalities are supported only with assemblers "
1069 "supporting .cfi_personality directive");
1070 }
1071 }
1072
1073 /* Output a marker (i.e. a label) for the end of the generated code
1074 for a function prologue. This gets called *after* the prologue code has
1075 been generated. */
1076
1077 void
1078 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1079 const char *file ATTRIBUTE_UNUSED)
1080 {
1081 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1082
1083 /* Output a label to mark the endpoint of the code generated for this
1084 function. */
1085 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1086 current_function_funcdef_no);
1087 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1088 current_function_funcdef_no);
1089 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1090 }
1091
1092 /* Output a marker (i.e. a label) for the beginning of the generated code
1093 for a function epilogue. This gets called *before* the prologue code has
1094 been generated. */
1095
1096 void
1097 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1098 const char *file ATTRIBUTE_UNUSED)
1099 {
1100 dw_fde_ref fde = cfun->fde;
1101 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1102
1103 if (fde->dw_fde_vms_begin_epilogue)
1104 return;
1105
1106 /* Output a label to mark the endpoint of the code generated for this
1107 function. */
1108 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1109 current_function_funcdef_no);
1110 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1111 current_function_funcdef_no);
1112 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1113 }
1114
1115 /* Output a marker (i.e. a label) for the absolute end of the generated code
1116 for a function definition. This gets called *after* the epilogue code has
1117 been generated. */
1118
1119 void
1120 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1121 const char *file ATTRIBUTE_UNUSED)
1122 {
1123 dw_fde_ref fde;
1124 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1125
1126 last_var_location_insn = NULL_RTX;
1127 cached_next_real_insn = NULL_RTX;
1128
1129 if (dwarf2out_do_cfi_asm ())
1130 fprintf (asm_out_file, "\t.cfi_endproc\n");
1131
1132 /* Output a label to mark the endpoint of the code generated for this
1133 function. */
1134 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1135 current_function_funcdef_no);
1136 ASM_OUTPUT_LABEL (asm_out_file, label);
1137 fde = cfun->fde;
1138 gcc_assert (fde != NULL);
1139 if (fde->dw_fde_second_begin == NULL)
1140 fde->dw_fde_end = xstrdup (label);
1141 }
1142
1143 void
1144 dwarf2out_frame_finish (void)
1145 {
1146 /* Output call frame information. */
1147 if (targetm.debug_unwind_info () == UI_DWARF2)
1148 output_call_frame_info (0);
1149
1150 /* Output another copy for the unwinder. */
1151 if ((flag_unwind_tables || flag_exceptions)
1152 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1153 output_call_frame_info (1);
1154 }
1155
1156 /* Note that the current function section is being used for code. */
1157
1158 static void
1159 dwarf2out_note_section_used (void)
1160 {
1161 section *sec = current_function_section ();
1162 if (sec == text_section)
1163 text_section_used = true;
1164 else if (sec == cold_text_section)
1165 cold_text_section_used = true;
1166 }
1167
1168 static void var_location_switch_text_section (void);
1169 static void set_cur_line_info_table (section *);
1170
1171 void
1172 dwarf2out_switch_text_section (void)
1173 {
1174 section *sect;
1175 dw_fde_ref fde = cfun->fde;
1176
1177 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1178
1179 if (!in_cold_section_p)
1180 {
1181 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1182 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1183 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1184 }
1185 else
1186 {
1187 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1188 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1189 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1190 }
1191 have_multiple_function_sections = true;
1192
1193 /* There is no need to mark used sections when not debugging. */
1194 if (cold_text_section != NULL)
1195 dwarf2out_note_section_used ();
1196
1197 if (dwarf2out_do_cfi_asm ())
1198 fprintf (asm_out_file, "\t.cfi_endproc\n");
1199
1200 /* Now do the real section switch. */
1201 sect = current_function_section ();
1202 switch_to_section (sect);
1203
1204 fde->second_in_std_section
1205 = (sect == text_section
1206 || (cold_text_section && sect == cold_text_section));
1207
1208 if (dwarf2out_do_cfi_asm ())
1209 dwarf2out_do_cfi_startproc (true);
1210
1211 var_location_switch_text_section ();
1212
1213 if (cold_text_section != NULL)
1214 set_cur_line_info_table (sect);
1215 }
1216 \f
1217 /* And now, the subset of the debugging information support code necessary
1218 for emitting location expressions. */
1219
1220 /* Data about a single source file. */
1221 struct GTY(()) dwarf_file_data {
1222 const char * filename;
1223 int emitted_number;
1224 };
1225
1226 typedef struct GTY(()) deferred_locations_struct
1227 {
1228 tree variable;
1229 dw_die_ref die;
1230 } deferred_locations;
1231
1232
1233 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1234
1235
1236 /* Describe an entry into the .debug_addr section. */
1237
1238 enum ate_kind {
1239 ate_kind_rtx,
1240 ate_kind_rtx_dtprel,
1241 ate_kind_label
1242 };
1243
1244 typedef struct GTY(()) addr_table_entry_struct {
1245 enum ate_kind kind;
1246 unsigned int refcount;
1247 unsigned int index;
1248 union addr_table_entry_struct_union
1249 {
1250 rtx GTY ((tag ("0"))) rtl;
1251 char * GTY ((tag ("1"))) label;
1252 }
1253 GTY ((desc ("%1.kind"))) addr;
1254 }
1255 addr_table_entry;
1256
1257 /* Location lists are ranges + location descriptions for that range,
1258 so you can track variables that are in different places over
1259 their entire life. */
1260 typedef struct GTY(()) dw_loc_list_struct {
1261 dw_loc_list_ref dw_loc_next;
1262 const char *begin; /* Label and addr_entry for start of range */
1263 addr_table_entry *begin_entry;
1264 const char *end; /* Label for end of range */
1265 char *ll_symbol; /* Label for beginning of location list.
1266 Only on head of list */
1267 const char *section; /* Section this loclist is relative to */
1268 dw_loc_descr_ref expr;
1269 hashval_t hash;
1270 /* True if all addresses in this and subsequent lists are known to be
1271 resolved. */
1272 bool resolved_addr;
1273 /* True if this list has been replaced by dw_loc_next. */
1274 bool replaced;
1275 bool emitted;
1276 /* True if the range should be emitted even if begin and end
1277 are the same. */
1278 bool force;
1279 } dw_loc_list_node;
1280
1281 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1282
1283 /* Convert a DWARF stack opcode into its string name. */
1284
1285 static const char *
1286 dwarf_stack_op_name (unsigned int op)
1287 {
1288 const char *name = get_DW_OP_name (op);
1289
1290 if (name != NULL)
1291 return name;
1292
1293 return "OP_<unknown>";
1294 }
1295
1296 /* Return a pointer to a newly allocated location description. Location
1297 descriptions are simple expression terms that can be strung
1298 together to form more complicated location (address) descriptions. */
1299
1300 static inline dw_loc_descr_ref
1301 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1302 unsigned HOST_WIDE_INT oprnd2)
1303 {
1304 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1305
1306 descr->dw_loc_opc = op;
1307 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1308 descr->dw_loc_oprnd1.val_entry = NULL;
1309 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1310 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1311 descr->dw_loc_oprnd2.val_entry = NULL;
1312 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1313
1314 return descr;
1315 }
1316
1317 /* Return a pointer to a newly allocated location description for
1318 REG and OFFSET. */
1319
1320 static inline dw_loc_descr_ref
1321 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1322 {
1323 if (reg <= 31)
1324 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1325 offset, 0);
1326 else
1327 return new_loc_descr (DW_OP_bregx, reg, offset);
1328 }
1329
1330 /* Add a location description term to a location description expression. */
1331
1332 static inline void
1333 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1334 {
1335 dw_loc_descr_ref *d;
1336
1337 /* Find the end of the chain. */
1338 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1339 ;
1340
1341 *d = descr;
1342 }
1343
1344 /* Compare two location operands for exact equality. */
1345
1346 static bool
1347 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1348 {
1349 if (a->val_class != b->val_class)
1350 return false;
1351 switch (a->val_class)
1352 {
1353 case dw_val_class_none:
1354 return true;
1355 case dw_val_class_addr:
1356 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1357
1358 case dw_val_class_offset:
1359 case dw_val_class_unsigned_const:
1360 case dw_val_class_const:
1361 case dw_val_class_range_list:
1362 case dw_val_class_lineptr:
1363 case dw_val_class_macptr:
1364 /* These are all HOST_WIDE_INT, signed or unsigned. */
1365 return a->v.val_unsigned == b->v.val_unsigned;
1366
1367 case dw_val_class_loc:
1368 return a->v.val_loc == b->v.val_loc;
1369 case dw_val_class_loc_list:
1370 return a->v.val_loc_list == b->v.val_loc_list;
1371 case dw_val_class_die_ref:
1372 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1373 case dw_val_class_fde_ref:
1374 return a->v.val_fde_index == b->v.val_fde_index;
1375 case dw_val_class_lbl_id:
1376 case dw_val_class_high_pc:
1377 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1378 case dw_val_class_str:
1379 return a->v.val_str == b->v.val_str;
1380 case dw_val_class_flag:
1381 return a->v.val_flag == b->v.val_flag;
1382 case dw_val_class_file:
1383 return a->v.val_file == b->v.val_file;
1384 case dw_val_class_decl_ref:
1385 return a->v.val_decl_ref == b->v.val_decl_ref;
1386
1387 case dw_val_class_const_double:
1388 return (a->v.val_double.high == b->v.val_double.high
1389 && a->v.val_double.low == b->v.val_double.low);
1390
1391 case dw_val_class_vec:
1392 {
1393 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1394 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1395
1396 return (a_len == b_len
1397 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1398 }
1399
1400 case dw_val_class_data8:
1401 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1402
1403 case dw_val_class_vms_delta:
1404 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1405 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1406 }
1407 gcc_unreachable ();
1408 }
1409
1410 /* Compare two location atoms for exact equality. */
1411
1412 static bool
1413 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1414 {
1415 if (a->dw_loc_opc != b->dw_loc_opc)
1416 return false;
1417
1418 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1419 address size, but since we always allocate cleared storage it
1420 should be zero for other types of locations. */
1421 if (a->dtprel != b->dtprel)
1422 return false;
1423
1424 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1425 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1426 }
1427
1428 /* Compare two complete location expressions for exact equality. */
1429
1430 bool
1431 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1432 {
1433 while (1)
1434 {
1435 if (a == b)
1436 return true;
1437 if (a == NULL || b == NULL)
1438 return false;
1439 if (!loc_descr_equal_p_1 (a, b))
1440 return false;
1441
1442 a = a->dw_loc_next;
1443 b = b->dw_loc_next;
1444 }
1445 }
1446
1447
1448 /* Add a constant OFFSET to a location expression. */
1449
1450 static void
1451 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1452 {
1453 dw_loc_descr_ref loc;
1454 HOST_WIDE_INT *p;
1455
1456 gcc_assert (*list_head != NULL);
1457
1458 if (!offset)
1459 return;
1460
1461 /* Find the end of the chain. */
1462 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1463 ;
1464
1465 p = NULL;
1466 if (loc->dw_loc_opc == DW_OP_fbreg
1467 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1468 p = &loc->dw_loc_oprnd1.v.val_int;
1469 else if (loc->dw_loc_opc == DW_OP_bregx)
1470 p = &loc->dw_loc_oprnd2.v.val_int;
1471
1472 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1473 offset. Don't optimize if an signed integer overflow would happen. */
1474 if (p != NULL
1475 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1476 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1477 *p += offset;
1478
1479 else if (offset > 0)
1480 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1481
1482 else
1483 {
1484 loc->dw_loc_next = int_loc_descriptor (-offset);
1485 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1486 }
1487 }
1488
1489 /* Add a constant OFFSET to a location list. */
1490
1491 static void
1492 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1493 {
1494 dw_loc_list_ref d;
1495 for (d = list_head; d != NULL; d = d->dw_loc_next)
1496 loc_descr_plus_const (&d->expr, offset);
1497 }
1498
1499 #define DWARF_REF_SIZE \
1500 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1501
1502 static unsigned long int get_base_type_offset (dw_die_ref);
1503
1504 /* Return the size of a location descriptor. */
1505
1506 static unsigned long
1507 size_of_loc_descr (dw_loc_descr_ref loc)
1508 {
1509 unsigned long size = 1;
1510
1511 switch (loc->dw_loc_opc)
1512 {
1513 case DW_OP_addr:
1514 size += DWARF2_ADDR_SIZE;
1515 break;
1516 case DW_OP_GNU_addr_index:
1517 case DW_OP_GNU_const_index:
1518 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1519 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1520 break;
1521 case DW_OP_const1u:
1522 case DW_OP_const1s:
1523 size += 1;
1524 break;
1525 case DW_OP_const2u:
1526 case DW_OP_const2s:
1527 size += 2;
1528 break;
1529 case DW_OP_const4u:
1530 case DW_OP_const4s:
1531 size += 4;
1532 break;
1533 case DW_OP_const8u:
1534 case DW_OP_const8s:
1535 size += 8;
1536 break;
1537 case DW_OP_constu:
1538 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1539 break;
1540 case DW_OP_consts:
1541 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1542 break;
1543 case DW_OP_pick:
1544 size += 1;
1545 break;
1546 case DW_OP_plus_uconst:
1547 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1548 break;
1549 case DW_OP_skip:
1550 case DW_OP_bra:
1551 size += 2;
1552 break;
1553 case DW_OP_breg0:
1554 case DW_OP_breg1:
1555 case DW_OP_breg2:
1556 case DW_OP_breg3:
1557 case DW_OP_breg4:
1558 case DW_OP_breg5:
1559 case DW_OP_breg6:
1560 case DW_OP_breg7:
1561 case DW_OP_breg8:
1562 case DW_OP_breg9:
1563 case DW_OP_breg10:
1564 case DW_OP_breg11:
1565 case DW_OP_breg12:
1566 case DW_OP_breg13:
1567 case DW_OP_breg14:
1568 case DW_OP_breg15:
1569 case DW_OP_breg16:
1570 case DW_OP_breg17:
1571 case DW_OP_breg18:
1572 case DW_OP_breg19:
1573 case DW_OP_breg20:
1574 case DW_OP_breg21:
1575 case DW_OP_breg22:
1576 case DW_OP_breg23:
1577 case DW_OP_breg24:
1578 case DW_OP_breg25:
1579 case DW_OP_breg26:
1580 case DW_OP_breg27:
1581 case DW_OP_breg28:
1582 case DW_OP_breg29:
1583 case DW_OP_breg30:
1584 case DW_OP_breg31:
1585 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1586 break;
1587 case DW_OP_regx:
1588 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1589 break;
1590 case DW_OP_fbreg:
1591 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1592 break;
1593 case DW_OP_bregx:
1594 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1595 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1596 break;
1597 case DW_OP_piece:
1598 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1599 break;
1600 case DW_OP_bit_piece:
1601 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1602 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1603 break;
1604 case DW_OP_deref_size:
1605 case DW_OP_xderef_size:
1606 size += 1;
1607 break;
1608 case DW_OP_call2:
1609 size += 2;
1610 break;
1611 case DW_OP_call4:
1612 size += 4;
1613 break;
1614 case DW_OP_call_ref:
1615 size += DWARF_REF_SIZE;
1616 break;
1617 case DW_OP_implicit_value:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1619 + loc->dw_loc_oprnd1.v.val_unsigned;
1620 break;
1621 case DW_OP_GNU_implicit_pointer:
1622 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1623 break;
1624 case DW_OP_GNU_entry_value:
1625 {
1626 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1627 size += size_of_uleb128 (op_size) + op_size;
1628 break;
1629 }
1630 case DW_OP_GNU_const_type:
1631 {
1632 unsigned long o
1633 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1634 size += size_of_uleb128 (o) + 1;
1635 switch (loc->dw_loc_oprnd2.val_class)
1636 {
1637 case dw_val_class_vec:
1638 size += loc->dw_loc_oprnd2.v.val_vec.length
1639 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1640 break;
1641 case dw_val_class_const:
1642 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1643 break;
1644 case dw_val_class_const_double:
1645 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1646 break;
1647 default:
1648 gcc_unreachable ();
1649 }
1650 break;
1651 }
1652 case DW_OP_GNU_regval_type:
1653 {
1654 unsigned long o
1655 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1656 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1657 + size_of_uleb128 (o);
1658 }
1659 break;
1660 case DW_OP_GNU_deref_type:
1661 {
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1664 size += 1 + size_of_uleb128 (o);
1665 }
1666 break;
1667 case DW_OP_GNU_convert:
1668 case DW_OP_GNU_reinterpret:
1669 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1670 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1671 else
1672 {
1673 unsigned long o
1674 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1675 size += size_of_uleb128 (o);
1676 }
1677 break;
1678 case DW_OP_GNU_parameter_ref:
1679 size += 4;
1680 break;
1681 default:
1682 break;
1683 }
1684
1685 return size;
1686 }
1687
1688 /* Return the size of a series of location descriptors. */
1689
1690 unsigned long
1691 size_of_locs (dw_loc_descr_ref loc)
1692 {
1693 dw_loc_descr_ref l;
1694 unsigned long size;
1695
1696 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1697 field, to avoid writing to a PCH file. */
1698 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1699 {
1700 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1701 break;
1702 size += size_of_loc_descr (l);
1703 }
1704 if (! l)
1705 return size;
1706
1707 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1708 {
1709 l->dw_loc_addr = size;
1710 size += size_of_loc_descr (l);
1711 }
1712
1713 return size;
1714 }
1715
1716 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1717 static void get_ref_die_offset_label (char *, dw_die_ref);
1718 static unsigned long int get_ref_die_offset (dw_die_ref);
1719
1720 /* Output location description stack opcode's operands (if any).
1721 The for_eh_or_skip parameter controls whether register numbers are
1722 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1723 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1724 info). This should be suppressed for the cases that have not been converted
1725 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1726
1727 static void
1728 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1729 {
1730 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1731 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1732
1733 switch (loc->dw_loc_opc)
1734 {
1735 #ifdef DWARF2_DEBUGGING_INFO
1736 case DW_OP_const2u:
1737 case DW_OP_const2s:
1738 dw2_asm_output_data (2, val1->v.val_int, NULL);
1739 break;
1740 case DW_OP_const4u:
1741 if (loc->dtprel)
1742 {
1743 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1744 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1745 val1->v.val_addr);
1746 fputc ('\n', asm_out_file);
1747 break;
1748 }
1749 /* FALLTHRU */
1750 case DW_OP_const4s:
1751 dw2_asm_output_data (4, val1->v.val_int, NULL);
1752 break;
1753 case DW_OP_const8u:
1754 if (loc->dtprel)
1755 {
1756 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1757 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1758 val1->v.val_addr);
1759 fputc ('\n', asm_out_file);
1760 break;
1761 }
1762 /* FALLTHRU */
1763 case DW_OP_const8s:
1764 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1765 dw2_asm_output_data (8, val1->v.val_int, NULL);
1766 break;
1767 case DW_OP_skip:
1768 case DW_OP_bra:
1769 {
1770 int offset;
1771
1772 gcc_assert (val1->val_class == dw_val_class_loc);
1773 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1774
1775 dw2_asm_output_data (2, offset, NULL);
1776 }
1777 break;
1778 case DW_OP_implicit_value:
1779 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1780 switch (val2->val_class)
1781 {
1782 case dw_val_class_const:
1783 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1784 break;
1785 case dw_val_class_vec:
1786 {
1787 unsigned int elt_size = val2->v.val_vec.elt_size;
1788 unsigned int len = val2->v.val_vec.length;
1789 unsigned int i;
1790 unsigned char *p;
1791
1792 if (elt_size > sizeof (HOST_WIDE_INT))
1793 {
1794 elt_size /= 2;
1795 len *= 2;
1796 }
1797 for (i = 0, p = val2->v.val_vec.array;
1798 i < len;
1799 i++, p += elt_size)
1800 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1801 "fp or vector constant word %u", i);
1802 }
1803 break;
1804 case dw_val_class_const_double:
1805 {
1806 unsigned HOST_WIDE_INT first, second;
1807
1808 if (WORDS_BIG_ENDIAN)
1809 {
1810 first = val2->v.val_double.high;
1811 second = val2->v.val_double.low;
1812 }
1813 else
1814 {
1815 first = val2->v.val_double.low;
1816 second = val2->v.val_double.high;
1817 }
1818 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1819 first, NULL);
1820 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1821 second, NULL);
1822 }
1823 break;
1824 case dw_val_class_addr:
1825 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1826 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1827 break;
1828 default:
1829 gcc_unreachable ();
1830 }
1831 break;
1832 #else
1833 case DW_OP_const2u:
1834 case DW_OP_const2s:
1835 case DW_OP_const4u:
1836 case DW_OP_const4s:
1837 case DW_OP_const8u:
1838 case DW_OP_const8s:
1839 case DW_OP_skip:
1840 case DW_OP_bra:
1841 case DW_OP_implicit_value:
1842 /* We currently don't make any attempt to make sure these are
1843 aligned properly like we do for the main unwind info, so
1844 don't support emitting things larger than a byte if we're
1845 only doing unwinding. */
1846 gcc_unreachable ();
1847 #endif
1848 case DW_OP_const1u:
1849 case DW_OP_const1s:
1850 dw2_asm_output_data (1, val1->v.val_int, NULL);
1851 break;
1852 case DW_OP_constu:
1853 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1854 break;
1855 case DW_OP_consts:
1856 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1857 break;
1858 case DW_OP_pick:
1859 dw2_asm_output_data (1, val1->v.val_int, NULL);
1860 break;
1861 case DW_OP_plus_uconst:
1862 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1863 break;
1864 case DW_OP_breg0:
1865 case DW_OP_breg1:
1866 case DW_OP_breg2:
1867 case DW_OP_breg3:
1868 case DW_OP_breg4:
1869 case DW_OP_breg5:
1870 case DW_OP_breg6:
1871 case DW_OP_breg7:
1872 case DW_OP_breg8:
1873 case DW_OP_breg9:
1874 case DW_OP_breg10:
1875 case DW_OP_breg11:
1876 case DW_OP_breg12:
1877 case DW_OP_breg13:
1878 case DW_OP_breg14:
1879 case DW_OP_breg15:
1880 case DW_OP_breg16:
1881 case DW_OP_breg17:
1882 case DW_OP_breg18:
1883 case DW_OP_breg19:
1884 case DW_OP_breg20:
1885 case DW_OP_breg21:
1886 case DW_OP_breg22:
1887 case DW_OP_breg23:
1888 case DW_OP_breg24:
1889 case DW_OP_breg25:
1890 case DW_OP_breg26:
1891 case DW_OP_breg27:
1892 case DW_OP_breg28:
1893 case DW_OP_breg29:
1894 case DW_OP_breg30:
1895 case DW_OP_breg31:
1896 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_regx:
1899 {
1900 unsigned r = val1->v.val_unsigned;
1901 if (for_eh_or_skip >= 0)
1902 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1903 gcc_assert (size_of_uleb128 (r)
1904 == size_of_uleb128 (val1->v.val_unsigned));
1905 dw2_asm_output_data_uleb128 (r, NULL);
1906 }
1907 break;
1908 case DW_OP_fbreg:
1909 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1910 break;
1911 case DW_OP_bregx:
1912 {
1913 unsigned r = val1->v.val_unsigned;
1914 if (for_eh_or_skip >= 0)
1915 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1916 gcc_assert (size_of_uleb128 (r)
1917 == size_of_uleb128 (val1->v.val_unsigned));
1918 dw2_asm_output_data_uleb128 (r, NULL);
1919 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1920 }
1921 break;
1922 case DW_OP_piece:
1923 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1924 break;
1925 case DW_OP_bit_piece:
1926 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1927 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_deref_size:
1930 case DW_OP_xderef_size:
1931 dw2_asm_output_data (1, val1->v.val_int, NULL);
1932 break;
1933
1934 case DW_OP_addr:
1935 if (loc->dtprel)
1936 {
1937 if (targetm.asm_out.output_dwarf_dtprel)
1938 {
1939 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1940 DWARF2_ADDR_SIZE,
1941 val1->v.val_addr);
1942 fputc ('\n', asm_out_file);
1943 }
1944 else
1945 gcc_unreachable ();
1946 }
1947 else
1948 {
1949 #ifdef DWARF2_DEBUGGING_INFO
1950 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1951 #else
1952 gcc_unreachable ();
1953 #endif
1954 }
1955 break;
1956
1957 case DW_OP_GNU_addr_index:
1958 case DW_OP_GNU_const_index:
1959 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1960 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1961 "(index into .debug_addr)");
1962 break;
1963
1964 case DW_OP_GNU_implicit_pointer:
1965 {
1966 char label[MAX_ARTIFICIAL_LABEL_BYTES
1967 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1968 gcc_assert (val1->val_class == dw_val_class_die_ref);
1969 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1970 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1972 }
1973 break;
1974
1975 case DW_OP_GNU_entry_value:
1976 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1977 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1978 break;
1979
1980 case DW_OP_GNU_const_type:
1981 {
1982 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1983 gcc_assert (o);
1984 dw2_asm_output_data_uleb128 (o, NULL);
1985 switch (val2->val_class)
1986 {
1987 case dw_val_class_const:
1988 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1989 dw2_asm_output_data (1, l, NULL);
1990 dw2_asm_output_data (l, val2->v.val_int, NULL);
1991 break;
1992 case dw_val_class_vec:
1993 {
1994 unsigned int elt_size = val2->v.val_vec.elt_size;
1995 unsigned int len = val2->v.val_vec.length;
1996 unsigned int i;
1997 unsigned char *p;
1998
1999 l = len * elt_size;
2000 dw2_asm_output_data (1, l, NULL);
2001 if (elt_size > sizeof (HOST_WIDE_INT))
2002 {
2003 elt_size /= 2;
2004 len *= 2;
2005 }
2006 for (i = 0, p = val2->v.val_vec.array;
2007 i < len;
2008 i++, p += elt_size)
2009 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2010 "fp or vector constant word %u", i);
2011 }
2012 break;
2013 case dw_val_class_const_double:
2014 {
2015 unsigned HOST_WIDE_INT first, second;
2016 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2017
2018 dw2_asm_output_data (1, 2 * l, NULL);
2019 if (WORDS_BIG_ENDIAN)
2020 {
2021 first = val2->v.val_double.high;
2022 second = val2->v.val_double.low;
2023 }
2024 else
2025 {
2026 first = val2->v.val_double.low;
2027 second = val2->v.val_double.high;
2028 }
2029 dw2_asm_output_data (l, first, NULL);
2030 dw2_asm_output_data (l, second, NULL);
2031 }
2032 break;
2033 default:
2034 gcc_unreachable ();
2035 }
2036 }
2037 break;
2038 case DW_OP_GNU_regval_type:
2039 {
2040 unsigned r = val1->v.val_unsigned;
2041 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2042 gcc_assert (o);
2043 if (for_eh_or_skip >= 0)
2044 {
2045 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2046 gcc_assert (size_of_uleb128 (r)
2047 == size_of_uleb128 (val1->v.val_unsigned));
2048 }
2049 dw2_asm_output_data_uleb128 (r, NULL);
2050 dw2_asm_output_data_uleb128 (o, NULL);
2051 }
2052 break;
2053 case DW_OP_GNU_deref_type:
2054 {
2055 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2056 gcc_assert (o);
2057 dw2_asm_output_data (1, val1->v.val_int, NULL);
2058 dw2_asm_output_data_uleb128 (o, NULL);
2059 }
2060 break;
2061 case DW_OP_GNU_convert:
2062 case DW_OP_GNU_reinterpret:
2063 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2064 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2065 else
2066 {
2067 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2068 gcc_assert (o);
2069 dw2_asm_output_data_uleb128 (o, NULL);
2070 }
2071 break;
2072
2073 case DW_OP_GNU_parameter_ref:
2074 {
2075 unsigned long o;
2076 gcc_assert (val1->val_class == dw_val_class_die_ref);
2077 o = get_ref_die_offset (val1->v.val_die_ref.die);
2078 dw2_asm_output_data (4, o, NULL);
2079 }
2080 break;
2081
2082 default:
2083 /* Other codes have no operands. */
2084 break;
2085 }
2086 }
2087
2088 /* Output a sequence of location operations.
2089 The for_eh_or_skip parameter controls whether register numbers are
2090 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2091 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2092 info). This should be suppressed for the cases that have not been converted
2093 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2094
2095 void
2096 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2097 {
2098 for (; loc != NULL; loc = loc->dw_loc_next)
2099 {
2100 enum dwarf_location_atom opc = loc->dw_loc_opc;
2101 /* Output the opcode. */
2102 if (for_eh_or_skip >= 0
2103 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2104 {
2105 unsigned r = (opc - DW_OP_breg0);
2106 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2107 gcc_assert (r <= 31);
2108 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2109 }
2110 else if (for_eh_or_skip >= 0
2111 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2112 {
2113 unsigned r = (opc - DW_OP_reg0);
2114 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2115 gcc_assert (r <= 31);
2116 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2117 }
2118
2119 dw2_asm_output_data (1, opc,
2120 "%s", dwarf_stack_op_name (opc));
2121
2122 /* Output the operand(s) (if any). */
2123 output_loc_operands (loc, for_eh_or_skip);
2124 }
2125 }
2126
2127 /* Output location description stack opcode's operands (if any).
2128 The output is single bytes on a line, suitable for .cfi_escape. */
2129
2130 static void
2131 output_loc_operands_raw (dw_loc_descr_ref loc)
2132 {
2133 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2134 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2135
2136 switch (loc->dw_loc_opc)
2137 {
2138 case DW_OP_addr:
2139 case DW_OP_GNU_addr_index:
2140 case DW_OP_GNU_const_index:
2141 case DW_OP_implicit_value:
2142 /* We cannot output addresses in .cfi_escape, only bytes. */
2143 gcc_unreachable ();
2144
2145 case DW_OP_const1u:
2146 case DW_OP_const1s:
2147 case DW_OP_pick:
2148 case DW_OP_deref_size:
2149 case DW_OP_xderef_size:
2150 fputc (',', asm_out_file);
2151 dw2_asm_output_data_raw (1, val1->v.val_int);
2152 break;
2153
2154 case DW_OP_const2u:
2155 case DW_OP_const2s:
2156 fputc (',', asm_out_file);
2157 dw2_asm_output_data_raw (2, val1->v.val_int);
2158 break;
2159
2160 case DW_OP_const4u:
2161 case DW_OP_const4s:
2162 fputc (',', asm_out_file);
2163 dw2_asm_output_data_raw (4, val1->v.val_int);
2164 break;
2165
2166 case DW_OP_const8u:
2167 case DW_OP_const8s:
2168 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2169 fputc (',', asm_out_file);
2170 dw2_asm_output_data_raw (8, val1->v.val_int);
2171 break;
2172
2173 case DW_OP_skip:
2174 case DW_OP_bra:
2175 {
2176 int offset;
2177
2178 gcc_assert (val1->val_class == dw_val_class_loc);
2179 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2180
2181 fputc (',', asm_out_file);
2182 dw2_asm_output_data_raw (2, offset);
2183 }
2184 break;
2185
2186 case DW_OP_regx:
2187 {
2188 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2189 gcc_assert (size_of_uleb128 (r)
2190 == size_of_uleb128 (val1->v.val_unsigned));
2191 fputc (',', asm_out_file);
2192 dw2_asm_output_data_uleb128_raw (r);
2193 }
2194 break;
2195
2196 case DW_OP_constu:
2197 case DW_OP_plus_uconst:
2198 case DW_OP_piece:
2199 fputc (',', asm_out_file);
2200 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2201 break;
2202
2203 case DW_OP_bit_piece:
2204 fputc (',', asm_out_file);
2205 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2206 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2207 break;
2208
2209 case DW_OP_consts:
2210 case DW_OP_breg0:
2211 case DW_OP_breg1:
2212 case DW_OP_breg2:
2213 case DW_OP_breg3:
2214 case DW_OP_breg4:
2215 case DW_OP_breg5:
2216 case DW_OP_breg6:
2217 case DW_OP_breg7:
2218 case DW_OP_breg8:
2219 case DW_OP_breg9:
2220 case DW_OP_breg10:
2221 case DW_OP_breg11:
2222 case DW_OP_breg12:
2223 case DW_OP_breg13:
2224 case DW_OP_breg14:
2225 case DW_OP_breg15:
2226 case DW_OP_breg16:
2227 case DW_OP_breg17:
2228 case DW_OP_breg18:
2229 case DW_OP_breg19:
2230 case DW_OP_breg20:
2231 case DW_OP_breg21:
2232 case DW_OP_breg22:
2233 case DW_OP_breg23:
2234 case DW_OP_breg24:
2235 case DW_OP_breg25:
2236 case DW_OP_breg26:
2237 case DW_OP_breg27:
2238 case DW_OP_breg28:
2239 case DW_OP_breg29:
2240 case DW_OP_breg30:
2241 case DW_OP_breg31:
2242 case DW_OP_fbreg:
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2245 break;
2246
2247 case DW_OP_bregx:
2248 {
2249 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2250 gcc_assert (size_of_uleb128 (r)
2251 == size_of_uleb128 (val1->v.val_unsigned));
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_uleb128_raw (r);
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2256 }
2257 break;
2258
2259 case DW_OP_GNU_implicit_pointer:
2260 case DW_OP_GNU_entry_value:
2261 case DW_OP_GNU_const_type:
2262 case DW_OP_GNU_regval_type:
2263 case DW_OP_GNU_deref_type:
2264 case DW_OP_GNU_convert:
2265 case DW_OP_GNU_reinterpret:
2266 case DW_OP_GNU_parameter_ref:
2267 gcc_unreachable ();
2268 break;
2269
2270 default:
2271 /* Other codes have no operands. */
2272 break;
2273 }
2274 }
2275
2276 void
2277 output_loc_sequence_raw (dw_loc_descr_ref loc)
2278 {
2279 while (1)
2280 {
2281 enum dwarf_location_atom opc = loc->dw_loc_opc;
2282 /* Output the opcode. */
2283 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2284 {
2285 unsigned r = (opc - DW_OP_breg0);
2286 r = DWARF2_FRAME_REG_OUT (r, 1);
2287 gcc_assert (r <= 31);
2288 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2289 }
2290 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2291 {
2292 unsigned r = (opc - DW_OP_reg0);
2293 r = DWARF2_FRAME_REG_OUT (r, 1);
2294 gcc_assert (r <= 31);
2295 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2296 }
2297 /* Output the opcode. */
2298 fprintf (asm_out_file, "%#x", opc);
2299 output_loc_operands_raw (loc);
2300
2301 if (!loc->dw_loc_next)
2302 break;
2303 loc = loc->dw_loc_next;
2304
2305 fputc (',', asm_out_file);
2306 }
2307 }
2308
2309 /* This function builds a dwarf location descriptor sequence from a
2310 dw_cfa_location, adding the given OFFSET to the result of the
2311 expression. */
2312
2313 struct dw_loc_descr_struct *
2314 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2315 {
2316 struct dw_loc_descr_struct *head, *tmp;
2317
2318 offset += cfa->offset;
2319
2320 if (cfa->indirect)
2321 {
2322 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2323 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2324 head->dw_loc_oprnd1.val_entry = NULL;
2325 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2326 add_loc_descr (&head, tmp);
2327 if (offset != 0)
2328 {
2329 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2330 add_loc_descr (&head, tmp);
2331 }
2332 }
2333 else
2334 head = new_reg_loc_descr (cfa->reg, offset);
2335
2336 return head;
2337 }
2338
2339 /* This function builds a dwarf location descriptor sequence for
2340 the address at OFFSET from the CFA when stack is aligned to
2341 ALIGNMENT byte. */
2342
2343 struct dw_loc_descr_struct *
2344 build_cfa_aligned_loc (dw_cfa_location *cfa,
2345 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2346 {
2347 struct dw_loc_descr_struct *head;
2348 unsigned int dwarf_fp
2349 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2350
2351 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2352 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2353 {
2354 head = new_reg_loc_descr (dwarf_fp, 0);
2355 add_loc_descr (&head, int_loc_descriptor (alignment));
2356 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2357 loc_descr_plus_const (&head, offset);
2358 }
2359 else
2360 head = new_reg_loc_descr (dwarf_fp, offset);
2361 return head;
2362 }
2363 \f
2364 /* And now, the support for symbolic debugging information. */
2365
2366 /* .debug_str support. */
2367 static int output_indirect_string (void **, void *);
2368
2369 static void dwarf2out_init (const char *);
2370 static void dwarf2out_finish (const char *);
2371 static void dwarf2out_assembly_start (void);
2372 static void dwarf2out_define (unsigned int, const char *);
2373 static void dwarf2out_undef (unsigned int, const char *);
2374 static void dwarf2out_start_source_file (unsigned, const char *);
2375 static void dwarf2out_end_source_file (unsigned);
2376 static void dwarf2out_function_decl (tree);
2377 static void dwarf2out_begin_block (unsigned, unsigned);
2378 static void dwarf2out_end_block (unsigned, unsigned);
2379 static bool dwarf2out_ignore_block (const_tree);
2380 static void dwarf2out_global_decl (tree);
2381 static void dwarf2out_type_decl (tree, int);
2382 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2383 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2384 dw_die_ref);
2385 static void dwarf2out_abstract_function (tree);
2386 static void dwarf2out_var_location (rtx);
2387 static void dwarf2out_begin_function (tree);
2388 static void dwarf2out_end_function (unsigned int);
2389 static void dwarf2out_set_name (tree, tree);
2390
2391 /* The debug hooks structure. */
2392
2393 const struct gcc_debug_hooks dwarf2_debug_hooks =
2394 {
2395 dwarf2out_init,
2396 dwarf2out_finish,
2397 dwarf2out_assembly_start,
2398 dwarf2out_define,
2399 dwarf2out_undef,
2400 dwarf2out_start_source_file,
2401 dwarf2out_end_source_file,
2402 dwarf2out_begin_block,
2403 dwarf2out_end_block,
2404 dwarf2out_ignore_block,
2405 dwarf2out_source_line,
2406 dwarf2out_begin_prologue,
2407 #if VMS_DEBUGGING_INFO
2408 dwarf2out_vms_end_prologue,
2409 dwarf2out_vms_begin_epilogue,
2410 #else
2411 debug_nothing_int_charstar,
2412 debug_nothing_int_charstar,
2413 #endif
2414 dwarf2out_end_epilogue,
2415 dwarf2out_begin_function,
2416 dwarf2out_end_function, /* end_function */
2417 dwarf2out_function_decl, /* function_decl */
2418 dwarf2out_global_decl,
2419 dwarf2out_type_decl, /* type_decl */
2420 dwarf2out_imported_module_or_decl,
2421 debug_nothing_tree, /* deferred_inline_function */
2422 /* The DWARF 2 backend tries to reduce debugging bloat by not
2423 emitting the abstract description of inline functions until
2424 something tries to reference them. */
2425 dwarf2out_abstract_function, /* outlining_inline_function */
2426 debug_nothing_rtx, /* label */
2427 debug_nothing_int, /* handle_pch */
2428 dwarf2out_var_location,
2429 dwarf2out_switch_text_section,
2430 dwarf2out_set_name,
2431 1, /* start_end_main_source_file */
2432 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2433 };
2434 \f
2435 /* NOTE: In the comments in this file, many references are made to
2436 "Debugging Information Entries". This term is abbreviated as `DIE'
2437 throughout the remainder of this file. */
2438
2439 /* An internal representation of the DWARF output is built, and then
2440 walked to generate the DWARF debugging info. The walk of the internal
2441 representation is done after the entire program has been compiled.
2442 The types below are used to describe the internal representation. */
2443
2444 /* Whether to put type DIEs into their own section .debug_types instead
2445 of making them part of the .debug_info section. Only supported for
2446 Dwarf V4 or higher and the user didn't disable them through
2447 -fno-debug-types-section. It is more efficient to put them in a
2448 separate comdat sections since the linker will then be able to
2449 remove duplicates. But not all tools support .debug_types sections
2450 yet. */
2451
2452 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2453
2454 /* Various DIE's use offsets relative to the beginning of the
2455 .debug_info section to refer to each other. */
2456
2457 typedef long int dw_offset;
2458
2459 /* Define typedefs here to avoid circular dependencies. */
2460
2461 typedef struct dw_attr_struct *dw_attr_ref;
2462 typedef struct dw_line_info_struct *dw_line_info_ref;
2463 typedef struct pubname_struct *pubname_ref;
2464 typedef struct dw_ranges_struct *dw_ranges_ref;
2465 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2466 typedef struct comdat_type_struct *comdat_type_node_ref;
2467
2468 /* The entries in the line_info table more-or-less mirror the opcodes
2469 that are used in the real dwarf line table. Arrays of these entries
2470 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2471 supported. */
2472
2473 enum dw_line_info_opcode {
2474 /* Emit DW_LNE_set_address; the operand is the label index. */
2475 LI_set_address,
2476
2477 /* Emit a row to the matrix with the given line. This may be done
2478 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2479 special opcodes. */
2480 LI_set_line,
2481
2482 /* Emit a DW_LNS_set_file. */
2483 LI_set_file,
2484
2485 /* Emit a DW_LNS_set_column. */
2486 LI_set_column,
2487
2488 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2489 LI_negate_stmt,
2490
2491 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2492 LI_set_prologue_end,
2493 LI_set_epilogue_begin,
2494
2495 /* Emit a DW_LNE_set_discriminator. */
2496 LI_set_discriminator
2497 };
2498
2499 typedef struct GTY(()) dw_line_info_struct {
2500 enum dw_line_info_opcode opcode;
2501 unsigned int val;
2502 } dw_line_info_entry;
2503
2504
2505 typedef struct GTY(()) dw_line_info_table_struct {
2506 /* The label that marks the end of this section. */
2507 const char *end_label;
2508
2509 /* The values for the last row of the matrix, as collected in the table.
2510 These are used to minimize the changes to the next row. */
2511 unsigned int file_num;
2512 unsigned int line_num;
2513 unsigned int column_num;
2514 int discrim_num;
2515 bool is_stmt;
2516 bool in_use;
2517
2518 vec<dw_line_info_entry, va_gc> *entries;
2519 } dw_line_info_table;
2520
2521 typedef dw_line_info_table *dw_line_info_table_p;
2522
2523
2524 /* Each DIE attribute has a field specifying the attribute kind,
2525 a link to the next attribute in the chain, and an attribute value.
2526 Attributes are typically linked below the DIE they modify. */
2527
2528 typedef struct GTY(()) dw_attr_struct {
2529 enum dwarf_attribute dw_attr;
2530 dw_val_node dw_attr_val;
2531 }
2532 dw_attr_node;
2533
2534
2535 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2536 The children of each node form a circular list linked by
2537 die_sib. die_child points to the node *before* the "first" child node. */
2538
2539 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2540 union die_symbol_or_type_node
2541 {
2542 const char * GTY ((tag ("0"))) die_symbol;
2543 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2544 }
2545 GTY ((desc ("%0.comdat_type_p"))) die_id;
2546 vec<dw_attr_node, va_gc> *die_attr;
2547 dw_die_ref die_parent;
2548 dw_die_ref die_child;
2549 dw_die_ref die_sib;
2550 dw_die_ref die_definition; /* ref from a specification to its definition */
2551 dw_offset die_offset;
2552 unsigned long die_abbrev;
2553 int die_mark;
2554 unsigned int decl_id;
2555 enum dwarf_tag die_tag;
2556 /* Die is used and must not be pruned as unused. */
2557 BOOL_BITFIELD die_perennial_p : 1;
2558 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2559 /* Lots of spare bits. */
2560 }
2561 die_node;
2562
2563 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2564 #define FOR_EACH_CHILD(die, c, expr) do { \
2565 c = die->die_child; \
2566 if (c) do { \
2567 c = c->die_sib; \
2568 expr; \
2569 } while (c != die->die_child); \
2570 } while (0)
2571
2572 /* The pubname structure */
2573
2574 typedef struct GTY(()) pubname_struct {
2575 dw_die_ref die;
2576 const char *name;
2577 }
2578 pubname_entry;
2579
2580
2581 struct GTY(()) dw_ranges_struct {
2582 /* If this is positive, it's a block number, otherwise it's a
2583 bitwise-negated index into dw_ranges_by_label. */
2584 int num;
2585 };
2586
2587 /* A structure to hold a macinfo entry. */
2588
2589 typedef struct GTY(()) macinfo_struct {
2590 unsigned char code;
2591 unsigned HOST_WIDE_INT lineno;
2592 const char *info;
2593 }
2594 macinfo_entry;
2595
2596
2597 struct GTY(()) dw_ranges_by_label_struct {
2598 const char *begin;
2599 const char *end;
2600 };
2601
2602 /* The comdat type node structure. */
2603 typedef struct GTY(()) comdat_type_struct
2604 {
2605 dw_die_ref root_die;
2606 dw_die_ref type_die;
2607 dw_die_ref skeleton_die;
2608 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2609 struct comdat_type_struct *next;
2610 }
2611 comdat_type_node;
2612
2613 /* The limbo die list structure. */
2614 typedef struct GTY(()) limbo_die_struct {
2615 dw_die_ref die;
2616 tree created_for;
2617 struct limbo_die_struct *next;
2618 }
2619 limbo_die_node;
2620
2621 typedef struct skeleton_chain_struct
2622 {
2623 dw_die_ref old_die;
2624 dw_die_ref new_die;
2625 struct skeleton_chain_struct *parent;
2626 }
2627 skeleton_chain_node;
2628
2629 /* Define a macro which returns nonzero for a TYPE_DECL which was
2630 implicitly generated for a type.
2631
2632 Note that, unlike the C front-end (which generates a NULL named
2633 TYPE_DECL node for each complete tagged type, each array type,
2634 and each function type node created) the C++ front-end generates
2635 a _named_ TYPE_DECL node for each tagged type node created.
2636 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2637 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2638 front-end, but for each type, tagged or not. */
2639
2640 #define TYPE_DECL_IS_STUB(decl) \
2641 (DECL_NAME (decl) == NULL_TREE \
2642 || (DECL_ARTIFICIAL (decl) \
2643 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2644 /* This is necessary for stub decls that \
2645 appear in nested inline functions. */ \
2646 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2647 && (decl_ultimate_origin (decl) \
2648 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2649
2650 /* Information concerning the compilation unit's programming
2651 language, and compiler version. */
2652
2653 /* Fixed size portion of the DWARF compilation unit header. */
2654 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2655 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2656
2657 /* Fixed size portion of the DWARF comdat type unit header. */
2658 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2659 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2660 + DWARF_OFFSET_SIZE)
2661
2662 /* Fixed size portion of public names info. */
2663 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2664
2665 /* Fixed size portion of the address range info. */
2666 #define DWARF_ARANGES_HEADER_SIZE \
2667 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2668 DWARF2_ADDR_SIZE * 2) \
2669 - DWARF_INITIAL_LENGTH_SIZE)
2670
2671 /* Size of padding portion in the address range info. It must be
2672 aligned to twice the pointer size. */
2673 #define DWARF_ARANGES_PAD_SIZE \
2674 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2675 DWARF2_ADDR_SIZE * 2) \
2676 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2677
2678 /* Use assembler line directives if available. */
2679 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2680 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2681 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2682 #else
2683 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2684 #endif
2685 #endif
2686
2687 /* Minimum line offset in a special line info. opcode.
2688 This value was chosen to give a reasonable range of values. */
2689 #define DWARF_LINE_BASE -10
2690
2691 /* First special line opcode - leave room for the standard opcodes. */
2692 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2693
2694 /* Range of line offsets in a special line info. opcode. */
2695 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2696
2697 /* Flag that indicates the initial value of the is_stmt_start flag.
2698 In the present implementation, we do not mark any lines as
2699 the beginning of a source statement, because that information
2700 is not made available by the GCC front-end. */
2701 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2702
2703 /* Maximum number of operations per instruction bundle. */
2704 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2705 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2706 #endif
2707
2708 /* This location is used by calc_die_sizes() to keep track
2709 the offset of each DIE within the .debug_info section. */
2710 static unsigned long next_die_offset;
2711
2712 /* Record the root of the DIE's built for the current compilation unit. */
2713 static GTY(()) dw_die_ref single_comp_unit_die;
2714
2715 /* A list of type DIEs that have been separated into comdat sections. */
2716 static GTY(()) comdat_type_node *comdat_type_list;
2717
2718 /* A list of DIEs with a NULL parent waiting to be relocated. */
2719 static GTY(()) limbo_die_node *limbo_die_list;
2720
2721 /* A list of DIEs for which we may have to generate
2722 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2723 static GTY(()) limbo_die_node *deferred_asm_name;
2724
2725 /* Filenames referenced by this compilation unit. */
2726 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2727
2728 /* A hash table of references to DIE's that describe declarations.
2729 The key is a DECL_UID() which is a unique number identifying each decl. */
2730 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2731
2732 /* A hash table of references to DIE's that describe COMMON blocks.
2733 The key is DECL_UID() ^ die_parent. */
2734 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2735
2736 typedef struct GTY(()) die_arg_entry_struct {
2737 dw_die_ref die;
2738 tree arg;
2739 } die_arg_entry;
2740
2741
2742 /* Node of the variable location list. */
2743 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2744 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2745 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2746 in mode of the EXPR_LIST node and first EXPR_LIST operand
2747 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2748 location or NULL for padding. For larger bitsizes,
2749 mode is 0 and first operand is a CONCAT with bitsize
2750 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2751 NULL as second operand. */
2752 rtx GTY (()) loc;
2753 const char * GTY (()) label;
2754 struct var_loc_node * GTY (()) next;
2755 };
2756
2757 /* Variable location list. */
2758 struct GTY (()) var_loc_list_def {
2759 struct var_loc_node * GTY (()) first;
2760
2761 /* Pointer to the last but one or last element of the
2762 chained list. If the list is empty, both first and
2763 last are NULL, if the list contains just one node
2764 or the last node certainly is not redundant, it points
2765 to the last node, otherwise points to the last but one.
2766 Do not mark it for GC because it is marked through the chain. */
2767 struct var_loc_node * GTY ((skip ("%h"))) last;
2768
2769 /* Pointer to the last element before section switch,
2770 if NULL, either sections weren't switched or first
2771 is after section switch. */
2772 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2773
2774 /* DECL_UID of the variable decl. */
2775 unsigned int decl_id;
2776 };
2777 typedef struct var_loc_list_def var_loc_list;
2778
2779 /* Call argument location list. */
2780 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2781 rtx GTY (()) call_arg_loc_note;
2782 const char * GTY (()) label;
2783 tree GTY (()) block;
2784 bool tail_call_p;
2785 rtx GTY (()) symbol_ref;
2786 struct call_arg_loc_node * GTY (()) next;
2787 };
2788
2789
2790 /* Table of decl location linked lists. */
2791 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2792
2793 /* Head and tail of call_arg_loc chain. */
2794 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2795 static struct call_arg_loc_node *call_arg_loc_last;
2796
2797 /* Number of call sites in the current function. */
2798 static int call_site_count = -1;
2799 /* Number of tail call sites in the current function. */
2800 static int tail_call_site_count = -1;
2801
2802 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2803 DIEs. */
2804 static vec<dw_die_ref> block_map;
2805
2806 /* A cached location list. */
2807 struct GTY (()) cached_dw_loc_list_def {
2808 /* The DECL_UID of the decl that this entry describes. */
2809 unsigned int decl_id;
2810
2811 /* The cached location list. */
2812 dw_loc_list_ref loc_list;
2813 };
2814 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2815
2816 /* Table of cached location lists. */
2817 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2818
2819 /* A pointer to the base of a list of references to DIE's that
2820 are uniquely identified by their tag, presence/absence of
2821 children DIE's, and list of attribute/value pairs. */
2822 static GTY((length ("abbrev_die_table_allocated")))
2823 dw_die_ref *abbrev_die_table;
2824
2825 /* Number of elements currently allocated for abbrev_die_table. */
2826 static GTY(()) unsigned abbrev_die_table_allocated;
2827
2828 /* Number of elements in type_die_table currently in use. */
2829 static GTY(()) unsigned abbrev_die_table_in_use;
2830
2831 /* Size (in elements) of increments by which we may expand the
2832 abbrev_die_table. */
2833 #define ABBREV_DIE_TABLE_INCREMENT 256
2834
2835 /* A global counter for generating labels for line number data. */
2836 static unsigned int line_info_label_num;
2837
2838 /* The current table to which we should emit line number information
2839 for the current function. This will be set up at the beginning of
2840 assembly for the function. */
2841 static dw_line_info_table *cur_line_info_table;
2842
2843 /* The two default tables of line number info. */
2844 static GTY(()) dw_line_info_table *text_section_line_info;
2845 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2846
2847 /* The set of all non-default tables of line number info. */
2848 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2849
2850 /* A flag to tell pubnames/types export if there is an info section to
2851 refer to. */
2852 static bool info_section_emitted;
2853
2854 /* A pointer to the base of a table that contains a list of publicly
2855 accessible names. */
2856 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2857
2858 /* A pointer to the base of a table that contains a list of publicly
2859 accessible types. */
2860 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2861
2862 /* A pointer to the base of a table that contains a list of macro
2863 defines/undefines (and file start/end markers). */
2864 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2865
2866 /* True if .debug_macinfo or .debug_macros section is going to be
2867 emitted. */
2868 #define have_macinfo \
2869 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2870 && !macinfo_table->is_empty ())
2871
2872 /* Array of dies for which we should generate .debug_ranges info. */
2873 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2874
2875 /* Number of elements currently allocated for ranges_table. */
2876 static GTY(()) unsigned ranges_table_allocated;
2877
2878 /* Number of elements in ranges_table currently in use. */
2879 static GTY(()) unsigned ranges_table_in_use;
2880
2881 /* Array of pairs of labels referenced in ranges_table. */
2882 static GTY ((length ("ranges_by_label_allocated")))
2883 dw_ranges_by_label_ref ranges_by_label;
2884
2885 /* Number of elements currently allocated for ranges_by_label. */
2886 static GTY(()) unsigned ranges_by_label_allocated;
2887
2888 /* Number of elements in ranges_by_label currently in use. */
2889 static GTY(()) unsigned ranges_by_label_in_use;
2890
2891 /* Size (in elements) of increments by which we may expand the
2892 ranges_table. */
2893 #define RANGES_TABLE_INCREMENT 64
2894
2895 /* Whether we have location lists that need outputting */
2896 static GTY(()) bool have_location_lists;
2897
2898 /* Unique label counter. */
2899 static GTY(()) unsigned int loclabel_num;
2900
2901 /* Unique label counter for point-of-call tables. */
2902 static GTY(()) unsigned int poc_label_num;
2903
2904 /* Record whether the function being analyzed contains inlined functions. */
2905 static int current_function_has_inlines;
2906
2907 /* The last file entry emitted by maybe_emit_file(). */
2908 static GTY(()) struct dwarf_file_data * last_emitted_file;
2909
2910 /* Number of internal labels generated by gen_internal_sym(). */
2911 static GTY(()) int label_num;
2912
2913 /* Cached result of previous call to lookup_filename. */
2914 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2915
2916 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2917
2918 /* Instances of generic types for which we need to generate debug
2919 info that describe their generic parameters and arguments. That
2920 generation needs to happen once all types are properly laid out so
2921 we do it at the end of compilation. */
2922 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2923
2924 /* Offset from the "steady-state frame pointer" to the frame base,
2925 within the current function. */
2926 static HOST_WIDE_INT frame_pointer_fb_offset;
2927 static bool frame_pointer_fb_offset_valid;
2928
2929 static vec<dw_die_ref> base_types;
2930
2931 /* Forward declarations for functions defined in this file. */
2932
2933 static int is_pseudo_reg (const_rtx);
2934 static tree type_main_variant (tree);
2935 static int is_tagged_type (const_tree);
2936 static const char *dwarf_tag_name (unsigned);
2937 static const char *dwarf_attr_name (unsigned);
2938 static const char *dwarf_form_name (unsigned);
2939 static tree decl_ultimate_origin (const_tree);
2940 static tree decl_class_context (tree);
2941 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2942 static inline enum dw_val_class AT_class (dw_attr_ref);
2943 static inline unsigned int AT_index (dw_attr_ref);
2944 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2945 static inline unsigned AT_flag (dw_attr_ref);
2946 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2947 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2948 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2949 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2950 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2951 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2952 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2953 unsigned int, unsigned char *);
2954 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2955 static hashval_t debug_str_do_hash (const void *);
2956 static int debug_str_eq (const void *, const void *);
2957 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2958 static inline const char *AT_string (dw_attr_ref);
2959 static enum dwarf_form AT_string_form (dw_attr_ref);
2960 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2961 static void add_AT_specification (dw_die_ref, dw_die_ref);
2962 static inline dw_die_ref AT_ref (dw_attr_ref);
2963 static inline int AT_ref_external (dw_attr_ref);
2964 static inline void set_AT_ref_external (dw_attr_ref, int);
2965 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2966 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2967 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2968 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2969 dw_loc_list_ref);
2970 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2971 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2972 static void remove_addr_table_entry (addr_table_entry *);
2973 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2974 static inline rtx AT_addr (dw_attr_ref);
2975 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2976 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2977 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2978 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2979 unsigned HOST_WIDE_INT);
2980 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2981 unsigned long, bool);
2982 static inline const char *AT_lbl (dw_attr_ref);
2983 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2984 static const char *get_AT_low_pc (dw_die_ref);
2985 static const char *get_AT_hi_pc (dw_die_ref);
2986 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2987 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2988 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2989 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2990 static bool is_cxx (void);
2991 static bool is_fortran (void);
2992 static bool is_ada (void);
2993 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2994 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2995 static void add_child_die (dw_die_ref, dw_die_ref);
2996 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2997 static dw_die_ref lookup_type_die (tree);
2998 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2999 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3000 static void equate_type_number_to_die (tree, dw_die_ref);
3001 static hashval_t decl_die_table_hash (const void *);
3002 static int decl_die_table_eq (const void *, const void *);
3003 static dw_die_ref lookup_decl_die (tree);
3004 static hashval_t common_block_die_table_hash (const void *);
3005 static int common_block_die_table_eq (const void *, const void *);
3006 static hashval_t decl_loc_table_hash (const void *);
3007 static int decl_loc_table_eq (const void *, const void *);
3008 static var_loc_list *lookup_decl_loc (const_tree);
3009 static void equate_decl_number_to_die (tree, dw_die_ref);
3010 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3011 static void print_spaces (FILE *);
3012 static void print_die (dw_die_ref, FILE *);
3013 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3014 static dw_die_ref pop_compile_unit (dw_die_ref);
3015 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3016 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3017 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3018 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3019 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3020 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3021 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3022 struct md5_ctx *, int *);
3023 struct checksum_attributes;
3024 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3025 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3026 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3027 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3028 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3029 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3030 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3031 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3032 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3033 static void compute_section_prefix (dw_die_ref);
3034 static int is_type_die (dw_die_ref);
3035 static int is_comdat_die (dw_die_ref);
3036 static int is_symbol_die (dw_die_ref);
3037 static inline bool is_template_instantiation (dw_die_ref);
3038 static void assign_symbol_names (dw_die_ref);
3039 static void break_out_includes (dw_die_ref);
3040 static int is_declaration_die (dw_die_ref);
3041 static int should_move_die_to_comdat (dw_die_ref);
3042 static dw_die_ref clone_as_declaration (dw_die_ref);
3043 static dw_die_ref clone_die (dw_die_ref);
3044 static dw_die_ref clone_tree (dw_die_ref);
3045 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3046 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3047 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3048 static dw_die_ref generate_skeleton (dw_die_ref);
3049 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3050 dw_die_ref,
3051 dw_die_ref);
3052 static void break_out_comdat_types (dw_die_ref);
3053 static void copy_decls_for_unworthy_types (dw_die_ref);
3054
3055 static void add_sibling_attributes (dw_die_ref);
3056 static void output_location_lists (dw_die_ref);
3057 static int constant_size (unsigned HOST_WIDE_INT);
3058 static unsigned long size_of_die (dw_die_ref);
3059 static void calc_die_sizes (dw_die_ref);
3060 static void calc_base_type_die_sizes (void);
3061 static void mark_dies (dw_die_ref);
3062 static void unmark_dies (dw_die_ref);
3063 static void unmark_all_dies (dw_die_ref);
3064 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3065 static unsigned long size_of_aranges (void);
3066 static enum dwarf_form value_format (dw_attr_ref);
3067 static void output_value_format (dw_attr_ref);
3068 static void output_abbrev_section (void);
3069 static void output_die_abbrevs (unsigned long, dw_die_ref);
3070 static void output_die_symbol (dw_die_ref);
3071 static void output_die (dw_die_ref);
3072 static void output_compilation_unit_header (void);
3073 static void output_comp_unit (dw_die_ref, int);
3074 static void output_comdat_type_unit (comdat_type_node *);
3075 static const char *dwarf2_name (tree, int);
3076 static void add_pubname (tree, dw_die_ref);
3077 static void add_enumerator_pubname (const char *, dw_die_ref);
3078 static void add_pubname_string (const char *, dw_die_ref);
3079 static void add_pubtype (tree, dw_die_ref);
3080 static void output_pubnames (vec<pubname_entry, va_gc> *);
3081 static void output_aranges (unsigned long);
3082 static unsigned int add_ranges_num (int);
3083 static unsigned int add_ranges (const_tree);
3084 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3085 bool *, bool);
3086 static void output_ranges (void);
3087 static dw_line_info_table *new_line_info_table (void);
3088 static void output_line_info (bool);
3089 static void output_file_names (void);
3090 static dw_die_ref base_type_die (tree);
3091 static int is_base_type (tree);
3092 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3093 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3094 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3095 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3096 static int type_is_enum (const_tree);
3097 static unsigned int dbx_reg_number (const_rtx);
3098 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3099 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3100 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3101 enum var_init_status);
3102 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3103 enum var_init_status);
3104 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3105 enum var_init_status);
3106 static int is_based_loc (const_rtx);
3107 static int resolve_one_addr (rtx *, void *);
3108 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3109 enum var_init_status);
3110 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3111 enum var_init_status);
3112 static dw_loc_list_ref loc_list_from_tree (tree, int);
3113 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3114 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3115 static tree field_type (const_tree);
3116 static unsigned int simple_type_align_in_bits (const_tree);
3117 static unsigned int simple_decl_align_in_bits (const_tree);
3118 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3119 static HOST_WIDE_INT field_byte_offset (const_tree);
3120 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3121 dw_loc_list_ref);
3122 static void add_data_member_location_attribute (dw_die_ref, tree);
3123 static bool add_const_value_attribute (dw_die_ref, rtx);
3124 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3125 static void insert_double (double_int, unsigned char *);
3126 static void insert_float (const_rtx, unsigned char *);
3127 static rtx rtl_for_decl_location (tree);
3128 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3129 enum dwarf_attribute);
3130 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3131 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3132 static void add_name_attribute (dw_die_ref, const char *);
3133 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3134 static void add_comp_dir_attribute (dw_die_ref);
3135 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3136 static void add_subscript_info (dw_die_ref, tree, bool);
3137 static void add_byte_size_attribute (dw_die_ref, tree);
3138 static void add_bit_offset_attribute (dw_die_ref, tree);
3139 static void add_bit_size_attribute (dw_die_ref, tree);
3140 static void add_prototyped_attribute (dw_die_ref, tree);
3141 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3142 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3143 static void add_src_coords_attributes (dw_die_ref, tree);
3144 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3145 static void push_decl_scope (tree);
3146 static void pop_decl_scope (void);
3147 static dw_die_ref scope_die_for (tree, dw_die_ref);
3148 static inline int local_scope_p (dw_die_ref);
3149 static inline int class_scope_p (dw_die_ref);
3150 static inline int class_or_namespace_scope_p (dw_die_ref);
3151 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3152 static void add_calling_convention_attribute (dw_die_ref, tree);
3153 static const char *type_tag (const_tree);
3154 static tree member_declared_type (const_tree);
3155 #if 0
3156 static const char *decl_start_label (tree);
3157 #endif
3158 static void gen_array_type_die (tree, dw_die_ref);
3159 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3160 #if 0
3161 static void gen_entry_point_die (tree, dw_die_ref);
3162 #endif
3163 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3164 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3165 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3166 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3167 static void gen_formal_types_die (tree, dw_die_ref);
3168 static void gen_subprogram_die (tree, dw_die_ref);
3169 static void gen_variable_die (tree, tree, dw_die_ref);
3170 static void gen_const_die (tree, dw_die_ref);
3171 static void gen_label_die (tree, dw_die_ref);
3172 static void gen_lexical_block_die (tree, dw_die_ref, int);
3173 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3174 static void gen_field_die (tree, dw_die_ref);
3175 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3176 static dw_die_ref gen_compile_unit_die (const char *);
3177 static void gen_inheritance_die (tree, tree, dw_die_ref);
3178 static void gen_member_die (tree, dw_die_ref);
3179 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3180 enum debug_info_usage);
3181 static void gen_subroutine_type_die (tree, dw_die_ref);
3182 static void gen_typedef_die (tree, dw_die_ref);
3183 static void gen_type_die (tree, dw_die_ref);
3184 static void gen_block_die (tree, dw_die_ref, int);
3185 static void decls_for_scope (tree, dw_die_ref, int);
3186 static inline int is_redundant_typedef (const_tree);
3187 static bool is_naming_typedef_decl (const_tree);
3188 static inline dw_die_ref get_context_die (tree);
3189 static void gen_namespace_die (tree, dw_die_ref);
3190 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3191 static dw_die_ref force_decl_die (tree);
3192 static dw_die_ref force_type_die (tree);
3193 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3194 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3195 static struct dwarf_file_data * lookup_filename (const char *);
3196 static void retry_incomplete_types (void);
3197 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3198 static void gen_generic_params_dies (tree);
3199 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3200 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3201 static void splice_child_die (dw_die_ref, dw_die_ref);
3202 static int file_info_cmp (const void *, const void *);
3203 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3204 const char *, const char *);
3205 static void output_loc_list (dw_loc_list_ref);
3206 static char *gen_internal_sym (const char *);
3207 static bool want_pubnames (void);
3208
3209 static void prune_unmark_dies (dw_die_ref);
3210 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3211 static void prune_unused_types_mark (dw_die_ref, int);
3212 static void prune_unused_types_walk (dw_die_ref);
3213 static void prune_unused_types_walk_attribs (dw_die_ref);
3214 static void prune_unused_types_prune (dw_die_ref);
3215 static void prune_unused_types (void);
3216 static int maybe_emit_file (struct dwarf_file_data *fd);
3217 static inline const char *AT_vms_delta1 (dw_attr_ref);
3218 static inline const char *AT_vms_delta2 (dw_attr_ref);
3219 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3220 const char *, const char *);
3221 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3222 static void gen_remaining_tmpl_value_param_die_attribute (void);
3223 static bool generic_type_p (tree);
3224 static void schedule_generic_params_dies_gen (tree t);
3225 static void gen_scheduled_generic_parms_dies (void);
3226
3227 static const char *comp_dir_string (void);
3228
3229 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3230
3231 /* enum for tracking thread-local variables whose address is really an offset
3232 relative to the TLS pointer, which will need link-time relocation, but will
3233 not need relocation by the DWARF consumer. */
3234
3235 enum dtprel_bool
3236 {
3237 dtprel_false = 0,
3238 dtprel_true = 1
3239 };
3240
3241 /* Return the operator to use for an address of a variable. For dtprel_true, we
3242 use DW_OP_const*. For regular variables, which need both link-time
3243 relocation and consumer-level relocation (e.g., to account for shared objects
3244 loaded at a random address), we use DW_OP_addr*. */
3245
3246 static inline enum dwarf_location_atom
3247 dw_addr_op (enum dtprel_bool dtprel)
3248 {
3249 if (dtprel == dtprel_true)
3250 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3251 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3252 else
3253 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3254 }
3255
3256 /* Return a pointer to a newly allocated address location description. If
3257 dwarf_split_debug_info is true, then record the address with the appropriate
3258 relocation. */
3259 static inline dw_loc_descr_ref
3260 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3261 {
3262 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3263
3264 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3265 ref->dw_loc_oprnd1.v.val_addr = addr;
3266 ref->dtprel = dtprel;
3267 if (dwarf_split_debug_info)
3268 ref->dw_loc_oprnd1.val_entry
3269 = add_addr_table_entry (addr,
3270 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3271 else
3272 ref->dw_loc_oprnd1.val_entry = NULL;
3273
3274 return ref;
3275 }
3276
3277 /* Section names used to hold DWARF debugging information. */
3278
3279 #ifndef DEBUG_INFO_SECTION
3280 #define DEBUG_INFO_SECTION ".debug_info"
3281 #endif
3282 #ifndef DEBUG_DWO_INFO_SECTION
3283 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3284 #endif
3285 #ifndef DEBUG_ABBREV_SECTION
3286 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3287 #endif
3288 #ifndef DEBUG_DWO_ABBREV_SECTION
3289 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3290 #endif
3291 #ifndef DEBUG_ARANGES_SECTION
3292 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3293 #endif
3294 #ifndef DEBUG_ADDR_SECTION
3295 #define DEBUG_ADDR_SECTION ".debug_addr"
3296 #endif
3297 #ifndef DEBUG_NORM_MACINFO_SECTION
3298 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3299 #endif
3300 #ifndef DEBUG_DWO_MACINFO_SECTION
3301 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3302 #endif
3303 #ifndef DEBUG_MACINFO_SECTION
3304 #define DEBUG_MACINFO_SECTION \
3305 (!dwarf_split_debug_info \
3306 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3307 #endif
3308 #ifndef DEBUG_NORM_MACRO_SECTION
3309 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3310 #endif
3311 #ifndef DEBUG_DWO_MACRO_SECTION
3312 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3313 #endif
3314 #ifndef DEBUG_MACRO_SECTION
3315 #define DEBUG_MACRO_SECTION \
3316 (!dwarf_split_debug_info \
3317 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3318 #endif
3319 #ifndef DEBUG_LINE_SECTION
3320 #define DEBUG_LINE_SECTION ".debug_line"
3321 #endif
3322 #ifndef DEBUG_DWO_LINE_SECTION
3323 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3324 #endif
3325 #ifndef DEBUG_LOC_SECTION
3326 #define DEBUG_LOC_SECTION ".debug_loc"
3327 #endif
3328 #ifndef DEBUG_DWO_LOC_SECTION
3329 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3330 #endif
3331 #ifndef DEBUG_PUBNAMES_SECTION
3332 #define DEBUG_PUBNAMES_SECTION \
3333 ((debug_generate_pub_sections == 2) \
3334 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3335 #endif
3336 #ifndef DEBUG_PUBTYPES_SECTION
3337 #define DEBUG_PUBTYPES_SECTION \
3338 ((debug_generate_pub_sections == 2) \
3339 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3340 #endif
3341 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3342 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3343 #ifndef DEBUG_STR_OFFSETS_SECTION
3344 #define DEBUG_STR_OFFSETS_SECTION \
3345 (!dwarf_split_debug_info \
3346 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3347 #endif
3348 #ifndef DEBUG_STR_DWO_SECTION
3349 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3350 #endif
3351 #ifndef DEBUG_STR_SECTION
3352 #define DEBUG_STR_SECTION ".debug_str"
3353 #endif
3354 #ifndef DEBUG_RANGES_SECTION
3355 #define DEBUG_RANGES_SECTION ".debug_ranges"
3356 #endif
3357
3358 /* Standard ELF section names for compiled code and data. */
3359 #ifndef TEXT_SECTION_NAME
3360 #define TEXT_SECTION_NAME ".text"
3361 #endif
3362
3363 /* Section flags for .debug_macinfo/.debug_macro section. */
3364 #define DEBUG_MACRO_SECTION_FLAGS \
3365 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3366
3367 /* Section flags for .debug_str section. */
3368 #define DEBUG_STR_SECTION_FLAGS \
3369 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3370 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3371 : SECTION_DEBUG)
3372
3373 /* Section flags for .debug_str.dwo section. */
3374 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3375
3376 /* Labels we insert at beginning sections we can reference instead of
3377 the section names themselves. */
3378
3379 #ifndef TEXT_SECTION_LABEL
3380 #define TEXT_SECTION_LABEL "Ltext"
3381 #endif
3382 #ifndef COLD_TEXT_SECTION_LABEL
3383 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3384 #endif
3385 #ifndef DEBUG_LINE_SECTION_LABEL
3386 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3387 #endif
3388 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3389 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3390 #endif
3391 #ifndef DEBUG_INFO_SECTION_LABEL
3392 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3393 #endif
3394 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3395 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3396 #endif
3397 #ifndef DEBUG_ABBREV_SECTION_LABEL
3398 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3399 #endif
3400 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3401 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3402 #endif
3403 #ifndef DEBUG_ADDR_SECTION_LABEL
3404 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3405 #endif
3406 #ifndef DEBUG_LOC_SECTION_LABEL
3407 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3408 #endif
3409 #ifndef DEBUG_RANGES_SECTION_LABEL
3410 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3411 #endif
3412 #ifndef DEBUG_MACINFO_SECTION_LABEL
3413 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3414 #endif
3415 #ifndef DEBUG_MACRO_SECTION_LABEL
3416 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3417 #endif
3418 #define SKELETON_COMP_DIE_ABBREV 1
3419 #define SKELETON_TYPE_DIE_ABBREV 2
3420
3421 /* Definitions of defaults for formats and names of various special
3422 (artificial) labels which may be generated within this file (when the -g
3423 options is used and DWARF2_DEBUGGING_INFO is in effect.
3424 If necessary, these may be overridden from within the tm.h file, but
3425 typically, overriding these defaults is unnecessary. */
3426
3427 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3435 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3436 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3437 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3438 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3439 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3440 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3441
3442 #ifndef TEXT_END_LABEL
3443 #define TEXT_END_LABEL "Letext"
3444 #endif
3445 #ifndef COLD_END_LABEL
3446 #define COLD_END_LABEL "Letext_cold"
3447 #endif
3448 #ifndef BLOCK_BEGIN_LABEL
3449 #define BLOCK_BEGIN_LABEL "LBB"
3450 #endif
3451 #ifndef BLOCK_END_LABEL
3452 #define BLOCK_END_LABEL "LBE"
3453 #endif
3454 #ifndef LINE_CODE_LABEL
3455 #define LINE_CODE_LABEL "LM"
3456 #endif
3457
3458 \f
3459 /* Return the root of the DIE's built for the current compilation unit. */
3460 static dw_die_ref
3461 comp_unit_die (void)
3462 {
3463 if (!single_comp_unit_die)
3464 single_comp_unit_die = gen_compile_unit_die (NULL);
3465 return single_comp_unit_die;
3466 }
3467
3468 /* We allow a language front-end to designate a function that is to be
3469 called to "demangle" any name before it is put into a DIE. */
3470
3471 static const char *(*demangle_name_func) (const char *);
3472
3473 void
3474 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3475 {
3476 demangle_name_func = func;
3477 }
3478
3479 /* Test if rtl node points to a pseudo register. */
3480
3481 static inline int
3482 is_pseudo_reg (const_rtx rtl)
3483 {
3484 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3485 || (GET_CODE (rtl) == SUBREG
3486 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3487 }
3488
3489 /* Return a reference to a type, with its const and volatile qualifiers
3490 removed. */
3491
3492 static inline tree
3493 type_main_variant (tree type)
3494 {
3495 type = TYPE_MAIN_VARIANT (type);
3496
3497 /* ??? There really should be only one main variant among any group of
3498 variants of a given type (and all of the MAIN_VARIANT values for all
3499 members of the group should point to that one type) but sometimes the C
3500 front-end messes this up for array types, so we work around that bug
3501 here. */
3502 if (TREE_CODE (type) == ARRAY_TYPE)
3503 while (type != TYPE_MAIN_VARIANT (type))
3504 type = TYPE_MAIN_VARIANT (type);
3505
3506 return type;
3507 }
3508
3509 /* Return nonzero if the given type node represents a tagged type. */
3510
3511 static inline int
3512 is_tagged_type (const_tree type)
3513 {
3514 enum tree_code code = TREE_CODE (type);
3515
3516 return (code == RECORD_TYPE || code == UNION_TYPE
3517 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3518 }
3519
3520 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3521
3522 static void
3523 get_ref_die_offset_label (char *label, dw_die_ref ref)
3524 {
3525 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3526 }
3527
3528 /* Return die_offset of a DIE reference to a base type. */
3529
3530 static unsigned long int
3531 get_base_type_offset (dw_die_ref ref)
3532 {
3533 if (ref->die_offset)
3534 return ref->die_offset;
3535 if (comp_unit_die ()->die_abbrev)
3536 {
3537 calc_base_type_die_sizes ();
3538 gcc_assert (ref->die_offset);
3539 }
3540 return ref->die_offset;
3541 }
3542
3543 /* Return die_offset of a DIE reference other than base type. */
3544
3545 static unsigned long int
3546 get_ref_die_offset (dw_die_ref ref)
3547 {
3548 gcc_assert (ref->die_offset);
3549 return ref->die_offset;
3550 }
3551
3552 /* Convert a DIE tag into its string name. */
3553
3554 static const char *
3555 dwarf_tag_name (unsigned int tag)
3556 {
3557 const char *name = get_DW_TAG_name (tag);
3558
3559 if (name != NULL)
3560 return name;
3561
3562 return "DW_TAG_<unknown>";
3563 }
3564
3565 /* Convert a DWARF attribute code into its string name. */
3566
3567 static const char *
3568 dwarf_attr_name (unsigned int attr)
3569 {
3570 const char *name;
3571
3572 switch (attr)
3573 {
3574 #if VMS_DEBUGGING_INFO
3575 case DW_AT_HP_prologue:
3576 return "DW_AT_HP_prologue";
3577 #else
3578 case DW_AT_MIPS_loop_unroll_factor:
3579 return "DW_AT_MIPS_loop_unroll_factor";
3580 #endif
3581
3582 #if VMS_DEBUGGING_INFO
3583 case DW_AT_HP_epilogue:
3584 return "DW_AT_HP_epilogue";
3585 #else
3586 case DW_AT_MIPS_stride:
3587 return "DW_AT_MIPS_stride";
3588 #endif
3589 }
3590
3591 name = get_DW_AT_name (attr);
3592
3593 if (name != NULL)
3594 return name;
3595
3596 return "DW_AT_<unknown>";
3597 }
3598
3599 /* Convert a DWARF value form code into its string name. */
3600
3601 static const char *
3602 dwarf_form_name (unsigned int form)
3603 {
3604 const char *name = get_DW_FORM_name (form);
3605
3606 if (name != NULL)
3607 return name;
3608
3609 return "DW_FORM_<unknown>";
3610 }
3611 \f
3612 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3613 instance of an inlined instance of a decl which is local to an inline
3614 function, so we have to trace all of the way back through the origin chain
3615 to find out what sort of node actually served as the original seed for the
3616 given block. */
3617
3618 static tree
3619 decl_ultimate_origin (const_tree decl)
3620 {
3621 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3622 return NULL_TREE;
3623
3624 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3625 nodes in the function to point to themselves; ignore that if
3626 we're trying to output the abstract instance of this function. */
3627 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3628 return NULL_TREE;
3629
3630 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3631 most distant ancestor, this should never happen. */
3632 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3633
3634 return DECL_ABSTRACT_ORIGIN (decl);
3635 }
3636
3637 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3638 of a virtual function may refer to a base class, so we check the 'this'
3639 parameter. */
3640
3641 static tree
3642 decl_class_context (tree decl)
3643 {
3644 tree context = NULL_TREE;
3645
3646 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3647 context = DECL_CONTEXT (decl);
3648 else
3649 context = TYPE_MAIN_VARIANT
3650 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3651
3652 if (context && !TYPE_P (context))
3653 context = NULL_TREE;
3654
3655 return context;
3656 }
3657 \f
3658 /* Add an attribute/value pair to a DIE. */
3659
3660 static inline void
3661 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3662 {
3663 /* Maybe this should be an assert? */
3664 if (die == NULL)
3665 return;
3666
3667 vec_safe_reserve (die->die_attr, 1);
3668 vec_safe_push (die->die_attr, *attr);
3669 }
3670
3671 static inline enum dw_val_class
3672 AT_class (dw_attr_ref a)
3673 {
3674 return a->dw_attr_val.val_class;
3675 }
3676
3677 /* Return the index for any attribute that will be referenced with a
3678 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3679 are stored in dw_attr_val.v.val_str for reference counting
3680 pruning. */
3681
3682 static inline unsigned int
3683 AT_index (dw_attr_ref a)
3684 {
3685 if (AT_class (a) == dw_val_class_str)
3686 return a->dw_attr_val.v.val_str->index;
3687 else if (a->dw_attr_val.val_entry != NULL)
3688 return a->dw_attr_val.val_entry->index;
3689 return NOT_INDEXED;
3690 }
3691
3692 /* Add a flag value attribute to a DIE. */
3693
3694 static inline void
3695 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3696 {
3697 dw_attr_node attr;
3698
3699 attr.dw_attr = attr_kind;
3700 attr.dw_attr_val.val_class = dw_val_class_flag;
3701 attr.dw_attr_val.val_entry = NULL;
3702 attr.dw_attr_val.v.val_flag = flag;
3703 add_dwarf_attr (die, &attr);
3704 }
3705
3706 static inline unsigned
3707 AT_flag (dw_attr_ref a)
3708 {
3709 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3710 return a->dw_attr_val.v.val_flag;
3711 }
3712
3713 /* Add a signed integer attribute value to a DIE. */
3714
3715 static inline void
3716 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3717 {
3718 dw_attr_node attr;
3719
3720 attr.dw_attr = attr_kind;
3721 attr.dw_attr_val.val_class = dw_val_class_const;
3722 attr.dw_attr_val.val_entry = NULL;
3723 attr.dw_attr_val.v.val_int = int_val;
3724 add_dwarf_attr (die, &attr);
3725 }
3726
3727 static inline HOST_WIDE_INT
3728 AT_int (dw_attr_ref a)
3729 {
3730 gcc_assert (a && AT_class (a) == dw_val_class_const);
3731 return a->dw_attr_val.v.val_int;
3732 }
3733
3734 /* Add an unsigned integer attribute value to a DIE. */
3735
3736 static inline void
3737 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3738 unsigned HOST_WIDE_INT unsigned_val)
3739 {
3740 dw_attr_node attr;
3741
3742 attr.dw_attr = attr_kind;
3743 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3744 attr.dw_attr_val.val_entry = NULL;
3745 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3746 add_dwarf_attr (die, &attr);
3747 }
3748
3749 static inline unsigned HOST_WIDE_INT
3750 AT_unsigned (dw_attr_ref a)
3751 {
3752 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3753 return a->dw_attr_val.v.val_unsigned;
3754 }
3755
3756 /* Add an unsigned double integer attribute value to a DIE. */
3757
3758 static inline void
3759 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3760 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3761 {
3762 dw_attr_node attr;
3763
3764 attr.dw_attr = attr_kind;
3765 attr.dw_attr_val.val_class = dw_val_class_const_double;
3766 attr.dw_attr_val.val_entry = NULL;
3767 attr.dw_attr_val.v.val_double.high = high;
3768 attr.dw_attr_val.v.val_double.low = low;
3769 add_dwarf_attr (die, &attr);
3770 }
3771
3772 /* Add a floating point attribute value to a DIE and return it. */
3773
3774 static inline void
3775 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3776 unsigned int length, unsigned int elt_size, unsigned char *array)
3777 {
3778 dw_attr_node attr;
3779
3780 attr.dw_attr = attr_kind;
3781 attr.dw_attr_val.val_class = dw_val_class_vec;
3782 attr.dw_attr_val.val_entry = NULL;
3783 attr.dw_attr_val.v.val_vec.length = length;
3784 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3785 attr.dw_attr_val.v.val_vec.array = array;
3786 add_dwarf_attr (die, &attr);
3787 }
3788
3789 /* Add an 8-byte data attribute value to a DIE. */
3790
3791 static inline void
3792 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3793 unsigned char data8[8])
3794 {
3795 dw_attr_node attr;
3796
3797 attr.dw_attr = attr_kind;
3798 attr.dw_attr_val.val_class = dw_val_class_data8;
3799 attr.dw_attr_val.val_entry = NULL;
3800 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3801 add_dwarf_attr (die, &attr);
3802 }
3803
3804 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3805 dwarf_split_debug_info, address attributes in dies destined for the
3806 final executable have force_direct set to avoid using indexed
3807 references. */
3808
3809 static inline void
3810 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3811 bool force_direct)
3812 {
3813 dw_attr_node attr;
3814 char * lbl_id;
3815
3816 lbl_id = xstrdup (lbl_low);
3817 attr.dw_attr = DW_AT_low_pc;
3818 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3819 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3820 if (dwarf_split_debug_info && !force_direct)
3821 attr.dw_attr_val.val_entry
3822 = add_addr_table_entry (lbl_id, ate_kind_label);
3823 else
3824 attr.dw_attr_val.val_entry = NULL;
3825 add_dwarf_attr (die, &attr);
3826
3827 attr.dw_attr = DW_AT_high_pc;
3828 if (dwarf_version < 4)
3829 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3830 else
3831 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3832 lbl_id = xstrdup (lbl_high);
3833 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3834 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3835 && dwarf_split_debug_info && !force_direct)
3836 attr.dw_attr_val.val_entry
3837 = add_addr_table_entry (lbl_id, ate_kind_label);
3838 else
3839 attr.dw_attr_val.val_entry = NULL;
3840 add_dwarf_attr (die, &attr);
3841 }
3842
3843 /* Hash and equality functions for debug_str_hash. */
3844
3845 static hashval_t
3846 debug_str_do_hash (const void *x)
3847 {
3848 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3849 }
3850
3851 static int
3852 debug_str_eq (const void *x1, const void *x2)
3853 {
3854 return strcmp ((((const struct indirect_string_node *)x1)->str),
3855 (const char *)x2) == 0;
3856 }
3857
3858 /* Add STR to the given string hash table. */
3859
3860 static struct indirect_string_node *
3861 find_AT_string_in_table (const char *str, htab_t table)
3862 {
3863 struct indirect_string_node *node;
3864 void **slot;
3865
3866 slot = htab_find_slot_with_hash (table, str,
3867 htab_hash_string (str), INSERT);
3868 if (*slot == NULL)
3869 {
3870 node = ggc_alloc_cleared_indirect_string_node ();
3871 node->str = ggc_strdup (str);
3872 *slot = node;
3873 }
3874 else
3875 node = (struct indirect_string_node *) *slot;
3876
3877 node->refcount++;
3878 return node;
3879 }
3880
3881 /* Add STR to the indirect string hash table. */
3882
3883 static struct indirect_string_node *
3884 find_AT_string (const char *str)
3885 {
3886 if (! debug_str_hash)
3887 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3888 debug_str_eq, NULL);
3889
3890 return find_AT_string_in_table (str, debug_str_hash);
3891 }
3892
3893 /* Add a string attribute value to a DIE. */
3894
3895 static inline void
3896 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3897 {
3898 dw_attr_node attr;
3899 struct indirect_string_node *node;
3900
3901 node = find_AT_string (str);
3902
3903 attr.dw_attr = attr_kind;
3904 attr.dw_attr_val.val_class = dw_val_class_str;
3905 attr.dw_attr_val.val_entry = NULL;
3906 attr.dw_attr_val.v.val_str = node;
3907 add_dwarf_attr (die, &attr);
3908 }
3909
3910 static inline const char *
3911 AT_string (dw_attr_ref a)
3912 {
3913 gcc_assert (a && AT_class (a) == dw_val_class_str);
3914 return a->dw_attr_val.v.val_str->str;
3915 }
3916
3917 /* Call this function directly to bypass AT_string_form's logic to put
3918 the string inline in the die. */
3919
3920 static void
3921 set_indirect_string (struct indirect_string_node *node)
3922 {
3923 char label[32];
3924 /* Already indirect is a no op. */
3925 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3926 {
3927 gcc_assert (node->label);
3928 return;
3929 }
3930 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3931 ++dw2_string_counter;
3932 node->label = xstrdup (label);
3933
3934 if (!dwarf_split_debug_info)
3935 {
3936 node->form = DW_FORM_strp;
3937 node->index = NOT_INDEXED;
3938 }
3939 else
3940 {
3941 node->form = DW_FORM_GNU_str_index;
3942 node->index = NO_INDEX_ASSIGNED;
3943 }
3944 }
3945
3946 /* Find out whether a string should be output inline in DIE
3947 or out-of-line in .debug_str section. */
3948
3949 static enum dwarf_form
3950 find_string_form (struct indirect_string_node *node)
3951 {
3952 unsigned int len;
3953
3954 if (node->form)
3955 return node->form;
3956
3957 len = strlen (node->str) + 1;
3958
3959 /* If the string is shorter or equal to the size of the reference, it is
3960 always better to put it inline. */
3961 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3962 return node->form = DW_FORM_string;
3963
3964 /* If we cannot expect the linker to merge strings in .debug_str
3965 section, only put it into .debug_str if it is worth even in this
3966 single module. */
3967 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3968 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3969 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3970 return node->form = DW_FORM_string;
3971
3972 set_indirect_string (node);
3973
3974 return node->form;
3975 }
3976
3977 /* Find out whether the string referenced from the attribute should be
3978 output inline in DIE or out-of-line in .debug_str section. */
3979
3980 static enum dwarf_form
3981 AT_string_form (dw_attr_ref a)
3982 {
3983 gcc_assert (a && AT_class (a) == dw_val_class_str);
3984 return find_string_form (a->dw_attr_val.v.val_str);
3985 }
3986
3987 /* Add a DIE reference attribute value to a DIE. */
3988
3989 static inline void
3990 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3991 {
3992 dw_attr_node attr;
3993
3994 #ifdef ENABLE_CHECKING
3995 gcc_assert (targ_die != NULL);
3996 #else
3997 /* With LTO we can end up trying to reference something we didn't create
3998 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3999 if (targ_die == NULL)
4000 return;
4001 #endif
4002
4003 attr.dw_attr = attr_kind;
4004 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4005 attr.dw_attr_val.val_entry = NULL;
4006 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4007 attr.dw_attr_val.v.val_die_ref.external = 0;
4008 add_dwarf_attr (die, &attr);
4009 }
4010
4011 /* Change DIE reference REF to point to NEW_DIE instead. */
4012
4013 static inline void
4014 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4015 {
4016 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4017 ref->dw_attr_val.v.val_die_ref.die = new_die;
4018 ref->dw_attr_val.v.val_die_ref.external = 0;
4019 }
4020
4021 /* Add an AT_specification attribute to a DIE, and also make the back
4022 pointer from the specification to the definition. */
4023
4024 static inline void
4025 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4026 {
4027 add_AT_die_ref (die, DW_AT_specification, targ_die);
4028 gcc_assert (!targ_die->die_definition);
4029 targ_die->die_definition = die;
4030 }
4031
4032 static inline dw_die_ref
4033 AT_ref (dw_attr_ref a)
4034 {
4035 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4036 return a->dw_attr_val.v.val_die_ref.die;
4037 }
4038
4039 static inline int
4040 AT_ref_external (dw_attr_ref a)
4041 {
4042 if (a && AT_class (a) == dw_val_class_die_ref)
4043 return a->dw_attr_val.v.val_die_ref.external;
4044
4045 return 0;
4046 }
4047
4048 static inline void
4049 set_AT_ref_external (dw_attr_ref a, int i)
4050 {
4051 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4052 a->dw_attr_val.v.val_die_ref.external = i;
4053 }
4054
4055 /* Add an FDE reference attribute value to a DIE. */
4056
4057 static inline void
4058 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4059 {
4060 dw_attr_node attr;
4061
4062 attr.dw_attr = attr_kind;
4063 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4064 attr.dw_attr_val.val_entry = NULL;
4065 attr.dw_attr_val.v.val_fde_index = targ_fde;
4066 add_dwarf_attr (die, &attr);
4067 }
4068
4069 /* Add a location description attribute value to a DIE. */
4070
4071 static inline void
4072 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4073 {
4074 dw_attr_node attr;
4075
4076 attr.dw_attr = attr_kind;
4077 attr.dw_attr_val.val_class = dw_val_class_loc;
4078 attr.dw_attr_val.val_entry = NULL;
4079 attr.dw_attr_val.v.val_loc = loc;
4080 add_dwarf_attr (die, &attr);
4081 }
4082
4083 static inline dw_loc_descr_ref
4084 AT_loc (dw_attr_ref a)
4085 {
4086 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4087 return a->dw_attr_val.v.val_loc;
4088 }
4089
4090 static inline void
4091 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4092 {
4093 dw_attr_node attr;
4094
4095 attr.dw_attr = attr_kind;
4096 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4097 attr.dw_attr_val.val_entry = NULL;
4098 attr.dw_attr_val.v.val_loc_list = loc_list;
4099 add_dwarf_attr (die, &attr);
4100 have_location_lists = true;
4101 }
4102
4103 static inline dw_loc_list_ref
4104 AT_loc_list (dw_attr_ref a)
4105 {
4106 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4107 return a->dw_attr_val.v.val_loc_list;
4108 }
4109
4110 static inline dw_loc_list_ref *
4111 AT_loc_list_ptr (dw_attr_ref a)
4112 {
4113 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4114 return &a->dw_attr_val.v.val_loc_list;
4115 }
4116
4117 /* Table of entries into the .debug_addr section. */
4118
4119 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4120
4121 /* Hash an address_table_entry. */
4122
4123 static hashval_t
4124 addr_table_entry_do_hash (const void *x)
4125 {
4126 const addr_table_entry *a = (const addr_table_entry *) x;
4127 switch (a->kind)
4128 {
4129 case ate_kind_rtx:
4130 return iterative_hash_rtx (a->addr.rtl, 0);
4131 case ate_kind_rtx_dtprel:
4132 return iterative_hash_rtx (a->addr.rtl, 1);
4133 case ate_kind_label:
4134 return htab_hash_string (a->addr.label);
4135 default:
4136 gcc_unreachable ();
4137 }
4138 }
4139
4140 /* Determine equality for two address_table_entries. */
4141
4142 static int
4143 addr_table_entry_eq (const void *x1, const void *x2)
4144 {
4145 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4146 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4147
4148 if (a1->kind != a2->kind)
4149 return 0;
4150 switch (a1->kind)
4151 {
4152 case ate_kind_rtx:
4153 case ate_kind_rtx_dtprel:
4154 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4155 case ate_kind_label:
4156 return strcmp (a1->addr.label, a2->addr.label) == 0;
4157 default:
4158 gcc_unreachable ();
4159 }
4160 }
4161
4162 /* Initialize an addr_table_entry. */
4163
4164 void
4165 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4166 {
4167 e->kind = kind;
4168 switch (kind)
4169 {
4170 case ate_kind_rtx:
4171 case ate_kind_rtx_dtprel:
4172 e->addr.rtl = (rtx) addr;
4173 break;
4174 case ate_kind_label:
4175 e->addr.label = (char *) addr;
4176 break;
4177 }
4178 e->refcount = 0;
4179 e->index = NO_INDEX_ASSIGNED;
4180 }
4181
4182 /* Add attr to the address table entry to the table. Defer setting an
4183 index until output time. */
4184
4185 static addr_table_entry *
4186 add_addr_table_entry (void *addr, enum ate_kind kind)
4187 {
4188 addr_table_entry *node;
4189 addr_table_entry finder;
4190 void **slot;
4191
4192 gcc_assert (dwarf_split_debug_info);
4193 if (! addr_index_table)
4194 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4195 addr_table_entry_eq, NULL);
4196 init_addr_table_entry (&finder, kind, addr);
4197 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4198
4199 if (*slot == HTAB_EMPTY_ENTRY)
4200 {
4201 node = ggc_alloc_cleared_addr_table_entry ();
4202 init_addr_table_entry (node, kind, addr);
4203 *slot = node;
4204 }
4205 else
4206 node = (addr_table_entry *) *slot;
4207
4208 node->refcount++;
4209 return node;
4210 }
4211
4212 /* Remove an entry from the addr table by decrementing its refcount.
4213 Strictly, decrementing the refcount would be enough, but the
4214 assertion that the entry is actually in the table has found
4215 bugs. */
4216
4217 static void
4218 remove_addr_table_entry (addr_table_entry *entry)
4219 {
4220 addr_table_entry *node;
4221
4222 gcc_assert (dwarf_split_debug_info && addr_index_table);
4223 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4224 /* After an index is assigned, the table is frozen. */
4225 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4226 node->refcount--;
4227 }
4228
4229 /* Given a location list, remove all addresses it refers to from the
4230 address_table. */
4231
4232 static void
4233 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4234 {
4235 for (; descr; descr = descr->dw_loc_next)
4236 if (descr->dw_loc_oprnd1.val_entry != NULL)
4237 {
4238 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4239 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4240 }
4241 }
4242
4243 /* A helper function for dwarf2out_finish called through
4244 htab_traverse. Assign an addr_table_entry its index. All entries
4245 must be collected into the table when this function is called,
4246 because the indexing code relies on htab_traverse to traverse nodes
4247 in the same order for each run. */
4248
4249 static int
4250 index_addr_table_entry (void **h, void *v)
4251 {
4252 addr_table_entry *node = (addr_table_entry *) *h;
4253 unsigned int *index = (unsigned int *) v;
4254
4255 /* Don't index unreferenced nodes. */
4256 if (node->refcount == 0)
4257 return 1;
4258
4259 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4260 node->index = *index;
4261 *index += 1;
4262
4263 return 1;
4264 }
4265
4266 /* Add an address constant attribute value to a DIE. When using
4267 dwarf_split_debug_info, address attributes in dies destined for the
4268 final executable should be direct references--setting the parameter
4269 force_direct ensures this behavior. */
4270
4271 static inline void
4272 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4273 bool force_direct)
4274 {
4275 dw_attr_node attr;
4276
4277 attr.dw_attr = attr_kind;
4278 attr.dw_attr_val.val_class = dw_val_class_addr;
4279 attr.dw_attr_val.v.val_addr = addr;
4280 if (dwarf_split_debug_info && !force_direct)
4281 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4282 else
4283 attr.dw_attr_val.val_entry = NULL;
4284 add_dwarf_attr (die, &attr);
4285 }
4286
4287 /* Get the RTX from to an address DIE attribute. */
4288
4289 static inline rtx
4290 AT_addr (dw_attr_ref a)
4291 {
4292 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4293 return a->dw_attr_val.v.val_addr;
4294 }
4295
4296 /* Add a file attribute value to a DIE. */
4297
4298 static inline void
4299 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4300 struct dwarf_file_data *fd)
4301 {
4302 dw_attr_node attr;
4303
4304 attr.dw_attr = attr_kind;
4305 attr.dw_attr_val.val_class = dw_val_class_file;
4306 attr.dw_attr_val.val_entry = NULL;
4307 attr.dw_attr_val.v.val_file = fd;
4308 add_dwarf_attr (die, &attr);
4309 }
4310
4311 /* Get the dwarf_file_data from a file DIE attribute. */
4312
4313 static inline struct dwarf_file_data *
4314 AT_file (dw_attr_ref a)
4315 {
4316 gcc_assert (a && AT_class (a) == dw_val_class_file);
4317 return a->dw_attr_val.v.val_file;
4318 }
4319
4320 /* Add a vms delta attribute value to a DIE. */
4321
4322 static inline void
4323 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4324 const char *lbl1, const char *lbl2)
4325 {
4326 dw_attr_node attr;
4327
4328 attr.dw_attr = attr_kind;
4329 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4330 attr.dw_attr_val.val_entry = NULL;
4331 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4332 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4333 add_dwarf_attr (die, &attr);
4334 }
4335
4336 /* Add a label identifier attribute value to a DIE. */
4337
4338 static inline void
4339 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4340 const char *lbl_id)
4341 {
4342 dw_attr_node attr;
4343
4344 attr.dw_attr = attr_kind;
4345 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4346 attr.dw_attr_val.val_entry = NULL;
4347 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4348 if (dwarf_split_debug_info)
4349 attr.dw_attr_val.val_entry
4350 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4351 ate_kind_label);
4352 add_dwarf_attr (die, &attr);
4353 }
4354
4355 /* Add a section offset attribute value to a DIE, an offset into the
4356 debug_line section. */
4357
4358 static inline void
4359 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4360 const char *label)
4361 {
4362 dw_attr_node attr;
4363
4364 attr.dw_attr = attr_kind;
4365 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4366 attr.dw_attr_val.val_entry = NULL;
4367 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4368 add_dwarf_attr (die, &attr);
4369 }
4370
4371 /* Add a section offset attribute value to a DIE, an offset into the
4372 debug_macinfo section. */
4373
4374 static inline void
4375 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4376 const char *label)
4377 {
4378 dw_attr_node attr;
4379
4380 attr.dw_attr = attr_kind;
4381 attr.dw_attr_val.val_class = dw_val_class_macptr;
4382 attr.dw_attr_val.val_entry = NULL;
4383 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4384 add_dwarf_attr (die, &attr);
4385 }
4386
4387 /* Add an offset attribute value to a DIE. */
4388
4389 static inline void
4390 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4391 unsigned HOST_WIDE_INT offset)
4392 {
4393 dw_attr_node attr;
4394
4395 attr.dw_attr = attr_kind;
4396 attr.dw_attr_val.val_class = dw_val_class_offset;
4397 attr.dw_attr_val.val_entry = NULL;
4398 attr.dw_attr_val.v.val_offset = offset;
4399 add_dwarf_attr (die, &attr);
4400 }
4401
4402 /* Add a range_list attribute value to a DIE. When using
4403 dwarf_split_debug_info, address attributes in dies destined for the
4404 final executable should be direct references--setting the parameter
4405 force_direct ensures this behavior. */
4406
4407 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4408 #define RELOCATED_OFFSET (NULL)
4409
4410 static void
4411 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4412 long unsigned int offset, bool force_direct)
4413 {
4414 dw_attr_node attr;
4415
4416 attr.dw_attr = attr_kind;
4417 attr.dw_attr_val.val_class = dw_val_class_range_list;
4418 /* For the range_list attribute, use val_entry to store whether the
4419 offset should follow split-debug-info or normal semantics. This
4420 value is read in output_range_list_offset. */
4421 if (dwarf_split_debug_info && !force_direct)
4422 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4423 else
4424 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4425 attr.dw_attr_val.v.val_offset = offset;
4426 add_dwarf_attr (die, &attr);
4427 }
4428
4429 /* Return the start label of a delta attribute. */
4430
4431 static inline const char *
4432 AT_vms_delta1 (dw_attr_ref a)
4433 {
4434 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4435 return a->dw_attr_val.v.val_vms_delta.lbl1;
4436 }
4437
4438 /* Return the end label of a delta attribute. */
4439
4440 static inline const char *
4441 AT_vms_delta2 (dw_attr_ref a)
4442 {
4443 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4444 return a->dw_attr_val.v.val_vms_delta.lbl2;
4445 }
4446
4447 static inline const char *
4448 AT_lbl (dw_attr_ref a)
4449 {
4450 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4451 || AT_class (a) == dw_val_class_lineptr
4452 || AT_class (a) == dw_val_class_macptr
4453 || AT_class (a) == dw_val_class_high_pc));
4454 return a->dw_attr_val.v.val_lbl_id;
4455 }
4456
4457 /* Get the attribute of type attr_kind. */
4458
4459 static dw_attr_ref
4460 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4461 {
4462 dw_attr_ref a;
4463 unsigned ix;
4464 dw_die_ref spec = NULL;
4465
4466 if (! die)
4467 return NULL;
4468
4469 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4470 if (a->dw_attr == attr_kind)
4471 return a;
4472 else if (a->dw_attr == DW_AT_specification
4473 || a->dw_attr == DW_AT_abstract_origin)
4474 spec = AT_ref (a);
4475
4476 if (spec)
4477 return get_AT (spec, attr_kind);
4478
4479 return NULL;
4480 }
4481
4482 /* Returns the parent of the declaration of DIE. */
4483
4484 static dw_die_ref
4485 get_die_parent (dw_die_ref die)
4486 {
4487 dw_die_ref t;
4488
4489 if (!die)
4490 return NULL;
4491
4492 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4493 || (t = get_AT_ref (die, DW_AT_specification)))
4494 die = t;
4495
4496 return die->die_parent;
4497 }
4498
4499 /* Return the "low pc" attribute value, typically associated with a subprogram
4500 DIE. Return null if the "low pc" attribute is either not present, or if it
4501 cannot be represented as an assembler label identifier. */
4502
4503 static inline const char *
4504 get_AT_low_pc (dw_die_ref die)
4505 {
4506 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4507
4508 return a ? AT_lbl (a) : NULL;
4509 }
4510
4511 /* Return the "high pc" attribute value, typically associated with a subprogram
4512 DIE. Return null if the "high pc" attribute is either not present, or if it
4513 cannot be represented as an assembler label identifier. */
4514
4515 static inline const char *
4516 get_AT_hi_pc (dw_die_ref die)
4517 {
4518 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4519
4520 return a ? AT_lbl (a) : NULL;
4521 }
4522
4523 /* Return the value of the string attribute designated by ATTR_KIND, or
4524 NULL if it is not present. */
4525
4526 static inline const char *
4527 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4528 {
4529 dw_attr_ref a = get_AT (die, attr_kind);
4530
4531 return a ? AT_string (a) : NULL;
4532 }
4533
4534 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4535 if it is not present. */
4536
4537 static inline int
4538 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4539 {
4540 dw_attr_ref a = get_AT (die, attr_kind);
4541
4542 return a ? AT_flag (a) : 0;
4543 }
4544
4545 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4546 if it is not present. */
4547
4548 static inline unsigned
4549 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4550 {
4551 dw_attr_ref a = get_AT (die, attr_kind);
4552
4553 return a ? AT_unsigned (a) : 0;
4554 }
4555
4556 static inline dw_die_ref
4557 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4558 {
4559 dw_attr_ref a = get_AT (die, attr_kind);
4560
4561 return a ? AT_ref (a) : NULL;
4562 }
4563
4564 static inline struct dwarf_file_data *
4565 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4566 {
4567 dw_attr_ref a = get_AT (die, attr_kind);
4568
4569 return a ? AT_file (a) : NULL;
4570 }
4571
4572 /* Return TRUE if the language is C++. */
4573
4574 static inline bool
4575 is_cxx (void)
4576 {
4577 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4578
4579 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4580 }
4581
4582 /* Return TRUE if the language is Java. */
4583
4584 static inline bool
4585 is_java (void)
4586 {
4587 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4588
4589 return lang == DW_LANG_Java;
4590 }
4591
4592 /* Return TRUE if the language is Fortran. */
4593
4594 static inline bool
4595 is_fortran (void)
4596 {
4597 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4598
4599 return (lang == DW_LANG_Fortran77
4600 || lang == DW_LANG_Fortran90
4601 || lang == DW_LANG_Fortran95);
4602 }
4603
4604 /* Return TRUE if the language is Ada. */
4605
4606 static inline bool
4607 is_ada (void)
4608 {
4609 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4610
4611 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4612 }
4613
4614 /* Remove the specified attribute if present. */
4615
4616 static void
4617 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4618 {
4619 dw_attr_ref a;
4620 unsigned ix;
4621
4622 if (! die)
4623 return;
4624
4625 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4626 if (a->dw_attr == attr_kind)
4627 {
4628 if (AT_class (a) == dw_val_class_str)
4629 if (a->dw_attr_val.v.val_str->refcount)
4630 a->dw_attr_val.v.val_str->refcount--;
4631
4632 /* vec::ordered_remove should help reduce the number of abbrevs
4633 that are needed. */
4634 die->die_attr->ordered_remove (ix);
4635 return;
4636 }
4637 }
4638
4639 /* Remove CHILD from its parent. PREV must have the property that
4640 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4641
4642 static void
4643 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4644 {
4645 gcc_assert (child->die_parent == prev->die_parent);
4646 gcc_assert (prev->die_sib == child);
4647 if (prev == child)
4648 {
4649 gcc_assert (child->die_parent->die_child == child);
4650 prev = NULL;
4651 }
4652 else
4653 prev->die_sib = child->die_sib;
4654 if (child->die_parent->die_child == child)
4655 child->die_parent->die_child = prev;
4656 }
4657
4658 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4659 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4660
4661 static void
4662 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4663 {
4664 dw_die_ref parent = old_child->die_parent;
4665
4666 gcc_assert (parent == prev->die_parent);
4667 gcc_assert (prev->die_sib == old_child);
4668
4669 new_child->die_parent = parent;
4670 if (prev == old_child)
4671 {
4672 gcc_assert (parent->die_child == old_child);
4673 new_child->die_sib = new_child;
4674 }
4675 else
4676 {
4677 prev->die_sib = new_child;
4678 new_child->die_sib = old_child->die_sib;
4679 }
4680 if (old_child->die_parent->die_child == old_child)
4681 old_child->die_parent->die_child = new_child;
4682 }
4683
4684 /* Move all children from OLD_PARENT to NEW_PARENT. */
4685
4686 static void
4687 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4688 {
4689 dw_die_ref c;
4690 new_parent->die_child = old_parent->die_child;
4691 old_parent->die_child = NULL;
4692 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4693 }
4694
4695 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4696 matches TAG. */
4697
4698 static void
4699 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4700 {
4701 dw_die_ref c;
4702
4703 c = die->die_child;
4704 if (c) do {
4705 dw_die_ref prev = c;
4706 c = c->die_sib;
4707 while (c->die_tag == tag)
4708 {
4709 remove_child_with_prev (c, prev);
4710 /* Might have removed every child. */
4711 if (c == c->die_sib)
4712 return;
4713 c = c->die_sib;
4714 }
4715 } while (c != die->die_child);
4716 }
4717
4718 /* Add a CHILD_DIE as the last child of DIE. */
4719
4720 static void
4721 add_child_die (dw_die_ref die, dw_die_ref child_die)
4722 {
4723 /* FIXME this should probably be an assert. */
4724 if (! die || ! child_die)
4725 return;
4726 gcc_assert (die != child_die);
4727
4728 child_die->die_parent = die;
4729 if (die->die_child)
4730 {
4731 child_die->die_sib = die->die_child->die_sib;
4732 die->die_child->die_sib = child_die;
4733 }
4734 else
4735 child_die->die_sib = child_die;
4736 die->die_child = child_die;
4737 }
4738
4739 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4740 is the specification, to the end of PARENT's list of children.
4741 This is done by removing and re-adding it. */
4742
4743 static void
4744 splice_child_die (dw_die_ref parent, dw_die_ref child)
4745 {
4746 dw_die_ref p;
4747
4748 /* We want the declaration DIE from inside the class, not the
4749 specification DIE at toplevel. */
4750 if (child->die_parent != parent)
4751 {
4752 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4753
4754 if (tmp)
4755 child = tmp;
4756 }
4757
4758 gcc_assert (child->die_parent == parent
4759 || (child->die_parent
4760 == get_AT_ref (parent, DW_AT_specification)));
4761
4762 for (p = child->die_parent->die_child; ; p = p->die_sib)
4763 if (p->die_sib == child)
4764 {
4765 remove_child_with_prev (child, p);
4766 break;
4767 }
4768
4769 add_child_die (parent, child);
4770 }
4771
4772 /* Return a pointer to a newly created DIE node. */
4773
4774 static inline dw_die_ref
4775 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4776 {
4777 dw_die_ref die = ggc_alloc_cleared_die_node ();
4778
4779 die->die_tag = tag_value;
4780
4781 if (parent_die != NULL)
4782 add_child_die (parent_die, die);
4783 else
4784 {
4785 limbo_die_node *limbo_node;
4786
4787 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4788 limbo_node->die = die;
4789 limbo_node->created_for = t;
4790 limbo_node->next = limbo_die_list;
4791 limbo_die_list = limbo_node;
4792 }
4793
4794 return die;
4795 }
4796
4797 /* Return the DIE associated with the given type specifier. */
4798
4799 static inline dw_die_ref
4800 lookup_type_die (tree type)
4801 {
4802 return TYPE_SYMTAB_DIE (type);
4803 }
4804
4805 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4806 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4807 anonymous type instead the one of the naming typedef. */
4808
4809 static inline dw_die_ref
4810 strip_naming_typedef (tree type, dw_die_ref type_die)
4811 {
4812 if (type
4813 && TREE_CODE (type) == RECORD_TYPE
4814 && type_die
4815 && type_die->die_tag == DW_TAG_typedef
4816 && is_naming_typedef_decl (TYPE_NAME (type)))
4817 type_die = get_AT_ref (type_die, DW_AT_type);
4818 return type_die;
4819 }
4820
4821 /* Like lookup_type_die, but if type is an anonymous type named by a
4822 typedef[1], return the DIE of the anonymous type instead the one of
4823 the naming typedef. This is because in gen_typedef_die, we did
4824 equate the anonymous struct named by the typedef with the DIE of
4825 the naming typedef. So by default, lookup_type_die on an anonymous
4826 struct yields the DIE of the naming typedef.
4827
4828 [1]: Read the comment of is_naming_typedef_decl to learn about what
4829 a naming typedef is. */
4830
4831 static inline dw_die_ref
4832 lookup_type_die_strip_naming_typedef (tree type)
4833 {
4834 dw_die_ref die = lookup_type_die (type);
4835 return strip_naming_typedef (type, die);
4836 }
4837
4838 /* Equate a DIE to a given type specifier. */
4839
4840 static inline void
4841 equate_type_number_to_die (tree type, dw_die_ref type_die)
4842 {
4843 TYPE_SYMTAB_DIE (type) = type_die;
4844 }
4845
4846 /* Returns a hash value for X (which really is a die_struct). */
4847
4848 static hashval_t
4849 decl_die_table_hash (const void *x)
4850 {
4851 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4852 }
4853
4854 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4855
4856 static int
4857 decl_die_table_eq (const void *x, const void *y)
4858 {
4859 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4860 }
4861
4862 /* Return the DIE associated with a given declaration. */
4863
4864 static inline dw_die_ref
4865 lookup_decl_die (tree decl)
4866 {
4867 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4868 }
4869
4870 /* Returns a hash value for X (which really is a var_loc_list). */
4871
4872 static hashval_t
4873 decl_loc_table_hash (const void *x)
4874 {
4875 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4876 }
4877
4878 /* Return nonzero if decl_id of var_loc_list X is the same as
4879 UID of decl *Y. */
4880
4881 static int
4882 decl_loc_table_eq (const void *x, const void *y)
4883 {
4884 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4885 }
4886
4887 /* Return the var_loc list associated with a given declaration. */
4888
4889 static inline var_loc_list *
4890 lookup_decl_loc (const_tree decl)
4891 {
4892 if (!decl_loc_table)
4893 return NULL;
4894 return (var_loc_list *)
4895 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4896 }
4897
4898 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4899
4900 static hashval_t
4901 cached_dw_loc_list_table_hash (const void *x)
4902 {
4903 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4904 }
4905
4906 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4907 UID of decl *Y. */
4908
4909 static int
4910 cached_dw_loc_list_table_eq (const void *x, const void *y)
4911 {
4912 return (((const cached_dw_loc_list *) x)->decl_id
4913 == DECL_UID ((const_tree) y));
4914 }
4915
4916 /* Equate a DIE to a particular declaration. */
4917
4918 static void
4919 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4920 {
4921 unsigned int decl_id = DECL_UID (decl);
4922 void **slot;
4923
4924 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4925 *slot = decl_die;
4926 decl_die->decl_id = decl_id;
4927 }
4928
4929 /* Return how many bits covers PIECE EXPR_LIST. */
4930
4931 static int
4932 decl_piece_bitsize (rtx piece)
4933 {
4934 int ret = (int) GET_MODE (piece);
4935 if (ret)
4936 return ret;
4937 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4938 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4939 return INTVAL (XEXP (XEXP (piece, 0), 0));
4940 }
4941
4942 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4943
4944 static rtx *
4945 decl_piece_varloc_ptr (rtx piece)
4946 {
4947 if ((int) GET_MODE (piece))
4948 return &XEXP (piece, 0);
4949 else
4950 return &XEXP (XEXP (piece, 0), 1);
4951 }
4952
4953 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4954 Next is the chain of following piece nodes. */
4955
4956 static rtx
4957 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4958 {
4959 if (bitsize <= (int) MAX_MACHINE_MODE)
4960 return alloc_EXPR_LIST (bitsize, loc_note, next);
4961 else
4962 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4963 GEN_INT (bitsize),
4964 loc_note), next);
4965 }
4966
4967 /* Return rtx that should be stored into loc field for
4968 LOC_NOTE and BITPOS/BITSIZE. */
4969
4970 static rtx
4971 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4972 HOST_WIDE_INT bitsize)
4973 {
4974 if (bitsize != -1)
4975 {
4976 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4977 if (bitpos != 0)
4978 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4979 }
4980 return loc_note;
4981 }
4982
4983 /* This function either modifies location piece list *DEST in
4984 place (if SRC and INNER is NULL), or copies location piece list
4985 *SRC to *DEST while modifying it. Location BITPOS is modified
4986 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4987 not copied and if needed some padding around it is added.
4988 When modifying in place, DEST should point to EXPR_LIST where
4989 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4990 to the start of the whole list and INNER points to the EXPR_LIST
4991 where earlier pieces cover PIECE_BITPOS bits. */
4992
4993 static void
4994 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4995 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4996 HOST_WIDE_INT bitsize, rtx loc_note)
4997 {
4998 int diff;
4999 bool copy = inner != NULL;
5000
5001 if (copy)
5002 {
5003 /* First copy all nodes preceding the current bitpos. */
5004 while (src != inner)
5005 {
5006 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5007 decl_piece_bitsize (*src), NULL_RTX);
5008 dest = &XEXP (*dest, 1);
5009 src = &XEXP (*src, 1);
5010 }
5011 }
5012 /* Add padding if needed. */
5013 if (bitpos != piece_bitpos)
5014 {
5015 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5016 copy ? NULL_RTX : *dest);
5017 dest = &XEXP (*dest, 1);
5018 }
5019 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5020 {
5021 gcc_assert (!copy);
5022 /* A piece with correct bitpos and bitsize already exist,
5023 just update the location for it and return. */
5024 *decl_piece_varloc_ptr (*dest) = loc_note;
5025 return;
5026 }
5027 /* Add the piece that changed. */
5028 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5029 dest = &XEXP (*dest, 1);
5030 /* Skip over pieces that overlap it. */
5031 diff = bitpos - piece_bitpos + bitsize;
5032 if (!copy)
5033 src = dest;
5034 while (diff > 0 && *src)
5035 {
5036 rtx piece = *src;
5037 diff -= decl_piece_bitsize (piece);
5038 if (copy)
5039 src = &XEXP (piece, 1);
5040 else
5041 {
5042 *src = XEXP (piece, 1);
5043 free_EXPR_LIST_node (piece);
5044 }
5045 }
5046 /* Add padding if needed. */
5047 if (diff < 0 && *src)
5048 {
5049 if (!copy)
5050 dest = src;
5051 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5052 dest = &XEXP (*dest, 1);
5053 }
5054 if (!copy)
5055 return;
5056 /* Finally copy all nodes following it. */
5057 while (*src)
5058 {
5059 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5060 decl_piece_bitsize (*src), NULL_RTX);
5061 dest = &XEXP (*dest, 1);
5062 src = &XEXP (*src, 1);
5063 }
5064 }
5065
5066 /* Add a variable location node to the linked list for DECL. */
5067
5068 static struct var_loc_node *
5069 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5070 {
5071 unsigned int decl_id;
5072 var_loc_list *temp;
5073 void **slot;
5074 struct var_loc_node *loc = NULL;
5075 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5076
5077 if (TREE_CODE (decl) == VAR_DECL
5078 && DECL_HAS_DEBUG_EXPR_P (decl))
5079 {
5080 tree realdecl = DECL_DEBUG_EXPR (decl);
5081 if (handled_component_p (realdecl)
5082 || (TREE_CODE (realdecl) == MEM_REF
5083 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5084 {
5085 HOST_WIDE_INT maxsize;
5086 tree innerdecl;
5087 innerdecl
5088 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5089 if (!DECL_P (innerdecl)
5090 || DECL_IGNORED_P (innerdecl)
5091 || TREE_STATIC (innerdecl)
5092 || bitsize <= 0
5093 || bitpos + bitsize > 256
5094 || bitsize != maxsize)
5095 return NULL;
5096 decl = innerdecl;
5097 }
5098 }
5099
5100 decl_id = DECL_UID (decl);
5101 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5102 if (*slot == NULL)
5103 {
5104 temp = ggc_alloc_cleared_var_loc_list ();
5105 temp->decl_id = decl_id;
5106 *slot = temp;
5107 }
5108 else
5109 temp = (var_loc_list *) *slot;
5110
5111 /* For PARM_DECLs try to keep around the original incoming value,
5112 even if that means we'll emit a zero-range .debug_loc entry. */
5113 if (temp->last
5114 && temp->first == temp->last
5115 && TREE_CODE (decl) == PARM_DECL
5116 && NOTE_P (temp->first->loc)
5117 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5118 && DECL_INCOMING_RTL (decl)
5119 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5120 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5121 == GET_CODE (DECL_INCOMING_RTL (decl))
5122 && prev_real_insn (temp->first->loc) == NULL_RTX
5123 && (bitsize != -1
5124 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5125 NOTE_VAR_LOCATION_LOC (loc_note))
5126 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5127 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5128 {
5129 loc = ggc_alloc_cleared_var_loc_node ();
5130 temp->first->next = loc;
5131 temp->last = loc;
5132 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5133 }
5134 else if (temp->last)
5135 {
5136 struct var_loc_node *last = temp->last, *unused = NULL;
5137 rtx *piece_loc = NULL, last_loc_note;
5138 int piece_bitpos = 0;
5139 if (last->next)
5140 {
5141 last = last->next;
5142 gcc_assert (last->next == NULL);
5143 }
5144 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5145 {
5146 piece_loc = &last->loc;
5147 do
5148 {
5149 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5150 if (piece_bitpos + cur_bitsize > bitpos)
5151 break;
5152 piece_bitpos += cur_bitsize;
5153 piece_loc = &XEXP (*piece_loc, 1);
5154 }
5155 while (*piece_loc);
5156 }
5157 /* TEMP->LAST here is either pointer to the last but one or
5158 last element in the chained list, LAST is pointer to the
5159 last element. */
5160 if (label && strcmp (last->label, label) == 0)
5161 {
5162 /* For SRA optimized variables if there weren't any real
5163 insns since last note, just modify the last node. */
5164 if (piece_loc != NULL)
5165 {
5166 adjust_piece_list (piece_loc, NULL, NULL,
5167 bitpos, piece_bitpos, bitsize, loc_note);
5168 return NULL;
5169 }
5170 /* If the last note doesn't cover any instructions, remove it. */
5171 if (temp->last != last)
5172 {
5173 temp->last->next = NULL;
5174 unused = last;
5175 last = temp->last;
5176 gcc_assert (strcmp (last->label, label) != 0);
5177 }
5178 else
5179 {
5180 gcc_assert (temp->first == temp->last
5181 || (temp->first->next == temp->last
5182 && TREE_CODE (decl) == PARM_DECL));
5183 memset (temp->last, '\0', sizeof (*temp->last));
5184 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5185 return temp->last;
5186 }
5187 }
5188 if (bitsize == -1 && NOTE_P (last->loc))
5189 last_loc_note = last->loc;
5190 else if (piece_loc != NULL
5191 && *piece_loc != NULL_RTX
5192 && piece_bitpos == bitpos
5193 && decl_piece_bitsize (*piece_loc) == bitsize)
5194 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5195 else
5196 last_loc_note = NULL_RTX;
5197 /* If the current location is the same as the end of the list,
5198 and either both or neither of the locations is uninitialized,
5199 we have nothing to do. */
5200 if (last_loc_note == NULL_RTX
5201 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5202 NOTE_VAR_LOCATION_LOC (loc_note)))
5203 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5204 != NOTE_VAR_LOCATION_STATUS (loc_note))
5205 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5206 == VAR_INIT_STATUS_UNINITIALIZED)
5207 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5208 == VAR_INIT_STATUS_UNINITIALIZED))))
5209 {
5210 /* Add LOC to the end of list and update LAST. If the last
5211 element of the list has been removed above, reuse its
5212 memory for the new node, otherwise allocate a new one. */
5213 if (unused)
5214 {
5215 loc = unused;
5216 memset (loc, '\0', sizeof (*loc));
5217 }
5218 else
5219 loc = ggc_alloc_cleared_var_loc_node ();
5220 if (bitsize == -1 || piece_loc == NULL)
5221 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5222 else
5223 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5224 bitpos, piece_bitpos, bitsize, loc_note);
5225 last->next = loc;
5226 /* Ensure TEMP->LAST will point either to the new last but one
5227 element of the chain, or to the last element in it. */
5228 if (last != temp->last)
5229 temp->last = last;
5230 }
5231 else if (unused)
5232 ggc_free (unused);
5233 }
5234 else
5235 {
5236 loc = ggc_alloc_cleared_var_loc_node ();
5237 temp->first = loc;
5238 temp->last = loc;
5239 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5240 }
5241 return loc;
5242 }
5243 \f
5244 /* Keep track of the number of spaces used to indent the
5245 output of the debugging routines that print the structure of
5246 the DIE internal representation. */
5247 static int print_indent;
5248
5249 /* Indent the line the number of spaces given by print_indent. */
5250
5251 static inline void
5252 print_spaces (FILE *outfile)
5253 {
5254 fprintf (outfile, "%*s", print_indent, "");
5255 }
5256
5257 /* Print a type signature in hex. */
5258
5259 static inline void
5260 print_signature (FILE *outfile, char *sig)
5261 {
5262 int i;
5263
5264 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5265 fprintf (outfile, "%02x", sig[i] & 0xff);
5266 }
5267
5268 /* Print the information associated with a given DIE, and its children.
5269 This routine is a debugging aid only. */
5270
5271 static void
5272 print_die (dw_die_ref die, FILE *outfile)
5273 {
5274 dw_attr_ref a;
5275 dw_die_ref c;
5276 unsigned ix;
5277
5278 print_spaces (outfile);
5279 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5280 die->die_offset, dwarf_tag_name (die->die_tag),
5281 (void*) die);
5282 print_spaces (outfile);
5283 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5284 fprintf (outfile, " offset: %ld", die->die_offset);
5285 fprintf (outfile, " mark: %d\n", die->die_mark);
5286
5287 if (die->comdat_type_p)
5288 {
5289 print_spaces (outfile);
5290 fprintf (outfile, " signature: ");
5291 print_signature (outfile, die->die_id.die_type_node->signature);
5292 fprintf (outfile, "\n");
5293 }
5294
5295 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5296 {
5297 print_spaces (outfile);
5298 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5299
5300 switch (AT_class (a))
5301 {
5302 case dw_val_class_addr:
5303 fprintf (outfile, "address");
5304 break;
5305 case dw_val_class_offset:
5306 fprintf (outfile, "offset");
5307 break;
5308 case dw_val_class_loc:
5309 fprintf (outfile, "location descriptor");
5310 break;
5311 case dw_val_class_loc_list:
5312 fprintf (outfile, "location list -> label:%s",
5313 AT_loc_list (a)->ll_symbol);
5314 break;
5315 case dw_val_class_range_list:
5316 fprintf (outfile, "range list");
5317 break;
5318 case dw_val_class_const:
5319 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5320 break;
5321 case dw_val_class_unsigned_const:
5322 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5323 break;
5324 case dw_val_class_const_double:
5325 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5326 HOST_WIDE_INT_PRINT_UNSIGNED")",
5327 a->dw_attr_val.v.val_double.high,
5328 a->dw_attr_val.v.val_double.low);
5329 break;
5330 case dw_val_class_vec:
5331 fprintf (outfile, "floating-point or vector constant");
5332 break;
5333 case dw_val_class_flag:
5334 fprintf (outfile, "%u", AT_flag (a));
5335 break;
5336 case dw_val_class_die_ref:
5337 if (AT_ref (a) != NULL)
5338 {
5339 if (AT_ref (a)->comdat_type_p)
5340 {
5341 fprintf (outfile, "die -> signature: ");
5342 print_signature (outfile,
5343 AT_ref (a)->die_id.die_type_node->signature);
5344 }
5345 else if (AT_ref (a)->die_id.die_symbol)
5346 fprintf (outfile, "die -> label: %s",
5347 AT_ref (a)->die_id.die_symbol);
5348 else
5349 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5350 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5351 }
5352 else
5353 fprintf (outfile, "die -> <null>");
5354 break;
5355 case dw_val_class_vms_delta:
5356 fprintf (outfile, "delta: @slotcount(%s-%s)",
5357 AT_vms_delta2 (a), AT_vms_delta1 (a));
5358 break;
5359 case dw_val_class_lbl_id:
5360 case dw_val_class_lineptr:
5361 case dw_val_class_macptr:
5362 case dw_val_class_high_pc:
5363 fprintf (outfile, "label: %s", AT_lbl (a));
5364 break;
5365 case dw_val_class_str:
5366 if (AT_string (a) != NULL)
5367 fprintf (outfile, "\"%s\"", AT_string (a));
5368 else
5369 fprintf (outfile, "<null>");
5370 break;
5371 case dw_val_class_file:
5372 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5373 AT_file (a)->emitted_number);
5374 break;
5375 case dw_val_class_data8:
5376 {
5377 int i;
5378
5379 for (i = 0; i < 8; i++)
5380 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5381 break;
5382 }
5383 default:
5384 break;
5385 }
5386
5387 fprintf (outfile, "\n");
5388 }
5389
5390 if (die->die_child != NULL)
5391 {
5392 print_indent += 4;
5393 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5394 print_indent -= 4;
5395 }
5396 if (print_indent == 0)
5397 fprintf (outfile, "\n");
5398 }
5399
5400 /* Print the information collected for a given DIE. */
5401
5402 DEBUG_FUNCTION void
5403 debug_dwarf_die (dw_die_ref die)
5404 {
5405 print_die (die, stderr);
5406 }
5407
5408 DEBUG_FUNCTION void
5409 debug (die_struct &ref)
5410 {
5411 print_die (&ref, stderr);
5412 }
5413
5414 DEBUG_FUNCTION void
5415 debug (die_struct *ptr)
5416 {
5417 if (ptr)
5418 debug (*ptr);
5419 else
5420 fprintf (stderr, "<nil>\n");
5421 }
5422
5423
5424 /* Print all DWARF information collected for the compilation unit.
5425 This routine is a debugging aid only. */
5426
5427 DEBUG_FUNCTION void
5428 debug_dwarf (void)
5429 {
5430 print_indent = 0;
5431 print_die (comp_unit_die (), stderr);
5432 }
5433 \f
5434 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5435 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5436 DIE that marks the start of the DIEs for this include file. */
5437
5438 static dw_die_ref
5439 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5440 {
5441 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5442 dw_die_ref new_unit = gen_compile_unit_die (filename);
5443
5444 new_unit->die_sib = old_unit;
5445 return new_unit;
5446 }
5447
5448 /* Close an include-file CU and reopen the enclosing one. */
5449
5450 static dw_die_ref
5451 pop_compile_unit (dw_die_ref old_unit)
5452 {
5453 dw_die_ref new_unit = old_unit->die_sib;
5454
5455 old_unit->die_sib = NULL;
5456 return new_unit;
5457 }
5458
5459 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5460 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5461 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5462
5463 /* Calculate the checksum of a location expression. */
5464
5465 static inline void
5466 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5467 {
5468 int tem;
5469 hashval_t hash = 0;
5470
5471 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5472 CHECKSUM (tem);
5473 hash = hash_loc_operands (loc, hash);
5474 CHECKSUM (hash);
5475 }
5476
5477 /* Calculate the checksum of an attribute. */
5478
5479 static void
5480 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5481 {
5482 dw_loc_descr_ref loc;
5483 rtx r;
5484
5485 CHECKSUM (at->dw_attr);
5486
5487 /* We don't care that this was compiled with a different compiler
5488 snapshot; if the output is the same, that's what matters. */
5489 if (at->dw_attr == DW_AT_producer)
5490 return;
5491
5492 switch (AT_class (at))
5493 {
5494 case dw_val_class_const:
5495 CHECKSUM (at->dw_attr_val.v.val_int);
5496 break;
5497 case dw_val_class_unsigned_const:
5498 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5499 break;
5500 case dw_val_class_const_double:
5501 CHECKSUM (at->dw_attr_val.v.val_double);
5502 break;
5503 case dw_val_class_vec:
5504 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5505 (at->dw_attr_val.v.val_vec.length
5506 * at->dw_attr_val.v.val_vec.elt_size));
5507 break;
5508 case dw_val_class_flag:
5509 CHECKSUM (at->dw_attr_val.v.val_flag);
5510 break;
5511 case dw_val_class_str:
5512 CHECKSUM_STRING (AT_string (at));
5513 break;
5514
5515 case dw_val_class_addr:
5516 r = AT_addr (at);
5517 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5518 CHECKSUM_STRING (XSTR (r, 0));
5519 break;
5520
5521 case dw_val_class_offset:
5522 CHECKSUM (at->dw_attr_val.v.val_offset);
5523 break;
5524
5525 case dw_val_class_loc:
5526 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5527 loc_checksum (loc, ctx);
5528 break;
5529
5530 case dw_val_class_die_ref:
5531 die_checksum (AT_ref (at), ctx, mark);
5532 break;
5533
5534 case dw_val_class_fde_ref:
5535 case dw_val_class_vms_delta:
5536 case dw_val_class_lbl_id:
5537 case dw_val_class_lineptr:
5538 case dw_val_class_macptr:
5539 case dw_val_class_high_pc:
5540 break;
5541
5542 case dw_val_class_file:
5543 CHECKSUM_STRING (AT_file (at)->filename);
5544 break;
5545
5546 case dw_val_class_data8:
5547 CHECKSUM (at->dw_attr_val.v.val_data8);
5548 break;
5549
5550 default:
5551 break;
5552 }
5553 }
5554
5555 /* Calculate the checksum of a DIE. */
5556
5557 static void
5558 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5559 {
5560 dw_die_ref c;
5561 dw_attr_ref a;
5562 unsigned ix;
5563
5564 /* To avoid infinite recursion. */
5565 if (die->die_mark)
5566 {
5567 CHECKSUM (die->die_mark);
5568 return;
5569 }
5570 die->die_mark = ++(*mark);
5571
5572 CHECKSUM (die->die_tag);
5573
5574 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5575 attr_checksum (a, ctx, mark);
5576
5577 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5578 }
5579
5580 #undef CHECKSUM
5581 #undef CHECKSUM_BLOCK
5582 #undef CHECKSUM_STRING
5583
5584 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5585 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5586 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5587 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5588 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5589 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5590 #define CHECKSUM_ATTR(FOO) \
5591 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5592
5593 /* Calculate the checksum of a number in signed LEB128 format. */
5594
5595 static void
5596 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5597 {
5598 unsigned char byte;
5599 bool more;
5600
5601 while (1)
5602 {
5603 byte = (value & 0x7f);
5604 value >>= 7;
5605 more = !((value == 0 && (byte & 0x40) == 0)
5606 || (value == -1 && (byte & 0x40) != 0));
5607 if (more)
5608 byte |= 0x80;
5609 CHECKSUM (byte);
5610 if (!more)
5611 break;
5612 }
5613 }
5614
5615 /* Calculate the checksum of a number in unsigned LEB128 format. */
5616
5617 static void
5618 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5619 {
5620 while (1)
5621 {
5622 unsigned char byte = (value & 0x7f);
5623 value >>= 7;
5624 if (value != 0)
5625 /* More bytes to follow. */
5626 byte |= 0x80;
5627 CHECKSUM (byte);
5628 if (value == 0)
5629 break;
5630 }
5631 }
5632
5633 /* Checksum the context of the DIE. This adds the names of any
5634 surrounding namespaces or structures to the checksum. */
5635
5636 static void
5637 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5638 {
5639 const char *name;
5640 dw_die_ref spec;
5641 int tag = die->die_tag;
5642
5643 if (tag != DW_TAG_namespace
5644 && tag != DW_TAG_structure_type
5645 && tag != DW_TAG_class_type)
5646 return;
5647
5648 name = get_AT_string (die, DW_AT_name);
5649
5650 spec = get_AT_ref (die, DW_AT_specification);
5651 if (spec != NULL)
5652 die = spec;
5653
5654 if (die->die_parent != NULL)
5655 checksum_die_context (die->die_parent, ctx);
5656
5657 CHECKSUM_ULEB128 ('C');
5658 CHECKSUM_ULEB128 (tag);
5659 if (name != NULL)
5660 CHECKSUM_STRING (name);
5661 }
5662
5663 /* Calculate the checksum of a location expression. */
5664
5665 static inline void
5666 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5667 {
5668 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5669 were emitted as a DW_FORM_sdata instead of a location expression. */
5670 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5671 {
5672 CHECKSUM_ULEB128 (DW_FORM_sdata);
5673 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5674 return;
5675 }
5676
5677 /* Otherwise, just checksum the raw location expression. */
5678 while (loc != NULL)
5679 {
5680 hashval_t hash = 0;
5681
5682 CHECKSUM_ULEB128 (loc->dtprel);
5683 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5684 hash = hash_loc_operands (loc, hash);
5685 CHECKSUM (hash);
5686 loc = loc->dw_loc_next;
5687 }
5688 }
5689
5690 /* Calculate the checksum of an attribute. */
5691
5692 static void
5693 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5694 struct md5_ctx *ctx, int *mark)
5695 {
5696 dw_loc_descr_ref loc;
5697 rtx r;
5698
5699 if (AT_class (at) == dw_val_class_die_ref)
5700 {
5701 dw_die_ref target_die = AT_ref (at);
5702
5703 /* For pointer and reference types, we checksum only the (qualified)
5704 name of the target type (if there is a name). For friend entries,
5705 we checksum only the (qualified) name of the target type or function.
5706 This allows the checksum to remain the same whether the target type
5707 is complete or not. */
5708 if ((at->dw_attr == DW_AT_type
5709 && (tag == DW_TAG_pointer_type
5710 || tag == DW_TAG_reference_type
5711 || tag == DW_TAG_rvalue_reference_type
5712 || tag == DW_TAG_ptr_to_member_type))
5713 || (at->dw_attr == DW_AT_friend
5714 && tag == DW_TAG_friend))
5715 {
5716 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5717
5718 if (name_attr != NULL)
5719 {
5720 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5721
5722 if (decl == NULL)
5723 decl = target_die;
5724 CHECKSUM_ULEB128 ('N');
5725 CHECKSUM_ULEB128 (at->dw_attr);
5726 if (decl->die_parent != NULL)
5727 checksum_die_context (decl->die_parent, ctx);
5728 CHECKSUM_ULEB128 ('E');
5729 CHECKSUM_STRING (AT_string (name_attr));
5730 return;
5731 }
5732 }
5733
5734 /* For all other references to another DIE, we check to see if the
5735 target DIE has already been visited. If it has, we emit a
5736 backward reference; if not, we descend recursively. */
5737 if (target_die->die_mark > 0)
5738 {
5739 CHECKSUM_ULEB128 ('R');
5740 CHECKSUM_ULEB128 (at->dw_attr);
5741 CHECKSUM_ULEB128 (target_die->die_mark);
5742 }
5743 else
5744 {
5745 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5746
5747 if (decl == NULL)
5748 decl = target_die;
5749 target_die->die_mark = ++(*mark);
5750 CHECKSUM_ULEB128 ('T');
5751 CHECKSUM_ULEB128 (at->dw_attr);
5752 if (decl->die_parent != NULL)
5753 checksum_die_context (decl->die_parent, ctx);
5754 die_checksum_ordered (target_die, ctx, mark);
5755 }
5756 return;
5757 }
5758
5759 CHECKSUM_ULEB128 ('A');
5760 CHECKSUM_ULEB128 (at->dw_attr);
5761
5762 switch (AT_class (at))
5763 {
5764 case dw_val_class_const:
5765 CHECKSUM_ULEB128 (DW_FORM_sdata);
5766 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5767 break;
5768
5769 case dw_val_class_unsigned_const:
5770 CHECKSUM_ULEB128 (DW_FORM_sdata);
5771 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5772 break;
5773
5774 case dw_val_class_const_double:
5775 CHECKSUM_ULEB128 (DW_FORM_block);
5776 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5777 CHECKSUM (at->dw_attr_val.v.val_double);
5778 break;
5779
5780 case dw_val_class_vec:
5781 CHECKSUM_ULEB128 (DW_FORM_block);
5782 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5783 * at->dw_attr_val.v.val_vec.elt_size);
5784 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5785 (at->dw_attr_val.v.val_vec.length
5786 * at->dw_attr_val.v.val_vec.elt_size));
5787 break;
5788
5789 case dw_val_class_flag:
5790 CHECKSUM_ULEB128 (DW_FORM_flag);
5791 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5792 break;
5793
5794 case dw_val_class_str:
5795 CHECKSUM_ULEB128 (DW_FORM_string);
5796 CHECKSUM_STRING (AT_string (at));
5797 break;
5798
5799 case dw_val_class_addr:
5800 r = AT_addr (at);
5801 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5802 CHECKSUM_ULEB128 (DW_FORM_string);
5803 CHECKSUM_STRING (XSTR (r, 0));
5804 break;
5805
5806 case dw_val_class_offset:
5807 CHECKSUM_ULEB128 (DW_FORM_sdata);
5808 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5809 break;
5810
5811 case dw_val_class_loc:
5812 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5813 loc_checksum_ordered (loc, ctx);
5814 break;
5815
5816 case dw_val_class_fde_ref:
5817 case dw_val_class_lbl_id:
5818 case dw_val_class_lineptr:
5819 case dw_val_class_macptr:
5820 case dw_val_class_high_pc:
5821 break;
5822
5823 case dw_val_class_file:
5824 CHECKSUM_ULEB128 (DW_FORM_string);
5825 CHECKSUM_STRING (AT_file (at)->filename);
5826 break;
5827
5828 case dw_val_class_data8:
5829 CHECKSUM (at->dw_attr_val.v.val_data8);
5830 break;
5831
5832 default:
5833 break;
5834 }
5835 }
5836
5837 struct checksum_attributes
5838 {
5839 dw_attr_ref at_name;
5840 dw_attr_ref at_type;
5841 dw_attr_ref at_friend;
5842 dw_attr_ref at_accessibility;
5843 dw_attr_ref at_address_class;
5844 dw_attr_ref at_allocated;
5845 dw_attr_ref at_artificial;
5846 dw_attr_ref at_associated;
5847 dw_attr_ref at_binary_scale;
5848 dw_attr_ref at_bit_offset;
5849 dw_attr_ref at_bit_size;
5850 dw_attr_ref at_bit_stride;
5851 dw_attr_ref at_byte_size;
5852 dw_attr_ref at_byte_stride;
5853 dw_attr_ref at_const_value;
5854 dw_attr_ref at_containing_type;
5855 dw_attr_ref at_count;
5856 dw_attr_ref at_data_location;
5857 dw_attr_ref at_data_member_location;
5858 dw_attr_ref at_decimal_scale;
5859 dw_attr_ref at_decimal_sign;
5860 dw_attr_ref at_default_value;
5861 dw_attr_ref at_digit_count;
5862 dw_attr_ref at_discr;
5863 dw_attr_ref at_discr_list;
5864 dw_attr_ref at_discr_value;
5865 dw_attr_ref at_encoding;
5866 dw_attr_ref at_endianity;
5867 dw_attr_ref at_explicit;
5868 dw_attr_ref at_is_optional;
5869 dw_attr_ref at_location;
5870 dw_attr_ref at_lower_bound;
5871 dw_attr_ref at_mutable;
5872 dw_attr_ref at_ordering;
5873 dw_attr_ref at_picture_string;
5874 dw_attr_ref at_prototyped;
5875 dw_attr_ref at_small;
5876 dw_attr_ref at_segment;
5877 dw_attr_ref at_string_length;
5878 dw_attr_ref at_threads_scaled;
5879 dw_attr_ref at_upper_bound;
5880 dw_attr_ref at_use_location;
5881 dw_attr_ref at_use_UTF8;
5882 dw_attr_ref at_variable_parameter;
5883 dw_attr_ref at_virtuality;
5884 dw_attr_ref at_visibility;
5885 dw_attr_ref at_vtable_elem_location;
5886 };
5887
5888 /* Collect the attributes that we will want to use for the checksum. */
5889
5890 static void
5891 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5892 {
5893 dw_attr_ref a;
5894 unsigned ix;
5895
5896 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5897 {
5898 switch (a->dw_attr)
5899 {
5900 case DW_AT_name:
5901 attrs->at_name = a;
5902 break;
5903 case DW_AT_type:
5904 attrs->at_type = a;
5905 break;
5906 case DW_AT_friend:
5907 attrs->at_friend = a;
5908 break;
5909 case DW_AT_accessibility:
5910 attrs->at_accessibility = a;
5911 break;
5912 case DW_AT_address_class:
5913 attrs->at_address_class = a;
5914 break;
5915 case DW_AT_allocated:
5916 attrs->at_allocated = a;
5917 break;
5918 case DW_AT_artificial:
5919 attrs->at_artificial = a;
5920 break;
5921 case DW_AT_associated:
5922 attrs->at_associated = a;
5923 break;
5924 case DW_AT_binary_scale:
5925 attrs->at_binary_scale = a;
5926 break;
5927 case DW_AT_bit_offset:
5928 attrs->at_bit_offset = a;
5929 break;
5930 case DW_AT_bit_size:
5931 attrs->at_bit_size = a;
5932 break;
5933 case DW_AT_bit_stride:
5934 attrs->at_bit_stride = a;
5935 break;
5936 case DW_AT_byte_size:
5937 attrs->at_byte_size = a;
5938 break;
5939 case DW_AT_byte_stride:
5940 attrs->at_byte_stride = a;
5941 break;
5942 case DW_AT_const_value:
5943 attrs->at_const_value = a;
5944 break;
5945 case DW_AT_containing_type:
5946 attrs->at_containing_type = a;
5947 break;
5948 case DW_AT_count:
5949 attrs->at_count = a;
5950 break;
5951 case DW_AT_data_location:
5952 attrs->at_data_location = a;
5953 break;
5954 case DW_AT_data_member_location:
5955 attrs->at_data_member_location = a;
5956 break;
5957 case DW_AT_decimal_scale:
5958 attrs->at_decimal_scale = a;
5959 break;
5960 case DW_AT_decimal_sign:
5961 attrs->at_decimal_sign = a;
5962 break;
5963 case DW_AT_default_value:
5964 attrs->at_default_value = a;
5965 break;
5966 case DW_AT_digit_count:
5967 attrs->at_digit_count = a;
5968 break;
5969 case DW_AT_discr:
5970 attrs->at_discr = a;
5971 break;
5972 case DW_AT_discr_list:
5973 attrs->at_discr_list = a;
5974 break;
5975 case DW_AT_discr_value:
5976 attrs->at_discr_value = a;
5977 break;
5978 case DW_AT_encoding:
5979 attrs->at_encoding = a;
5980 break;
5981 case DW_AT_endianity:
5982 attrs->at_endianity = a;
5983 break;
5984 case DW_AT_explicit:
5985 attrs->at_explicit = a;
5986 break;
5987 case DW_AT_is_optional:
5988 attrs->at_is_optional = a;
5989 break;
5990 case DW_AT_location:
5991 attrs->at_location = a;
5992 break;
5993 case DW_AT_lower_bound:
5994 attrs->at_lower_bound = a;
5995 break;
5996 case DW_AT_mutable:
5997 attrs->at_mutable = a;
5998 break;
5999 case DW_AT_ordering:
6000 attrs->at_ordering = a;
6001 break;
6002 case DW_AT_picture_string:
6003 attrs->at_picture_string = a;
6004 break;
6005 case DW_AT_prototyped:
6006 attrs->at_prototyped = a;
6007 break;
6008 case DW_AT_small:
6009 attrs->at_small = a;
6010 break;
6011 case DW_AT_segment:
6012 attrs->at_segment = a;
6013 break;
6014 case DW_AT_string_length:
6015 attrs->at_string_length = a;
6016 break;
6017 case DW_AT_threads_scaled:
6018 attrs->at_threads_scaled = a;
6019 break;
6020 case DW_AT_upper_bound:
6021 attrs->at_upper_bound = a;
6022 break;
6023 case DW_AT_use_location:
6024 attrs->at_use_location = a;
6025 break;
6026 case DW_AT_use_UTF8:
6027 attrs->at_use_UTF8 = a;
6028 break;
6029 case DW_AT_variable_parameter:
6030 attrs->at_variable_parameter = a;
6031 break;
6032 case DW_AT_virtuality:
6033 attrs->at_virtuality = a;
6034 break;
6035 case DW_AT_visibility:
6036 attrs->at_visibility = a;
6037 break;
6038 case DW_AT_vtable_elem_location:
6039 attrs->at_vtable_elem_location = a;
6040 break;
6041 default:
6042 break;
6043 }
6044 }
6045 }
6046
6047 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6048
6049 static void
6050 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6051 {
6052 dw_die_ref c;
6053 dw_die_ref decl;
6054 struct checksum_attributes attrs;
6055
6056 CHECKSUM_ULEB128 ('D');
6057 CHECKSUM_ULEB128 (die->die_tag);
6058
6059 memset (&attrs, 0, sizeof (attrs));
6060
6061 decl = get_AT_ref (die, DW_AT_specification);
6062 if (decl != NULL)
6063 collect_checksum_attributes (&attrs, decl);
6064 collect_checksum_attributes (&attrs, die);
6065
6066 CHECKSUM_ATTR (attrs.at_name);
6067 CHECKSUM_ATTR (attrs.at_accessibility);
6068 CHECKSUM_ATTR (attrs.at_address_class);
6069 CHECKSUM_ATTR (attrs.at_allocated);
6070 CHECKSUM_ATTR (attrs.at_artificial);
6071 CHECKSUM_ATTR (attrs.at_associated);
6072 CHECKSUM_ATTR (attrs.at_binary_scale);
6073 CHECKSUM_ATTR (attrs.at_bit_offset);
6074 CHECKSUM_ATTR (attrs.at_bit_size);
6075 CHECKSUM_ATTR (attrs.at_bit_stride);
6076 CHECKSUM_ATTR (attrs.at_byte_size);
6077 CHECKSUM_ATTR (attrs.at_byte_stride);
6078 CHECKSUM_ATTR (attrs.at_const_value);
6079 CHECKSUM_ATTR (attrs.at_containing_type);
6080 CHECKSUM_ATTR (attrs.at_count);
6081 CHECKSUM_ATTR (attrs.at_data_location);
6082 CHECKSUM_ATTR (attrs.at_data_member_location);
6083 CHECKSUM_ATTR (attrs.at_decimal_scale);
6084 CHECKSUM_ATTR (attrs.at_decimal_sign);
6085 CHECKSUM_ATTR (attrs.at_default_value);
6086 CHECKSUM_ATTR (attrs.at_digit_count);
6087 CHECKSUM_ATTR (attrs.at_discr);
6088 CHECKSUM_ATTR (attrs.at_discr_list);
6089 CHECKSUM_ATTR (attrs.at_discr_value);
6090 CHECKSUM_ATTR (attrs.at_encoding);
6091 CHECKSUM_ATTR (attrs.at_endianity);
6092 CHECKSUM_ATTR (attrs.at_explicit);
6093 CHECKSUM_ATTR (attrs.at_is_optional);
6094 CHECKSUM_ATTR (attrs.at_location);
6095 CHECKSUM_ATTR (attrs.at_lower_bound);
6096 CHECKSUM_ATTR (attrs.at_mutable);
6097 CHECKSUM_ATTR (attrs.at_ordering);
6098 CHECKSUM_ATTR (attrs.at_picture_string);
6099 CHECKSUM_ATTR (attrs.at_prototyped);
6100 CHECKSUM_ATTR (attrs.at_small);
6101 CHECKSUM_ATTR (attrs.at_segment);
6102 CHECKSUM_ATTR (attrs.at_string_length);
6103 CHECKSUM_ATTR (attrs.at_threads_scaled);
6104 CHECKSUM_ATTR (attrs.at_upper_bound);
6105 CHECKSUM_ATTR (attrs.at_use_location);
6106 CHECKSUM_ATTR (attrs.at_use_UTF8);
6107 CHECKSUM_ATTR (attrs.at_variable_parameter);
6108 CHECKSUM_ATTR (attrs.at_virtuality);
6109 CHECKSUM_ATTR (attrs.at_visibility);
6110 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6111 CHECKSUM_ATTR (attrs.at_type);
6112 CHECKSUM_ATTR (attrs.at_friend);
6113
6114 /* Checksum the child DIEs. */
6115 c = die->die_child;
6116 if (c) do {
6117 dw_attr_ref name_attr;
6118
6119 c = c->die_sib;
6120 name_attr = get_AT (c, DW_AT_name);
6121 if (is_template_instantiation (c))
6122 {
6123 /* Ignore instantiations of member type and function templates. */
6124 }
6125 else if (name_attr != NULL
6126 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6127 {
6128 /* Use a shallow checksum for named nested types and member
6129 functions. */
6130 CHECKSUM_ULEB128 ('S');
6131 CHECKSUM_ULEB128 (c->die_tag);
6132 CHECKSUM_STRING (AT_string (name_attr));
6133 }
6134 else
6135 {
6136 /* Use a deep checksum for other children. */
6137 /* Mark this DIE so it gets processed when unmarking. */
6138 if (c->die_mark == 0)
6139 c->die_mark = -1;
6140 die_checksum_ordered (c, ctx, mark);
6141 }
6142 } while (c != die->die_child);
6143
6144 CHECKSUM_ULEB128 (0);
6145 }
6146
6147 /* Add a type name and tag to a hash. */
6148 static void
6149 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6150 {
6151 CHECKSUM_ULEB128 (tag);
6152 CHECKSUM_STRING (name);
6153 }
6154
6155 #undef CHECKSUM
6156 #undef CHECKSUM_STRING
6157 #undef CHECKSUM_ATTR
6158 #undef CHECKSUM_LEB128
6159 #undef CHECKSUM_ULEB128
6160
6161 /* Generate the type signature for DIE. This is computed by generating an
6162 MD5 checksum over the DIE's tag, its relevant attributes, and its
6163 children. Attributes that are references to other DIEs are processed
6164 by recursion, using the MARK field to prevent infinite recursion.
6165 If the DIE is nested inside a namespace or another type, we also
6166 need to include that context in the signature. The lower 64 bits
6167 of the resulting MD5 checksum comprise the signature. */
6168
6169 static void
6170 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6171 {
6172 int mark;
6173 const char *name;
6174 unsigned char checksum[16];
6175 struct md5_ctx ctx;
6176 dw_die_ref decl;
6177 dw_die_ref parent;
6178
6179 name = get_AT_string (die, DW_AT_name);
6180 decl = get_AT_ref (die, DW_AT_specification);
6181 parent = get_die_parent (die);
6182
6183 /* First, compute a signature for just the type name (and its surrounding
6184 context, if any. This is stored in the type unit DIE for link-time
6185 ODR (one-definition rule) checking. */
6186
6187 if (is_cxx () && name != NULL)
6188 {
6189 md5_init_ctx (&ctx);
6190
6191 /* Checksum the names of surrounding namespaces and structures. */
6192 if (parent != NULL)
6193 checksum_die_context (parent, &ctx);
6194
6195 /* Checksum the current DIE. */
6196 die_odr_checksum (die->die_tag, name, &ctx);
6197 md5_finish_ctx (&ctx, checksum);
6198
6199 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6200 }
6201
6202 /* Next, compute the complete type signature. */
6203
6204 md5_init_ctx (&ctx);
6205 mark = 1;
6206 die->die_mark = mark;
6207
6208 /* Checksum the names of surrounding namespaces and structures. */
6209 if (parent != NULL)
6210 checksum_die_context (parent, &ctx);
6211
6212 /* Checksum the DIE and its children. */
6213 die_checksum_ordered (die, &ctx, &mark);
6214 unmark_all_dies (die);
6215 md5_finish_ctx (&ctx, checksum);
6216
6217 /* Store the signature in the type node and link the type DIE and the
6218 type node together. */
6219 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6220 DWARF_TYPE_SIGNATURE_SIZE);
6221 die->comdat_type_p = true;
6222 die->die_id.die_type_node = type_node;
6223 type_node->type_die = die;
6224
6225 /* If the DIE is a specification, link its declaration to the type node
6226 as well. */
6227 if (decl != NULL)
6228 {
6229 decl->comdat_type_p = true;
6230 decl->die_id.die_type_node = type_node;
6231 }
6232 }
6233
6234 /* Do the location expressions look same? */
6235 static inline int
6236 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6237 {
6238 return loc1->dw_loc_opc == loc2->dw_loc_opc
6239 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6240 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6241 }
6242
6243 /* Do the values look the same? */
6244 static int
6245 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6246 {
6247 dw_loc_descr_ref loc1, loc2;
6248 rtx r1, r2;
6249
6250 if (v1->val_class != v2->val_class)
6251 return 0;
6252
6253 switch (v1->val_class)
6254 {
6255 case dw_val_class_const:
6256 return v1->v.val_int == v2->v.val_int;
6257 case dw_val_class_unsigned_const:
6258 return v1->v.val_unsigned == v2->v.val_unsigned;
6259 case dw_val_class_const_double:
6260 return v1->v.val_double.high == v2->v.val_double.high
6261 && v1->v.val_double.low == v2->v.val_double.low;
6262 case dw_val_class_vec:
6263 if (v1->v.val_vec.length != v2->v.val_vec.length
6264 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6265 return 0;
6266 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6267 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6268 return 0;
6269 return 1;
6270 case dw_val_class_flag:
6271 return v1->v.val_flag == v2->v.val_flag;
6272 case dw_val_class_str:
6273 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6274
6275 case dw_val_class_addr:
6276 r1 = v1->v.val_addr;
6277 r2 = v2->v.val_addr;
6278 if (GET_CODE (r1) != GET_CODE (r2))
6279 return 0;
6280 return !rtx_equal_p (r1, r2);
6281
6282 case dw_val_class_offset:
6283 return v1->v.val_offset == v2->v.val_offset;
6284
6285 case dw_val_class_loc:
6286 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6287 loc1 && loc2;
6288 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6289 if (!same_loc_p (loc1, loc2, mark))
6290 return 0;
6291 return !loc1 && !loc2;
6292
6293 case dw_val_class_die_ref:
6294 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6295
6296 case dw_val_class_fde_ref:
6297 case dw_val_class_vms_delta:
6298 case dw_val_class_lbl_id:
6299 case dw_val_class_lineptr:
6300 case dw_val_class_macptr:
6301 case dw_val_class_high_pc:
6302 return 1;
6303
6304 case dw_val_class_file:
6305 return v1->v.val_file == v2->v.val_file;
6306
6307 case dw_val_class_data8:
6308 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6309
6310 default:
6311 return 1;
6312 }
6313 }
6314
6315 /* Do the attributes look the same? */
6316
6317 static int
6318 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6319 {
6320 if (at1->dw_attr != at2->dw_attr)
6321 return 0;
6322
6323 /* We don't care that this was compiled with a different compiler
6324 snapshot; if the output is the same, that's what matters. */
6325 if (at1->dw_attr == DW_AT_producer)
6326 return 1;
6327
6328 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6329 }
6330
6331 /* Do the dies look the same? */
6332
6333 static int
6334 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6335 {
6336 dw_die_ref c1, c2;
6337 dw_attr_ref a1;
6338 unsigned ix;
6339
6340 /* To avoid infinite recursion. */
6341 if (die1->die_mark)
6342 return die1->die_mark == die2->die_mark;
6343 die1->die_mark = die2->die_mark = ++(*mark);
6344
6345 if (die1->die_tag != die2->die_tag)
6346 return 0;
6347
6348 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6349 return 0;
6350
6351 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6352 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6353 return 0;
6354
6355 c1 = die1->die_child;
6356 c2 = die2->die_child;
6357 if (! c1)
6358 {
6359 if (c2)
6360 return 0;
6361 }
6362 else
6363 for (;;)
6364 {
6365 if (!same_die_p (c1, c2, mark))
6366 return 0;
6367 c1 = c1->die_sib;
6368 c2 = c2->die_sib;
6369 if (c1 == die1->die_child)
6370 {
6371 if (c2 == die2->die_child)
6372 break;
6373 else
6374 return 0;
6375 }
6376 }
6377
6378 return 1;
6379 }
6380
6381 /* Do the dies look the same? Wrapper around same_die_p. */
6382
6383 static int
6384 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6385 {
6386 int mark = 0;
6387 int ret = same_die_p (die1, die2, &mark);
6388
6389 unmark_all_dies (die1);
6390 unmark_all_dies (die2);
6391
6392 return ret;
6393 }
6394
6395 /* The prefix to attach to symbols on DIEs in the current comdat debug
6396 info section. */
6397 static const char *comdat_symbol_id;
6398
6399 /* The index of the current symbol within the current comdat CU. */
6400 static unsigned int comdat_symbol_number;
6401
6402 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6403 children, and set comdat_symbol_id accordingly. */
6404
6405 static void
6406 compute_section_prefix (dw_die_ref unit_die)
6407 {
6408 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6409 const char *base = die_name ? lbasename (die_name) : "anonymous";
6410 char *name = XALLOCAVEC (char, strlen (base) + 64);
6411 char *p;
6412 int i, mark;
6413 unsigned char checksum[16];
6414 struct md5_ctx ctx;
6415
6416 /* Compute the checksum of the DIE, then append part of it as hex digits to
6417 the name filename of the unit. */
6418
6419 md5_init_ctx (&ctx);
6420 mark = 0;
6421 die_checksum (unit_die, &ctx, &mark);
6422 unmark_all_dies (unit_die);
6423 md5_finish_ctx (&ctx, checksum);
6424
6425 sprintf (name, "%s.", base);
6426 clean_symbol_name (name);
6427
6428 p = name + strlen (name);
6429 for (i = 0; i < 4; i++)
6430 {
6431 sprintf (p, "%.2x", checksum[i]);
6432 p += 2;
6433 }
6434
6435 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6436 comdat_symbol_number = 0;
6437 }
6438
6439 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6440
6441 static int
6442 is_type_die (dw_die_ref die)
6443 {
6444 switch (die->die_tag)
6445 {
6446 case DW_TAG_array_type:
6447 case DW_TAG_class_type:
6448 case DW_TAG_interface_type:
6449 case DW_TAG_enumeration_type:
6450 case DW_TAG_pointer_type:
6451 case DW_TAG_reference_type:
6452 case DW_TAG_rvalue_reference_type:
6453 case DW_TAG_string_type:
6454 case DW_TAG_structure_type:
6455 case DW_TAG_subroutine_type:
6456 case DW_TAG_union_type:
6457 case DW_TAG_ptr_to_member_type:
6458 case DW_TAG_set_type:
6459 case DW_TAG_subrange_type:
6460 case DW_TAG_base_type:
6461 case DW_TAG_const_type:
6462 case DW_TAG_file_type:
6463 case DW_TAG_packed_type:
6464 case DW_TAG_volatile_type:
6465 case DW_TAG_typedef:
6466 return 1;
6467 default:
6468 return 0;
6469 }
6470 }
6471
6472 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6473 Basically, we want to choose the bits that are likely to be shared between
6474 compilations (types) and leave out the bits that are specific to individual
6475 compilations (functions). */
6476
6477 static int
6478 is_comdat_die (dw_die_ref c)
6479 {
6480 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6481 we do for stabs. The advantage is a greater likelihood of sharing between
6482 objects that don't include headers in the same order (and therefore would
6483 put the base types in a different comdat). jason 8/28/00 */
6484
6485 if (c->die_tag == DW_TAG_base_type)
6486 return 0;
6487
6488 if (c->die_tag == DW_TAG_pointer_type
6489 || c->die_tag == DW_TAG_reference_type
6490 || c->die_tag == DW_TAG_rvalue_reference_type
6491 || c->die_tag == DW_TAG_const_type
6492 || c->die_tag == DW_TAG_volatile_type)
6493 {
6494 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6495
6496 return t ? is_comdat_die (t) : 0;
6497 }
6498
6499 return is_type_die (c);
6500 }
6501
6502 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6503 compilation unit. */
6504
6505 static int
6506 is_symbol_die (dw_die_ref c)
6507 {
6508 return (is_type_die (c)
6509 || is_declaration_die (c)
6510 || c->die_tag == DW_TAG_namespace
6511 || c->die_tag == DW_TAG_module);
6512 }
6513
6514 /* Returns true iff C is a compile-unit DIE. */
6515
6516 static inline bool
6517 is_cu_die (dw_die_ref c)
6518 {
6519 return c && c->die_tag == DW_TAG_compile_unit;
6520 }
6521
6522 /* Returns true iff C is a unit DIE of some sort. */
6523
6524 static inline bool
6525 is_unit_die (dw_die_ref c)
6526 {
6527 return c && (c->die_tag == DW_TAG_compile_unit
6528 || c->die_tag == DW_TAG_partial_unit
6529 || c->die_tag == DW_TAG_type_unit);
6530 }
6531
6532 /* Returns true iff C is a namespace DIE. */
6533
6534 static inline bool
6535 is_namespace_die (dw_die_ref c)
6536 {
6537 return c && c->die_tag == DW_TAG_namespace;
6538 }
6539
6540 /* Returns true iff C is a class or structure DIE. */
6541
6542 static inline bool
6543 is_class_die (dw_die_ref c)
6544 {
6545 return c && (c->die_tag == DW_TAG_class_type
6546 || c->die_tag == DW_TAG_structure_type);
6547 }
6548
6549 /* Return non-zero if this DIE is a template parameter. */
6550
6551 static inline bool
6552 is_template_parameter (dw_die_ref die)
6553 {
6554 switch (die->die_tag)
6555 {
6556 case DW_TAG_template_type_param:
6557 case DW_TAG_template_value_param:
6558 case DW_TAG_GNU_template_template_param:
6559 case DW_TAG_GNU_template_parameter_pack:
6560 return true;
6561 default:
6562 return false;
6563 }
6564 }
6565
6566 /* Return non-zero if this DIE represents a template instantiation. */
6567
6568 static inline bool
6569 is_template_instantiation (dw_die_ref die)
6570 {
6571 dw_die_ref c;
6572
6573 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6574 return false;
6575 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6576 return false;
6577 }
6578
6579 static char *
6580 gen_internal_sym (const char *prefix)
6581 {
6582 char buf[256];
6583
6584 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6585 return xstrdup (buf);
6586 }
6587
6588 /* Assign symbols to all worthy DIEs under DIE. */
6589
6590 static void
6591 assign_symbol_names (dw_die_ref die)
6592 {
6593 dw_die_ref c;
6594
6595 if (is_symbol_die (die) && !die->comdat_type_p)
6596 {
6597 if (comdat_symbol_id)
6598 {
6599 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6600
6601 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6602 comdat_symbol_id, comdat_symbol_number++);
6603 die->die_id.die_symbol = xstrdup (p);
6604 }
6605 else
6606 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6607 }
6608
6609 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6610 }
6611
6612 struct cu_hash_table_entry
6613 {
6614 dw_die_ref cu;
6615 unsigned min_comdat_num, max_comdat_num;
6616 struct cu_hash_table_entry *next;
6617 };
6618
6619 /* Helpers to manipulate hash table of CUs. */
6620
6621 struct cu_hash_table_entry_hasher
6622 {
6623 typedef cu_hash_table_entry value_type;
6624 typedef die_struct compare_type;
6625 static inline hashval_t hash (const value_type *);
6626 static inline bool equal (const value_type *, const compare_type *);
6627 static inline void remove (value_type *);
6628 };
6629
6630 inline hashval_t
6631 cu_hash_table_entry_hasher::hash (const value_type *entry)
6632 {
6633 return htab_hash_string (entry->cu->die_id.die_symbol);
6634 }
6635
6636 inline bool
6637 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6638 const compare_type *entry2)
6639 {
6640 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6641 }
6642
6643 inline void
6644 cu_hash_table_entry_hasher::remove (value_type *entry)
6645 {
6646 struct cu_hash_table_entry *next;
6647
6648 while (entry)
6649 {
6650 next = entry->next;
6651 free (entry);
6652 entry = next;
6653 }
6654 }
6655
6656 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6657
6658 /* Check whether we have already seen this CU and set up SYM_NUM
6659 accordingly. */
6660 static int
6661 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6662 {
6663 struct cu_hash_table_entry dummy;
6664 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6665
6666 dummy.max_comdat_num = 0;
6667
6668 slot = htable.find_slot_with_hash (cu,
6669 htab_hash_string (cu->die_id.die_symbol),
6670 INSERT);
6671 entry = *slot;
6672
6673 for (; entry; last = entry, entry = entry->next)
6674 {
6675 if (same_die_p_wrap (cu, entry->cu))
6676 break;
6677 }
6678
6679 if (entry)
6680 {
6681 *sym_num = entry->min_comdat_num;
6682 return 1;
6683 }
6684
6685 entry = XCNEW (struct cu_hash_table_entry);
6686 entry->cu = cu;
6687 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6688 entry->next = *slot;
6689 *slot = entry;
6690
6691 return 0;
6692 }
6693
6694 /* Record SYM_NUM to record of CU in HTABLE. */
6695 static void
6696 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6697 unsigned int sym_num)
6698 {
6699 struct cu_hash_table_entry **slot, *entry;
6700
6701 slot = htable.find_slot_with_hash (cu,
6702 htab_hash_string (cu->die_id.die_symbol),
6703 NO_INSERT);
6704 entry = *slot;
6705
6706 entry->max_comdat_num = sym_num;
6707 }
6708
6709 /* Traverse the DIE (which is always comp_unit_die), and set up
6710 additional compilation units for each of the include files we see
6711 bracketed by BINCL/EINCL. */
6712
6713 static void
6714 break_out_includes (dw_die_ref die)
6715 {
6716 dw_die_ref c;
6717 dw_die_ref unit = NULL;
6718 limbo_die_node *node, **pnode;
6719 cu_hash_type cu_hash_table;
6720
6721 c = die->die_child;
6722 if (c) do {
6723 dw_die_ref prev = c;
6724 c = c->die_sib;
6725 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6726 || (unit && is_comdat_die (c)))
6727 {
6728 dw_die_ref next = c->die_sib;
6729
6730 /* This DIE is for a secondary CU; remove it from the main one. */
6731 remove_child_with_prev (c, prev);
6732
6733 if (c->die_tag == DW_TAG_GNU_BINCL)
6734 unit = push_new_compile_unit (unit, c);
6735 else if (c->die_tag == DW_TAG_GNU_EINCL)
6736 unit = pop_compile_unit (unit);
6737 else
6738 add_child_die (unit, c);
6739 c = next;
6740 if (c == die->die_child)
6741 break;
6742 }
6743 } while (c != die->die_child);
6744
6745 #if 0
6746 /* We can only use this in debugging, since the frontend doesn't check
6747 to make sure that we leave every include file we enter. */
6748 gcc_assert (!unit);
6749 #endif
6750
6751 assign_symbol_names (die);
6752 cu_hash_table.create (10);
6753 for (node = limbo_die_list, pnode = &limbo_die_list;
6754 node;
6755 node = node->next)
6756 {
6757 int is_dupl;
6758
6759 compute_section_prefix (node->die);
6760 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6761 &comdat_symbol_number);
6762 assign_symbol_names (node->die);
6763 if (is_dupl)
6764 *pnode = node->next;
6765 else
6766 {
6767 pnode = &node->next;
6768 record_comdat_symbol_number (node->die, cu_hash_table,
6769 comdat_symbol_number);
6770 }
6771 }
6772 cu_hash_table.dispose ();
6773 }
6774
6775 /* Return non-zero if this DIE is a declaration. */
6776
6777 static int
6778 is_declaration_die (dw_die_ref die)
6779 {
6780 dw_attr_ref a;
6781 unsigned ix;
6782
6783 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6784 if (a->dw_attr == DW_AT_declaration)
6785 return 1;
6786
6787 return 0;
6788 }
6789
6790 /* Return non-zero if this DIE is nested inside a subprogram. */
6791
6792 static int
6793 is_nested_in_subprogram (dw_die_ref die)
6794 {
6795 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6796
6797 if (decl == NULL)
6798 decl = die;
6799 return local_scope_p (decl);
6800 }
6801
6802 /* Return non-zero if this DIE contains a defining declaration of a
6803 subprogram. */
6804
6805 static int
6806 contains_subprogram_definition (dw_die_ref die)
6807 {
6808 dw_die_ref c;
6809
6810 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6811 return 1;
6812 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6813 return 0;
6814 }
6815
6816 /* Return non-zero if this is a type DIE that should be moved to a
6817 COMDAT .debug_types section. */
6818
6819 static int
6820 should_move_die_to_comdat (dw_die_ref die)
6821 {
6822 switch (die->die_tag)
6823 {
6824 case DW_TAG_class_type:
6825 case DW_TAG_structure_type:
6826 case DW_TAG_enumeration_type:
6827 case DW_TAG_union_type:
6828 /* Don't move declarations, inlined instances, or types nested in a
6829 subprogram. */
6830 if (is_declaration_die (die)
6831 || get_AT (die, DW_AT_abstract_origin)
6832 || is_nested_in_subprogram (die))
6833 return 0;
6834 /* A type definition should never contain a subprogram definition. */
6835 gcc_assert (!contains_subprogram_definition (die));
6836 return 1;
6837 case DW_TAG_array_type:
6838 case DW_TAG_interface_type:
6839 case DW_TAG_pointer_type:
6840 case DW_TAG_reference_type:
6841 case DW_TAG_rvalue_reference_type:
6842 case DW_TAG_string_type:
6843 case DW_TAG_subroutine_type:
6844 case DW_TAG_ptr_to_member_type:
6845 case DW_TAG_set_type:
6846 case DW_TAG_subrange_type:
6847 case DW_TAG_base_type:
6848 case DW_TAG_const_type:
6849 case DW_TAG_file_type:
6850 case DW_TAG_packed_type:
6851 case DW_TAG_volatile_type:
6852 case DW_TAG_typedef:
6853 default:
6854 return 0;
6855 }
6856 }
6857
6858 /* Make a clone of DIE. */
6859
6860 static dw_die_ref
6861 clone_die (dw_die_ref die)
6862 {
6863 dw_die_ref clone;
6864 dw_attr_ref a;
6865 unsigned ix;
6866
6867 clone = ggc_alloc_cleared_die_node ();
6868 clone->die_tag = die->die_tag;
6869
6870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6871 add_dwarf_attr (clone, a);
6872
6873 return clone;
6874 }
6875
6876 /* Make a clone of the tree rooted at DIE. */
6877
6878 static dw_die_ref
6879 clone_tree (dw_die_ref die)
6880 {
6881 dw_die_ref c;
6882 dw_die_ref clone = clone_die (die);
6883
6884 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6885
6886 return clone;
6887 }
6888
6889 /* Make a clone of DIE as a declaration. */
6890
6891 static dw_die_ref
6892 clone_as_declaration (dw_die_ref die)
6893 {
6894 dw_die_ref clone;
6895 dw_die_ref decl;
6896 dw_attr_ref a;
6897 unsigned ix;
6898
6899 /* If the DIE is already a declaration, just clone it. */
6900 if (is_declaration_die (die))
6901 return clone_die (die);
6902
6903 /* If the DIE is a specification, just clone its declaration DIE. */
6904 decl = get_AT_ref (die, DW_AT_specification);
6905 if (decl != NULL)
6906 {
6907 clone = clone_die (decl);
6908 if (die->comdat_type_p)
6909 add_AT_die_ref (clone, DW_AT_signature, die);
6910 return clone;
6911 }
6912
6913 clone = ggc_alloc_cleared_die_node ();
6914 clone->die_tag = die->die_tag;
6915
6916 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6917 {
6918 /* We don't want to copy over all attributes.
6919 For example we don't want DW_AT_byte_size because otherwise we will no
6920 longer have a declaration and GDB will treat it as a definition. */
6921
6922 switch (a->dw_attr)
6923 {
6924 case DW_AT_artificial:
6925 case DW_AT_containing_type:
6926 case DW_AT_external:
6927 case DW_AT_name:
6928 case DW_AT_type:
6929 case DW_AT_virtuality:
6930 case DW_AT_linkage_name:
6931 case DW_AT_MIPS_linkage_name:
6932 add_dwarf_attr (clone, a);
6933 break;
6934 case DW_AT_byte_size:
6935 default:
6936 break;
6937 }
6938 }
6939
6940 if (die->comdat_type_p)
6941 add_AT_die_ref (clone, DW_AT_signature, die);
6942
6943 add_AT_flag (clone, DW_AT_declaration, 1);
6944 return clone;
6945 }
6946
6947
6948 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6949
6950 struct decl_table_entry
6951 {
6952 dw_die_ref orig;
6953 dw_die_ref copy;
6954 };
6955
6956 /* Helpers to manipulate hash table of copied declarations. */
6957
6958 /* Hashtable helpers. */
6959
6960 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6961 {
6962 typedef decl_table_entry value_type;
6963 typedef die_struct compare_type;
6964 static inline hashval_t hash (const value_type *);
6965 static inline bool equal (const value_type *, const compare_type *);
6966 };
6967
6968 inline hashval_t
6969 decl_table_entry_hasher::hash (const value_type *entry)
6970 {
6971 return htab_hash_pointer (entry->orig);
6972 }
6973
6974 inline bool
6975 decl_table_entry_hasher::equal (const value_type *entry1,
6976 const compare_type *entry2)
6977 {
6978 return entry1->orig == entry2;
6979 }
6980
6981 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6982
6983 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6984 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6985 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6986 to check if the ancestor has already been copied into UNIT. */
6987
6988 static dw_die_ref
6989 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6990 {
6991 dw_die_ref parent = die->die_parent;
6992 dw_die_ref new_parent = unit;
6993 dw_die_ref copy;
6994 decl_table_entry **slot = NULL;
6995 struct decl_table_entry *entry = NULL;
6996
6997 if (decl_table.is_created ())
6998 {
6999 /* Check if the entry has already been copied to UNIT. */
7000 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7001 INSERT);
7002 if (*slot != HTAB_EMPTY_ENTRY)
7003 {
7004 entry = *slot;
7005 return entry->copy;
7006 }
7007
7008 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7009 entry = XCNEW (struct decl_table_entry);
7010 entry->orig = die;
7011 entry->copy = NULL;
7012 *slot = entry;
7013 }
7014
7015 if (parent != NULL)
7016 {
7017 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7018 if (spec != NULL)
7019 parent = spec;
7020 if (!is_unit_die (parent))
7021 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7022 }
7023
7024 copy = clone_as_declaration (die);
7025 add_child_die (new_parent, copy);
7026
7027 if (decl_table.is_created ())
7028 {
7029 /* Record the pointer to the copy. */
7030 entry->copy = copy;
7031 }
7032
7033 return copy;
7034 }
7035 /* Copy the declaration context to the new type unit DIE. This includes
7036 any surrounding namespace or type declarations. If the DIE has an
7037 AT_specification attribute, it also includes attributes and children
7038 attached to the specification, and returns a pointer to the original
7039 parent of the declaration DIE. Returns NULL otherwise. */
7040
7041 static dw_die_ref
7042 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7043 {
7044 dw_die_ref decl;
7045 dw_die_ref new_decl;
7046 dw_die_ref orig_parent = NULL;
7047
7048 decl = get_AT_ref (die, DW_AT_specification);
7049 if (decl == NULL)
7050 decl = die;
7051 else
7052 {
7053 unsigned ix;
7054 dw_die_ref c;
7055 dw_attr_ref a;
7056
7057 /* The original DIE will be changed to a declaration, and must
7058 be moved to be a child of the original declaration DIE. */
7059 orig_parent = decl->die_parent;
7060
7061 /* Copy the type node pointer from the new DIE to the original
7062 declaration DIE so we can forward references later. */
7063 decl->comdat_type_p = true;
7064 decl->die_id.die_type_node = die->die_id.die_type_node;
7065
7066 remove_AT (die, DW_AT_specification);
7067
7068 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7069 {
7070 if (a->dw_attr != DW_AT_name
7071 && a->dw_attr != DW_AT_declaration
7072 && a->dw_attr != DW_AT_external)
7073 add_dwarf_attr (die, a);
7074 }
7075
7076 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7077 }
7078
7079 if (decl->die_parent != NULL
7080 && !is_unit_die (decl->die_parent))
7081 {
7082 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7083 if (new_decl != NULL)
7084 {
7085 remove_AT (new_decl, DW_AT_signature);
7086 add_AT_specification (die, new_decl);
7087 }
7088 }
7089
7090 return orig_parent;
7091 }
7092
7093 /* Generate the skeleton ancestor tree for the given NODE, then clone
7094 the DIE and add the clone into the tree. */
7095
7096 static void
7097 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7098 {
7099 if (node->new_die != NULL)
7100 return;
7101
7102 node->new_die = clone_as_declaration (node->old_die);
7103
7104 if (node->parent != NULL)
7105 {
7106 generate_skeleton_ancestor_tree (node->parent);
7107 add_child_die (node->parent->new_die, node->new_die);
7108 }
7109 }
7110
7111 /* Generate a skeleton tree of DIEs containing any declarations that are
7112 found in the original tree. We traverse the tree looking for declaration
7113 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7114
7115 static void
7116 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7117 {
7118 skeleton_chain_node node;
7119 dw_die_ref c;
7120 dw_die_ref first;
7121 dw_die_ref prev = NULL;
7122 dw_die_ref next = NULL;
7123
7124 node.parent = parent;
7125
7126 first = c = parent->old_die->die_child;
7127 if (c)
7128 next = c->die_sib;
7129 if (c) do {
7130 if (prev == NULL || prev->die_sib == c)
7131 prev = c;
7132 c = next;
7133 next = (c == first ? NULL : c->die_sib);
7134 node.old_die = c;
7135 node.new_die = NULL;
7136 if (is_declaration_die (c))
7137 {
7138 if (is_template_instantiation (c))
7139 {
7140 /* Instantiated templates do not need to be cloned into the
7141 type unit. Just move the DIE and its children back to
7142 the skeleton tree (in the main CU). */
7143 remove_child_with_prev (c, prev);
7144 add_child_die (parent->new_die, c);
7145 c = prev;
7146 }
7147 else
7148 {
7149 /* Clone the existing DIE, move the original to the skeleton
7150 tree (which is in the main CU), and put the clone, with
7151 all the original's children, where the original came from
7152 (which is about to be moved to the type unit). */
7153 dw_die_ref clone = clone_die (c);
7154 move_all_children (c, clone);
7155
7156 replace_child (c, clone, prev);
7157 generate_skeleton_ancestor_tree (parent);
7158 add_child_die (parent->new_die, c);
7159 node.new_die = c;
7160 c = clone;
7161 }
7162 }
7163 generate_skeleton_bottom_up (&node);
7164 } while (next != NULL);
7165 }
7166
7167 /* Wrapper function for generate_skeleton_bottom_up. */
7168
7169 static dw_die_ref
7170 generate_skeleton (dw_die_ref die)
7171 {
7172 skeleton_chain_node node;
7173
7174 node.old_die = die;
7175 node.new_die = NULL;
7176 node.parent = NULL;
7177
7178 /* If this type definition is nested inside another type,
7179 and is not an instantiation of a template, always leave
7180 at least a declaration in its place. */
7181 if (die->die_parent != NULL
7182 && is_type_die (die->die_parent)
7183 && !is_template_instantiation (die))
7184 node.new_die = clone_as_declaration (die);
7185
7186 generate_skeleton_bottom_up (&node);
7187 return node.new_die;
7188 }
7189
7190 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7191 declaration. The original DIE is moved to a new compile unit so that
7192 existing references to it follow it to the new location. If any of the
7193 original DIE's descendants is a declaration, we need to replace the
7194 original DIE with a skeleton tree and move the declarations back into the
7195 skeleton tree. */
7196
7197 static dw_die_ref
7198 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7199 dw_die_ref prev)
7200 {
7201 dw_die_ref skeleton, orig_parent;
7202
7203 /* Copy the declaration context to the type unit DIE. If the returned
7204 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7205 that DIE. */
7206 orig_parent = copy_declaration_context (unit, child);
7207
7208 skeleton = generate_skeleton (child);
7209 if (skeleton == NULL)
7210 remove_child_with_prev (child, prev);
7211 else
7212 {
7213 skeleton->comdat_type_p = true;
7214 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7215
7216 /* If the original DIE was a specification, we need to put
7217 the skeleton under the parent DIE of the declaration.
7218 This leaves the original declaration in the tree, but
7219 it will be pruned later since there are no longer any
7220 references to it. */
7221 if (orig_parent != NULL)
7222 {
7223 remove_child_with_prev (child, prev);
7224 add_child_die (orig_parent, skeleton);
7225 }
7226 else
7227 replace_child (child, skeleton, prev);
7228 }
7229
7230 return skeleton;
7231 }
7232
7233 /* Traverse the DIE and set up additional .debug_types sections for each
7234 type worthy of being placed in a COMDAT section. */
7235
7236 static void
7237 break_out_comdat_types (dw_die_ref die)
7238 {
7239 dw_die_ref c;
7240 dw_die_ref first;
7241 dw_die_ref prev = NULL;
7242 dw_die_ref next = NULL;
7243 dw_die_ref unit = NULL;
7244
7245 first = c = die->die_child;
7246 if (c)
7247 next = c->die_sib;
7248 if (c) do {
7249 if (prev == NULL || prev->die_sib == c)
7250 prev = c;
7251 c = next;
7252 next = (c == first ? NULL : c->die_sib);
7253 if (should_move_die_to_comdat (c))
7254 {
7255 dw_die_ref replacement;
7256 comdat_type_node_ref type_node;
7257
7258 /* Break out nested types into their own type units. */
7259 break_out_comdat_types (c);
7260
7261 /* Create a new type unit DIE as the root for the new tree, and
7262 add it to the list of comdat types. */
7263 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7264 add_AT_unsigned (unit, DW_AT_language,
7265 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7266 type_node = ggc_alloc_cleared_comdat_type_node ();
7267 type_node->root_die = unit;
7268 type_node->next = comdat_type_list;
7269 comdat_type_list = type_node;
7270
7271 /* Generate the type signature. */
7272 generate_type_signature (c, type_node);
7273
7274 /* Copy the declaration context, attributes, and children of the
7275 declaration into the new type unit DIE, then remove this DIE
7276 from the main CU (or replace it with a skeleton if necessary). */
7277 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7278 type_node->skeleton_die = replacement;
7279
7280 /* Add the DIE to the new compunit. */
7281 add_child_die (unit, c);
7282
7283 if (replacement != NULL)
7284 c = replacement;
7285 }
7286 else if (c->die_tag == DW_TAG_namespace
7287 || c->die_tag == DW_TAG_class_type
7288 || c->die_tag == DW_TAG_structure_type
7289 || c->die_tag == DW_TAG_union_type)
7290 {
7291 /* Look for nested types that can be broken out. */
7292 break_out_comdat_types (c);
7293 }
7294 } while (next != NULL);
7295 }
7296
7297 /* Like clone_tree, but additionally enter all the children into
7298 the hash table decl_table. */
7299
7300 static dw_die_ref
7301 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7302 {
7303 dw_die_ref c;
7304 dw_die_ref clone = clone_die (die);
7305 struct decl_table_entry *entry;
7306 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7307 htab_hash_pointer (die), INSERT);
7308 /* Assert that DIE isn't in the hash table yet. If it would be there
7309 before, the ancestors would be necessarily there as well, therefore
7310 clone_tree_hash wouldn't be called. */
7311 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7312 entry = XCNEW (struct decl_table_entry);
7313 entry->orig = die;
7314 entry->copy = clone;
7315 *slot = entry;
7316
7317 FOR_EACH_CHILD (die, c,
7318 add_child_die (clone, clone_tree_hash (c, decl_table)));
7319
7320 return clone;
7321 }
7322
7323 /* Walk the DIE and its children, looking for references to incomplete
7324 or trivial types that are unmarked (i.e., that are not in the current
7325 type_unit). */
7326
7327 static void
7328 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7329 {
7330 dw_die_ref c;
7331 dw_attr_ref a;
7332 unsigned ix;
7333
7334 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7335 {
7336 if (AT_class (a) == dw_val_class_die_ref)
7337 {
7338 dw_die_ref targ = AT_ref (a);
7339 decl_table_entry **slot;
7340 struct decl_table_entry *entry;
7341
7342 if (targ->die_mark != 0 || targ->comdat_type_p)
7343 continue;
7344
7345 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7346 INSERT);
7347
7348 if (*slot != HTAB_EMPTY_ENTRY)
7349 {
7350 /* TARG has already been copied, so we just need to
7351 modify the reference to point to the copy. */
7352 entry = *slot;
7353 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7354 }
7355 else
7356 {
7357 dw_die_ref parent = unit;
7358 dw_die_ref copy = clone_die (targ);
7359
7360 /* Record in DECL_TABLE that TARG has been copied.
7361 Need to do this now, before the recursive call,
7362 because DECL_TABLE may be expanded and SLOT
7363 would no longer be a valid pointer. */
7364 entry = XCNEW (struct decl_table_entry);
7365 entry->orig = targ;
7366 entry->copy = copy;
7367 *slot = entry;
7368
7369 FOR_EACH_CHILD (targ, c,
7370 add_child_die (copy,
7371 clone_tree_hash (c, decl_table)));
7372
7373 /* Make sure the cloned tree is marked as part of the
7374 type unit. */
7375 mark_dies (copy);
7376
7377 /* If TARG has surrounding context, copy its ancestor tree
7378 into the new type unit. */
7379 if (targ->die_parent != NULL
7380 && !is_unit_die (targ->die_parent))
7381 parent = copy_ancestor_tree (unit, targ->die_parent,
7382 decl_table);
7383
7384 add_child_die (parent, copy);
7385 a->dw_attr_val.v.val_die_ref.die = copy;
7386
7387 /* Make sure the newly-copied DIE is walked. If it was
7388 installed in a previously-added context, it won't
7389 get visited otherwise. */
7390 if (parent != unit)
7391 {
7392 /* Find the highest point of the newly-added tree,
7393 mark each node along the way, and walk from there. */
7394 parent->die_mark = 1;
7395 while (parent->die_parent
7396 && parent->die_parent->die_mark == 0)
7397 {
7398 parent = parent->die_parent;
7399 parent->die_mark = 1;
7400 }
7401 copy_decls_walk (unit, parent, decl_table);
7402 }
7403 }
7404 }
7405 }
7406
7407 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7408 }
7409
7410 /* Copy declarations for "unworthy" types into the new comdat section.
7411 Incomplete types, modified types, and certain other types aren't broken
7412 out into comdat sections of their own, so they don't have a signature,
7413 and we need to copy the declaration into the same section so that we
7414 don't have an external reference. */
7415
7416 static void
7417 copy_decls_for_unworthy_types (dw_die_ref unit)
7418 {
7419 decl_hash_type decl_table;
7420
7421 mark_dies (unit);
7422 decl_table.create (10);
7423 copy_decls_walk (unit, unit, decl_table);
7424 decl_table.dispose ();
7425 unmark_dies (unit);
7426 }
7427
7428 /* Traverse the DIE and add a sibling attribute if it may have the
7429 effect of speeding up access to siblings. To save some space,
7430 avoid generating sibling attributes for DIE's without children. */
7431
7432 static void
7433 add_sibling_attributes (dw_die_ref die)
7434 {
7435 dw_die_ref c;
7436
7437 if (! die->die_child)
7438 return;
7439
7440 if (die->die_parent && die != die->die_parent->die_child)
7441 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7442
7443 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7444 }
7445
7446 /* Output all location lists for the DIE and its children. */
7447
7448 static void
7449 output_location_lists (dw_die_ref die)
7450 {
7451 dw_die_ref c;
7452 dw_attr_ref a;
7453 unsigned ix;
7454
7455 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7456 if (AT_class (a) == dw_val_class_loc_list)
7457 output_loc_list (AT_loc_list (a));
7458
7459 FOR_EACH_CHILD (die, c, output_location_lists (c));
7460 }
7461
7462 /* We want to limit the number of external references, because they are
7463 larger than local references: a relocation takes multiple words, and
7464 even a sig8 reference is always eight bytes, whereas a local reference
7465 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7466 So if we encounter multiple external references to the same type DIE, we
7467 make a local typedef stub for it and redirect all references there.
7468
7469 This is the element of the hash table for keeping track of these
7470 references. */
7471
7472 struct external_ref
7473 {
7474 dw_die_ref type;
7475 dw_die_ref stub;
7476 unsigned n_refs;
7477 };
7478
7479 /* Hashtable helpers. */
7480
7481 struct external_ref_hasher : typed_free_remove <external_ref>
7482 {
7483 typedef external_ref value_type;
7484 typedef external_ref compare_type;
7485 static inline hashval_t hash (const value_type *);
7486 static inline bool equal (const value_type *, const compare_type *);
7487 };
7488
7489 inline hashval_t
7490 external_ref_hasher::hash (const value_type *r)
7491 {
7492 dw_die_ref die = r->type;
7493 hashval_t h = 0;
7494
7495 /* We can't use the address of the DIE for hashing, because
7496 that will make the order of the stub DIEs non-deterministic. */
7497 if (! die->comdat_type_p)
7498 /* We have a symbol; use it to compute a hash. */
7499 h = htab_hash_string (die->die_id.die_symbol);
7500 else
7501 {
7502 /* We have a type signature; use a subset of the bits as the hash.
7503 The 8-byte signature is at least as large as hashval_t. */
7504 comdat_type_node_ref type_node = die->die_id.die_type_node;
7505 memcpy (&h, type_node->signature, sizeof (h));
7506 }
7507 return h;
7508 }
7509
7510 inline bool
7511 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7512 {
7513 return r1->type == r2->type;
7514 }
7515
7516 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7517
7518 /* Return a pointer to the external_ref for references to DIE. */
7519
7520 static struct external_ref *
7521 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7522 {
7523 struct external_ref ref, *ref_p;
7524 external_ref **slot;
7525
7526 ref.type = die;
7527 slot = map.find_slot (&ref, INSERT);
7528 if (*slot != HTAB_EMPTY_ENTRY)
7529 return *slot;
7530
7531 ref_p = XCNEW (struct external_ref);
7532 ref_p->type = die;
7533 *slot = ref_p;
7534 return ref_p;
7535 }
7536
7537 /* Subroutine of optimize_external_refs, below.
7538
7539 If we see a type skeleton, record it as our stub. If we see external
7540 references, remember how many we've seen. */
7541
7542 static void
7543 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7544 {
7545 dw_die_ref c;
7546 dw_attr_ref a;
7547 unsigned ix;
7548 struct external_ref *ref_p;
7549
7550 if (is_type_die (die)
7551 && (c = get_AT_ref (die, DW_AT_signature)))
7552 {
7553 /* This is a local skeleton; use it for local references. */
7554 ref_p = lookup_external_ref (map, c);
7555 ref_p->stub = die;
7556 }
7557
7558 /* Scan the DIE references, and remember any that refer to DIEs from
7559 other CUs (i.e. those which are not marked). */
7560 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7561 if (AT_class (a) == dw_val_class_die_ref
7562 && (c = AT_ref (a))->die_mark == 0
7563 && is_type_die (c))
7564 {
7565 ref_p = lookup_external_ref (map, c);
7566 ref_p->n_refs++;
7567 }
7568
7569 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7570 }
7571
7572 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7573 points to an external_ref, DATA is the CU we're processing. If we don't
7574 already have a local stub, and we have multiple refs, build a stub. */
7575
7576 int
7577 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7578 {
7579 struct external_ref *ref_p = *slot;
7580
7581 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7582 {
7583 /* We have multiple references to this type, so build a small stub.
7584 Both of these forms are a bit dodgy from the perspective of the
7585 DWARF standard, since technically they should have names. */
7586 dw_die_ref cu = data;
7587 dw_die_ref type = ref_p->type;
7588 dw_die_ref stub = NULL;
7589
7590 if (type->comdat_type_p)
7591 {
7592 /* If we refer to this type via sig8, use AT_signature. */
7593 stub = new_die (type->die_tag, cu, NULL_TREE);
7594 add_AT_die_ref (stub, DW_AT_signature, type);
7595 }
7596 else
7597 {
7598 /* Otherwise, use a typedef with no name. */
7599 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7600 add_AT_die_ref (stub, DW_AT_type, type);
7601 }
7602
7603 stub->die_mark++;
7604 ref_p->stub = stub;
7605 }
7606 return 1;
7607 }
7608
7609 /* DIE is a unit; look through all the DIE references to see if there are
7610 any external references to types, and if so, create local stubs for
7611 them which will be applied in build_abbrev_table. This is useful because
7612 references to local DIEs are smaller. */
7613
7614 static external_ref_hash_type
7615 optimize_external_refs (dw_die_ref die)
7616 {
7617 external_ref_hash_type map;
7618 map.create (10);
7619 optimize_external_refs_1 (die, map);
7620 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7621 return map;
7622 }
7623
7624 /* The format of each DIE (and its attribute value pairs) is encoded in an
7625 abbreviation table. This routine builds the abbreviation table and assigns
7626 a unique abbreviation id for each abbreviation entry. The children of each
7627 die are visited recursively. */
7628
7629 static void
7630 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7631 {
7632 unsigned long abbrev_id;
7633 unsigned int n_alloc;
7634 dw_die_ref c;
7635 dw_attr_ref a;
7636 unsigned ix;
7637
7638 /* Scan the DIE references, and replace any that refer to
7639 DIEs from other CUs (i.e. those which are not marked) with
7640 the local stubs we built in optimize_external_refs. */
7641 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7642 if (AT_class (a) == dw_val_class_die_ref
7643 && (c = AT_ref (a))->die_mark == 0)
7644 {
7645 struct external_ref *ref_p;
7646 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7647
7648 ref_p = lookup_external_ref (extern_map, c);
7649 if (ref_p->stub && ref_p->stub != die)
7650 change_AT_die_ref (a, ref_p->stub);
7651 else
7652 /* We aren't changing this reference, so mark it external. */
7653 set_AT_ref_external (a, 1);
7654 }
7655
7656 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7657 {
7658 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7659 dw_attr_ref die_a, abbrev_a;
7660 unsigned ix;
7661 bool ok = true;
7662
7663 if (abbrev->die_tag != die->die_tag)
7664 continue;
7665 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7666 continue;
7667
7668 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7669 continue;
7670
7671 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7672 {
7673 abbrev_a = &(*abbrev->die_attr)[ix];
7674 if ((abbrev_a->dw_attr != die_a->dw_attr)
7675 || (value_format (abbrev_a) != value_format (die_a)))
7676 {
7677 ok = false;
7678 break;
7679 }
7680 }
7681 if (ok)
7682 break;
7683 }
7684
7685 if (abbrev_id >= abbrev_die_table_in_use)
7686 {
7687 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7688 {
7689 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7690 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7691 n_alloc);
7692
7693 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7694 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7695 abbrev_die_table_allocated = n_alloc;
7696 }
7697
7698 ++abbrev_die_table_in_use;
7699 abbrev_die_table[abbrev_id] = die;
7700 }
7701
7702 die->die_abbrev = abbrev_id;
7703 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7704 }
7705 \f
7706 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7707
7708 static int
7709 constant_size (unsigned HOST_WIDE_INT value)
7710 {
7711 int log;
7712
7713 if (value == 0)
7714 log = 0;
7715 else
7716 log = floor_log2 (value);
7717
7718 log = log / 8;
7719 log = 1 << (floor_log2 (log) + 1);
7720
7721 return log;
7722 }
7723
7724 /* Return the size of a DIE as it is represented in the
7725 .debug_info section. */
7726
7727 static unsigned long
7728 size_of_die (dw_die_ref die)
7729 {
7730 unsigned long size = 0;
7731 dw_attr_ref a;
7732 unsigned ix;
7733 enum dwarf_form form;
7734
7735 size += size_of_uleb128 (die->die_abbrev);
7736 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7737 {
7738 switch (AT_class (a))
7739 {
7740 case dw_val_class_addr:
7741 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7742 {
7743 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7744 size += size_of_uleb128 (AT_index (a));
7745 }
7746 else
7747 size += DWARF2_ADDR_SIZE;
7748 break;
7749 case dw_val_class_offset:
7750 size += DWARF_OFFSET_SIZE;
7751 break;
7752 case dw_val_class_loc:
7753 {
7754 unsigned long lsize = size_of_locs (AT_loc (a));
7755
7756 /* Block length. */
7757 if (dwarf_version >= 4)
7758 size += size_of_uleb128 (lsize);
7759 else
7760 size += constant_size (lsize);
7761 size += lsize;
7762 }
7763 break;
7764 case dw_val_class_loc_list:
7765 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7766 {
7767 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7768 size += size_of_uleb128 (AT_index (a));
7769 }
7770 else
7771 size += DWARF_OFFSET_SIZE;
7772 break;
7773 case dw_val_class_range_list:
7774 size += DWARF_OFFSET_SIZE;
7775 break;
7776 case dw_val_class_const:
7777 size += size_of_sleb128 (AT_int (a));
7778 break;
7779 case dw_val_class_unsigned_const:
7780 {
7781 int csize = constant_size (AT_unsigned (a));
7782 if (dwarf_version == 3
7783 && a->dw_attr == DW_AT_data_member_location
7784 && csize >= 4)
7785 size += size_of_uleb128 (AT_unsigned (a));
7786 else
7787 size += csize;
7788 }
7789 break;
7790 case dw_val_class_const_double:
7791 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7792 if (HOST_BITS_PER_WIDE_INT >= 64)
7793 size++; /* block */
7794 break;
7795 case dw_val_class_vec:
7796 size += constant_size (a->dw_attr_val.v.val_vec.length
7797 * a->dw_attr_val.v.val_vec.elt_size)
7798 + a->dw_attr_val.v.val_vec.length
7799 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7800 break;
7801 case dw_val_class_flag:
7802 if (dwarf_version >= 4)
7803 /* Currently all add_AT_flag calls pass in 1 as last argument,
7804 so DW_FORM_flag_present can be used. If that ever changes,
7805 we'll need to use DW_FORM_flag and have some optimization
7806 in build_abbrev_table that will change those to
7807 DW_FORM_flag_present if it is set to 1 in all DIEs using
7808 the same abbrev entry. */
7809 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7810 else
7811 size += 1;
7812 break;
7813 case dw_val_class_die_ref:
7814 if (AT_ref_external (a))
7815 {
7816 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7817 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7818 is sized by target address length, whereas in DWARF3
7819 it's always sized as an offset. */
7820 if (use_debug_types)
7821 size += DWARF_TYPE_SIGNATURE_SIZE;
7822 else if (dwarf_version == 2)
7823 size += DWARF2_ADDR_SIZE;
7824 else
7825 size += DWARF_OFFSET_SIZE;
7826 }
7827 else
7828 size += DWARF_OFFSET_SIZE;
7829 break;
7830 case dw_val_class_fde_ref:
7831 size += DWARF_OFFSET_SIZE;
7832 break;
7833 case dw_val_class_lbl_id:
7834 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7835 {
7836 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7837 size += size_of_uleb128 (AT_index (a));
7838 }
7839 else
7840 size += DWARF2_ADDR_SIZE;
7841 break;
7842 case dw_val_class_lineptr:
7843 case dw_val_class_macptr:
7844 size += DWARF_OFFSET_SIZE;
7845 break;
7846 case dw_val_class_str:
7847 form = AT_string_form (a);
7848 if (form == DW_FORM_strp)
7849 size += DWARF_OFFSET_SIZE;
7850 else if (form == DW_FORM_GNU_str_index)
7851 size += size_of_uleb128 (AT_index (a));
7852 else
7853 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7854 break;
7855 case dw_val_class_file:
7856 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7857 break;
7858 case dw_val_class_data8:
7859 size += 8;
7860 break;
7861 case dw_val_class_vms_delta:
7862 size += DWARF_OFFSET_SIZE;
7863 break;
7864 case dw_val_class_high_pc:
7865 size += DWARF2_ADDR_SIZE;
7866 break;
7867 default:
7868 gcc_unreachable ();
7869 }
7870 }
7871
7872 return size;
7873 }
7874
7875 /* Size the debugging information associated with a given DIE. Visits the
7876 DIE's children recursively. Updates the global variable next_die_offset, on
7877 each time through. Uses the current value of next_die_offset to update the
7878 die_offset field in each DIE. */
7879
7880 static void
7881 calc_die_sizes (dw_die_ref die)
7882 {
7883 dw_die_ref c;
7884
7885 gcc_assert (die->die_offset == 0
7886 || (unsigned long int) die->die_offset == next_die_offset);
7887 die->die_offset = next_die_offset;
7888 next_die_offset += size_of_die (die);
7889
7890 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7891
7892 if (die->die_child != NULL)
7893 /* Count the null byte used to terminate sibling lists. */
7894 next_die_offset += 1;
7895 }
7896
7897 /* Size just the base type children at the start of the CU.
7898 This is needed because build_abbrev needs to size locs
7899 and sizing of type based stack ops needs to know die_offset
7900 values for the base types. */
7901
7902 static void
7903 calc_base_type_die_sizes (void)
7904 {
7905 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7906 unsigned int i;
7907 dw_die_ref base_type;
7908 #if ENABLE_ASSERT_CHECKING
7909 dw_die_ref prev = comp_unit_die ()->die_child;
7910 #endif
7911
7912 die_offset += size_of_die (comp_unit_die ());
7913 for (i = 0; base_types.iterate (i, &base_type); i++)
7914 {
7915 #if ENABLE_ASSERT_CHECKING
7916 gcc_assert (base_type->die_offset == 0
7917 && prev->die_sib == base_type
7918 && base_type->die_child == NULL
7919 && base_type->die_abbrev);
7920 prev = base_type;
7921 #endif
7922 base_type->die_offset = die_offset;
7923 die_offset += size_of_die (base_type);
7924 }
7925 }
7926
7927 /* Set the marks for a die and its children. We do this so
7928 that we know whether or not a reference needs to use FORM_ref_addr; only
7929 DIEs in the same CU will be marked. We used to clear out the offset
7930 and use that as the flag, but ran into ordering problems. */
7931
7932 static void
7933 mark_dies (dw_die_ref die)
7934 {
7935 dw_die_ref c;
7936
7937 gcc_assert (!die->die_mark);
7938
7939 die->die_mark = 1;
7940 FOR_EACH_CHILD (die, c, mark_dies (c));
7941 }
7942
7943 /* Clear the marks for a die and its children. */
7944
7945 static void
7946 unmark_dies (dw_die_ref die)
7947 {
7948 dw_die_ref c;
7949
7950 if (! use_debug_types)
7951 gcc_assert (die->die_mark);
7952
7953 die->die_mark = 0;
7954 FOR_EACH_CHILD (die, c, unmark_dies (c));
7955 }
7956
7957 /* Clear the marks for a die, its children and referred dies. */
7958
7959 static void
7960 unmark_all_dies (dw_die_ref die)
7961 {
7962 dw_die_ref c;
7963 dw_attr_ref a;
7964 unsigned ix;
7965
7966 if (!die->die_mark)
7967 return;
7968 die->die_mark = 0;
7969
7970 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7971
7972 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7973 if (AT_class (a) == dw_val_class_die_ref)
7974 unmark_all_dies (AT_ref (a));
7975 }
7976
7977 /* Calculate if the entry should appear in the final output file. It may be
7978 from a pruned a type. */
7979
7980 static bool
7981 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7982 {
7983 /* By limiting gnu pubnames to definitions only, gold can generate a
7984 gdb index without entries for declarations, which don't include
7985 enough information to be useful. */
7986 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7987 return false;
7988
7989 if (table == pubname_table)
7990 {
7991 /* Enumerator names are part of the pubname table, but the
7992 parent DW_TAG_enumeration_type die may have been pruned.
7993 Don't output them if that is the case. */
7994 if (p->die->die_tag == DW_TAG_enumerator &&
7995 (p->die->die_parent == NULL
7996 || !p->die->die_parent->die_perennial_p))
7997 return false;
7998
7999 /* Everything else in the pubname table is included. */
8000 return true;
8001 }
8002
8003 /* The pubtypes table shouldn't include types that have been
8004 pruned. */
8005 return (p->die->die_offset != 0
8006 || !flag_eliminate_unused_debug_types);
8007 }
8008
8009 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8010 generated for the compilation unit. */
8011
8012 static unsigned long
8013 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8014 {
8015 unsigned long size;
8016 unsigned i;
8017 pubname_ref p;
8018 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8019
8020 size = DWARF_PUBNAMES_HEADER_SIZE;
8021 FOR_EACH_VEC_ELT (*names, i, p)
8022 if (include_pubname_in_output (names, p))
8023 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8024
8025 size += DWARF_OFFSET_SIZE;
8026 return size;
8027 }
8028
8029 /* Return the size of the information in the .debug_aranges section. */
8030
8031 static unsigned long
8032 size_of_aranges (void)
8033 {
8034 unsigned long size;
8035
8036 size = DWARF_ARANGES_HEADER_SIZE;
8037
8038 /* Count the address/length pair for this compilation unit. */
8039 if (text_section_used)
8040 size += 2 * DWARF2_ADDR_SIZE;
8041 if (cold_text_section_used)
8042 size += 2 * DWARF2_ADDR_SIZE;
8043 if (have_multiple_function_sections)
8044 {
8045 unsigned fde_idx;
8046 dw_fde_ref fde;
8047
8048 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8049 {
8050 if (DECL_IGNORED_P (fde->decl))
8051 continue;
8052 if (!fde->in_std_section)
8053 size += 2 * DWARF2_ADDR_SIZE;
8054 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8055 size += 2 * DWARF2_ADDR_SIZE;
8056 }
8057 }
8058
8059 /* Count the two zero words used to terminated the address range table. */
8060 size += 2 * DWARF2_ADDR_SIZE;
8061 return size;
8062 }
8063 \f
8064 /* Select the encoding of an attribute value. */
8065
8066 static enum dwarf_form
8067 value_format (dw_attr_ref a)
8068 {
8069 switch (AT_class (a))
8070 {
8071 case dw_val_class_addr:
8072 /* Only very few attributes allow DW_FORM_addr. */
8073 switch (a->dw_attr)
8074 {
8075 case DW_AT_low_pc:
8076 case DW_AT_high_pc:
8077 case DW_AT_entry_pc:
8078 case DW_AT_trampoline:
8079 return (AT_index (a) == NOT_INDEXED
8080 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8081 default:
8082 break;
8083 }
8084 switch (DWARF2_ADDR_SIZE)
8085 {
8086 case 1:
8087 return DW_FORM_data1;
8088 case 2:
8089 return DW_FORM_data2;
8090 case 4:
8091 return DW_FORM_data4;
8092 case 8:
8093 return DW_FORM_data8;
8094 default:
8095 gcc_unreachable ();
8096 }
8097 case dw_val_class_range_list:
8098 case dw_val_class_loc_list:
8099 if (dwarf_version >= 4)
8100 return DW_FORM_sec_offset;
8101 /* FALLTHRU */
8102 case dw_val_class_vms_delta:
8103 case dw_val_class_offset:
8104 switch (DWARF_OFFSET_SIZE)
8105 {
8106 case 4:
8107 return DW_FORM_data4;
8108 case 8:
8109 return DW_FORM_data8;
8110 default:
8111 gcc_unreachable ();
8112 }
8113 case dw_val_class_loc:
8114 if (dwarf_version >= 4)
8115 return DW_FORM_exprloc;
8116 switch (constant_size (size_of_locs (AT_loc (a))))
8117 {
8118 case 1:
8119 return DW_FORM_block1;
8120 case 2:
8121 return DW_FORM_block2;
8122 case 4:
8123 return DW_FORM_block4;
8124 default:
8125 gcc_unreachable ();
8126 }
8127 case dw_val_class_const:
8128 return DW_FORM_sdata;
8129 case dw_val_class_unsigned_const:
8130 switch (constant_size (AT_unsigned (a)))
8131 {
8132 case 1:
8133 return DW_FORM_data1;
8134 case 2:
8135 return DW_FORM_data2;
8136 case 4:
8137 /* In DWARF3 DW_AT_data_member_location with
8138 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8139 constant, so we need to use DW_FORM_udata if we need
8140 a large constant. */
8141 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8142 return DW_FORM_udata;
8143 return DW_FORM_data4;
8144 case 8:
8145 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8146 return DW_FORM_udata;
8147 return DW_FORM_data8;
8148 default:
8149 gcc_unreachable ();
8150 }
8151 case dw_val_class_const_double:
8152 switch (HOST_BITS_PER_WIDE_INT)
8153 {
8154 case 8:
8155 return DW_FORM_data2;
8156 case 16:
8157 return DW_FORM_data4;
8158 case 32:
8159 return DW_FORM_data8;
8160 case 64:
8161 default:
8162 return DW_FORM_block1;
8163 }
8164 case dw_val_class_vec:
8165 switch (constant_size (a->dw_attr_val.v.val_vec.length
8166 * a->dw_attr_val.v.val_vec.elt_size))
8167 {
8168 case 1:
8169 return DW_FORM_block1;
8170 case 2:
8171 return DW_FORM_block2;
8172 case 4:
8173 return DW_FORM_block4;
8174 default:
8175 gcc_unreachable ();
8176 }
8177 case dw_val_class_flag:
8178 if (dwarf_version >= 4)
8179 {
8180 /* Currently all add_AT_flag calls pass in 1 as last argument,
8181 so DW_FORM_flag_present can be used. If that ever changes,
8182 we'll need to use DW_FORM_flag and have some optimization
8183 in build_abbrev_table that will change those to
8184 DW_FORM_flag_present if it is set to 1 in all DIEs using
8185 the same abbrev entry. */
8186 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8187 return DW_FORM_flag_present;
8188 }
8189 return DW_FORM_flag;
8190 case dw_val_class_die_ref:
8191 if (AT_ref_external (a))
8192 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8193 else
8194 return DW_FORM_ref;
8195 case dw_val_class_fde_ref:
8196 return DW_FORM_data;
8197 case dw_val_class_lbl_id:
8198 return (AT_index (a) == NOT_INDEXED
8199 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8200 case dw_val_class_lineptr:
8201 case dw_val_class_macptr:
8202 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8203 case dw_val_class_str:
8204 return AT_string_form (a);
8205 case dw_val_class_file:
8206 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8207 {
8208 case 1:
8209 return DW_FORM_data1;
8210 case 2:
8211 return DW_FORM_data2;
8212 case 4:
8213 return DW_FORM_data4;
8214 default:
8215 gcc_unreachable ();
8216 }
8217
8218 case dw_val_class_data8:
8219 return DW_FORM_data8;
8220
8221 case dw_val_class_high_pc:
8222 switch (DWARF2_ADDR_SIZE)
8223 {
8224 case 1:
8225 return DW_FORM_data1;
8226 case 2:
8227 return DW_FORM_data2;
8228 case 4:
8229 return DW_FORM_data4;
8230 case 8:
8231 return DW_FORM_data8;
8232 default:
8233 gcc_unreachable ();
8234 }
8235
8236 default:
8237 gcc_unreachable ();
8238 }
8239 }
8240
8241 /* Output the encoding of an attribute value. */
8242
8243 static void
8244 output_value_format (dw_attr_ref a)
8245 {
8246 enum dwarf_form form = value_format (a);
8247
8248 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8249 }
8250
8251 /* Given a die and id, produce the appropriate abbreviations. */
8252
8253 static void
8254 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8255 {
8256 unsigned ix;
8257 dw_attr_ref a_attr;
8258
8259 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8260 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8261 dwarf_tag_name (abbrev->die_tag));
8262
8263 if (abbrev->die_child != NULL)
8264 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8265 else
8266 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8267
8268 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8269 {
8270 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8271 dwarf_attr_name (a_attr->dw_attr));
8272 output_value_format (a_attr);
8273 }
8274
8275 dw2_asm_output_data (1, 0, NULL);
8276 dw2_asm_output_data (1, 0, NULL);
8277 }
8278
8279
8280 /* Output the .debug_abbrev section which defines the DIE abbreviation
8281 table. */
8282
8283 static void
8284 output_abbrev_section (void)
8285 {
8286 unsigned long abbrev_id;
8287
8288 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8289 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8290
8291 /* Terminate the table. */
8292 dw2_asm_output_data (1, 0, NULL);
8293 }
8294
8295 /* Output a symbol we can use to refer to this DIE from another CU. */
8296
8297 static inline void
8298 output_die_symbol (dw_die_ref die)
8299 {
8300 const char *sym = die->die_id.die_symbol;
8301
8302 gcc_assert (!die->comdat_type_p);
8303
8304 if (sym == 0)
8305 return;
8306
8307 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8308 /* We make these global, not weak; if the target doesn't support
8309 .linkonce, it doesn't support combining the sections, so debugging
8310 will break. */
8311 targetm.asm_out.globalize_label (asm_out_file, sym);
8312
8313 ASM_OUTPUT_LABEL (asm_out_file, sym);
8314 }
8315
8316 /* Return a new location list, given the begin and end range, and the
8317 expression. */
8318
8319 static inline dw_loc_list_ref
8320 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8321 const char *section)
8322 {
8323 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8324
8325 retlist->begin = begin;
8326 retlist->begin_entry = NULL;
8327 retlist->end = end;
8328 retlist->expr = expr;
8329 retlist->section = section;
8330
8331 return retlist;
8332 }
8333
8334 /* Generate a new internal symbol for this location list node, if it
8335 hasn't got one yet. */
8336
8337 static inline void
8338 gen_llsym (dw_loc_list_ref list)
8339 {
8340 gcc_assert (!list->ll_symbol);
8341 list->ll_symbol = gen_internal_sym ("LLST");
8342 }
8343
8344 /* Output the location list given to us. */
8345
8346 static void
8347 output_loc_list (dw_loc_list_ref list_head)
8348 {
8349 dw_loc_list_ref curr = list_head;
8350
8351 if (list_head->emitted)
8352 return;
8353 list_head->emitted = true;
8354
8355 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8356
8357 /* Walk the location list, and output each range + expression. */
8358 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8359 {
8360 unsigned long size;
8361 /* Don't output an entry that starts and ends at the same address. */
8362 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8363 continue;
8364 size = size_of_locs (curr->expr);
8365 /* If the expression is too large, drop it on the floor. We could
8366 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8367 in the expression, but >= 64KB expressions for a single value
8368 in a single range are unlikely very useful. */
8369 if (size > 0xffff)
8370 continue;
8371 if (dwarf_split_debug_info)
8372 {
8373 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8374 "Location list start/length entry (%s)",
8375 list_head->ll_symbol);
8376 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8377 "Location list range start index (%s)",
8378 curr->begin);
8379 /* The length field is 4 bytes. If we ever need to support
8380 an 8-byte length, we can add a new DW_LLE code or fall back
8381 to DW_LLE_GNU_start_end_entry. */
8382 dw2_asm_output_delta (4, curr->end, curr->begin,
8383 "Location list range length (%s)",
8384 list_head->ll_symbol);
8385 }
8386 else if (!have_multiple_function_sections)
8387 {
8388 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8389 "Location list begin address (%s)",
8390 list_head->ll_symbol);
8391 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8392 "Location list end address (%s)",
8393 list_head->ll_symbol);
8394 }
8395 else
8396 {
8397 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8398 "Location list begin address (%s)",
8399 list_head->ll_symbol);
8400 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8401 "Location list end address (%s)",
8402 list_head->ll_symbol);
8403 }
8404
8405 /* Output the block length for this list of location operations. */
8406 gcc_assert (size <= 0xffff);
8407 dw2_asm_output_data (2, size, "%s", "Location expression size");
8408
8409 output_loc_sequence (curr->expr, -1);
8410 }
8411
8412 if (dwarf_split_debug_info)
8413 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8414 "Location list terminator (%s)",
8415 list_head->ll_symbol);
8416 else
8417 {
8418 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8419 "Location list terminator begin (%s)",
8420 list_head->ll_symbol);
8421 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8422 "Location list terminator end (%s)",
8423 list_head->ll_symbol);
8424 }
8425 }
8426
8427 /* Output a range_list offset into the debug_range section. Emit a
8428 relocated reference if val_entry is NULL, otherwise, emit an
8429 indirect reference. */
8430
8431 static void
8432 output_range_list_offset (dw_attr_ref a)
8433 {
8434 const char *name = dwarf_attr_name (a->dw_attr);
8435
8436 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8437 {
8438 char *p = strchr (ranges_section_label, '\0');
8439 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8440 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8441 debug_ranges_section, "%s", name);
8442 *p = '\0';
8443 }
8444 else
8445 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8446 "%s (offset from %s)", name, ranges_section_label);
8447 }
8448
8449 /* Output the offset into the debug_loc section. */
8450
8451 static void
8452 output_loc_list_offset (dw_attr_ref a)
8453 {
8454 char *sym = AT_loc_list (a)->ll_symbol;
8455
8456 gcc_assert (sym);
8457 if (dwarf_split_debug_info)
8458 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8459 "%s", dwarf_attr_name (a->dw_attr));
8460 else
8461 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8462 "%s", dwarf_attr_name (a->dw_attr));
8463 }
8464
8465 /* Output an attribute's index or value appropriately. */
8466
8467 static void
8468 output_attr_index_or_value (dw_attr_ref a)
8469 {
8470 const char *name = dwarf_attr_name (a->dw_attr);
8471
8472 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8473 {
8474 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8475 return;
8476 }
8477 switch (AT_class (a))
8478 {
8479 case dw_val_class_addr:
8480 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8481 break;
8482 case dw_val_class_high_pc:
8483 case dw_val_class_lbl_id:
8484 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8485 break;
8486 case dw_val_class_loc_list:
8487 output_loc_list_offset (a);
8488 break;
8489 default:
8490 gcc_unreachable ();
8491 }
8492 }
8493
8494 /* Output a type signature. */
8495
8496 static inline void
8497 output_signature (const char *sig, const char *name)
8498 {
8499 int i;
8500
8501 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8502 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8503 }
8504
8505 /* Output the DIE and its attributes. Called recursively to generate
8506 the definitions of each child DIE. */
8507
8508 static void
8509 output_die (dw_die_ref die)
8510 {
8511 dw_attr_ref a;
8512 dw_die_ref c;
8513 unsigned long size;
8514 unsigned ix;
8515
8516 /* If someone in another CU might refer to us, set up a symbol for
8517 them to point to. */
8518 if (! die->comdat_type_p && die->die_id.die_symbol)
8519 output_die_symbol (die);
8520
8521 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8522 (unsigned long)die->die_offset,
8523 dwarf_tag_name (die->die_tag));
8524
8525 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8526 {
8527 const char *name = dwarf_attr_name (a->dw_attr);
8528
8529 switch (AT_class (a))
8530 {
8531 case dw_val_class_addr:
8532 output_attr_index_or_value (a);
8533 break;
8534
8535 case dw_val_class_offset:
8536 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8537 "%s", name);
8538 break;
8539
8540 case dw_val_class_range_list:
8541 output_range_list_offset (a);
8542 break;
8543
8544 case dw_val_class_loc:
8545 size = size_of_locs (AT_loc (a));
8546
8547 /* Output the block length for this list of location operations. */
8548 if (dwarf_version >= 4)
8549 dw2_asm_output_data_uleb128 (size, "%s", name);
8550 else
8551 dw2_asm_output_data (constant_size (size), size, "%s", name);
8552
8553 output_loc_sequence (AT_loc (a), -1);
8554 break;
8555
8556 case dw_val_class_const:
8557 /* ??? It would be slightly more efficient to use a scheme like is
8558 used for unsigned constants below, but gdb 4.x does not sign
8559 extend. Gdb 5.x does sign extend. */
8560 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8561 break;
8562
8563 case dw_val_class_unsigned_const:
8564 {
8565 int csize = constant_size (AT_unsigned (a));
8566 if (dwarf_version == 3
8567 && a->dw_attr == DW_AT_data_member_location
8568 && csize >= 4)
8569 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8570 else
8571 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8572 }
8573 break;
8574
8575 case dw_val_class_const_double:
8576 {
8577 unsigned HOST_WIDE_INT first, second;
8578
8579 if (HOST_BITS_PER_WIDE_INT >= 64)
8580 dw2_asm_output_data (1,
8581 HOST_BITS_PER_DOUBLE_INT
8582 / HOST_BITS_PER_CHAR,
8583 NULL);
8584
8585 if (WORDS_BIG_ENDIAN)
8586 {
8587 first = a->dw_attr_val.v.val_double.high;
8588 second = a->dw_attr_val.v.val_double.low;
8589 }
8590 else
8591 {
8592 first = a->dw_attr_val.v.val_double.low;
8593 second = a->dw_attr_val.v.val_double.high;
8594 }
8595
8596 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8597 first, "%s", name);
8598 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8599 second, NULL);
8600 }
8601 break;
8602
8603 case dw_val_class_vec:
8604 {
8605 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8606 unsigned int len = a->dw_attr_val.v.val_vec.length;
8607 unsigned int i;
8608 unsigned char *p;
8609
8610 dw2_asm_output_data (constant_size (len * elt_size),
8611 len * elt_size, "%s", name);
8612 if (elt_size > sizeof (HOST_WIDE_INT))
8613 {
8614 elt_size /= 2;
8615 len *= 2;
8616 }
8617 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8618 i < len;
8619 i++, p += elt_size)
8620 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8621 "fp or vector constant word %u", i);
8622 break;
8623 }
8624
8625 case dw_val_class_flag:
8626 if (dwarf_version >= 4)
8627 {
8628 /* Currently all add_AT_flag calls pass in 1 as last argument,
8629 so DW_FORM_flag_present can be used. If that ever changes,
8630 we'll need to use DW_FORM_flag and have some optimization
8631 in build_abbrev_table that will change those to
8632 DW_FORM_flag_present if it is set to 1 in all DIEs using
8633 the same abbrev entry. */
8634 gcc_assert (AT_flag (a) == 1);
8635 if (flag_debug_asm)
8636 fprintf (asm_out_file, "\t\t\t%s %s\n",
8637 ASM_COMMENT_START, name);
8638 break;
8639 }
8640 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8641 break;
8642
8643 case dw_val_class_loc_list:
8644 output_attr_index_or_value (a);
8645 break;
8646
8647 case dw_val_class_die_ref:
8648 if (AT_ref_external (a))
8649 {
8650 if (AT_ref (a)->comdat_type_p)
8651 {
8652 comdat_type_node_ref type_node =
8653 AT_ref (a)->die_id.die_type_node;
8654
8655 gcc_assert (type_node);
8656 output_signature (type_node->signature, name);
8657 }
8658 else
8659 {
8660 const char *sym = AT_ref (a)->die_id.die_symbol;
8661 int size;
8662
8663 gcc_assert (sym);
8664 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8665 length, whereas in DWARF3 it's always sized as an
8666 offset. */
8667 if (dwarf_version == 2)
8668 size = DWARF2_ADDR_SIZE;
8669 else
8670 size = DWARF_OFFSET_SIZE;
8671 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8672 name);
8673 }
8674 }
8675 else
8676 {
8677 gcc_assert (AT_ref (a)->die_offset);
8678 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8679 "%s", name);
8680 }
8681 break;
8682
8683 case dw_val_class_fde_ref:
8684 {
8685 char l1[20];
8686
8687 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8688 a->dw_attr_val.v.val_fde_index * 2);
8689 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8690 "%s", name);
8691 }
8692 break;
8693
8694 case dw_val_class_vms_delta:
8695 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8696 AT_vms_delta2 (a), AT_vms_delta1 (a),
8697 "%s", name);
8698 break;
8699
8700 case dw_val_class_lbl_id:
8701 output_attr_index_or_value (a);
8702 break;
8703
8704 case dw_val_class_lineptr:
8705 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8706 debug_line_section, "%s", name);
8707 break;
8708
8709 case dw_val_class_macptr:
8710 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8711 debug_macinfo_section, "%s", name);
8712 break;
8713
8714 case dw_val_class_str:
8715 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8716 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8717 a->dw_attr_val.v.val_str->label,
8718 debug_str_section,
8719 "%s: \"%s\"", name, AT_string (a));
8720 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8721 dw2_asm_output_data_uleb128 (AT_index (a),
8722 "%s: \"%s\"", name, AT_string (a));
8723 else
8724 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8725 break;
8726
8727 case dw_val_class_file:
8728 {
8729 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8730
8731 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8732 a->dw_attr_val.v.val_file->filename);
8733 break;
8734 }
8735
8736 case dw_val_class_data8:
8737 {
8738 int i;
8739
8740 for (i = 0; i < 8; i++)
8741 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8742 i == 0 ? "%s" : NULL, name);
8743 break;
8744 }
8745
8746 case dw_val_class_high_pc:
8747 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8748 get_AT_low_pc (die), "DW_AT_high_pc");
8749 break;
8750
8751 default:
8752 gcc_unreachable ();
8753 }
8754 }
8755
8756 FOR_EACH_CHILD (die, c, output_die (c));
8757
8758 /* Add null byte to terminate sibling list. */
8759 if (die->die_child != NULL)
8760 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8761 (unsigned long) die->die_offset);
8762 }
8763
8764 /* Output the compilation unit that appears at the beginning of the
8765 .debug_info section, and precedes the DIE descriptions. */
8766
8767 static void
8768 output_compilation_unit_header (void)
8769 {
8770 int ver = dwarf_version;
8771
8772 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8773 dw2_asm_output_data (4, 0xffffffff,
8774 "Initial length escape value indicating 64-bit DWARF extension");
8775 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8776 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8777 "Length of Compilation Unit Info");
8778 dw2_asm_output_data (2, ver, "DWARF version number");
8779 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8780 debug_abbrev_section,
8781 "Offset Into Abbrev. Section");
8782 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8783 }
8784
8785 /* Output the compilation unit DIE and its children. */
8786
8787 static void
8788 output_comp_unit (dw_die_ref die, int output_if_empty)
8789 {
8790 const char *secname, *oldsym;
8791 char *tmp;
8792 external_ref_hash_type extern_map;
8793
8794 /* Unless we are outputting main CU, we may throw away empty ones. */
8795 if (!output_if_empty && die->die_child == NULL)
8796 return;
8797
8798 /* Even if there are no children of this DIE, we must output the information
8799 about the compilation unit. Otherwise, on an empty translation unit, we
8800 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8801 will then complain when examining the file. First mark all the DIEs in
8802 this CU so we know which get local refs. */
8803 mark_dies (die);
8804
8805 extern_map = optimize_external_refs (die);
8806
8807 build_abbrev_table (die, extern_map);
8808
8809 extern_map.dispose ();
8810
8811 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8812 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8813 calc_die_sizes (die);
8814
8815 oldsym = die->die_id.die_symbol;
8816 if (oldsym)
8817 {
8818 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8819
8820 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8821 secname = tmp;
8822 die->die_id.die_symbol = NULL;
8823 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8824 }
8825 else
8826 {
8827 switch_to_section (debug_info_section);
8828 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8829 info_section_emitted = true;
8830 }
8831
8832 /* Output debugging information. */
8833 output_compilation_unit_header ();
8834 output_die (die);
8835
8836 /* Leave the marks on the main CU, so we can check them in
8837 output_pubnames. */
8838 if (oldsym)
8839 {
8840 unmark_dies (die);
8841 die->die_id.die_symbol = oldsym;
8842 }
8843 }
8844
8845 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8846 and .debug_pubtypes. This is configured per-target, but can be
8847 overridden by the -gpubnames or -gno-pubnames options. */
8848
8849 static inline bool
8850 want_pubnames (void)
8851 {
8852 if (debug_info_level <= DINFO_LEVEL_TERSE)
8853 return false;
8854 if (debug_generate_pub_sections != -1)
8855 return debug_generate_pub_sections;
8856 return targetm.want_debug_pub_sections;
8857 }
8858
8859 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8860
8861 static void
8862 add_AT_pubnames (dw_die_ref die)
8863 {
8864 if (want_pubnames ())
8865 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8866 }
8867
8868 /* Add a string attribute value to a skeleton DIE. */
8869
8870 static inline void
8871 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8872 const char *str)
8873 {
8874 dw_attr_node attr;
8875 struct indirect_string_node *node;
8876
8877 if (! skeleton_debug_str_hash)
8878 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8879 debug_str_eq, NULL);
8880
8881 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8882 find_string_form (node);
8883 if (node->form == DW_FORM_GNU_str_index)
8884 node->form = DW_FORM_strp;
8885
8886 attr.dw_attr = attr_kind;
8887 attr.dw_attr_val.val_class = dw_val_class_str;
8888 attr.dw_attr_val.val_entry = NULL;
8889 attr.dw_attr_val.v.val_str = node;
8890 add_dwarf_attr (die, &attr);
8891 }
8892
8893 /* Helper function to generate top-level dies for skeleton debug_info and
8894 debug_types. */
8895
8896 static void
8897 add_top_level_skeleton_die_attrs (dw_die_ref die)
8898 {
8899 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8900 const char *comp_dir = comp_dir_string ();
8901
8902 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8903 if (comp_dir != NULL)
8904 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8905 add_AT_pubnames (die);
8906 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8907 }
8908
8909 /* Return the single type-unit die for skeleton type units. */
8910
8911 static dw_die_ref
8912 get_skeleton_type_unit (void)
8913 {
8914 /* For dwarf_split_debug_sections with use_type info, all type units in the
8915 skeleton sections have identical dies (but different headers). This
8916 single die will be output many times. */
8917
8918 static dw_die_ref skeleton_type_unit = NULL;
8919
8920 if (skeleton_type_unit == NULL)
8921 {
8922 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8923 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8924 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8925 }
8926 return skeleton_type_unit;
8927 }
8928
8929 /* Output skeleton debug sections that point to the dwo file. */
8930
8931 static void
8932 output_skeleton_debug_sections (dw_die_ref comp_unit)
8933 {
8934 /* These attributes will be found in the full debug_info section. */
8935 remove_AT (comp_unit, DW_AT_producer);
8936 remove_AT (comp_unit, DW_AT_language);
8937
8938 switch_to_section (debug_skeleton_info_section);
8939 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8940
8941 /* Produce the skeleton compilation-unit header. This one differs enough from
8942 a normal CU header that it's better not to call output_compilation_unit
8943 header. */
8944 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8945 dw2_asm_output_data (4, 0xffffffff,
8946 "Initial length escape value indicating 64-bit DWARF extension");
8947
8948 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8949 DWARF_COMPILE_UNIT_HEADER_SIZE
8950 - DWARF_INITIAL_LENGTH_SIZE
8951 + size_of_die (comp_unit),
8952 "Length of Compilation Unit Info");
8953 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8954 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8955 debug_abbrev_section,
8956 "Offset Into Abbrev. Section");
8957 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8958
8959 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8960 output_die (comp_unit);
8961
8962 /* Build the skeleton debug_abbrev section. */
8963 switch_to_section (debug_skeleton_abbrev_section);
8964 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8965
8966 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8967 if (use_debug_types)
8968 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8969
8970 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8971 }
8972
8973 /* Output a comdat type unit DIE and its children. */
8974
8975 static void
8976 output_comdat_type_unit (comdat_type_node *node)
8977 {
8978 const char *secname;
8979 char *tmp;
8980 int i;
8981 #if defined (OBJECT_FORMAT_ELF)
8982 tree comdat_key;
8983 #endif
8984 external_ref_hash_type extern_map;
8985
8986 /* First mark all the DIEs in this CU so we know which get local refs. */
8987 mark_dies (node->root_die);
8988
8989 extern_map = optimize_external_refs (node->root_die);
8990
8991 build_abbrev_table (node->root_die, extern_map);
8992
8993 extern_map.dispose ();
8994
8995 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8996 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8997 calc_die_sizes (node->root_die);
8998
8999 #if defined (OBJECT_FORMAT_ELF)
9000 if (!dwarf_split_debug_info)
9001 secname = ".debug_types";
9002 else
9003 secname = ".debug_types.dwo";
9004
9005 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9006 sprintf (tmp, "wt.");
9007 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9008 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9009 comdat_key = get_identifier (tmp);
9010 targetm.asm_out.named_section (secname,
9011 SECTION_DEBUG | SECTION_LINKONCE,
9012 comdat_key);
9013 #else
9014 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9015 sprintf (tmp, ".gnu.linkonce.wt.");
9016 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9017 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9018 secname = tmp;
9019 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9020 #endif
9021
9022 /* Output debugging information. */
9023 output_compilation_unit_header ();
9024 output_signature (node->signature, "Type Signature");
9025 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9026 "Offset to Type DIE");
9027 output_die (node->root_die);
9028
9029 unmark_dies (node->root_die);
9030
9031 #if defined (OBJECT_FORMAT_ELF)
9032 if (dwarf_split_debug_info)
9033 {
9034 /* Produce the skeleton type-unit header. */
9035 const char *secname = ".debug_types";
9036
9037 targetm.asm_out.named_section (secname,
9038 SECTION_DEBUG | SECTION_LINKONCE,
9039 comdat_key);
9040 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9041 dw2_asm_output_data (4, 0xffffffff,
9042 "Initial length escape value indicating 64-bit DWARF extension");
9043
9044 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9045 DWARF_COMPILE_UNIT_HEADER_SIZE
9046 - DWARF_INITIAL_LENGTH_SIZE
9047 + size_of_die (get_skeleton_type_unit ())
9048 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9049 "Length of Type Unit Info");
9050 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9051 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9052 debug_skeleton_abbrev_section_label,
9053 debug_abbrev_section,
9054 "Offset Into Abbrev. Section");
9055 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9056 output_signature (node->signature, "Type Signature");
9057 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9058
9059 output_die (get_skeleton_type_unit ());
9060 }
9061 #endif
9062 }
9063
9064 /* Return the DWARF2/3 pubname associated with a decl. */
9065
9066 static const char *
9067 dwarf2_name (tree decl, int scope)
9068 {
9069 if (DECL_NAMELESS (decl))
9070 return NULL;
9071 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9072 }
9073
9074 /* Add a new entry to .debug_pubnames if appropriate. */
9075
9076 static void
9077 add_pubname_string (const char *str, dw_die_ref die)
9078 {
9079 pubname_entry e;
9080
9081 e.die = die;
9082 e.name = xstrdup (str);
9083 vec_safe_push (pubname_table, e);
9084 }
9085
9086 static void
9087 add_pubname (tree decl, dw_die_ref die)
9088 {
9089 if (!want_pubnames ())
9090 return;
9091
9092 /* Don't add items to the table when we expect that the consumer will have
9093 just read the enclosing die. For example, if the consumer is looking at a
9094 class_member, it will either be inside the class already, or will have just
9095 looked up the class to find the member. Either way, searching the class is
9096 faster than searching the index. */
9097 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9098 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9099 {
9100 const char *name = dwarf2_name (decl, 1);
9101
9102 if (name)
9103 add_pubname_string (name, die);
9104 }
9105 }
9106
9107 /* Add an enumerator to the pubnames section. */
9108
9109 static void
9110 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9111 {
9112 pubname_entry e;
9113
9114 gcc_assert (scope_name);
9115 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9116 e.die = die;
9117 vec_safe_push (pubname_table, e);
9118 }
9119
9120 /* Add a new entry to .debug_pubtypes if appropriate. */
9121
9122 static void
9123 add_pubtype (tree decl, dw_die_ref die)
9124 {
9125 pubname_entry e;
9126
9127 if (!want_pubnames ())
9128 return;
9129
9130 if ((TREE_PUBLIC (decl)
9131 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9132 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9133 {
9134 tree scope = NULL;
9135 const char *scope_name = "";
9136 const char *sep = is_cxx () ? "::" : ".";
9137 const char *name;
9138
9139 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9140 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9141 {
9142 scope_name = lang_hooks.dwarf_name (scope, 1);
9143 if (scope_name != NULL && scope_name[0] != '\0')
9144 scope_name = concat (scope_name, sep, NULL);
9145 else
9146 scope_name = "";
9147 }
9148
9149 if (TYPE_P (decl))
9150 name = type_tag (decl);
9151 else
9152 name = lang_hooks.dwarf_name (decl, 1);
9153
9154 /* If we don't have a name for the type, there's no point in adding
9155 it to the table. */
9156 if (name != NULL && name[0] != '\0')
9157 {
9158 e.die = die;
9159 e.name = concat (scope_name, name, NULL);
9160 vec_safe_push (pubtype_table, e);
9161 }
9162
9163 /* Although it might be more consistent to add the pubinfo for the
9164 enumerators as their dies are created, they should only be added if the
9165 enum type meets the criteria above. So rather than re-check the parent
9166 enum type whenever an enumerator die is created, just output them all
9167 here. This isn't protected by the name conditional because anonymous
9168 enums don't have names. */
9169 if (die->die_tag == DW_TAG_enumeration_type)
9170 {
9171 dw_die_ref c;
9172
9173 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9174 }
9175 }
9176 }
9177
9178 /* Output a single entry in the pubnames table. */
9179
9180 static void
9181 output_pubname (dw_offset die_offset, pubname_entry *entry)
9182 {
9183 dw_die_ref die = entry->die;
9184 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9185
9186 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9187
9188 if (debug_generate_pub_sections == 2)
9189 {
9190 /* This logic follows gdb's method for determining the value of the flag
9191 byte. */
9192 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9193 switch (die->die_tag)
9194 {
9195 case DW_TAG_typedef:
9196 case DW_TAG_base_type:
9197 case DW_TAG_subrange_type:
9198 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9199 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9200 break;
9201 case DW_TAG_enumerator:
9202 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9203 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9204 if (!is_cxx () && !is_java ())
9205 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9206 break;
9207 case DW_TAG_subprogram:
9208 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9209 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9210 if (!is_ada ())
9211 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9212 break;
9213 case DW_TAG_constant:
9214 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9215 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9216 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9217 break;
9218 case DW_TAG_variable:
9219 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9220 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9221 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9222 break;
9223 case DW_TAG_namespace:
9224 case DW_TAG_imported_declaration:
9225 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9226 break;
9227 case DW_TAG_class_type:
9228 case DW_TAG_interface_type:
9229 case DW_TAG_structure_type:
9230 case DW_TAG_union_type:
9231 case DW_TAG_enumeration_type:
9232 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9233 if (!is_cxx () && !is_java ())
9234 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9235 break;
9236 default:
9237 /* An unusual tag. Leave the flag-byte empty. */
9238 break;
9239 }
9240 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9241 "GDB-index flags");
9242 }
9243
9244 dw2_asm_output_nstring (entry->name, -1, "external name");
9245 }
9246
9247
9248 /* Output the public names table used to speed up access to externally
9249 visible names; or the public types table used to find type definitions. */
9250
9251 static void
9252 output_pubnames (vec<pubname_entry, va_gc> *names)
9253 {
9254 unsigned i;
9255 unsigned long pubnames_length = size_of_pubnames (names);
9256 pubname_ref pub;
9257
9258 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9259 dw2_asm_output_data (4, 0xffffffff,
9260 "Initial length escape value indicating 64-bit DWARF extension");
9261 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9262
9263 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9264 dw2_asm_output_data (2, 2, "DWARF Version");
9265
9266 if (dwarf_split_debug_info)
9267 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9268 debug_skeleton_info_section,
9269 "Offset of Compilation Unit Info");
9270 else
9271 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9272 debug_info_section,
9273 "Offset of Compilation Unit Info");
9274 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9275 "Compilation Unit Length");
9276
9277 FOR_EACH_VEC_ELT (*names, i, pub)
9278 {
9279 if (include_pubname_in_output (names, pub))
9280 {
9281 dw_offset die_offset = pub->die->die_offset;
9282
9283 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9284 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9285 gcc_assert (pub->die->die_mark);
9286
9287 /* If we're putting types in their own .debug_types sections,
9288 the .debug_pubtypes table will still point to the compile
9289 unit (not the type unit), so we want to use the offset of
9290 the skeleton DIE (if there is one). */
9291 if (pub->die->comdat_type_p && names == pubtype_table)
9292 {
9293 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9294
9295 if (type_node != NULL)
9296 die_offset = (type_node->skeleton_die != NULL
9297 ? type_node->skeleton_die->die_offset
9298 : 0);
9299 }
9300
9301 output_pubname (die_offset, pub);
9302 }
9303 }
9304
9305 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9306 }
9307
9308 /* Output public names and types tables if necessary. */
9309
9310 static void
9311 output_pubtables (void)
9312 {
9313 if (!want_pubnames () || !info_section_emitted)
9314 return;
9315
9316 switch_to_section (debug_pubnames_section);
9317 output_pubnames (pubname_table);
9318 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9319 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9320 simply won't look for the section. */
9321 switch_to_section (debug_pubtypes_section);
9322 output_pubnames (pubtype_table);
9323 }
9324
9325
9326 /* Output the information that goes into the .debug_aranges table.
9327 Namely, define the beginning and ending address range of the
9328 text section generated for this compilation unit. */
9329
9330 static void
9331 output_aranges (unsigned long aranges_length)
9332 {
9333 unsigned i;
9334
9335 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9336 dw2_asm_output_data (4, 0xffffffff,
9337 "Initial length escape value indicating 64-bit DWARF extension");
9338 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9339 "Length of Address Ranges Info");
9340 /* Version number for aranges is still 2, even in DWARF3. */
9341 dw2_asm_output_data (2, 2, "DWARF Version");
9342 if (dwarf_split_debug_info)
9343 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9344 debug_skeleton_info_section,
9345 "Offset of Compilation Unit Info");
9346 else
9347 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9348 debug_info_section,
9349 "Offset of Compilation Unit Info");
9350 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9351 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9352
9353 /* We need to align to twice the pointer size here. */
9354 if (DWARF_ARANGES_PAD_SIZE)
9355 {
9356 /* Pad using a 2 byte words so that padding is correct for any
9357 pointer size. */
9358 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9359 2 * DWARF2_ADDR_SIZE);
9360 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9361 dw2_asm_output_data (2, 0, NULL);
9362 }
9363
9364 /* It is necessary not to output these entries if the sections were
9365 not used; if the sections were not used, the length will be 0 and
9366 the address may end up as 0 if the section is discarded by ld
9367 --gc-sections, leaving an invalid (0, 0) entry that can be
9368 confused with the terminator. */
9369 if (text_section_used)
9370 {
9371 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9372 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9373 text_section_label, "Length");
9374 }
9375 if (cold_text_section_used)
9376 {
9377 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9378 "Address");
9379 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9380 cold_text_section_label, "Length");
9381 }
9382
9383 if (have_multiple_function_sections)
9384 {
9385 unsigned fde_idx;
9386 dw_fde_ref fde;
9387
9388 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9389 {
9390 if (DECL_IGNORED_P (fde->decl))
9391 continue;
9392 if (!fde->in_std_section)
9393 {
9394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9395 "Address");
9396 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9397 fde->dw_fde_begin, "Length");
9398 }
9399 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9400 {
9401 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9402 "Address");
9403 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9404 fde->dw_fde_second_begin, "Length");
9405 }
9406 }
9407 }
9408
9409 /* Output the terminator words. */
9410 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9411 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9412 }
9413
9414 /* Add a new entry to .debug_ranges. Return the offset at which it
9415 was placed. */
9416
9417 static unsigned int
9418 add_ranges_num (int num)
9419 {
9420 unsigned int in_use = ranges_table_in_use;
9421
9422 if (in_use == ranges_table_allocated)
9423 {
9424 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9425 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9426 ranges_table_allocated);
9427 memset (ranges_table + ranges_table_in_use, 0,
9428 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9429 }
9430
9431 ranges_table[in_use].num = num;
9432 ranges_table_in_use = in_use + 1;
9433
9434 return in_use * 2 * DWARF2_ADDR_SIZE;
9435 }
9436
9437 /* Add a new entry to .debug_ranges corresponding to a block, or a
9438 range terminator if BLOCK is NULL. */
9439
9440 static unsigned int
9441 add_ranges (const_tree block)
9442 {
9443 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9444 }
9445
9446 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9447 When using dwarf_split_debug_info, address attributes in dies destined
9448 for the final executable should be direct references--setting the
9449 parameter force_direct ensures this behavior. */
9450
9451 static void
9452 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9453 bool *added, bool force_direct)
9454 {
9455 unsigned int in_use = ranges_by_label_in_use;
9456 unsigned int offset;
9457
9458 if (in_use == ranges_by_label_allocated)
9459 {
9460 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9461 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9462 ranges_by_label,
9463 ranges_by_label_allocated);
9464 memset (ranges_by_label + ranges_by_label_in_use, 0,
9465 RANGES_TABLE_INCREMENT
9466 * sizeof (struct dw_ranges_by_label_struct));
9467 }
9468
9469 ranges_by_label[in_use].begin = begin;
9470 ranges_by_label[in_use].end = end;
9471 ranges_by_label_in_use = in_use + 1;
9472
9473 offset = add_ranges_num (-(int)in_use - 1);
9474 if (!*added)
9475 {
9476 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9477 *added = true;
9478 }
9479 }
9480
9481 static void
9482 output_ranges (void)
9483 {
9484 unsigned i;
9485 static const char *const start_fmt = "Offset %#x";
9486 const char *fmt = start_fmt;
9487
9488 for (i = 0; i < ranges_table_in_use; i++)
9489 {
9490 int block_num = ranges_table[i].num;
9491
9492 if (block_num > 0)
9493 {
9494 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9495 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9496
9497 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9498 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9499
9500 /* If all code is in the text section, then the compilation
9501 unit base address defaults to DW_AT_low_pc, which is the
9502 base of the text section. */
9503 if (!have_multiple_function_sections)
9504 {
9505 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9506 text_section_label,
9507 fmt, i * 2 * DWARF2_ADDR_SIZE);
9508 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9509 text_section_label, NULL);
9510 }
9511
9512 /* Otherwise, the compilation unit base address is zero,
9513 which allows us to use absolute addresses, and not worry
9514 about whether the target supports cross-section
9515 arithmetic. */
9516 else
9517 {
9518 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9519 fmt, i * 2 * DWARF2_ADDR_SIZE);
9520 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9521 }
9522
9523 fmt = NULL;
9524 }
9525
9526 /* Negative block_num stands for an index into ranges_by_label. */
9527 else if (block_num < 0)
9528 {
9529 int lab_idx = - block_num - 1;
9530
9531 if (!have_multiple_function_sections)
9532 {
9533 gcc_unreachable ();
9534 #if 0
9535 /* If we ever use add_ranges_by_labels () for a single
9536 function section, all we have to do is to take out
9537 the #if 0 above. */
9538 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9539 ranges_by_label[lab_idx].begin,
9540 text_section_label,
9541 fmt, i * 2 * DWARF2_ADDR_SIZE);
9542 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9543 ranges_by_label[lab_idx].end,
9544 text_section_label, NULL);
9545 #endif
9546 }
9547 else
9548 {
9549 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9550 ranges_by_label[lab_idx].begin,
9551 fmt, i * 2 * DWARF2_ADDR_SIZE);
9552 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9553 ranges_by_label[lab_idx].end,
9554 NULL);
9555 }
9556 }
9557 else
9558 {
9559 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9560 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9561 fmt = start_fmt;
9562 }
9563 }
9564 }
9565
9566 /* Data structure containing information about input files. */
9567 struct file_info
9568 {
9569 const char *path; /* Complete file name. */
9570 const char *fname; /* File name part. */
9571 int length; /* Length of entire string. */
9572 struct dwarf_file_data * file_idx; /* Index in input file table. */
9573 int dir_idx; /* Index in directory table. */
9574 };
9575
9576 /* Data structure containing information about directories with source
9577 files. */
9578 struct dir_info
9579 {
9580 const char *path; /* Path including directory name. */
9581 int length; /* Path length. */
9582 int prefix; /* Index of directory entry which is a prefix. */
9583 int count; /* Number of files in this directory. */
9584 int dir_idx; /* Index of directory used as base. */
9585 };
9586
9587 /* Callback function for file_info comparison. We sort by looking at
9588 the directories in the path. */
9589
9590 static int
9591 file_info_cmp (const void *p1, const void *p2)
9592 {
9593 const struct file_info *const s1 = (const struct file_info *) p1;
9594 const struct file_info *const s2 = (const struct file_info *) p2;
9595 const unsigned char *cp1;
9596 const unsigned char *cp2;
9597
9598 /* Take care of file names without directories. We need to make sure that
9599 we return consistent values to qsort since some will get confused if
9600 we return the same value when identical operands are passed in opposite
9601 orders. So if neither has a directory, return 0 and otherwise return
9602 1 or -1 depending on which one has the directory. */
9603 if ((s1->path == s1->fname || s2->path == s2->fname))
9604 return (s2->path == s2->fname) - (s1->path == s1->fname);
9605
9606 cp1 = (const unsigned char *) s1->path;
9607 cp2 = (const unsigned char *) s2->path;
9608
9609 while (1)
9610 {
9611 ++cp1;
9612 ++cp2;
9613 /* Reached the end of the first path? If so, handle like above. */
9614 if ((cp1 == (const unsigned char *) s1->fname)
9615 || (cp2 == (const unsigned char *) s2->fname))
9616 return ((cp2 == (const unsigned char *) s2->fname)
9617 - (cp1 == (const unsigned char *) s1->fname));
9618
9619 /* Character of current path component the same? */
9620 else if (*cp1 != *cp2)
9621 return *cp1 - *cp2;
9622 }
9623 }
9624
9625 struct file_name_acquire_data
9626 {
9627 struct file_info *files;
9628 int used_files;
9629 int max_files;
9630 };
9631
9632 /* Traversal function for the hash table. */
9633
9634 static int
9635 file_name_acquire (void ** slot, void *data)
9636 {
9637 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9638 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9639 struct file_info *fi;
9640 const char *f;
9641
9642 gcc_assert (fnad->max_files >= d->emitted_number);
9643
9644 if (! d->emitted_number)
9645 return 1;
9646
9647 gcc_assert (fnad->max_files != fnad->used_files);
9648
9649 fi = fnad->files + fnad->used_files++;
9650
9651 /* Skip all leading "./". */
9652 f = d->filename;
9653 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9654 f += 2;
9655
9656 /* Create a new array entry. */
9657 fi->path = f;
9658 fi->length = strlen (f);
9659 fi->file_idx = d;
9660
9661 /* Search for the file name part. */
9662 f = strrchr (f, DIR_SEPARATOR);
9663 #if defined (DIR_SEPARATOR_2)
9664 {
9665 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9666
9667 if (g != NULL)
9668 {
9669 if (f == NULL || f < g)
9670 f = g;
9671 }
9672 }
9673 #endif
9674
9675 fi->fname = f == NULL ? fi->path : f + 1;
9676 return 1;
9677 }
9678
9679 /* Output the directory table and the file name table. We try to minimize
9680 the total amount of memory needed. A heuristic is used to avoid large
9681 slowdowns with many input files. */
9682
9683 static void
9684 output_file_names (void)
9685 {
9686 struct file_name_acquire_data fnad;
9687 int numfiles;
9688 struct file_info *files;
9689 struct dir_info *dirs;
9690 int *saved;
9691 int *savehere;
9692 int *backmap;
9693 int ndirs;
9694 int idx_offset;
9695 int i;
9696
9697 if (!last_emitted_file)
9698 {
9699 dw2_asm_output_data (1, 0, "End directory table");
9700 dw2_asm_output_data (1, 0, "End file name table");
9701 return;
9702 }
9703
9704 numfiles = last_emitted_file->emitted_number;
9705
9706 /* Allocate the various arrays we need. */
9707 files = XALLOCAVEC (struct file_info, numfiles);
9708 dirs = XALLOCAVEC (struct dir_info, numfiles);
9709
9710 fnad.files = files;
9711 fnad.used_files = 0;
9712 fnad.max_files = numfiles;
9713 htab_traverse (file_table, file_name_acquire, &fnad);
9714 gcc_assert (fnad.used_files == fnad.max_files);
9715
9716 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9717
9718 /* Find all the different directories used. */
9719 dirs[0].path = files[0].path;
9720 dirs[0].length = files[0].fname - files[0].path;
9721 dirs[0].prefix = -1;
9722 dirs[0].count = 1;
9723 dirs[0].dir_idx = 0;
9724 files[0].dir_idx = 0;
9725 ndirs = 1;
9726
9727 for (i = 1; i < numfiles; i++)
9728 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9729 && memcmp (dirs[ndirs - 1].path, files[i].path,
9730 dirs[ndirs - 1].length) == 0)
9731 {
9732 /* Same directory as last entry. */
9733 files[i].dir_idx = ndirs - 1;
9734 ++dirs[ndirs - 1].count;
9735 }
9736 else
9737 {
9738 int j;
9739
9740 /* This is a new directory. */
9741 dirs[ndirs].path = files[i].path;
9742 dirs[ndirs].length = files[i].fname - files[i].path;
9743 dirs[ndirs].count = 1;
9744 dirs[ndirs].dir_idx = ndirs;
9745 files[i].dir_idx = ndirs;
9746
9747 /* Search for a prefix. */
9748 dirs[ndirs].prefix = -1;
9749 for (j = 0; j < ndirs; j++)
9750 if (dirs[j].length < dirs[ndirs].length
9751 && dirs[j].length > 1
9752 && (dirs[ndirs].prefix == -1
9753 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9754 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9755 dirs[ndirs].prefix = j;
9756
9757 ++ndirs;
9758 }
9759
9760 /* Now to the actual work. We have to find a subset of the directories which
9761 allow expressing the file name using references to the directory table
9762 with the least amount of characters. We do not do an exhaustive search
9763 where we would have to check out every combination of every single
9764 possible prefix. Instead we use a heuristic which provides nearly optimal
9765 results in most cases and never is much off. */
9766 saved = XALLOCAVEC (int, ndirs);
9767 savehere = XALLOCAVEC (int, ndirs);
9768
9769 memset (saved, '\0', ndirs * sizeof (saved[0]));
9770 for (i = 0; i < ndirs; i++)
9771 {
9772 int j;
9773 int total;
9774
9775 /* We can always save some space for the current directory. But this
9776 does not mean it will be enough to justify adding the directory. */
9777 savehere[i] = dirs[i].length;
9778 total = (savehere[i] - saved[i]) * dirs[i].count;
9779
9780 for (j = i + 1; j < ndirs; j++)
9781 {
9782 savehere[j] = 0;
9783 if (saved[j] < dirs[i].length)
9784 {
9785 /* Determine whether the dirs[i] path is a prefix of the
9786 dirs[j] path. */
9787 int k;
9788
9789 k = dirs[j].prefix;
9790 while (k != -1 && k != (int) i)
9791 k = dirs[k].prefix;
9792
9793 if (k == (int) i)
9794 {
9795 /* Yes it is. We can possibly save some memory by
9796 writing the filenames in dirs[j] relative to
9797 dirs[i]. */
9798 savehere[j] = dirs[i].length;
9799 total += (savehere[j] - saved[j]) * dirs[j].count;
9800 }
9801 }
9802 }
9803
9804 /* Check whether we can save enough to justify adding the dirs[i]
9805 directory. */
9806 if (total > dirs[i].length + 1)
9807 {
9808 /* It's worthwhile adding. */
9809 for (j = i; j < ndirs; j++)
9810 if (savehere[j] > 0)
9811 {
9812 /* Remember how much we saved for this directory so far. */
9813 saved[j] = savehere[j];
9814
9815 /* Remember the prefix directory. */
9816 dirs[j].dir_idx = i;
9817 }
9818 }
9819 }
9820
9821 /* Emit the directory name table. */
9822 idx_offset = dirs[0].length > 0 ? 1 : 0;
9823 for (i = 1 - idx_offset; i < ndirs; i++)
9824 dw2_asm_output_nstring (dirs[i].path,
9825 dirs[i].length
9826 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9827 "Directory Entry: %#x", i + idx_offset);
9828
9829 dw2_asm_output_data (1, 0, "End directory table");
9830
9831 /* We have to emit them in the order of emitted_number since that's
9832 used in the debug info generation. To do this efficiently we
9833 generate a back-mapping of the indices first. */
9834 backmap = XALLOCAVEC (int, numfiles);
9835 for (i = 0; i < numfiles; i++)
9836 backmap[files[i].file_idx->emitted_number - 1] = i;
9837
9838 /* Now write all the file names. */
9839 for (i = 0; i < numfiles; i++)
9840 {
9841 int file_idx = backmap[i];
9842 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9843
9844 #ifdef VMS_DEBUGGING_INFO
9845 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9846
9847 /* Setting these fields can lead to debugger miscomparisons,
9848 but VMS Debug requires them to be set correctly. */
9849
9850 int ver;
9851 long long cdt;
9852 long siz;
9853 int maxfilelen = strlen (files[file_idx].path)
9854 + dirs[dir_idx].length
9855 + MAX_VMS_VERSION_LEN + 1;
9856 char *filebuf = XALLOCAVEC (char, maxfilelen);
9857
9858 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9859 snprintf (filebuf, maxfilelen, "%s;%d",
9860 files[file_idx].path + dirs[dir_idx].length, ver);
9861
9862 dw2_asm_output_nstring
9863 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9864
9865 /* Include directory index. */
9866 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9867
9868 /* Modification time. */
9869 dw2_asm_output_data_uleb128
9870 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9871 ? cdt : 0,
9872 NULL);
9873
9874 /* File length in bytes. */
9875 dw2_asm_output_data_uleb128
9876 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9877 ? siz : 0,
9878 NULL);
9879 #else
9880 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9881 "File Entry: %#x", (unsigned) i + 1);
9882
9883 /* Include directory index. */
9884 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9885
9886 /* Modification time. */
9887 dw2_asm_output_data_uleb128 (0, NULL);
9888
9889 /* File length in bytes. */
9890 dw2_asm_output_data_uleb128 (0, NULL);
9891 #endif /* VMS_DEBUGGING_INFO */
9892 }
9893
9894 dw2_asm_output_data (1, 0, "End file name table");
9895 }
9896
9897
9898 /* Output one line number table into the .debug_line section. */
9899
9900 static void
9901 output_one_line_info_table (dw_line_info_table *table)
9902 {
9903 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9904 unsigned int current_line = 1;
9905 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9906 dw_line_info_entry *ent;
9907 size_t i;
9908
9909 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9910 {
9911 switch (ent->opcode)
9912 {
9913 case LI_set_address:
9914 /* ??? Unfortunately, we have little choice here currently, and
9915 must always use the most general form. GCC does not know the
9916 address delta itself, so we can't use DW_LNS_advance_pc. Many
9917 ports do have length attributes which will give an upper bound
9918 on the address range. We could perhaps use length attributes
9919 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9920 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9921
9922 /* This can handle any delta. This takes
9923 4+DWARF2_ADDR_SIZE bytes. */
9924 dw2_asm_output_data (1, 0, "set address %s", line_label);
9925 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9926 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9928 break;
9929
9930 case LI_set_line:
9931 if (ent->val == current_line)
9932 {
9933 /* We still need to start a new row, so output a copy insn. */
9934 dw2_asm_output_data (1, DW_LNS_copy,
9935 "copy line %u", current_line);
9936 }
9937 else
9938 {
9939 int line_offset = ent->val - current_line;
9940 int line_delta = line_offset - DWARF_LINE_BASE;
9941
9942 current_line = ent->val;
9943 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9944 {
9945 /* This can handle deltas from -10 to 234, using the current
9946 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9947 This takes 1 byte. */
9948 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9949 "line %u", current_line);
9950 }
9951 else
9952 {
9953 /* This can handle any delta. This takes at least 4 bytes,
9954 depending on the value being encoded. */
9955 dw2_asm_output_data (1, DW_LNS_advance_line,
9956 "advance to line %u", current_line);
9957 dw2_asm_output_data_sleb128 (line_offset, NULL);
9958 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9959 }
9960 }
9961 break;
9962
9963 case LI_set_file:
9964 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9965 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9966 break;
9967
9968 case LI_set_column:
9969 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9970 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9971 break;
9972
9973 case LI_negate_stmt:
9974 current_is_stmt = !current_is_stmt;
9975 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9976 "is_stmt %d", current_is_stmt);
9977 break;
9978
9979 case LI_set_prologue_end:
9980 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9981 "set prologue end");
9982 break;
9983
9984 case LI_set_epilogue_begin:
9985 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9986 "set epilogue begin");
9987 break;
9988
9989 case LI_set_discriminator:
9990 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9991 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9992 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9993 dw2_asm_output_data_uleb128 (ent->val, NULL);
9994 break;
9995 }
9996 }
9997
9998 /* Emit debug info for the address of the end of the table. */
9999 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10000 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10001 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10002 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10003
10004 dw2_asm_output_data (1, 0, "end sequence");
10005 dw2_asm_output_data_uleb128 (1, NULL);
10006 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10007 }
10008
10009 /* Output the source line number correspondence information. This
10010 information goes into the .debug_line section. */
10011
10012 static void
10013 output_line_info (bool prologue_only)
10014 {
10015 char l1[20], l2[20], p1[20], p2[20];
10016 int ver = dwarf_version;
10017 bool saw_one = false;
10018 int opc;
10019
10020 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10021 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10022 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10023 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10024
10025 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10026 dw2_asm_output_data (4, 0xffffffff,
10027 "Initial length escape value indicating 64-bit DWARF extension");
10028 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10029 "Length of Source Line Info");
10030 ASM_OUTPUT_LABEL (asm_out_file, l1);
10031
10032 dw2_asm_output_data (2, ver, "DWARF Version");
10033 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10034 ASM_OUTPUT_LABEL (asm_out_file, p1);
10035
10036 /* Define the architecture-dependent minimum instruction length (in bytes).
10037 In this implementation of DWARF, this field is used for information
10038 purposes only. Since GCC generates assembly language, we have no
10039 a priori knowledge of how many instruction bytes are generated for each
10040 source line, and therefore can use only the DW_LNE_set_address and
10041 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10042 this as '1', which is "correct enough" for all architectures,
10043 and don't let the target override. */
10044 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10045
10046 if (ver >= 4)
10047 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10048 "Maximum Operations Per Instruction");
10049 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10050 "Default is_stmt_start flag");
10051 dw2_asm_output_data (1, DWARF_LINE_BASE,
10052 "Line Base Value (Special Opcodes)");
10053 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10054 "Line Range Value (Special Opcodes)");
10055 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10056 "Special Opcode Base");
10057
10058 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10059 {
10060 int n_op_args;
10061 switch (opc)
10062 {
10063 case DW_LNS_advance_pc:
10064 case DW_LNS_advance_line:
10065 case DW_LNS_set_file:
10066 case DW_LNS_set_column:
10067 case DW_LNS_fixed_advance_pc:
10068 case DW_LNS_set_isa:
10069 n_op_args = 1;
10070 break;
10071 default:
10072 n_op_args = 0;
10073 break;
10074 }
10075
10076 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10077 opc, n_op_args);
10078 }
10079
10080 /* Write out the information about the files we use. */
10081 output_file_names ();
10082 ASM_OUTPUT_LABEL (asm_out_file, p2);
10083 if (prologue_only)
10084 {
10085 /* Output the marker for the end of the line number info. */
10086 ASM_OUTPUT_LABEL (asm_out_file, l2);
10087 return;
10088 }
10089
10090 if (separate_line_info)
10091 {
10092 dw_line_info_table *table;
10093 size_t i;
10094
10095 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10096 if (table->in_use)
10097 {
10098 output_one_line_info_table (table);
10099 saw_one = true;
10100 }
10101 }
10102 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10103 {
10104 output_one_line_info_table (cold_text_section_line_info);
10105 saw_one = true;
10106 }
10107
10108 /* ??? Some Darwin linkers crash on a .debug_line section with no
10109 sequences. Further, merely a DW_LNE_end_sequence entry is not
10110 sufficient -- the address column must also be initialized.
10111 Make sure to output at least one set_address/end_sequence pair,
10112 choosing .text since that section is always present. */
10113 if (text_section_line_info->in_use || !saw_one)
10114 output_one_line_info_table (text_section_line_info);
10115
10116 /* Output the marker for the end of the line number info. */
10117 ASM_OUTPUT_LABEL (asm_out_file, l2);
10118 }
10119 \f
10120 /* Given a pointer to a tree node for some base type, return a pointer to
10121 a DIE that describes the given type.
10122
10123 This routine must only be called for GCC type nodes that correspond to
10124 Dwarf base (fundamental) types. */
10125
10126 static dw_die_ref
10127 base_type_die (tree type)
10128 {
10129 dw_die_ref base_type_result;
10130 enum dwarf_type encoding;
10131
10132 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10133 return 0;
10134
10135 /* If this is a subtype that should not be emitted as a subrange type,
10136 use the base type. See subrange_type_for_debug_p. */
10137 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10138 type = TREE_TYPE (type);
10139
10140 switch (TREE_CODE (type))
10141 {
10142 case INTEGER_TYPE:
10143 if ((dwarf_version >= 4 || !dwarf_strict)
10144 && TYPE_NAME (type)
10145 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10146 && DECL_IS_BUILTIN (TYPE_NAME (type))
10147 && DECL_NAME (TYPE_NAME (type)))
10148 {
10149 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10150 if (strcmp (name, "char16_t") == 0
10151 || strcmp (name, "char32_t") == 0)
10152 {
10153 encoding = DW_ATE_UTF;
10154 break;
10155 }
10156 }
10157 if (TYPE_STRING_FLAG (type))
10158 {
10159 if (TYPE_UNSIGNED (type))
10160 encoding = DW_ATE_unsigned_char;
10161 else
10162 encoding = DW_ATE_signed_char;
10163 }
10164 else if (TYPE_UNSIGNED (type))
10165 encoding = DW_ATE_unsigned;
10166 else
10167 encoding = DW_ATE_signed;
10168 break;
10169
10170 case REAL_TYPE:
10171 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10172 {
10173 if (dwarf_version >= 3 || !dwarf_strict)
10174 encoding = DW_ATE_decimal_float;
10175 else
10176 encoding = DW_ATE_lo_user;
10177 }
10178 else
10179 encoding = DW_ATE_float;
10180 break;
10181
10182 case FIXED_POINT_TYPE:
10183 if (!(dwarf_version >= 3 || !dwarf_strict))
10184 encoding = DW_ATE_lo_user;
10185 else if (TYPE_UNSIGNED (type))
10186 encoding = DW_ATE_unsigned_fixed;
10187 else
10188 encoding = DW_ATE_signed_fixed;
10189 break;
10190
10191 /* Dwarf2 doesn't know anything about complex ints, so use
10192 a user defined type for it. */
10193 case COMPLEX_TYPE:
10194 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10195 encoding = DW_ATE_complex_float;
10196 else
10197 encoding = DW_ATE_lo_user;
10198 break;
10199
10200 case BOOLEAN_TYPE:
10201 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10202 encoding = DW_ATE_boolean;
10203 break;
10204
10205 default:
10206 /* No other TREE_CODEs are Dwarf fundamental types. */
10207 gcc_unreachable ();
10208 }
10209
10210 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10211
10212 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10213 int_size_in_bytes (type));
10214 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10215 add_pubtype (type, base_type_result);
10216
10217 return base_type_result;
10218 }
10219
10220 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10221 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10222
10223 static inline int
10224 is_base_type (tree type)
10225 {
10226 switch (TREE_CODE (type))
10227 {
10228 case ERROR_MARK:
10229 case VOID_TYPE:
10230 case INTEGER_TYPE:
10231 case REAL_TYPE:
10232 case FIXED_POINT_TYPE:
10233 case COMPLEX_TYPE:
10234 case BOOLEAN_TYPE:
10235 return 1;
10236
10237 case ARRAY_TYPE:
10238 case RECORD_TYPE:
10239 case UNION_TYPE:
10240 case QUAL_UNION_TYPE:
10241 case ENUMERAL_TYPE:
10242 case FUNCTION_TYPE:
10243 case METHOD_TYPE:
10244 case POINTER_TYPE:
10245 case REFERENCE_TYPE:
10246 case NULLPTR_TYPE:
10247 case OFFSET_TYPE:
10248 case LANG_TYPE:
10249 case VECTOR_TYPE:
10250 return 0;
10251
10252 default:
10253 gcc_unreachable ();
10254 }
10255
10256 return 0;
10257 }
10258
10259 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10260 node, return the size in bits for the type if it is a constant, or else
10261 return the alignment for the type if the type's size is not constant, or
10262 else return BITS_PER_WORD if the type actually turns out to be an
10263 ERROR_MARK node. */
10264
10265 static inline unsigned HOST_WIDE_INT
10266 simple_type_size_in_bits (const_tree type)
10267 {
10268 if (TREE_CODE (type) == ERROR_MARK)
10269 return BITS_PER_WORD;
10270 else if (TYPE_SIZE (type) == NULL_TREE)
10271 return 0;
10272 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10273 return tree_to_uhwi (TYPE_SIZE (type));
10274 else
10275 return TYPE_ALIGN (type);
10276 }
10277
10278 /* Similarly, but return a double_int instead of UHWI. */
10279
10280 static inline double_int
10281 double_int_type_size_in_bits (const_tree type)
10282 {
10283 if (TREE_CODE (type) == ERROR_MARK)
10284 return double_int::from_uhwi (BITS_PER_WORD);
10285 else if (TYPE_SIZE (type) == NULL_TREE)
10286 return double_int_zero;
10287 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10288 return tree_to_double_int (TYPE_SIZE (type));
10289 else
10290 return double_int::from_uhwi (TYPE_ALIGN (type));
10291 }
10292
10293 /* Given a pointer to a tree node for a subrange type, return a pointer
10294 to a DIE that describes the given type. */
10295
10296 static dw_die_ref
10297 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10298 {
10299 dw_die_ref subrange_die;
10300 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10301
10302 if (context_die == NULL)
10303 context_die = comp_unit_die ();
10304
10305 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10306
10307 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10308 {
10309 /* The size of the subrange type and its base type do not match,
10310 so we need to generate a size attribute for the subrange type. */
10311 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10312 }
10313
10314 if (low)
10315 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10316 if (high)
10317 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10318
10319 return subrange_die;
10320 }
10321
10322 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10323 entry that chains various modifiers in front of the given type. */
10324
10325 static dw_die_ref
10326 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10327 dw_die_ref context_die)
10328 {
10329 enum tree_code code = TREE_CODE (type);
10330 dw_die_ref mod_type_die;
10331 dw_die_ref sub_die = NULL;
10332 tree item_type = NULL;
10333 tree qualified_type;
10334 tree name, low, high;
10335 dw_die_ref mod_scope;
10336
10337 if (code == ERROR_MARK)
10338 return NULL;
10339
10340 /* See if we already have the appropriately qualified variant of
10341 this type. */
10342 qualified_type
10343 = get_qualified_type (type,
10344 ((is_const_type ? TYPE_QUAL_CONST : 0)
10345 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10346
10347 if (qualified_type == sizetype
10348 && TYPE_NAME (qualified_type)
10349 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10350 {
10351 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10352
10353 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10354 && TYPE_PRECISION (t)
10355 == TYPE_PRECISION (qualified_type)
10356 && TYPE_UNSIGNED (t)
10357 == TYPE_UNSIGNED (qualified_type));
10358 qualified_type = t;
10359 }
10360
10361 /* If we do, then we can just use its DIE, if it exists. */
10362 if (qualified_type)
10363 {
10364 mod_type_die = lookup_type_die (qualified_type);
10365 if (mod_type_die)
10366 return mod_type_die;
10367 }
10368
10369 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10370
10371 /* Handle C typedef types. */
10372 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10373 && !DECL_ARTIFICIAL (name))
10374 {
10375 tree dtype = TREE_TYPE (name);
10376
10377 if (qualified_type == dtype)
10378 {
10379 /* For a named type, use the typedef. */
10380 gen_type_die (qualified_type, context_die);
10381 return lookup_type_die (qualified_type);
10382 }
10383 else if (is_const_type < TYPE_READONLY (dtype)
10384 || is_volatile_type < TYPE_VOLATILE (dtype)
10385 || (is_const_type <= TYPE_READONLY (dtype)
10386 && is_volatile_type <= TYPE_VOLATILE (dtype)
10387 && DECL_ORIGINAL_TYPE (name) != type))
10388 /* cv-unqualified version of named type. Just use the unnamed
10389 type to which it refers. */
10390 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10391 is_const_type, is_volatile_type,
10392 context_die);
10393 /* Else cv-qualified version of named type; fall through. */
10394 }
10395
10396 mod_scope = scope_die_for (type, context_die);
10397
10398 if (is_const_type
10399 /* If both is_const_type and is_volatile_type, prefer the path
10400 which leads to a qualified type. */
10401 && (!is_volatile_type
10402 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10403 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10404 {
10405 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10406 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10407 }
10408 else if (is_volatile_type)
10409 {
10410 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10411 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10412 }
10413 else if (code == POINTER_TYPE)
10414 {
10415 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10416 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10417 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10418 item_type = TREE_TYPE (type);
10419 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10420 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10421 TYPE_ADDR_SPACE (item_type));
10422 }
10423 else if (code == REFERENCE_TYPE)
10424 {
10425 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10426 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10427 type);
10428 else
10429 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10430 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10431 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10432 item_type = TREE_TYPE (type);
10433 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10434 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10435 TYPE_ADDR_SPACE (item_type));
10436 }
10437 else if (code == INTEGER_TYPE
10438 && TREE_TYPE (type) != NULL_TREE
10439 && subrange_type_for_debug_p (type, &low, &high))
10440 {
10441 mod_type_die = subrange_type_die (type, low, high, context_die);
10442 item_type = TREE_TYPE (type);
10443 }
10444 else if (is_base_type (type))
10445 mod_type_die = base_type_die (type);
10446 else
10447 {
10448 gen_type_die (type, context_die);
10449
10450 /* We have to get the type_main_variant here (and pass that to the
10451 `lookup_type_die' routine) because the ..._TYPE node we have
10452 might simply be a *copy* of some original type node (where the
10453 copy was created to help us keep track of typedef names) and
10454 that copy might have a different TYPE_UID from the original
10455 ..._TYPE node. */
10456 if (TREE_CODE (type) != VECTOR_TYPE)
10457 return lookup_type_die (type_main_variant (type));
10458 else
10459 /* Vectors have the debugging information in the type,
10460 not the main variant. */
10461 return lookup_type_die (type);
10462 }
10463
10464 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10465 don't output a DW_TAG_typedef, since there isn't one in the
10466 user's program; just attach a DW_AT_name to the type.
10467 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10468 if the base type already has the same name. */
10469 if (name
10470 && ((TREE_CODE (name) != TYPE_DECL
10471 && (qualified_type == TYPE_MAIN_VARIANT (type)
10472 || (!is_const_type && !is_volatile_type)))
10473 || (TREE_CODE (name) == TYPE_DECL
10474 && TREE_TYPE (name) == qualified_type
10475 && DECL_NAME (name))))
10476 {
10477 if (TREE_CODE (name) == TYPE_DECL)
10478 /* Could just call add_name_and_src_coords_attributes here,
10479 but since this is a builtin type it doesn't have any
10480 useful source coordinates anyway. */
10481 name = DECL_NAME (name);
10482 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10483 }
10484 /* This probably indicates a bug. */
10485 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10486 {
10487 name = TYPE_NAME (type);
10488 if (name
10489 && TREE_CODE (name) == TYPE_DECL)
10490 name = DECL_NAME (name);
10491 add_name_attribute (mod_type_die,
10492 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10493 }
10494
10495 if (qualified_type)
10496 equate_type_number_to_die (qualified_type, mod_type_die);
10497
10498 if (item_type)
10499 /* We must do this after the equate_type_number_to_die call, in case
10500 this is a recursive type. This ensures that the modified_type_die
10501 recursion will terminate even if the type is recursive. Recursive
10502 types are possible in Ada. */
10503 sub_die = modified_type_die (item_type,
10504 TYPE_READONLY (item_type),
10505 TYPE_VOLATILE (item_type),
10506 context_die);
10507
10508 if (sub_die != NULL)
10509 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10510
10511 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10512 if (TYPE_ARTIFICIAL (type))
10513 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10514
10515 return mod_type_die;
10516 }
10517
10518 /* Generate DIEs for the generic parameters of T.
10519 T must be either a generic type or a generic function.
10520 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10521
10522 static void
10523 gen_generic_params_dies (tree t)
10524 {
10525 tree parms, args;
10526 int parms_num, i;
10527 dw_die_ref die = NULL;
10528 int non_default;
10529
10530 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10531 return;
10532
10533 if (TYPE_P (t))
10534 die = lookup_type_die (t);
10535 else if (DECL_P (t))
10536 die = lookup_decl_die (t);
10537
10538 gcc_assert (die);
10539
10540 parms = lang_hooks.get_innermost_generic_parms (t);
10541 if (!parms)
10542 /* T has no generic parameter. It means T is neither a generic type
10543 or function. End of story. */
10544 return;
10545
10546 parms_num = TREE_VEC_LENGTH (parms);
10547 args = lang_hooks.get_innermost_generic_args (t);
10548 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10549 non_default = int_cst_value (TREE_CHAIN (args));
10550 else
10551 non_default = TREE_VEC_LENGTH (args);
10552 for (i = 0; i < parms_num; i++)
10553 {
10554 tree parm, arg, arg_pack_elems;
10555 dw_die_ref parm_die;
10556
10557 parm = TREE_VEC_ELT (parms, i);
10558 arg = TREE_VEC_ELT (args, i);
10559 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10560 gcc_assert (parm && TREE_VALUE (parm) && arg);
10561
10562 if (parm && TREE_VALUE (parm) && arg)
10563 {
10564 /* If PARM represents a template parameter pack,
10565 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10566 by DW_TAG_template_*_parameter DIEs for the argument
10567 pack elements of ARG. Note that ARG would then be
10568 an argument pack. */
10569 if (arg_pack_elems)
10570 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10571 arg_pack_elems,
10572 die);
10573 else
10574 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10575 true /* emit name */, die);
10576 if (i >= non_default)
10577 add_AT_flag (parm_die, DW_AT_default_value, 1);
10578 }
10579 }
10580 }
10581
10582 /* Create and return a DIE for PARM which should be
10583 the representation of a generic type parameter.
10584 For instance, in the C++ front end, PARM would be a template parameter.
10585 ARG is the argument to PARM.
10586 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10587 name of the PARM.
10588 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10589 as a child node. */
10590
10591 static dw_die_ref
10592 generic_parameter_die (tree parm, tree arg,
10593 bool emit_name_p,
10594 dw_die_ref parent_die)
10595 {
10596 dw_die_ref tmpl_die = NULL;
10597 const char *name = NULL;
10598
10599 if (!parm || !DECL_NAME (parm) || !arg)
10600 return NULL;
10601
10602 /* We support non-type generic parameters and arguments,
10603 type generic parameters and arguments, as well as
10604 generic generic parameters (a.k.a. template template parameters in C++)
10605 and arguments. */
10606 if (TREE_CODE (parm) == PARM_DECL)
10607 /* PARM is a nontype generic parameter */
10608 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10609 else if (TREE_CODE (parm) == TYPE_DECL)
10610 /* PARM is a type generic parameter. */
10611 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10612 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10613 /* PARM is a generic generic parameter.
10614 Its DIE is a GNU extension. It shall have a
10615 DW_AT_name attribute to represent the name of the template template
10616 parameter, and a DW_AT_GNU_template_name attribute to represent the
10617 name of the template template argument. */
10618 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10619 parent_die, parm);
10620 else
10621 gcc_unreachable ();
10622
10623 if (tmpl_die)
10624 {
10625 tree tmpl_type;
10626
10627 /* If PARM is a generic parameter pack, it means we are
10628 emitting debug info for a template argument pack element.
10629 In other terms, ARG is a template argument pack element.
10630 In that case, we don't emit any DW_AT_name attribute for
10631 the die. */
10632 if (emit_name_p)
10633 {
10634 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10635 gcc_assert (name);
10636 add_AT_string (tmpl_die, DW_AT_name, name);
10637 }
10638
10639 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10640 {
10641 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10642 TMPL_DIE should have a child DW_AT_type attribute that is set
10643 to the type of the argument to PARM, which is ARG.
10644 If PARM is a type generic parameter, TMPL_DIE should have a
10645 child DW_AT_type that is set to ARG. */
10646 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10647 add_type_attribute (tmpl_die, tmpl_type, 0,
10648 TREE_THIS_VOLATILE (tmpl_type),
10649 parent_die);
10650 }
10651 else
10652 {
10653 /* So TMPL_DIE is a DIE representing a
10654 a generic generic template parameter, a.k.a template template
10655 parameter in C++ and arg is a template. */
10656
10657 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10658 to the name of the argument. */
10659 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10660 if (name)
10661 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10662 }
10663
10664 if (TREE_CODE (parm) == PARM_DECL)
10665 /* So PARM is a non-type generic parameter.
10666 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10667 attribute of TMPL_DIE which value represents the value
10668 of ARG.
10669 We must be careful here:
10670 The value of ARG might reference some function decls.
10671 We might currently be emitting debug info for a generic
10672 type and types are emitted before function decls, we don't
10673 know if the function decls referenced by ARG will actually be
10674 emitted after cgraph computations.
10675 So must defer the generation of the DW_AT_const_value to
10676 after cgraph is ready. */
10677 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10678 }
10679
10680 return tmpl_die;
10681 }
10682
10683 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10684 PARM_PACK must be a template parameter pack. The returned DIE
10685 will be child DIE of PARENT_DIE. */
10686
10687 static dw_die_ref
10688 template_parameter_pack_die (tree parm_pack,
10689 tree parm_pack_args,
10690 dw_die_ref parent_die)
10691 {
10692 dw_die_ref die;
10693 int j;
10694
10695 gcc_assert (parent_die && parm_pack);
10696
10697 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10698 add_name_and_src_coords_attributes (die, parm_pack);
10699 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10700 generic_parameter_die (parm_pack,
10701 TREE_VEC_ELT (parm_pack_args, j),
10702 false /* Don't emit DW_AT_name */,
10703 die);
10704 return die;
10705 }
10706
10707 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10708 an enumerated type. */
10709
10710 static inline int
10711 type_is_enum (const_tree type)
10712 {
10713 return TREE_CODE (type) == ENUMERAL_TYPE;
10714 }
10715
10716 /* Return the DBX register number described by a given RTL node. */
10717
10718 static unsigned int
10719 dbx_reg_number (const_rtx rtl)
10720 {
10721 unsigned regno = REGNO (rtl);
10722
10723 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10724
10725 #ifdef LEAF_REG_REMAP
10726 if (crtl->uses_only_leaf_regs)
10727 {
10728 int leaf_reg = LEAF_REG_REMAP (regno);
10729 if (leaf_reg != -1)
10730 regno = (unsigned) leaf_reg;
10731 }
10732 #endif
10733
10734 regno = DBX_REGISTER_NUMBER (regno);
10735 gcc_assert (regno != INVALID_REGNUM);
10736 return regno;
10737 }
10738
10739 /* Optionally add a DW_OP_piece term to a location description expression.
10740 DW_OP_piece is only added if the location description expression already
10741 doesn't end with DW_OP_piece. */
10742
10743 static void
10744 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10745 {
10746 dw_loc_descr_ref loc;
10747
10748 if (*list_head != NULL)
10749 {
10750 /* Find the end of the chain. */
10751 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10752 ;
10753
10754 if (loc->dw_loc_opc != DW_OP_piece)
10755 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10756 }
10757 }
10758
10759 /* Return a location descriptor that designates a machine register or
10760 zero if there is none. */
10761
10762 static dw_loc_descr_ref
10763 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10764 {
10765 rtx regs;
10766
10767 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10768 return 0;
10769
10770 /* We only use "frame base" when we're sure we're talking about the
10771 post-prologue local stack frame. We do this by *not* running
10772 register elimination until this point, and recognizing the special
10773 argument pointer and soft frame pointer rtx's.
10774 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10775 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10776 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10777 {
10778 dw_loc_descr_ref result = NULL;
10779
10780 if (dwarf_version >= 4 || !dwarf_strict)
10781 {
10782 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10783 initialized);
10784 if (result)
10785 add_loc_descr (&result,
10786 new_loc_descr (DW_OP_stack_value, 0, 0));
10787 }
10788 return result;
10789 }
10790
10791 regs = targetm.dwarf_register_span (rtl);
10792
10793 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10794 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10795 else
10796 {
10797 unsigned int dbx_regnum = dbx_reg_number (rtl);
10798 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10799 return 0;
10800 return one_reg_loc_descriptor (dbx_regnum, initialized);
10801 }
10802 }
10803
10804 /* Return a location descriptor that designates a machine register for
10805 a given hard register number. */
10806
10807 static dw_loc_descr_ref
10808 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10809 {
10810 dw_loc_descr_ref reg_loc_descr;
10811
10812 if (regno <= 31)
10813 reg_loc_descr
10814 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10815 else
10816 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10817
10818 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10819 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10820
10821 return reg_loc_descr;
10822 }
10823
10824 /* Given an RTL of a register, return a location descriptor that
10825 designates a value that spans more than one register. */
10826
10827 static dw_loc_descr_ref
10828 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10829 enum var_init_status initialized)
10830 {
10831 int size, i;
10832 dw_loc_descr_ref loc_result = NULL;
10833
10834 /* Simple, contiguous registers. */
10835 if (regs == NULL_RTX)
10836 {
10837 unsigned reg = REGNO (rtl);
10838 int nregs;
10839
10840 #ifdef LEAF_REG_REMAP
10841 if (crtl->uses_only_leaf_regs)
10842 {
10843 int leaf_reg = LEAF_REG_REMAP (reg);
10844 if (leaf_reg != -1)
10845 reg = (unsigned) leaf_reg;
10846 }
10847 #endif
10848
10849 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10850 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10851
10852 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10853
10854 loc_result = NULL;
10855 while (nregs--)
10856 {
10857 dw_loc_descr_ref t;
10858
10859 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10860 VAR_INIT_STATUS_INITIALIZED);
10861 add_loc_descr (&loc_result, t);
10862 add_loc_descr_op_piece (&loc_result, size);
10863 ++reg;
10864 }
10865 return loc_result;
10866 }
10867
10868 /* Now onto stupid register sets in non contiguous locations. */
10869
10870 gcc_assert (GET_CODE (regs) == PARALLEL);
10871
10872 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10873 loc_result = NULL;
10874
10875 for (i = 0; i < XVECLEN (regs, 0); ++i)
10876 {
10877 dw_loc_descr_ref t;
10878
10879 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10880 VAR_INIT_STATUS_INITIALIZED);
10881 add_loc_descr (&loc_result, t);
10882 add_loc_descr_op_piece (&loc_result, size);
10883 }
10884
10885 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10886 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10887 return loc_result;
10888 }
10889
10890 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10891
10892 /* Return a location descriptor that designates a constant i,
10893 as a compound operation from constant (i >> shift), constant shift
10894 and DW_OP_shl. */
10895
10896 static dw_loc_descr_ref
10897 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10898 {
10899 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10900 add_loc_descr (&ret, int_loc_descriptor (shift));
10901 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10902 return ret;
10903 }
10904
10905 /* Return a location descriptor that designates a constant. */
10906
10907 static dw_loc_descr_ref
10908 int_loc_descriptor (HOST_WIDE_INT i)
10909 {
10910 enum dwarf_location_atom op;
10911
10912 /* Pick the smallest representation of a constant, rather than just
10913 defaulting to the LEB encoding. */
10914 if (i >= 0)
10915 {
10916 int clz = clz_hwi (i);
10917 int ctz = ctz_hwi (i);
10918 if (i <= 31)
10919 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10920 else if (i <= 0xff)
10921 op = DW_OP_const1u;
10922 else if (i <= 0xffff)
10923 op = DW_OP_const2u;
10924 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10925 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10926 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10927 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10928 while DW_OP_const4u is 5 bytes. */
10929 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10930 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10931 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10932 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10933 while DW_OP_const4u is 5 bytes. */
10934 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10935 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10936 op = DW_OP_const4u;
10937 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10938 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10939 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10940 while DW_OP_constu of constant >= 0x100000000 takes at least
10941 6 bytes. */
10942 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10943 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10944 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10945 >= HOST_BITS_PER_WIDE_INT)
10946 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10947 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10948 while DW_OP_constu takes in this case at least 6 bytes. */
10949 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10950 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10951 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10952 && size_of_uleb128 (i) > 6)
10953 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10954 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10955 else
10956 op = DW_OP_constu;
10957 }
10958 else
10959 {
10960 if (i >= -0x80)
10961 op = DW_OP_const1s;
10962 else if (i >= -0x8000)
10963 op = DW_OP_const2s;
10964 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10965 {
10966 if (size_of_int_loc_descriptor (i) < 5)
10967 {
10968 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10969 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10970 return ret;
10971 }
10972 op = DW_OP_const4s;
10973 }
10974 else
10975 {
10976 if (size_of_int_loc_descriptor (i)
10977 < (unsigned long) 1 + size_of_sleb128 (i))
10978 {
10979 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10980 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10981 return ret;
10982 }
10983 op = DW_OP_consts;
10984 }
10985 }
10986
10987 return new_loc_descr (op, i, 0);
10988 }
10989
10990 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10991 without actually allocating it. */
10992
10993 static unsigned long
10994 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10995 {
10996 return size_of_int_loc_descriptor (i >> shift)
10997 + size_of_int_loc_descriptor (shift)
10998 + 1;
10999 }
11000
11001 /* Return size_of_locs (int_loc_descriptor (i)) without
11002 actually allocating it. */
11003
11004 static unsigned long
11005 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11006 {
11007 unsigned long s;
11008
11009 if (i >= 0)
11010 {
11011 int clz, ctz;
11012 if (i <= 31)
11013 return 1;
11014 else if (i <= 0xff)
11015 return 2;
11016 else if (i <= 0xffff)
11017 return 3;
11018 clz = clz_hwi (i);
11019 ctz = ctz_hwi (i);
11020 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11021 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11022 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11023 - clz - 5);
11024 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11025 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11026 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11027 - clz - 8);
11028 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11029 return 5;
11030 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11031 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11032 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11033 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11034 - clz - 8);
11035 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11036 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11037 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11038 - clz - 16);
11039 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11040 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11041 && s > 6)
11042 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11043 - clz - 32);
11044 else
11045 return 1 + s;
11046 }
11047 else
11048 {
11049 if (i >= -0x80)
11050 return 2;
11051 else if (i >= -0x8000)
11052 return 3;
11053 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11054 {
11055 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11056 {
11057 s = size_of_int_loc_descriptor (-i) + 1;
11058 if (s < 5)
11059 return s;
11060 }
11061 return 5;
11062 }
11063 else
11064 {
11065 unsigned long r = 1 + size_of_sleb128 (i);
11066 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11067 {
11068 s = size_of_int_loc_descriptor (-i) + 1;
11069 if (s < r)
11070 return s;
11071 }
11072 return r;
11073 }
11074 }
11075 }
11076
11077 /* Return loc description representing "address" of integer value.
11078 This can appear only as toplevel expression. */
11079
11080 static dw_loc_descr_ref
11081 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11082 {
11083 int litsize;
11084 dw_loc_descr_ref loc_result = NULL;
11085
11086 if (!(dwarf_version >= 4 || !dwarf_strict))
11087 return NULL;
11088
11089 litsize = size_of_int_loc_descriptor (i);
11090 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11091 is more compact. For DW_OP_stack_value we need:
11092 litsize + 1 (DW_OP_stack_value)
11093 and for DW_OP_implicit_value:
11094 1 (DW_OP_implicit_value) + 1 (length) + size. */
11095 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11096 {
11097 loc_result = int_loc_descriptor (i);
11098 add_loc_descr (&loc_result,
11099 new_loc_descr (DW_OP_stack_value, 0, 0));
11100 return loc_result;
11101 }
11102
11103 loc_result = new_loc_descr (DW_OP_implicit_value,
11104 size, 0);
11105 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11106 loc_result->dw_loc_oprnd2.v.val_int = i;
11107 return loc_result;
11108 }
11109
11110 /* Return a location descriptor that designates a base+offset location. */
11111
11112 static dw_loc_descr_ref
11113 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11114 enum var_init_status initialized)
11115 {
11116 unsigned int regno;
11117 dw_loc_descr_ref result;
11118 dw_fde_ref fde = cfun->fde;
11119
11120 /* We only use "frame base" when we're sure we're talking about the
11121 post-prologue local stack frame. We do this by *not* running
11122 register elimination until this point, and recognizing the special
11123 argument pointer and soft frame pointer rtx's. */
11124 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11125 {
11126 rtx elim = (ira_use_lra_p
11127 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11128 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11129
11130 if (elim != reg)
11131 {
11132 if (GET_CODE (elim) == PLUS)
11133 {
11134 offset += INTVAL (XEXP (elim, 1));
11135 elim = XEXP (elim, 0);
11136 }
11137 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11138 && (elim == hard_frame_pointer_rtx
11139 || elim == stack_pointer_rtx))
11140 || elim == (frame_pointer_needed
11141 ? hard_frame_pointer_rtx
11142 : stack_pointer_rtx));
11143
11144 /* If drap register is used to align stack, use frame
11145 pointer + offset to access stack variables. If stack
11146 is aligned without drap, use stack pointer + offset to
11147 access stack variables. */
11148 if (crtl->stack_realign_tried
11149 && reg == frame_pointer_rtx)
11150 {
11151 int base_reg
11152 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11153 ? HARD_FRAME_POINTER_REGNUM
11154 : REGNO (elim));
11155 return new_reg_loc_descr (base_reg, offset);
11156 }
11157
11158 gcc_assert (frame_pointer_fb_offset_valid);
11159 offset += frame_pointer_fb_offset;
11160 return new_loc_descr (DW_OP_fbreg, offset, 0);
11161 }
11162 }
11163
11164 regno = REGNO (reg);
11165 #ifdef LEAF_REG_REMAP
11166 if (crtl->uses_only_leaf_regs)
11167 {
11168 int leaf_reg = LEAF_REG_REMAP (regno);
11169 if (leaf_reg != -1)
11170 regno = (unsigned) leaf_reg;
11171 }
11172 #endif
11173 regno = DWARF_FRAME_REGNUM (regno);
11174
11175 if (!optimize && fde
11176 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11177 {
11178 /* Use cfa+offset to represent the location of arguments passed
11179 on the stack when drap is used to align stack.
11180 Only do this when not optimizing, for optimized code var-tracking
11181 is supposed to track where the arguments live and the register
11182 used as vdrap or drap in some spot might be used for something
11183 else in other part of the routine. */
11184 return new_loc_descr (DW_OP_fbreg, offset, 0);
11185 }
11186
11187 if (regno <= 31)
11188 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11189 offset, 0);
11190 else
11191 result = new_loc_descr (DW_OP_bregx, regno, offset);
11192
11193 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11194 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11195
11196 return result;
11197 }
11198
11199 /* Return true if this RTL expression describes a base+offset calculation. */
11200
11201 static inline int
11202 is_based_loc (const_rtx rtl)
11203 {
11204 return (GET_CODE (rtl) == PLUS
11205 && ((REG_P (XEXP (rtl, 0))
11206 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11207 && CONST_INT_P (XEXP (rtl, 1)))));
11208 }
11209
11210 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11211 failed. */
11212
11213 static dw_loc_descr_ref
11214 tls_mem_loc_descriptor (rtx mem)
11215 {
11216 tree base;
11217 dw_loc_descr_ref loc_result;
11218
11219 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11220 return NULL;
11221
11222 base = get_base_address (MEM_EXPR (mem));
11223 if (base == NULL
11224 || TREE_CODE (base) != VAR_DECL
11225 || !DECL_THREAD_LOCAL_P (base))
11226 return NULL;
11227
11228 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11229 if (loc_result == NULL)
11230 return NULL;
11231
11232 if (MEM_OFFSET (mem))
11233 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11234
11235 return loc_result;
11236 }
11237
11238 /* Output debug info about reason why we failed to expand expression as dwarf
11239 expression. */
11240
11241 static void
11242 expansion_failed (tree expr, rtx rtl, char const *reason)
11243 {
11244 if (dump_file && (dump_flags & TDF_DETAILS))
11245 {
11246 fprintf (dump_file, "Failed to expand as dwarf: ");
11247 if (expr)
11248 print_generic_expr (dump_file, expr, dump_flags);
11249 if (rtl)
11250 {
11251 fprintf (dump_file, "\n");
11252 print_rtl (dump_file, rtl);
11253 }
11254 fprintf (dump_file, "\nReason: %s\n", reason);
11255 }
11256 }
11257
11258 /* Helper function for const_ok_for_output, called either directly
11259 or via for_each_rtx. */
11260
11261 static int
11262 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11263 {
11264 rtx rtl = *rtlp;
11265
11266 if (GET_CODE (rtl) == UNSPEC)
11267 {
11268 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11269 we can't express it in the debug info. */
11270 #ifdef ENABLE_CHECKING
11271 /* Don't complain about TLS UNSPECs, those are just too hard to
11272 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11273 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11274 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11275 if (XVECLEN (rtl, 0) == 0
11276 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11277 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11278 inform (current_function_decl
11279 ? DECL_SOURCE_LOCATION (current_function_decl)
11280 : UNKNOWN_LOCATION,
11281 #if NUM_UNSPEC_VALUES > 0
11282 "non-delegitimized UNSPEC %s (%d) found in variable location",
11283 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11284 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11285 XINT (rtl, 1));
11286 #else
11287 "non-delegitimized UNSPEC %d found in variable location",
11288 XINT (rtl, 1));
11289 #endif
11290 #endif
11291 expansion_failed (NULL_TREE, rtl,
11292 "UNSPEC hasn't been delegitimized.\n");
11293 return 1;
11294 }
11295
11296 if (targetm.const_not_ok_for_debug_p (rtl))
11297 {
11298 expansion_failed (NULL_TREE, rtl,
11299 "Expression rejected for debug by the backend.\n");
11300 return 1;
11301 }
11302
11303 if (GET_CODE (rtl) != SYMBOL_REF)
11304 return 0;
11305
11306 if (CONSTANT_POOL_ADDRESS_P (rtl))
11307 {
11308 bool marked;
11309 get_pool_constant_mark (rtl, &marked);
11310 /* If all references to this pool constant were optimized away,
11311 it was not output and thus we can't represent it. */
11312 if (!marked)
11313 {
11314 expansion_failed (NULL_TREE, rtl,
11315 "Constant was removed from constant pool.\n");
11316 return 1;
11317 }
11318 }
11319
11320 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11321 return 1;
11322
11323 /* Avoid references to external symbols in debug info, on several targets
11324 the linker might even refuse to link when linking a shared library,
11325 and in many other cases the relocations for .debug_info/.debug_loc are
11326 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11327 to be defined within the same shared library or executable are fine. */
11328 if (SYMBOL_REF_EXTERNAL_P (rtl))
11329 {
11330 tree decl = SYMBOL_REF_DECL (rtl);
11331
11332 if (decl == NULL || !targetm.binds_local_p (decl))
11333 {
11334 expansion_failed (NULL_TREE, rtl,
11335 "Symbol not defined in current TU.\n");
11336 return 1;
11337 }
11338 }
11339
11340 return 0;
11341 }
11342
11343 /* Return true if constant RTL can be emitted in DW_OP_addr or
11344 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11345 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11346
11347 static bool
11348 const_ok_for_output (rtx rtl)
11349 {
11350 if (GET_CODE (rtl) == SYMBOL_REF)
11351 return const_ok_for_output_1 (&rtl, NULL) == 0;
11352
11353 if (GET_CODE (rtl) == CONST)
11354 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11355
11356 return true;
11357 }
11358
11359 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11360 if possible, NULL otherwise. */
11361
11362 static dw_die_ref
11363 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11364 {
11365 dw_die_ref type_die;
11366 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11367
11368 if (type == NULL)
11369 return NULL;
11370 switch (TREE_CODE (type))
11371 {
11372 case INTEGER_TYPE:
11373 case REAL_TYPE:
11374 break;
11375 default:
11376 return NULL;
11377 }
11378 type_die = lookup_type_die (type);
11379 if (!type_die)
11380 type_die = modified_type_die (type, false, false, comp_unit_die ());
11381 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11382 return NULL;
11383 return type_die;
11384 }
11385
11386 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11387 type matching MODE, or, if MODE is narrower than or as wide as
11388 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11389 possible. */
11390
11391 static dw_loc_descr_ref
11392 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11393 {
11394 enum machine_mode outer_mode = mode;
11395 dw_die_ref type_die;
11396 dw_loc_descr_ref cvt;
11397
11398 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11399 {
11400 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11401 return op;
11402 }
11403 type_die = base_type_for_mode (outer_mode, 1);
11404 if (type_die == NULL)
11405 return NULL;
11406 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11407 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11408 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11409 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11410 add_loc_descr (&op, cvt);
11411 return op;
11412 }
11413
11414 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11415
11416 static dw_loc_descr_ref
11417 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11418 dw_loc_descr_ref op1)
11419 {
11420 dw_loc_descr_ref ret = op0;
11421 add_loc_descr (&ret, op1);
11422 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11423 if (STORE_FLAG_VALUE != 1)
11424 {
11425 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11426 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11427 }
11428 return ret;
11429 }
11430
11431 /* Return location descriptor for signed comparison OP RTL. */
11432
11433 static dw_loc_descr_ref
11434 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11435 enum machine_mode mem_mode)
11436 {
11437 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11438 dw_loc_descr_ref op0, op1;
11439 int shift;
11440
11441 if (op_mode == VOIDmode)
11442 op_mode = GET_MODE (XEXP (rtl, 1));
11443 if (op_mode == VOIDmode)
11444 return NULL;
11445
11446 if (dwarf_strict
11447 && (GET_MODE_CLASS (op_mode) != MODE_INT
11448 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11449 return NULL;
11450
11451 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11452 VAR_INIT_STATUS_INITIALIZED);
11453 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11454 VAR_INIT_STATUS_INITIALIZED);
11455
11456 if (op0 == NULL || op1 == NULL)
11457 return NULL;
11458
11459 if (GET_MODE_CLASS (op_mode) != MODE_INT
11460 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11461 return compare_loc_descriptor (op, op0, op1);
11462
11463 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11464 {
11465 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11466 dw_loc_descr_ref cvt;
11467
11468 if (type_die == NULL)
11469 return NULL;
11470 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11471 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11472 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11473 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11474 add_loc_descr (&op0, cvt);
11475 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11476 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11477 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11478 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11479 add_loc_descr (&op1, cvt);
11480 return compare_loc_descriptor (op, op0, op1);
11481 }
11482
11483 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11484 /* For eq/ne, if the operands are known to be zero-extended,
11485 there is no need to do the fancy shifting up. */
11486 if (op == DW_OP_eq || op == DW_OP_ne)
11487 {
11488 dw_loc_descr_ref last0, last1;
11489 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11490 ;
11491 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11492 ;
11493 /* deref_size zero extends, and for constants we can check
11494 whether they are zero extended or not. */
11495 if (((last0->dw_loc_opc == DW_OP_deref_size
11496 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11497 || (CONST_INT_P (XEXP (rtl, 0))
11498 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11499 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11500 && ((last1->dw_loc_opc == DW_OP_deref_size
11501 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11502 || (CONST_INT_P (XEXP (rtl, 1))
11503 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11504 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11505 return compare_loc_descriptor (op, op0, op1);
11506
11507 /* EQ/NE comparison against constant in narrower type than
11508 DWARF2_ADDR_SIZE can be performed either as
11509 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11510 DW_OP_{eq,ne}
11511 or
11512 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11513 DW_OP_{eq,ne}. Pick whatever is shorter. */
11514 if (CONST_INT_P (XEXP (rtl, 1))
11515 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11516 && (size_of_int_loc_descriptor (shift) + 1
11517 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11518 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11519 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11520 & GET_MODE_MASK (op_mode))))
11521 {
11522 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11523 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11524 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11525 & GET_MODE_MASK (op_mode));
11526 return compare_loc_descriptor (op, op0, op1);
11527 }
11528 }
11529 add_loc_descr (&op0, int_loc_descriptor (shift));
11530 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11531 if (CONST_INT_P (XEXP (rtl, 1)))
11532 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11533 else
11534 {
11535 add_loc_descr (&op1, int_loc_descriptor (shift));
11536 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11537 }
11538 return compare_loc_descriptor (op, op0, op1);
11539 }
11540
11541 /* Return location descriptor for unsigned comparison OP RTL. */
11542
11543 static dw_loc_descr_ref
11544 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11545 enum machine_mode mem_mode)
11546 {
11547 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11548 dw_loc_descr_ref op0, op1;
11549
11550 if (op_mode == VOIDmode)
11551 op_mode = GET_MODE (XEXP (rtl, 1));
11552 if (op_mode == VOIDmode)
11553 return NULL;
11554 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11555 return NULL;
11556
11557 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11558 return NULL;
11559
11560 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11561 VAR_INIT_STATUS_INITIALIZED);
11562 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11563 VAR_INIT_STATUS_INITIALIZED);
11564
11565 if (op0 == NULL || op1 == NULL)
11566 return NULL;
11567
11568 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11569 {
11570 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11571 dw_loc_descr_ref last0, last1;
11572 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11573 ;
11574 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11575 ;
11576 if (CONST_INT_P (XEXP (rtl, 0)))
11577 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11578 /* deref_size zero extends, so no need to mask it again. */
11579 else if (last0->dw_loc_opc != DW_OP_deref_size
11580 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11581 {
11582 add_loc_descr (&op0, int_loc_descriptor (mask));
11583 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11584 }
11585 if (CONST_INT_P (XEXP (rtl, 1)))
11586 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11587 /* deref_size zero extends, so no need to mask it again. */
11588 else if (last1->dw_loc_opc != DW_OP_deref_size
11589 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11590 {
11591 add_loc_descr (&op1, int_loc_descriptor (mask));
11592 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11593 }
11594 }
11595 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11596 {
11597 HOST_WIDE_INT bias = 1;
11598 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11599 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11600 if (CONST_INT_P (XEXP (rtl, 1)))
11601 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11602 + INTVAL (XEXP (rtl, 1)));
11603 else
11604 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11605 bias, 0));
11606 }
11607 return compare_loc_descriptor (op, op0, op1);
11608 }
11609
11610 /* Return location descriptor for {U,S}{MIN,MAX}. */
11611
11612 static dw_loc_descr_ref
11613 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11614 enum machine_mode mem_mode)
11615 {
11616 enum dwarf_location_atom op;
11617 dw_loc_descr_ref op0, op1, ret;
11618 dw_loc_descr_ref bra_node, drop_node;
11619
11620 if (dwarf_strict
11621 && (GET_MODE_CLASS (mode) != MODE_INT
11622 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11623 return NULL;
11624
11625 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11626 VAR_INIT_STATUS_INITIALIZED);
11627 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11628 VAR_INIT_STATUS_INITIALIZED);
11629
11630 if (op0 == NULL || op1 == NULL)
11631 return NULL;
11632
11633 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11634 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11635 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11636 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11637 {
11638 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11639 {
11640 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11641 add_loc_descr (&op0, int_loc_descriptor (mask));
11642 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11643 add_loc_descr (&op1, int_loc_descriptor (mask));
11644 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11645 }
11646 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11647 {
11648 HOST_WIDE_INT bias = 1;
11649 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11650 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11651 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11652 }
11653 }
11654 else if (GET_MODE_CLASS (mode) == MODE_INT
11655 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11656 {
11657 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11658 add_loc_descr (&op0, int_loc_descriptor (shift));
11659 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11660 add_loc_descr (&op1, int_loc_descriptor (shift));
11661 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11662 }
11663 else if (GET_MODE_CLASS (mode) == MODE_INT
11664 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11665 {
11666 dw_die_ref type_die = base_type_for_mode (mode, 0);
11667 dw_loc_descr_ref cvt;
11668 if (type_die == NULL)
11669 return NULL;
11670 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11671 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11672 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11673 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11674 add_loc_descr (&op0, cvt);
11675 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11676 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11677 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11678 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11679 add_loc_descr (&op1, cvt);
11680 }
11681
11682 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11683 op = DW_OP_lt;
11684 else
11685 op = DW_OP_gt;
11686 ret = op0;
11687 add_loc_descr (&ret, op1);
11688 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11689 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11690 add_loc_descr (&ret, bra_node);
11691 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11692 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11693 add_loc_descr (&ret, drop_node);
11694 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11695 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11696 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11697 && GET_MODE_CLASS (mode) == MODE_INT
11698 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11699 ret = convert_descriptor_to_mode (mode, ret);
11700 return ret;
11701 }
11702
11703 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11704 but after converting arguments to type_die, afterwards
11705 convert back to unsigned. */
11706
11707 static dw_loc_descr_ref
11708 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11709 enum machine_mode mode, enum machine_mode mem_mode)
11710 {
11711 dw_loc_descr_ref cvt, op0, op1;
11712
11713 if (type_die == NULL)
11714 return NULL;
11715 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11716 VAR_INIT_STATUS_INITIALIZED);
11717 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11718 VAR_INIT_STATUS_INITIALIZED);
11719 if (op0 == NULL || op1 == NULL)
11720 return NULL;
11721 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11722 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11723 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11724 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11725 add_loc_descr (&op0, cvt);
11726 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11727 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11728 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11729 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11730 add_loc_descr (&op1, cvt);
11731 add_loc_descr (&op0, op1);
11732 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11733 return convert_descriptor_to_mode (mode, op0);
11734 }
11735
11736 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11737 const0 is DW_OP_lit0 or corresponding typed constant,
11738 const1 is DW_OP_lit1 or corresponding typed constant
11739 and constMSB is constant with just the MSB bit set
11740 for the mode):
11741 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11742 L1: const0 DW_OP_swap
11743 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11744 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11745 L3: DW_OP_drop
11746 L4: DW_OP_nop
11747
11748 CTZ is similar:
11749 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11750 L1: const0 DW_OP_swap
11751 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11752 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11753 L3: DW_OP_drop
11754 L4: DW_OP_nop
11755
11756 FFS is similar:
11757 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11758 L1: const1 DW_OP_swap
11759 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11760 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11761 L3: DW_OP_drop
11762 L4: DW_OP_nop */
11763
11764 static dw_loc_descr_ref
11765 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11766 enum machine_mode mem_mode)
11767 {
11768 dw_loc_descr_ref op0, ret, tmp;
11769 HOST_WIDE_INT valv;
11770 dw_loc_descr_ref l1jump, l1label;
11771 dw_loc_descr_ref l2jump, l2label;
11772 dw_loc_descr_ref l3jump, l3label;
11773 dw_loc_descr_ref l4jump, l4label;
11774 rtx msb;
11775
11776 if (GET_MODE_CLASS (mode) != MODE_INT
11777 || GET_MODE (XEXP (rtl, 0)) != mode
11778 || (GET_CODE (rtl) == CLZ
11779 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11780 return NULL;
11781
11782 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11783 VAR_INIT_STATUS_INITIALIZED);
11784 if (op0 == NULL)
11785 return NULL;
11786 ret = op0;
11787 if (GET_CODE (rtl) == CLZ)
11788 {
11789 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11790 valv = GET_MODE_BITSIZE (mode);
11791 }
11792 else if (GET_CODE (rtl) == FFS)
11793 valv = 0;
11794 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11795 valv = GET_MODE_BITSIZE (mode);
11796 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11797 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11798 add_loc_descr (&ret, l1jump);
11799 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11800 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11801 VAR_INIT_STATUS_INITIALIZED);
11802 if (tmp == NULL)
11803 return NULL;
11804 add_loc_descr (&ret, tmp);
11805 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11806 add_loc_descr (&ret, l4jump);
11807 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11808 ? const1_rtx : const0_rtx,
11809 mode, mem_mode,
11810 VAR_INIT_STATUS_INITIALIZED);
11811 if (l1label == NULL)
11812 return NULL;
11813 add_loc_descr (&ret, l1label);
11814 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11815 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11816 add_loc_descr (&ret, l2label);
11817 if (GET_CODE (rtl) != CLZ)
11818 msb = const1_rtx;
11819 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11820 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11821 << (GET_MODE_BITSIZE (mode) - 1));
11822 else
11823 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11824 << (GET_MODE_BITSIZE (mode)
11825 - HOST_BITS_PER_WIDE_INT - 1), mode);
11826 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11827 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11828 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11829 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11830 else
11831 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11832 VAR_INIT_STATUS_INITIALIZED);
11833 if (tmp == NULL)
11834 return NULL;
11835 add_loc_descr (&ret, tmp);
11836 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11837 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11838 add_loc_descr (&ret, l3jump);
11839 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11840 VAR_INIT_STATUS_INITIALIZED);
11841 if (tmp == NULL)
11842 return NULL;
11843 add_loc_descr (&ret, tmp);
11844 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11845 ? DW_OP_shl : DW_OP_shr, 0, 0));
11846 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11847 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11848 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11849 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11850 add_loc_descr (&ret, l2jump);
11851 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11852 add_loc_descr (&ret, l3label);
11853 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11854 add_loc_descr (&ret, l4label);
11855 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11856 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11857 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11858 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11859 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11860 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11861 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11862 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11863 return ret;
11864 }
11865
11866 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11867 const1 is DW_OP_lit1 or corresponding typed constant):
11868 const0 DW_OP_swap
11869 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11870 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11871 L2: DW_OP_drop
11872
11873 PARITY is similar:
11874 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11875 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11876 L2: DW_OP_drop */
11877
11878 static dw_loc_descr_ref
11879 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11880 enum machine_mode mem_mode)
11881 {
11882 dw_loc_descr_ref op0, ret, tmp;
11883 dw_loc_descr_ref l1jump, l1label;
11884 dw_loc_descr_ref l2jump, l2label;
11885
11886 if (GET_MODE_CLASS (mode) != MODE_INT
11887 || GET_MODE (XEXP (rtl, 0)) != mode)
11888 return NULL;
11889
11890 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11891 VAR_INIT_STATUS_INITIALIZED);
11892 if (op0 == NULL)
11893 return NULL;
11894 ret = op0;
11895 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11896 VAR_INIT_STATUS_INITIALIZED);
11897 if (tmp == NULL)
11898 return NULL;
11899 add_loc_descr (&ret, tmp);
11900 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11901 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11902 add_loc_descr (&ret, l1label);
11903 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11904 add_loc_descr (&ret, l2jump);
11905 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11906 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11907 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11908 VAR_INIT_STATUS_INITIALIZED);
11909 if (tmp == NULL)
11910 return NULL;
11911 add_loc_descr (&ret, tmp);
11912 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11913 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11914 ? DW_OP_plus : DW_OP_xor, 0, 0));
11915 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11916 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11917 VAR_INIT_STATUS_INITIALIZED);
11918 add_loc_descr (&ret, tmp);
11919 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11920 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11921 add_loc_descr (&ret, l1jump);
11922 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11923 add_loc_descr (&ret, l2label);
11924 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11925 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11926 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11927 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11928 return ret;
11929 }
11930
11931 /* BSWAP (constS is initial shift count, either 56 or 24):
11932 constS const0
11933 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11934 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11935 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11936 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11937 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11938
11939 static dw_loc_descr_ref
11940 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11941 enum machine_mode mem_mode)
11942 {
11943 dw_loc_descr_ref op0, ret, tmp;
11944 dw_loc_descr_ref l1jump, l1label;
11945 dw_loc_descr_ref l2jump, l2label;
11946
11947 if (GET_MODE_CLASS (mode) != MODE_INT
11948 || BITS_PER_UNIT != 8
11949 || (GET_MODE_BITSIZE (mode) != 32
11950 && GET_MODE_BITSIZE (mode) != 64))
11951 return NULL;
11952
11953 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11954 VAR_INIT_STATUS_INITIALIZED);
11955 if (op0 == NULL)
11956 return NULL;
11957
11958 ret = op0;
11959 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11960 mode, mem_mode,
11961 VAR_INIT_STATUS_INITIALIZED);
11962 if (tmp == NULL)
11963 return NULL;
11964 add_loc_descr (&ret, tmp);
11965 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11966 VAR_INIT_STATUS_INITIALIZED);
11967 if (tmp == NULL)
11968 return NULL;
11969 add_loc_descr (&ret, tmp);
11970 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11971 add_loc_descr (&ret, l1label);
11972 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11973 mode, mem_mode,
11974 VAR_INIT_STATUS_INITIALIZED);
11975 add_loc_descr (&ret, tmp);
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11977 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11979 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11980 VAR_INIT_STATUS_INITIALIZED);
11981 if (tmp == NULL)
11982 return NULL;
11983 add_loc_descr (&ret, tmp);
11984 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11986 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11987 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11988 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11989 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11990 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11991 VAR_INIT_STATUS_INITIALIZED);
11992 add_loc_descr (&ret, tmp);
11993 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11994 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11995 add_loc_descr (&ret, l2jump);
11996 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11997 VAR_INIT_STATUS_INITIALIZED);
11998 add_loc_descr (&ret, tmp);
11999 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12000 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12001 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12002 add_loc_descr (&ret, l1jump);
12003 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12004 add_loc_descr (&ret, l2label);
12005 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12006 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12007 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12008 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12009 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12010 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12011 return ret;
12012 }
12013
12014 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12015 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12016 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12017 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12018
12019 ROTATERT is similar:
12020 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12021 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12022 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12023
12024 static dw_loc_descr_ref
12025 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12026 enum machine_mode mem_mode)
12027 {
12028 rtx rtlop1 = XEXP (rtl, 1);
12029 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12030 int i;
12031
12032 if (GET_MODE_CLASS (mode) != MODE_INT)
12033 return NULL;
12034
12035 if (GET_MODE (rtlop1) != VOIDmode
12036 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12037 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12038 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12039 VAR_INIT_STATUS_INITIALIZED);
12040 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12041 VAR_INIT_STATUS_INITIALIZED);
12042 if (op0 == NULL || op1 == NULL)
12043 return NULL;
12044 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12045 for (i = 0; i < 2; i++)
12046 {
12047 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12048 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12049 mode, mem_mode,
12050 VAR_INIT_STATUS_INITIALIZED);
12051 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12052 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12053 ? DW_OP_const4u
12054 : HOST_BITS_PER_WIDE_INT == 64
12055 ? DW_OP_const8u : DW_OP_constu,
12056 GET_MODE_MASK (mode), 0);
12057 else
12058 mask[i] = NULL;
12059 if (mask[i] == NULL)
12060 return NULL;
12061 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12062 }
12063 ret = op0;
12064 add_loc_descr (&ret, op1);
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12066 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12067 if (GET_CODE (rtl) == ROTATERT)
12068 {
12069 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12070 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12071 GET_MODE_BITSIZE (mode), 0));
12072 }
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12074 if (mask[0] != NULL)
12075 add_loc_descr (&ret, mask[0]);
12076 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12077 if (mask[1] != NULL)
12078 {
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12080 add_loc_descr (&ret, mask[1]);
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12082 }
12083 if (GET_CODE (rtl) == ROTATE)
12084 {
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12086 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12087 GET_MODE_BITSIZE (mode), 0));
12088 }
12089 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12090 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12091 return ret;
12092 }
12093
12094 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12095 for DEBUG_PARAMETER_REF RTL. */
12096
12097 static dw_loc_descr_ref
12098 parameter_ref_descriptor (rtx rtl)
12099 {
12100 dw_loc_descr_ref ret;
12101 dw_die_ref ref;
12102
12103 if (dwarf_strict)
12104 return NULL;
12105 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12106 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12107 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12108 if (ref)
12109 {
12110 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12111 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12112 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12113 }
12114 else
12115 {
12116 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12117 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12118 }
12119 return ret;
12120 }
12121
12122 /* The following routine converts the RTL for a variable or parameter
12123 (resident in memory) into an equivalent Dwarf representation of a
12124 mechanism for getting the address of that same variable onto the top of a
12125 hypothetical "address evaluation" stack.
12126
12127 When creating memory location descriptors, we are effectively transforming
12128 the RTL for a memory-resident object into its Dwarf postfix expression
12129 equivalent. This routine recursively descends an RTL tree, turning
12130 it into Dwarf postfix code as it goes.
12131
12132 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12133
12134 MEM_MODE is the mode of the memory reference, needed to handle some
12135 autoincrement addressing modes.
12136
12137 Return 0 if we can't represent the location. */
12138
12139 dw_loc_descr_ref
12140 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12141 enum machine_mode mem_mode,
12142 enum var_init_status initialized)
12143 {
12144 dw_loc_descr_ref mem_loc_result = NULL;
12145 enum dwarf_location_atom op;
12146 dw_loc_descr_ref op0, op1;
12147 rtx inner = NULL_RTX;
12148
12149 if (mode == VOIDmode)
12150 mode = GET_MODE (rtl);
12151
12152 /* Note that for a dynamically sized array, the location we will generate a
12153 description of here will be the lowest numbered location which is
12154 actually within the array. That's *not* necessarily the same as the
12155 zeroth element of the array. */
12156
12157 rtl = targetm.delegitimize_address (rtl);
12158
12159 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12160 return NULL;
12161
12162 switch (GET_CODE (rtl))
12163 {
12164 case POST_INC:
12165 case POST_DEC:
12166 case POST_MODIFY:
12167 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12168
12169 case SUBREG:
12170 /* The case of a subreg may arise when we have a local (register)
12171 variable or a formal (register) parameter which doesn't quite fill
12172 up an entire register. For now, just assume that it is
12173 legitimate to make the Dwarf info refer to the whole register which
12174 contains the given subreg. */
12175 if (!subreg_lowpart_p (rtl))
12176 break;
12177 inner = SUBREG_REG (rtl);
12178 case TRUNCATE:
12179 if (inner == NULL_RTX)
12180 inner = XEXP (rtl, 0);
12181 if (GET_MODE_CLASS (mode) == MODE_INT
12182 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12183 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12184 #ifdef POINTERS_EXTEND_UNSIGNED
12185 || (mode == Pmode && mem_mode != VOIDmode)
12186 #endif
12187 )
12188 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12189 {
12190 mem_loc_result = mem_loc_descriptor (inner,
12191 GET_MODE (inner),
12192 mem_mode, initialized);
12193 break;
12194 }
12195 if (dwarf_strict)
12196 break;
12197 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12198 break;
12199 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12200 && (GET_MODE_CLASS (mode) != MODE_INT
12201 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12202 break;
12203 else
12204 {
12205 dw_die_ref type_die;
12206 dw_loc_descr_ref cvt;
12207
12208 mem_loc_result = mem_loc_descriptor (inner,
12209 GET_MODE (inner),
12210 mem_mode, initialized);
12211 if (mem_loc_result == NULL)
12212 break;
12213 type_die = base_type_for_mode (mode,
12214 GET_MODE_CLASS (mode) == MODE_INT);
12215 if (type_die == NULL)
12216 {
12217 mem_loc_result = NULL;
12218 break;
12219 }
12220 if (GET_MODE_SIZE (mode)
12221 != GET_MODE_SIZE (GET_MODE (inner)))
12222 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12223 else
12224 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12225 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12226 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12227 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12228 add_loc_descr (&mem_loc_result, cvt);
12229 }
12230 break;
12231
12232 case REG:
12233 if (GET_MODE_CLASS (mode) != MODE_INT
12234 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12235 && rtl != arg_pointer_rtx
12236 && rtl != frame_pointer_rtx
12237 #ifdef POINTERS_EXTEND_UNSIGNED
12238 && (mode != Pmode || mem_mode == VOIDmode)
12239 #endif
12240 ))
12241 {
12242 dw_die_ref type_die;
12243 unsigned int dbx_regnum;
12244
12245 if (dwarf_strict)
12246 break;
12247 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12248 break;
12249 type_die = base_type_for_mode (mode,
12250 GET_MODE_CLASS (mode) == MODE_INT);
12251 if (type_die == NULL)
12252 break;
12253
12254 dbx_regnum = dbx_reg_number (rtl);
12255 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12256 break;
12257 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12258 dbx_regnum, 0);
12259 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12260 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12261 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12262 break;
12263 }
12264 /* Whenever a register number forms a part of the description of the
12265 method for calculating the (dynamic) address of a memory resident
12266 object, DWARF rules require the register number be referred to as
12267 a "base register". This distinction is not based in any way upon
12268 what category of register the hardware believes the given register
12269 belongs to. This is strictly DWARF terminology we're dealing with
12270 here. Note that in cases where the location of a memory-resident
12271 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12272 OP_CONST (0)) the actual DWARF location descriptor that we generate
12273 may just be OP_BASEREG (basereg). This may look deceptively like
12274 the object in question was allocated to a register (rather than in
12275 memory) so DWARF consumers need to be aware of the subtle
12276 distinction between OP_REG and OP_BASEREG. */
12277 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12278 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12279 else if (stack_realign_drap
12280 && crtl->drap_reg
12281 && crtl->args.internal_arg_pointer == rtl
12282 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12283 {
12284 /* If RTL is internal_arg_pointer, which has been optimized
12285 out, use DRAP instead. */
12286 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12287 VAR_INIT_STATUS_INITIALIZED);
12288 }
12289 break;
12290
12291 case SIGN_EXTEND:
12292 case ZERO_EXTEND:
12293 if (GET_MODE_CLASS (mode) != MODE_INT)
12294 break;
12295 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12296 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12297 if (op0 == 0)
12298 break;
12299 else if (GET_CODE (rtl) == ZERO_EXTEND
12300 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12301 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12302 < HOST_BITS_PER_WIDE_INT
12303 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12304 to expand zero extend as two shifts instead of
12305 masking. */
12306 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12307 {
12308 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12309 mem_loc_result = op0;
12310 add_loc_descr (&mem_loc_result,
12311 int_loc_descriptor (GET_MODE_MASK (imode)));
12312 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12313 }
12314 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12315 {
12316 int shift = DWARF2_ADDR_SIZE
12317 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12318 shift *= BITS_PER_UNIT;
12319 if (GET_CODE (rtl) == SIGN_EXTEND)
12320 op = DW_OP_shra;
12321 else
12322 op = DW_OP_shr;
12323 mem_loc_result = op0;
12324 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12325 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12326 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12327 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12328 }
12329 else if (!dwarf_strict)
12330 {
12331 dw_die_ref type_die1, type_die2;
12332 dw_loc_descr_ref cvt;
12333
12334 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12335 GET_CODE (rtl) == ZERO_EXTEND);
12336 if (type_die1 == NULL)
12337 break;
12338 type_die2 = base_type_for_mode (mode, 1);
12339 if (type_die2 == NULL)
12340 break;
12341 mem_loc_result = op0;
12342 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12343 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12344 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12345 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12346 add_loc_descr (&mem_loc_result, cvt);
12347 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12348 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12349 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12350 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12351 add_loc_descr (&mem_loc_result, cvt);
12352 }
12353 break;
12354
12355 case MEM:
12356 {
12357 rtx new_rtl = avoid_constant_pool_reference (rtl);
12358 if (new_rtl != rtl)
12359 {
12360 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12361 initialized);
12362 if (mem_loc_result != NULL)
12363 return mem_loc_result;
12364 }
12365 }
12366 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12367 get_address_mode (rtl), mode,
12368 VAR_INIT_STATUS_INITIALIZED);
12369 if (mem_loc_result == NULL)
12370 mem_loc_result = tls_mem_loc_descriptor (rtl);
12371 if (mem_loc_result != NULL)
12372 {
12373 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12374 || GET_MODE_CLASS (mode) != MODE_INT)
12375 {
12376 dw_die_ref type_die;
12377 dw_loc_descr_ref deref;
12378
12379 if (dwarf_strict)
12380 return NULL;
12381 type_die
12382 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12383 if (type_die == NULL)
12384 return NULL;
12385 deref = new_loc_descr (DW_OP_GNU_deref_type,
12386 GET_MODE_SIZE (mode), 0);
12387 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12388 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12389 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12390 add_loc_descr (&mem_loc_result, deref);
12391 }
12392 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12393 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12394 else
12395 add_loc_descr (&mem_loc_result,
12396 new_loc_descr (DW_OP_deref_size,
12397 GET_MODE_SIZE (mode), 0));
12398 }
12399 break;
12400
12401 case LO_SUM:
12402 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12403
12404 case LABEL_REF:
12405 /* Some ports can transform a symbol ref into a label ref, because
12406 the symbol ref is too far away and has to be dumped into a constant
12407 pool. */
12408 case CONST:
12409 case SYMBOL_REF:
12410 if (GET_MODE_CLASS (mode) != MODE_INT
12411 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12412 #ifdef POINTERS_EXTEND_UNSIGNED
12413 && (mode != Pmode || mem_mode == VOIDmode)
12414 #endif
12415 ))
12416 break;
12417 if (GET_CODE (rtl) == SYMBOL_REF
12418 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12419 {
12420 dw_loc_descr_ref temp;
12421
12422 /* If this is not defined, we have no way to emit the data. */
12423 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12424 break;
12425
12426 temp = new_addr_loc_descr (rtl, dtprel_true);
12427
12428 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12429 add_loc_descr (&mem_loc_result, temp);
12430
12431 break;
12432 }
12433
12434 if (!const_ok_for_output (rtl))
12435 break;
12436
12437 symref:
12438 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12439 vec_safe_push (used_rtx_array, rtl);
12440 break;
12441
12442 case CONCAT:
12443 case CONCATN:
12444 case VAR_LOCATION:
12445 case DEBUG_IMPLICIT_PTR:
12446 expansion_failed (NULL_TREE, rtl,
12447 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12448 return 0;
12449
12450 case ENTRY_VALUE:
12451 if (dwarf_strict)
12452 return NULL;
12453 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12454 {
12455 if (GET_MODE_CLASS (mode) != MODE_INT
12456 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12457 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12458 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12459 else
12460 {
12461 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12462 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12463 return NULL;
12464 op0 = one_reg_loc_descriptor (dbx_regnum,
12465 VAR_INIT_STATUS_INITIALIZED);
12466 }
12467 }
12468 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12469 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12470 {
12471 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12472 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12473 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12474 return NULL;
12475 }
12476 else
12477 gcc_unreachable ();
12478 if (op0 == NULL)
12479 return NULL;
12480 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12481 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12482 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12483 break;
12484
12485 case DEBUG_PARAMETER_REF:
12486 mem_loc_result = parameter_ref_descriptor (rtl);
12487 break;
12488
12489 case PRE_MODIFY:
12490 /* Extract the PLUS expression nested inside and fall into
12491 PLUS code below. */
12492 rtl = XEXP (rtl, 1);
12493 goto plus;
12494
12495 case PRE_INC:
12496 case PRE_DEC:
12497 /* Turn these into a PLUS expression and fall into the PLUS code
12498 below. */
12499 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12500 gen_int_mode (GET_CODE (rtl) == PRE_INC
12501 ? GET_MODE_UNIT_SIZE (mem_mode)
12502 : -GET_MODE_UNIT_SIZE (mem_mode),
12503 mode));
12504
12505 /* ... fall through ... */
12506
12507 case PLUS:
12508 plus:
12509 if (is_based_loc (rtl)
12510 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12511 || XEXP (rtl, 0) == arg_pointer_rtx
12512 || XEXP (rtl, 0) == frame_pointer_rtx)
12513 && GET_MODE_CLASS (mode) == MODE_INT)
12514 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12515 INTVAL (XEXP (rtl, 1)),
12516 VAR_INIT_STATUS_INITIALIZED);
12517 else
12518 {
12519 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12520 VAR_INIT_STATUS_INITIALIZED);
12521 if (mem_loc_result == 0)
12522 break;
12523
12524 if (CONST_INT_P (XEXP (rtl, 1))
12525 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12526 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12527 else
12528 {
12529 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12530 VAR_INIT_STATUS_INITIALIZED);
12531 if (op1 == 0)
12532 break;
12533 add_loc_descr (&mem_loc_result, op1);
12534 add_loc_descr (&mem_loc_result,
12535 new_loc_descr (DW_OP_plus, 0, 0));
12536 }
12537 }
12538 break;
12539
12540 /* If a pseudo-reg is optimized away, it is possible for it to
12541 be replaced with a MEM containing a multiply or shift. */
12542 case MINUS:
12543 op = DW_OP_minus;
12544 goto do_binop;
12545
12546 case MULT:
12547 op = DW_OP_mul;
12548 goto do_binop;
12549
12550 case DIV:
12551 if (!dwarf_strict
12552 && GET_MODE_CLASS (mode) == MODE_INT
12553 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12554 {
12555 mem_loc_result = typed_binop (DW_OP_div, rtl,
12556 base_type_for_mode (mode, 0),
12557 mode, mem_mode);
12558 break;
12559 }
12560 op = DW_OP_div;
12561 goto do_binop;
12562
12563 case UMOD:
12564 op = DW_OP_mod;
12565 goto do_binop;
12566
12567 case ASHIFT:
12568 op = DW_OP_shl;
12569 goto do_shift;
12570
12571 case ASHIFTRT:
12572 op = DW_OP_shra;
12573 goto do_shift;
12574
12575 case LSHIFTRT:
12576 op = DW_OP_shr;
12577 goto do_shift;
12578
12579 do_shift:
12580 if (GET_MODE_CLASS (mode) != MODE_INT)
12581 break;
12582 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12583 VAR_INIT_STATUS_INITIALIZED);
12584 {
12585 rtx rtlop1 = XEXP (rtl, 1);
12586 if (GET_MODE (rtlop1) != VOIDmode
12587 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12588 < GET_MODE_BITSIZE (mode))
12589 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12590 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12591 VAR_INIT_STATUS_INITIALIZED);
12592 }
12593
12594 if (op0 == 0 || op1 == 0)
12595 break;
12596
12597 mem_loc_result = op0;
12598 add_loc_descr (&mem_loc_result, op1);
12599 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12600 break;
12601
12602 case AND:
12603 op = DW_OP_and;
12604 goto do_binop;
12605
12606 case IOR:
12607 op = DW_OP_or;
12608 goto do_binop;
12609
12610 case XOR:
12611 op = DW_OP_xor;
12612 goto do_binop;
12613
12614 do_binop:
12615 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12617 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12618 VAR_INIT_STATUS_INITIALIZED);
12619
12620 if (op0 == 0 || op1 == 0)
12621 break;
12622
12623 mem_loc_result = op0;
12624 add_loc_descr (&mem_loc_result, op1);
12625 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12626 break;
12627
12628 case MOD:
12629 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12630 {
12631 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12632 base_type_for_mode (mode, 0),
12633 mode, mem_mode);
12634 break;
12635 }
12636
12637 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12638 VAR_INIT_STATUS_INITIALIZED);
12639 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12640 VAR_INIT_STATUS_INITIALIZED);
12641
12642 if (op0 == 0 || op1 == 0)
12643 break;
12644
12645 mem_loc_result = op0;
12646 add_loc_descr (&mem_loc_result, op1);
12647 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12648 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12649 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12650 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12651 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12652 break;
12653
12654 case UDIV:
12655 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12656 {
12657 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12658 {
12659 op = DW_OP_div;
12660 goto do_binop;
12661 }
12662 mem_loc_result = typed_binop (DW_OP_div, rtl,
12663 base_type_for_mode (mode, 1),
12664 mode, mem_mode);
12665 }
12666 break;
12667
12668 case NOT:
12669 op = DW_OP_not;
12670 goto do_unop;
12671
12672 case ABS:
12673 op = DW_OP_abs;
12674 goto do_unop;
12675
12676 case NEG:
12677 op = DW_OP_neg;
12678 goto do_unop;
12679
12680 do_unop:
12681 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12682 VAR_INIT_STATUS_INITIALIZED);
12683
12684 if (op0 == 0)
12685 break;
12686
12687 mem_loc_result = op0;
12688 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12689 break;
12690
12691 case CONST_INT:
12692 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12693 #ifdef POINTERS_EXTEND_UNSIGNED
12694 || (mode == Pmode
12695 && mem_mode != VOIDmode
12696 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12697 #endif
12698 )
12699 {
12700 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12701 break;
12702 }
12703 if (!dwarf_strict
12704 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12705 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12706 {
12707 dw_die_ref type_die = base_type_for_mode (mode, 1);
12708 enum machine_mode amode;
12709 if (type_die == NULL)
12710 return NULL;
12711 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12712 MODE_INT, 0);
12713 if (INTVAL (rtl) >= 0
12714 && amode != BLKmode
12715 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12716 /* const DW_OP_GNU_convert <XXX> vs.
12717 DW_OP_GNU_const_type <XXX, 1, const>. */
12718 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12719 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12720 {
12721 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12722 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12723 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12724 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12725 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12726 add_loc_descr (&mem_loc_result, op0);
12727 return mem_loc_result;
12728 }
12729 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12730 INTVAL (rtl));
12731 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12732 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12733 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12734 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12735 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12736 else
12737 {
12738 mem_loc_result->dw_loc_oprnd2.val_class
12739 = dw_val_class_const_double;
12740 mem_loc_result->dw_loc_oprnd2.v.val_double
12741 = double_int::from_shwi (INTVAL (rtl));
12742 }
12743 }
12744 break;
12745
12746 case CONST_DOUBLE:
12747 if (!dwarf_strict)
12748 {
12749 dw_die_ref type_die;
12750
12751 /* Note that a CONST_DOUBLE rtx could represent either an integer
12752 or a floating-point constant. A CONST_DOUBLE is used whenever
12753 the constant requires more than one word in order to be
12754 adequately represented. We output CONST_DOUBLEs as blocks. */
12755 if (mode == VOIDmode
12756 || (GET_MODE (rtl) == VOIDmode
12757 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12758 break;
12759 type_die = base_type_for_mode (mode,
12760 GET_MODE_CLASS (mode) == MODE_INT);
12761 if (type_die == NULL)
12762 return NULL;
12763 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12764 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12765 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12766 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12767 if (SCALAR_FLOAT_MODE_P (mode))
12768 {
12769 unsigned int length = GET_MODE_SIZE (mode);
12770 unsigned char *array
12771 = (unsigned char*) ggc_alloc_atomic (length);
12772
12773 insert_float (rtl, array);
12774 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12775 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12776 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12777 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12778 }
12779 else
12780 {
12781 mem_loc_result->dw_loc_oprnd2.val_class
12782 = dw_val_class_const_double;
12783 mem_loc_result->dw_loc_oprnd2.v.val_double
12784 = rtx_to_double_int (rtl);
12785 }
12786 }
12787 break;
12788
12789 case EQ:
12790 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12791 break;
12792
12793 case GE:
12794 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12795 break;
12796
12797 case GT:
12798 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12799 break;
12800
12801 case LE:
12802 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12803 break;
12804
12805 case LT:
12806 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12807 break;
12808
12809 case NE:
12810 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12811 break;
12812
12813 case GEU:
12814 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12815 break;
12816
12817 case GTU:
12818 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12819 break;
12820
12821 case LEU:
12822 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12823 break;
12824
12825 case LTU:
12826 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12827 break;
12828
12829 case UMIN:
12830 case UMAX:
12831 if (GET_MODE_CLASS (mode) != MODE_INT)
12832 break;
12833 /* FALLTHRU */
12834 case SMIN:
12835 case SMAX:
12836 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12837 break;
12838
12839 case ZERO_EXTRACT:
12840 case SIGN_EXTRACT:
12841 if (CONST_INT_P (XEXP (rtl, 1))
12842 && CONST_INT_P (XEXP (rtl, 2))
12843 && ((unsigned) INTVAL (XEXP (rtl, 1))
12844 + (unsigned) INTVAL (XEXP (rtl, 2))
12845 <= GET_MODE_BITSIZE (mode))
12846 && GET_MODE_CLASS (mode) == MODE_INT
12847 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12848 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12849 {
12850 int shift, size;
12851 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12852 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12853 if (op0 == 0)
12854 break;
12855 if (GET_CODE (rtl) == SIGN_EXTRACT)
12856 op = DW_OP_shra;
12857 else
12858 op = DW_OP_shr;
12859 mem_loc_result = op0;
12860 size = INTVAL (XEXP (rtl, 1));
12861 shift = INTVAL (XEXP (rtl, 2));
12862 if (BITS_BIG_ENDIAN)
12863 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12864 - shift - size;
12865 if (shift + size != (int) DWARF2_ADDR_SIZE)
12866 {
12867 add_loc_descr (&mem_loc_result,
12868 int_loc_descriptor (DWARF2_ADDR_SIZE
12869 - shift - size));
12870 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12871 }
12872 if (size != (int) DWARF2_ADDR_SIZE)
12873 {
12874 add_loc_descr (&mem_loc_result,
12875 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12876 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12877 }
12878 }
12879 break;
12880
12881 case IF_THEN_ELSE:
12882 {
12883 dw_loc_descr_ref op2, bra_node, drop_node;
12884 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12885 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12886 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12887 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12888 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12889 VAR_INIT_STATUS_INITIALIZED);
12890 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12891 VAR_INIT_STATUS_INITIALIZED);
12892 if (op0 == NULL || op1 == NULL || op2 == NULL)
12893 break;
12894
12895 mem_loc_result = op1;
12896 add_loc_descr (&mem_loc_result, op2);
12897 add_loc_descr (&mem_loc_result, op0);
12898 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12899 add_loc_descr (&mem_loc_result, bra_node);
12900 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12901 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12902 add_loc_descr (&mem_loc_result, drop_node);
12903 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12904 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12905 }
12906 break;
12907
12908 case FLOAT_EXTEND:
12909 case FLOAT_TRUNCATE:
12910 case FLOAT:
12911 case UNSIGNED_FLOAT:
12912 case FIX:
12913 case UNSIGNED_FIX:
12914 if (!dwarf_strict)
12915 {
12916 dw_die_ref type_die;
12917 dw_loc_descr_ref cvt;
12918
12919 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12920 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12921 if (op0 == NULL)
12922 break;
12923 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12924 && (GET_CODE (rtl) == FLOAT
12925 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12926 <= DWARF2_ADDR_SIZE))
12927 {
12928 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12929 GET_CODE (rtl) == UNSIGNED_FLOAT);
12930 if (type_die == NULL)
12931 break;
12932 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12933 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12934 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12935 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12936 add_loc_descr (&op0, cvt);
12937 }
12938 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12939 if (type_die == NULL)
12940 break;
12941 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12942 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12943 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12944 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12945 add_loc_descr (&op0, cvt);
12946 if (GET_MODE_CLASS (mode) == MODE_INT
12947 && (GET_CODE (rtl) == FIX
12948 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12949 {
12950 op0 = convert_descriptor_to_mode (mode, op0);
12951 if (op0 == NULL)
12952 break;
12953 }
12954 mem_loc_result = op0;
12955 }
12956 break;
12957
12958 case CLZ:
12959 case CTZ:
12960 case FFS:
12961 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12962 break;
12963
12964 case POPCOUNT:
12965 case PARITY:
12966 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12967 break;
12968
12969 case BSWAP:
12970 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12971 break;
12972
12973 case ROTATE:
12974 case ROTATERT:
12975 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12976 break;
12977
12978 case COMPARE:
12979 /* In theory, we could implement the above. */
12980 /* DWARF cannot represent the unsigned compare operations
12981 natively. */
12982 case SS_MULT:
12983 case US_MULT:
12984 case SS_DIV:
12985 case US_DIV:
12986 case SS_PLUS:
12987 case US_PLUS:
12988 case SS_MINUS:
12989 case US_MINUS:
12990 case SS_NEG:
12991 case US_NEG:
12992 case SS_ABS:
12993 case SS_ASHIFT:
12994 case US_ASHIFT:
12995 case SS_TRUNCATE:
12996 case US_TRUNCATE:
12997 case UNORDERED:
12998 case ORDERED:
12999 case UNEQ:
13000 case UNGE:
13001 case UNGT:
13002 case UNLE:
13003 case UNLT:
13004 case LTGT:
13005 case FRACT_CONVERT:
13006 case UNSIGNED_FRACT_CONVERT:
13007 case SAT_FRACT:
13008 case UNSIGNED_SAT_FRACT:
13009 case SQRT:
13010 case ASM_OPERANDS:
13011 case VEC_MERGE:
13012 case VEC_SELECT:
13013 case VEC_CONCAT:
13014 case VEC_DUPLICATE:
13015 case UNSPEC:
13016 case HIGH:
13017 case FMA:
13018 case STRICT_LOW_PART:
13019 case CONST_VECTOR:
13020 case CONST_FIXED:
13021 case CLRSB:
13022 case CLOBBER:
13023 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13024 can't express it in the debug info. This can happen e.g. with some
13025 TLS UNSPECs. */
13026 break;
13027
13028 case CONST_STRING:
13029 resolve_one_addr (&rtl, NULL);
13030 goto symref;
13031
13032 default:
13033 #ifdef ENABLE_CHECKING
13034 print_rtl (stderr, rtl);
13035 gcc_unreachable ();
13036 #else
13037 break;
13038 #endif
13039 }
13040
13041 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13042 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13043
13044 return mem_loc_result;
13045 }
13046
13047 /* Return a descriptor that describes the concatenation of two locations.
13048 This is typically a complex variable. */
13049
13050 static dw_loc_descr_ref
13051 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13052 {
13053 dw_loc_descr_ref cc_loc_result = NULL;
13054 dw_loc_descr_ref x0_ref
13055 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13056 dw_loc_descr_ref x1_ref
13057 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13058
13059 if (x0_ref == 0 || x1_ref == 0)
13060 return 0;
13061
13062 cc_loc_result = x0_ref;
13063 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13064
13065 add_loc_descr (&cc_loc_result, x1_ref);
13066 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13067
13068 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13069 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13070
13071 return cc_loc_result;
13072 }
13073
13074 /* Return a descriptor that describes the concatenation of N
13075 locations. */
13076
13077 static dw_loc_descr_ref
13078 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13079 {
13080 unsigned int i;
13081 dw_loc_descr_ref cc_loc_result = NULL;
13082 unsigned int n = XVECLEN (concatn, 0);
13083
13084 for (i = 0; i < n; ++i)
13085 {
13086 dw_loc_descr_ref ref;
13087 rtx x = XVECEXP (concatn, 0, i);
13088
13089 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13090 if (ref == NULL)
13091 return NULL;
13092
13093 add_loc_descr (&cc_loc_result, ref);
13094 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13095 }
13096
13097 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13098 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13099
13100 return cc_loc_result;
13101 }
13102
13103 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13104 for DEBUG_IMPLICIT_PTR RTL. */
13105
13106 static dw_loc_descr_ref
13107 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13108 {
13109 dw_loc_descr_ref ret;
13110 dw_die_ref ref;
13111
13112 if (dwarf_strict)
13113 return NULL;
13114 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13115 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13116 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13117 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13118 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13119 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13120 if (ref)
13121 {
13122 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13123 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13124 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13125 }
13126 else
13127 {
13128 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13129 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13130 }
13131 return ret;
13132 }
13133
13134 /* Output a proper Dwarf location descriptor for a variable or parameter
13135 which is either allocated in a register or in a memory location. For a
13136 register, we just generate an OP_REG and the register number. For a
13137 memory location we provide a Dwarf postfix expression describing how to
13138 generate the (dynamic) address of the object onto the address stack.
13139
13140 MODE is mode of the decl if this loc_descriptor is going to be used in
13141 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13142 allowed, VOIDmode otherwise.
13143
13144 If we don't know how to describe it, return 0. */
13145
13146 static dw_loc_descr_ref
13147 loc_descriptor (rtx rtl, enum machine_mode mode,
13148 enum var_init_status initialized)
13149 {
13150 dw_loc_descr_ref loc_result = NULL;
13151
13152 switch (GET_CODE (rtl))
13153 {
13154 case SUBREG:
13155 /* The case of a subreg may arise when we have a local (register)
13156 variable or a formal (register) parameter which doesn't quite fill
13157 up an entire register. For now, just assume that it is
13158 legitimate to make the Dwarf info refer to the whole register which
13159 contains the given subreg. */
13160 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13161 loc_result = loc_descriptor (SUBREG_REG (rtl),
13162 GET_MODE (SUBREG_REG (rtl)), initialized);
13163 else
13164 goto do_default;
13165 break;
13166
13167 case REG:
13168 loc_result = reg_loc_descriptor (rtl, initialized);
13169 break;
13170
13171 case MEM:
13172 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13173 GET_MODE (rtl), initialized);
13174 if (loc_result == NULL)
13175 loc_result = tls_mem_loc_descriptor (rtl);
13176 if (loc_result == NULL)
13177 {
13178 rtx new_rtl = avoid_constant_pool_reference (rtl);
13179 if (new_rtl != rtl)
13180 loc_result = loc_descriptor (new_rtl, mode, initialized);
13181 }
13182 break;
13183
13184 case CONCAT:
13185 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13186 initialized);
13187 break;
13188
13189 case CONCATN:
13190 loc_result = concatn_loc_descriptor (rtl, initialized);
13191 break;
13192
13193 case VAR_LOCATION:
13194 /* Single part. */
13195 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13196 {
13197 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13198 if (GET_CODE (loc) == EXPR_LIST)
13199 loc = XEXP (loc, 0);
13200 loc_result = loc_descriptor (loc, mode, initialized);
13201 break;
13202 }
13203
13204 rtl = XEXP (rtl, 1);
13205 /* FALLTHRU */
13206
13207 case PARALLEL:
13208 {
13209 rtvec par_elems = XVEC (rtl, 0);
13210 int num_elem = GET_NUM_ELEM (par_elems);
13211 enum machine_mode mode;
13212 int i;
13213
13214 /* Create the first one, so we have something to add to. */
13215 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13216 VOIDmode, initialized);
13217 if (loc_result == NULL)
13218 return NULL;
13219 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13220 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13221 for (i = 1; i < num_elem; i++)
13222 {
13223 dw_loc_descr_ref temp;
13224
13225 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13226 VOIDmode, initialized);
13227 if (temp == NULL)
13228 return NULL;
13229 add_loc_descr (&loc_result, temp);
13230 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13231 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13232 }
13233 }
13234 break;
13235
13236 case CONST_INT:
13237 if (mode != VOIDmode && mode != BLKmode)
13238 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13239 INTVAL (rtl));
13240 break;
13241
13242 case CONST_DOUBLE:
13243 if (mode == VOIDmode)
13244 mode = GET_MODE (rtl);
13245
13246 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13247 {
13248 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13249
13250 /* Note that a CONST_DOUBLE rtx could represent either an integer
13251 or a floating-point constant. A CONST_DOUBLE is used whenever
13252 the constant requires more than one word in order to be
13253 adequately represented. We output CONST_DOUBLEs as blocks. */
13254 loc_result = new_loc_descr (DW_OP_implicit_value,
13255 GET_MODE_SIZE (mode), 0);
13256 if (SCALAR_FLOAT_MODE_P (mode))
13257 {
13258 unsigned int length = GET_MODE_SIZE (mode);
13259 unsigned char *array
13260 = (unsigned char*) ggc_alloc_atomic (length);
13261
13262 insert_float (rtl, array);
13263 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13264 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13265 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13266 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13267 }
13268 else
13269 {
13270 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13271 loc_result->dw_loc_oprnd2.v.val_double
13272 = rtx_to_double_int (rtl);
13273 }
13274 }
13275 break;
13276
13277 case CONST_VECTOR:
13278 if (mode == VOIDmode)
13279 mode = GET_MODE (rtl);
13280
13281 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13282 {
13283 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13284 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13285 unsigned char *array = (unsigned char *)
13286 ggc_alloc_atomic (length * elt_size);
13287 unsigned int i;
13288 unsigned char *p;
13289
13290 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13291 switch (GET_MODE_CLASS (mode))
13292 {
13293 case MODE_VECTOR_INT:
13294 for (i = 0, p = array; i < length; i++, p += elt_size)
13295 {
13296 rtx elt = CONST_VECTOR_ELT (rtl, i);
13297 double_int val = rtx_to_double_int (elt);
13298
13299 if (elt_size <= sizeof (HOST_WIDE_INT))
13300 insert_int (val.to_shwi (), elt_size, p);
13301 else
13302 {
13303 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13304 insert_double (val, p);
13305 }
13306 }
13307 break;
13308
13309 case MODE_VECTOR_FLOAT:
13310 for (i = 0, p = array; i < length; i++, p += elt_size)
13311 {
13312 rtx elt = CONST_VECTOR_ELT (rtl, i);
13313 insert_float (elt, p);
13314 }
13315 break;
13316
13317 default:
13318 gcc_unreachable ();
13319 }
13320
13321 loc_result = new_loc_descr (DW_OP_implicit_value,
13322 length * elt_size, 0);
13323 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13324 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13325 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13326 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13327 }
13328 break;
13329
13330 case CONST:
13331 if (mode == VOIDmode
13332 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13333 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13334 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13335 {
13336 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13337 break;
13338 }
13339 /* FALLTHROUGH */
13340 case SYMBOL_REF:
13341 if (!const_ok_for_output (rtl))
13342 break;
13343 case LABEL_REF:
13344 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13345 && (dwarf_version >= 4 || !dwarf_strict))
13346 {
13347 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13348 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13349 vec_safe_push (used_rtx_array, rtl);
13350 }
13351 break;
13352
13353 case DEBUG_IMPLICIT_PTR:
13354 loc_result = implicit_ptr_descriptor (rtl, 0);
13355 break;
13356
13357 case PLUS:
13358 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13359 && CONST_INT_P (XEXP (rtl, 1)))
13360 {
13361 loc_result
13362 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13363 break;
13364 }
13365 /* FALLTHRU */
13366 do_default:
13367 default:
13368 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13369 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13370 && dwarf_version >= 4)
13371 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13372 {
13373 /* Value expression. */
13374 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13375 if (loc_result)
13376 add_loc_descr (&loc_result,
13377 new_loc_descr (DW_OP_stack_value, 0, 0));
13378 }
13379 break;
13380 }
13381
13382 return loc_result;
13383 }
13384
13385 /* We need to figure out what section we should use as the base for the
13386 address ranges where a given location is valid.
13387 1. If this particular DECL has a section associated with it, use that.
13388 2. If this function has a section associated with it, use that.
13389 3. Otherwise, use the text section.
13390 XXX: If you split a variable across multiple sections, we won't notice. */
13391
13392 static const char *
13393 secname_for_decl (const_tree decl)
13394 {
13395 const char *secname;
13396
13397 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13398 {
13399 tree sectree = DECL_SECTION_NAME (decl);
13400 secname = TREE_STRING_POINTER (sectree);
13401 }
13402 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13403 {
13404 tree sectree = DECL_SECTION_NAME (current_function_decl);
13405 secname = TREE_STRING_POINTER (sectree);
13406 }
13407 else if (cfun && in_cold_section_p)
13408 secname = crtl->subsections.cold_section_label;
13409 else
13410 secname = text_section_label;
13411
13412 return secname;
13413 }
13414
13415 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13416
13417 static bool
13418 decl_by_reference_p (tree decl)
13419 {
13420 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13421 || TREE_CODE (decl) == VAR_DECL)
13422 && DECL_BY_REFERENCE (decl));
13423 }
13424
13425 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13426 for VARLOC. */
13427
13428 static dw_loc_descr_ref
13429 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13430 enum var_init_status initialized)
13431 {
13432 int have_address = 0;
13433 dw_loc_descr_ref descr;
13434 enum machine_mode mode;
13435
13436 if (want_address != 2)
13437 {
13438 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13439 /* Single part. */
13440 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13441 {
13442 varloc = PAT_VAR_LOCATION_LOC (varloc);
13443 if (GET_CODE (varloc) == EXPR_LIST)
13444 varloc = XEXP (varloc, 0);
13445 mode = GET_MODE (varloc);
13446 if (MEM_P (varloc))
13447 {
13448 rtx addr = XEXP (varloc, 0);
13449 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13450 mode, initialized);
13451 if (descr)
13452 have_address = 1;
13453 else
13454 {
13455 rtx x = avoid_constant_pool_reference (varloc);
13456 if (x != varloc)
13457 descr = mem_loc_descriptor (x, mode, VOIDmode,
13458 initialized);
13459 }
13460 }
13461 else
13462 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13463 }
13464 else
13465 return 0;
13466 }
13467 else
13468 {
13469 if (GET_CODE (varloc) == VAR_LOCATION)
13470 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13471 else
13472 mode = DECL_MODE (loc);
13473 descr = loc_descriptor (varloc, mode, initialized);
13474 have_address = 1;
13475 }
13476
13477 if (!descr)
13478 return 0;
13479
13480 if (want_address == 2 && !have_address
13481 && (dwarf_version >= 4 || !dwarf_strict))
13482 {
13483 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13484 {
13485 expansion_failed (loc, NULL_RTX,
13486 "DWARF address size mismatch");
13487 return 0;
13488 }
13489 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13490 have_address = 1;
13491 }
13492 /* Show if we can't fill the request for an address. */
13493 if (want_address && !have_address)
13494 {
13495 expansion_failed (loc, NULL_RTX,
13496 "Want address and only have value");
13497 return 0;
13498 }
13499
13500 /* If we've got an address and don't want one, dereference. */
13501 if (!want_address && have_address)
13502 {
13503 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13504 enum dwarf_location_atom op;
13505
13506 if (size > DWARF2_ADDR_SIZE || size == -1)
13507 {
13508 expansion_failed (loc, NULL_RTX,
13509 "DWARF address size mismatch");
13510 return 0;
13511 }
13512 else if (size == DWARF2_ADDR_SIZE)
13513 op = DW_OP_deref;
13514 else
13515 op = DW_OP_deref_size;
13516
13517 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13518 }
13519
13520 return descr;
13521 }
13522
13523 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13524 if it is not possible. */
13525
13526 static dw_loc_descr_ref
13527 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13528 {
13529 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13530 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13531 else if (dwarf_version >= 3 || !dwarf_strict)
13532 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13533 else
13534 return NULL;
13535 }
13536
13537 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13538 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13539
13540 static dw_loc_descr_ref
13541 dw_sra_loc_expr (tree decl, rtx loc)
13542 {
13543 rtx p;
13544 unsigned int padsize = 0;
13545 dw_loc_descr_ref descr, *descr_tail;
13546 unsigned HOST_WIDE_INT decl_size;
13547 rtx varloc;
13548 enum var_init_status initialized;
13549
13550 if (DECL_SIZE (decl) == NULL
13551 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13552 return NULL;
13553
13554 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13555 descr = NULL;
13556 descr_tail = &descr;
13557
13558 for (p = loc; p; p = XEXP (p, 1))
13559 {
13560 unsigned int bitsize = decl_piece_bitsize (p);
13561 rtx loc_note = *decl_piece_varloc_ptr (p);
13562 dw_loc_descr_ref cur_descr;
13563 dw_loc_descr_ref *tail, last = NULL;
13564 unsigned int opsize = 0;
13565
13566 if (loc_note == NULL_RTX
13567 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13568 {
13569 padsize += bitsize;
13570 continue;
13571 }
13572 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13573 varloc = NOTE_VAR_LOCATION (loc_note);
13574 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13575 if (cur_descr == NULL)
13576 {
13577 padsize += bitsize;
13578 continue;
13579 }
13580
13581 /* Check that cur_descr either doesn't use
13582 DW_OP_*piece operations, or their sum is equal
13583 to bitsize. Otherwise we can't embed it. */
13584 for (tail = &cur_descr; *tail != NULL;
13585 tail = &(*tail)->dw_loc_next)
13586 if ((*tail)->dw_loc_opc == DW_OP_piece)
13587 {
13588 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13589 * BITS_PER_UNIT;
13590 last = *tail;
13591 }
13592 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13593 {
13594 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13595 last = *tail;
13596 }
13597
13598 if (last != NULL && opsize != bitsize)
13599 {
13600 padsize += bitsize;
13601 /* Discard the current piece of the descriptor and release any
13602 addr_table entries it uses. */
13603 remove_loc_list_addr_table_entries (cur_descr);
13604 continue;
13605 }
13606
13607 /* If there is a hole, add DW_OP_*piece after empty DWARF
13608 expression, which means that those bits are optimized out. */
13609 if (padsize)
13610 {
13611 if (padsize > decl_size)
13612 {
13613 remove_loc_list_addr_table_entries (cur_descr);
13614 goto discard_descr;
13615 }
13616 decl_size -= padsize;
13617 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13618 if (*descr_tail == NULL)
13619 {
13620 remove_loc_list_addr_table_entries (cur_descr);
13621 goto discard_descr;
13622 }
13623 descr_tail = &(*descr_tail)->dw_loc_next;
13624 padsize = 0;
13625 }
13626 *descr_tail = cur_descr;
13627 descr_tail = tail;
13628 if (bitsize > decl_size)
13629 goto discard_descr;
13630 decl_size -= bitsize;
13631 if (last == NULL)
13632 {
13633 HOST_WIDE_INT offset = 0;
13634 if (GET_CODE (varloc) == VAR_LOCATION
13635 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13636 {
13637 varloc = PAT_VAR_LOCATION_LOC (varloc);
13638 if (GET_CODE (varloc) == EXPR_LIST)
13639 varloc = XEXP (varloc, 0);
13640 }
13641 do
13642 {
13643 if (GET_CODE (varloc) == CONST
13644 || GET_CODE (varloc) == SIGN_EXTEND
13645 || GET_CODE (varloc) == ZERO_EXTEND)
13646 varloc = XEXP (varloc, 0);
13647 else if (GET_CODE (varloc) == SUBREG)
13648 varloc = SUBREG_REG (varloc);
13649 else
13650 break;
13651 }
13652 while (1);
13653 /* DW_OP_bit_size offset should be zero for register
13654 or implicit location descriptions and empty location
13655 descriptions, but for memory addresses needs big endian
13656 adjustment. */
13657 if (MEM_P (varloc))
13658 {
13659 unsigned HOST_WIDE_INT memsize
13660 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13661 if (memsize != bitsize)
13662 {
13663 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13664 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13665 goto discard_descr;
13666 if (memsize < bitsize)
13667 goto discard_descr;
13668 if (BITS_BIG_ENDIAN)
13669 offset = memsize - bitsize;
13670 }
13671 }
13672
13673 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13674 if (*descr_tail == NULL)
13675 goto discard_descr;
13676 descr_tail = &(*descr_tail)->dw_loc_next;
13677 }
13678 }
13679
13680 /* If there were any non-empty expressions, add padding till the end of
13681 the decl. */
13682 if (descr != NULL && decl_size != 0)
13683 {
13684 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13685 if (*descr_tail == NULL)
13686 goto discard_descr;
13687 }
13688 return descr;
13689
13690 discard_descr:
13691 /* Discard the descriptor and release any addr_table entries it uses. */
13692 remove_loc_list_addr_table_entries (descr);
13693 return NULL;
13694 }
13695
13696 /* Return the dwarf representation of the location list LOC_LIST of
13697 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13698 function. */
13699
13700 static dw_loc_list_ref
13701 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13702 {
13703 const char *endname, *secname;
13704 rtx varloc;
13705 enum var_init_status initialized;
13706 struct var_loc_node *node;
13707 dw_loc_descr_ref descr;
13708 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13709 dw_loc_list_ref list = NULL;
13710 dw_loc_list_ref *listp = &list;
13711
13712 /* Now that we know what section we are using for a base,
13713 actually construct the list of locations.
13714 The first location information is what is passed to the
13715 function that creates the location list, and the remaining
13716 locations just get added on to that list.
13717 Note that we only know the start address for a location
13718 (IE location changes), so to build the range, we use
13719 the range [current location start, next location start].
13720 This means we have to special case the last node, and generate
13721 a range of [last location start, end of function label]. */
13722
13723 secname = secname_for_decl (decl);
13724
13725 for (node = loc_list->first; node; node = node->next)
13726 if (GET_CODE (node->loc) == EXPR_LIST
13727 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13728 {
13729 if (GET_CODE (node->loc) == EXPR_LIST)
13730 {
13731 /* This requires DW_OP_{,bit_}piece, which is not usable
13732 inside DWARF expressions. */
13733 if (want_address != 2)
13734 continue;
13735 descr = dw_sra_loc_expr (decl, node->loc);
13736 if (descr == NULL)
13737 continue;
13738 }
13739 else
13740 {
13741 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13742 varloc = NOTE_VAR_LOCATION (node->loc);
13743 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13744 }
13745 if (descr)
13746 {
13747 bool range_across_switch = false;
13748 /* If section switch happens in between node->label
13749 and node->next->label (or end of function) and
13750 we can't emit it as a single entry list,
13751 emit two ranges, first one ending at the end
13752 of first partition and second one starting at the
13753 beginning of second partition. */
13754 if (node == loc_list->last_before_switch
13755 && (node != loc_list->first || loc_list->first->next)
13756 && current_function_decl)
13757 {
13758 endname = cfun->fde->dw_fde_end;
13759 range_across_switch = true;
13760 }
13761 /* The variable has a location between NODE->LABEL and
13762 NODE->NEXT->LABEL. */
13763 else if (node->next)
13764 endname = node->next->label;
13765 /* If the variable has a location at the last label
13766 it keeps its location until the end of function. */
13767 else if (!current_function_decl)
13768 endname = text_end_label;
13769 else
13770 {
13771 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13772 current_function_funcdef_no);
13773 endname = ggc_strdup (label_id);
13774 }
13775
13776 *listp = new_loc_list (descr, node->label, endname, secname);
13777 if (TREE_CODE (decl) == PARM_DECL
13778 && node == loc_list->first
13779 && NOTE_P (node->loc)
13780 && strcmp (node->label, endname) == 0)
13781 (*listp)->force = true;
13782 listp = &(*listp)->dw_loc_next;
13783
13784 if (range_across_switch)
13785 {
13786 if (GET_CODE (node->loc) == EXPR_LIST)
13787 descr = dw_sra_loc_expr (decl, node->loc);
13788 else
13789 {
13790 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13791 varloc = NOTE_VAR_LOCATION (node->loc);
13792 descr = dw_loc_list_1 (decl, varloc, want_address,
13793 initialized);
13794 }
13795 gcc_assert (descr);
13796 /* The variable has a location between NODE->LABEL and
13797 NODE->NEXT->LABEL. */
13798 if (node->next)
13799 endname = node->next->label;
13800 else
13801 endname = cfun->fde->dw_fde_second_end;
13802 *listp = new_loc_list (descr,
13803 cfun->fde->dw_fde_second_begin,
13804 endname, secname);
13805 listp = &(*listp)->dw_loc_next;
13806 }
13807 }
13808 }
13809
13810 /* Try to avoid the overhead of a location list emitting a location
13811 expression instead, but only if we didn't have more than one
13812 location entry in the first place. If some entries were not
13813 representable, we don't want to pretend a single entry that was
13814 applies to the entire scope in which the variable is
13815 available. */
13816 if (list && loc_list->first->next)
13817 gen_llsym (list);
13818
13819 return list;
13820 }
13821
13822 /* Return if the loc_list has only single element and thus can be represented
13823 as location description. */
13824
13825 static bool
13826 single_element_loc_list_p (dw_loc_list_ref list)
13827 {
13828 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13829 return !list->ll_symbol;
13830 }
13831
13832 /* To each location in list LIST add loc descr REF. */
13833
13834 static void
13835 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13836 {
13837 dw_loc_descr_ref copy;
13838 add_loc_descr (&list->expr, ref);
13839 list = list->dw_loc_next;
13840 while (list)
13841 {
13842 copy = ggc_alloc_dw_loc_descr_node ();
13843 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13844 add_loc_descr (&list->expr, copy);
13845 while (copy->dw_loc_next)
13846 {
13847 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13848 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13849 copy->dw_loc_next = new_copy;
13850 copy = new_copy;
13851 }
13852 list = list->dw_loc_next;
13853 }
13854 }
13855
13856 /* Given two lists RET and LIST
13857 produce location list that is result of adding expression in LIST
13858 to expression in RET on each position in program.
13859 Might be destructive on both RET and LIST.
13860
13861 TODO: We handle only simple cases of RET or LIST having at most one
13862 element. General case would inolve sorting the lists in program order
13863 and merging them that will need some additional work.
13864 Adding that will improve quality of debug info especially for SRA-ed
13865 structures. */
13866
13867 static void
13868 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13869 {
13870 if (!list)
13871 return;
13872 if (!*ret)
13873 {
13874 *ret = list;
13875 return;
13876 }
13877 if (!list->dw_loc_next)
13878 {
13879 add_loc_descr_to_each (*ret, list->expr);
13880 return;
13881 }
13882 if (!(*ret)->dw_loc_next)
13883 {
13884 add_loc_descr_to_each (list, (*ret)->expr);
13885 *ret = list;
13886 return;
13887 }
13888 expansion_failed (NULL_TREE, NULL_RTX,
13889 "Don't know how to merge two non-trivial"
13890 " location lists.\n");
13891 *ret = NULL;
13892 return;
13893 }
13894
13895 /* LOC is constant expression. Try a luck, look it up in constant
13896 pool and return its loc_descr of its address. */
13897
13898 static dw_loc_descr_ref
13899 cst_pool_loc_descr (tree loc)
13900 {
13901 /* Get an RTL for this, if something has been emitted. */
13902 rtx rtl = lookup_constant_def (loc);
13903
13904 if (!rtl || !MEM_P (rtl))
13905 {
13906 gcc_assert (!rtl);
13907 return 0;
13908 }
13909 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13910
13911 /* TODO: We might get more coverage if we was actually delaying expansion
13912 of all expressions till end of compilation when constant pools are fully
13913 populated. */
13914 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13915 {
13916 expansion_failed (loc, NULL_RTX,
13917 "CST value in contant pool but not marked.");
13918 return 0;
13919 }
13920 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13921 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13922 }
13923
13924 /* Return dw_loc_list representing address of addr_expr LOC
13925 by looking for inner INDIRECT_REF expression and turning
13926 it into simple arithmetics. */
13927
13928 static dw_loc_list_ref
13929 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13930 {
13931 tree obj, offset;
13932 HOST_WIDE_INT bitsize, bitpos, bytepos;
13933 enum machine_mode mode;
13934 int unsignedp, volatilep = 0;
13935 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13936
13937 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13938 &bitsize, &bitpos, &offset, &mode,
13939 &unsignedp, &volatilep, false);
13940 STRIP_NOPS (obj);
13941 if (bitpos % BITS_PER_UNIT)
13942 {
13943 expansion_failed (loc, NULL_RTX, "bitfield access");
13944 return 0;
13945 }
13946 if (!INDIRECT_REF_P (obj))
13947 {
13948 expansion_failed (obj,
13949 NULL_RTX, "no indirect ref in inner refrence");
13950 return 0;
13951 }
13952 if (!offset && !bitpos)
13953 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13954 else if (toplev
13955 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13956 && (dwarf_version >= 4 || !dwarf_strict))
13957 {
13958 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13959 if (!list_ret)
13960 return 0;
13961 if (offset)
13962 {
13963 /* Variable offset. */
13964 list_ret1 = loc_list_from_tree (offset, 0);
13965 if (list_ret1 == 0)
13966 return 0;
13967 add_loc_list (&list_ret, list_ret1);
13968 if (!list_ret)
13969 return 0;
13970 add_loc_descr_to_each (list_ret,
13971 new_loc_descr (DW_OP_plus, 0, 0));
13972 }
13973 bytepos = bitpos / BITS_PER_UNIT;
13974 if (bytepos > 0)
13975 add_loc_descr_to_each (list_ret,
13976 new_loc_descr (DW_OP_plus_uconst,
13977 bytepos, 0));
13978 else if (bytepos < 0)
13979 loc_list_plus_const (list_ret, bytepos);
13980 add_loc_descr_to_each (list_ret,
13981 new_loc_descr (DW_OP_stack_value, 0, 0));
13982 }
13983 return list_ret;
13984 }
13985
13986
13987 /* Generate Dwarf location list representing LOC.
13988 If WANT_ADDRESS is false, expression computing LOC will be computed
13989 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13990 if WANT_ADDRESS is 2, expression computing address useable in location
13991 will be returned (i.e. DW_OP_reg can be used
13992 to refer to register values). */
13993
13994 static dw_loc_list_ref
13995 loc_list_from_tree (tree loc, int want_address)
13996 {
13997 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13998 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13999 int have_address = 0;
14000 enum dwarf_location_atom op;
14001
14002 /* ??? Most of the time we do not take proper care for sign/zero
14003 extending the values properly. Hopefully this won't be a real
14004 problem... */
14005
14006 switch (TREE_CODE (loc))
14007 {
14008 case ERROR_MARK:
14009 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14010 return 0;
14011
14012 case PLACEHOLDER_EXPR:
14013 /* This case involves extracting fields from an object to determine the
14014 position of other fields. We don't try to encode this here. The
14015 only user of this is Ada, which encodes the needed information using
14016 the names of types. */
14017 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14018 return 0;
14019
14020 case CALL_EXPR:
14021 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14022 /* There are no opcodes for these operations. */
14023 return 0;
14024
14025 case PREINCREMENT_EXPR:
14026 case PREDECREMENT_EXPR:
14027 case POSTINCREMENT_EXPR:
14028 case POSTDECREMENT_EXPR:
14029 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14030 /* There are no opcodes for these operations. */
14031 return 0;
14032
14033 case ADDR_EXPR:
14034 /* If we already want an address, see if there is INDIRECT_REF inside
14035 e.g. for &this->field. */
14036 if (want_address)
14037 {
14038 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14039 (loc, want_address == 2);
14040 if (list_ret)
14041 have_address = 1;
14042 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14043 && (ret = cst_pool_loc_descr (loc)))
14044 have_address = 1;
14045 }
14046 /* Otherwise, process the argument and look for the address. */
14047 if (!list_ret && !ret)
14048 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14049 else
14050 {
14051 if (want_address)
14052 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14053 return NULL;
14054 }
14055 break;
14056
14057 case VAR_DECL:
14058 if (DECL_THREAD_LOCAL_P (loc))
14059 {
14060 rtx rtl;
14061 enum dwarf_location_atom tls_op;
14062 enum dtprel_bool dtprel = dtprel_false;
14063
14064 if (targetm.have_tls)
14065 {
14066 /* If this is not defined, we have no way to emit the
14067 data. */
14068 if (!targetm.asm_out.output_dwarf_dtprel)
14069 return 0;
14070
14071 /* The way DW_OP_GNU_push_tls_address is specified, we
14072 can only look up addresses of objects in the current
14073 module. We used DW_OP_addr as first op, but that's
14074 wrong, because DW_OP_addr is relocated by the debug
14075 info consumer, while DW_OP_GNU_push_tls_address
14076 operand shouldn't be. */
14077 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14078 return 0;
14079 dtprel = dtprel_true;
14080 tls_op = DW_OP_GNU_push_tls_address;
14081 }
14082 else
14083 {
14084 if (!targetm.emutls.debug_form_tls_address
14085 || !(dwarf_version >= 3 || !dwarf_strict))
14086 return 0;
14087 /* We stuffed the control variable into the DECL_VALUE_EXPR
14088 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14089 no longer appear in gimple code. We used the control
14090 variable in specific so that we could pick it up here. */
14091 loc = DECL_VALUE_EXPR (loc);
14092 tls_op = DW_OP_form_tls_address;
14093 }
14094
14095 rtl = rtl_for_decl_location (loc);
14096 if (rtl == NULL_RTX)
14097 return 0;
14098
14099 if (!MEM_P (rtl))
14100 return 0;
14101 rtl = XEXP (rtl, 0);
14102 if (! CONSTANT_P (rtl))
14103 return 0;
14104
14105 ret = new_addr_loc_descr (rtl, dtprel);
14106 ret1 = new_loc_descr (tls_op, 0, 0);
14107 add_loc_descr (&ret, ret1);
14108
14109 have_address = 1;
14110 break;
14111 }
14112 /* FALLTHRU */
14113
14114 case PARM_DECL:
14115 case RESULT_DECL:
14116 if (DECL_HAS_VALUE_EXPR_P (loc))
14117 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14118 want_address);
14119 /* FALLTHRU */
14120
14121 case FUNCTION_DECL:
14122 {
14123 rtx rtl;
14124 var_loc_list *loc_list = lookup_decl_loc (loc);
14125
14126 if (loc_list && loc_list->first)
14127 {
14128 list_ret = dw_loc_list (loc_list, loc, want_address);
14129 have_address = want_address != 0;
14130 break;
14131 }
14132 rtl = rtl_for_decl_location (loc);
14133 if (rtl == NULL_RTX)
14134 {
14135 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14136 return 0;
14137 }
14138 else if (CONST_INT_P (rtl))
14139 {
14140 HOST_WIDE_INT val = INTVAL (rtl);
14141 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14142 val &= GET_MODE_MASK (DECL_MODE (loc));
14143 ret = int_loc_descriptor (val);
14144 }
14145 else if (GET_CODE (rtl) == CONST_STRING)
14146 {
14147 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14148 return 0;
14149 }
14150 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14151 ret = new_addr_loc_descr (rtl, dtprel_false);
14152 else
14153 {
14154 enum machine_mode mode, mem_mode;
14155
14156 /* Certain constructs can only be represented at top-level. */
14157 if (want_address == 2)
14158 {
14159 ret = loc_descriptor (rtl, VOIDmode,
14160 VAR_INIT_STATUS_INITIALIZED);
14161 have_address = 1;
14162 }
14163 else
14164 {
14165 mode = GET_MODE (rtl);
14166 mem_mode = VOIDmode;
14167 if (MEM_P (rtl))
14168 {
14169 mem_mode = mode;
14170 mode = get_address_mode (rtl);
14171 rtl = XEXP (rtl, 0);
14172 have_address = 1;
14173 }
14174 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14175 VAR_INIT_STATUS_INITIALIZED);
14176 }
14177 if (!ret)
14178 expansion_failed (loc, rtl,
14179 "failed to produce loc descriptor for rtl");
14180 }
14181 }
14182 break;
14183
14184 case MEM_REF:
14185 /* ??? FIXME. */
14186 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14187 return 0;
14188 /* Fallthru. */
14189 case INDIRECT_REF:
14190 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14191 have_address = 1;
14192 break;
14193
14194 case COMPOUND_EXPR:
14195 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14196
14197 CASE_CONVERT:
14198 case VIEW_CONVERT_EXPR:
14199 case SAVE_EXPR:
14200 case MODIFY_EXPR:
14201 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14202
14203 case COMPONENT_REF:
14204 case BIT_FIELD_REF:
14205 case ARRAY_REF:
14206 case ARRAY_RANGE_REF:
14207 case REALPART_EXPR:
14208 case IMAGPART_EXPR:
14209 {
14210 tree obj, offset;
14211 HOST_WIDE_INT bitsize, bitpos, bytepos;
14212 enum machine_mode mode;
14213 int unsignedp, volatilep = 0;
14214
14215 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14216 &unsignedp, &volatilep, false);
14217
14218 gcc_assert (obj != loc);
14219
14220 list_ret = loc_list_from_tree (obj,
14221 want_address == 2
14222 && !bitpos && !offset ? 2 : 1);
14223 /* TODO: We can extract value of the small expression via shifting even
14224 for nonzero bitpos. */
14225 if (list_ret == 0)
14226 return 0;
14227 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14228 {
14229 expansion_failed (loc, NULL_RTX,
14230 "bitfield access");
14231 return 0;
14232 }
14233
14234 if (offset != NULL_TREE)
14235 {
14236 /* Variable offset. */
14237 list_ret1 = loc_list_from_tree (offset, 0);
14238 if (list_ret1 == 0)
14239 return 0;
14240 add_loc_list (&list_ret, list_ret1);
14241 if (!list_ret)
14242 return 0;
14243 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14244 }
14245
14246 bytepos = bitpos / BITS_PER_UNIT;
14247 if (bytepos > 0)
14248 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14249 else if (bytepos < 0)
14250 loc_list_plus_const (list_ret, bytepos);
14251
14252 have_address = 1;
14253 break;
14254 }
14255
14256 case INTEGER_CST:
14257 if ((want_address || !tree_fits_shwi_p (loc))
14258 && (ret = cst_pool_loc_descr (loc)))
14259 have_address = 1;
14260 else if (want_address == 2
14261 && tree_fits_shwi_p (loc)
14262 && (ret = address_of_int_loc_descriptor
14263 (int_size_in_bytes (TREE_TYPE (loc)),
14264 tree_to_shwi (loc))))
14265 have_address = 1;
14266 else if (tree_fits_shwi_p (loc))
14267 ret = int_loc_descriptor (tree_to_shwi (loc));
14268 else
14269 {
14270 expansion_failed (loc, NULL_RTX,
14271 "Integer operand is not host integer");
14272 return 0;
14273 }
14274 break;
14275
14276 case CONSTRUCTOR:
14277 case REAL_CST:
14278 case STRING_CST:
14279 case COMPLEX_CST:
14280 if ((ret = cst_pool_loc_descr (loc)))
14281 have_address = 1;
14282 else
14283 /* We can construct small constants here using int_loc_descriptor. */
14284 expansion_failed (loc, NULL_RTX,
14285 "constructor or constant not in constant pool");
14286 break;
14287
14288 case TRUTH_AND_EXPR:
14289 case TRUTH_ANDIF_EXPR:
14290 case BIT_AND_EXPR:
14291 op = DW_OP_and;
14292 goto do_binop;
14293
14294 case TRUTH_XOR_EXPR:
14295 case BIT_XOR_EXPR:
14296 op = DW_OP_xor;
14297 goto do_binop;
14298
14299 case TRUTH_OR_EXPR:
14300 case TRUTH_ORIF_EXPR:
14301 case BIT_IOR_EXPR:
14302 op = DW_OP_or;
14303 goto do_binop;
14304
14305 case FLOOR_DIV_EXPR:
14306 case CEIL_DIV_EXPR:
14307 case ROUND_DIV_EXPR:
14308 case TRUNC_DIV_EXPR:
14309 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14310 return 0;
14311 op = DW_OP_div;
14312 goto do_binop;
14313
14314 case MINUS_EXPR:
14315 op = DW_OP_minus;
14316 goto do_binop;
14317
14318 case FLOOR_MOD_EXPR:
14319 case CEIL_MOD_EXPR:
14320 case ROUND_MOD_EXPR:
14321 case TRUNC_MOD_EXPR:
14322 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14323 {
14324 op = DW_OP_mod;
14325 goto do_binop;
14326 }
14327 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14328 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14329 if (list_ret == 0 || list_ret1 == 0)
14330 return 0;
14331
14332 add_loc_list (&list_ret, list_ret1);
14333 if (list_ret == 0)
14334 return 0;
14335 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14336 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14337 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14338 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14339 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14340 break;
14341
14342 case MULT_EXPR:
14343 op = DW_OP_mul;
14344 goto do_binop;
14345
14346 case LSHIFT_EXPR:
14347 op = DW_OP_shl;
14348 goto do_binop;
14349
14350 case RSHIFT_EXPR:
14351 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14352 goto do_binop;
14353
14354 case POINTER_PLUS_EXPR:
14355 case PLUS_EXPR:
14356 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14357 {
14358 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14359 if (list_ret == 0)
14360 return 0;
14361
14362 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14363 break;
14364 }
14365
14366 op = DW_OP_plus;
14367 goto do_binop;
14368
14369 case LE_EXPR:
14370 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14371 return 0;
14372
14373 op = DW_OP_le;
14374 goto do_binop;
14375
14376 case GE_EXPR:
14377 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14378 return 0;
14379
14380 op = DW_OP_ge;
14381 goto do_binop;
14382
14383 case LT_EXPR:
14384 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14385 return 0;
14386
14387 op = DW_OP_lt;
14388 goto do_binop;
14389
14390 case GT_EXPR:
14391 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14392 return 0;
14393
14394 op = DW_OP_gt;
14395 goto do_binop;
14396
14397 case EQ_EXPR:
14398 op = DW_OP_eq;
14399 goto do_binop;
14400
14401 case NE_EXPR:
14402 op = DW_OP_ne;
14403 goto do_binop;
14404
14405 do_binop:
14406 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14407 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14408 if (list_ret == 0 || list_ret1 == 0)
14409 return 0;
14410
14411 add_loc_list (&list_ret, list_ret1);
14412 if (list_ret == 0)
14413 return 0;
14414 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14415 break;
14416
14417 case TRUTH_NOT_EXPR:
14418 case BIT_NOT_EXPR:
14419 op = DW_OP_not;
14420 goto do_unop;
14421
14422 case ABS_EXPR:
14423 op = DW_OP_abs;
14424 goto do_unop;
14425
14426 case NEGATE_EXPR:
14427 op = DW_OP_neg;
14428 goto do_unop;
14429
14430 do_unop:
14431 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14432 if (list_ret == 0)
14433 return 0;
14434
14435 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14436 break;
14437
14438 case MIN_EXPR:
14439 case MAX_EXPR:
14440 {
14441 const enum tree_code code =
14442 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14443
14444 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14445 build2 (code, integer_type_node,
14446 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14447 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14448 }
14449
14450 /* ... fall through ... */
14451
14452 case COND_EXPR:
14453 {
14454 dw_loc_descr_ref lhs
14455 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14456 dw_loc_list_ref rhs
14457 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14458 dw_loc_descr_ref bra_node, jump_node, tmp;
14459
14460 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14461 if (list_ret == 0 || lhs == 0 || rhs == 0)
14462 return 0;
14463
14464 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14465 add_loc_descr_to_each (list_ret, bra_node);
14466
14467 add_loc_list (&list_ret, rhs);
14468 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14469 add_loc_descr_to_each (list_ret, jump_node);
14470
14471 add_loc_descr_to_each (list_ret, lhs);
14472 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14473 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14474
14475 /* ??? Need a node to point the skip at. Use a nop. */
14476 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14477 add_loc_descr_to_each (list_ret, tmp);
14478 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14479 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14480 }
14481 break;
14482
14483 case FIX_TRUNC_EXPR:
14484 return 0;
14485
14486 default:
14487 /* Leave front-end specific codes as simply unknown. This comes
14488 up, for instance, with the C STMT_EXPR. */
14489 if ((unsigned int) TREE_CODE (loc)
14490 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14491 {
14492 expansion_failed (loc, NULL_RTX,
14493 "language specific tree node");
14494 return 0;
14495 }
14496
14497 #ifdef ENABLE_CHECKING
14498 /* Otherwise this is a generic code; we should just lists all of
14499 these explicitly. We forgot one. */
14500 gcc_unreachable ();
14501 #else
14502 /* In a release build, we want to degrade gracefully: better to
14503 generate incomplete debugging information than to crash. */
14504 return NULL;
14505 #endif
14506 }
14507
14508 if (!ret && !list_ret)
14509 return 0;
14510
14511 if (want_address == 2 && !have_address
14512 && (dwarf_version >= 4 || !dwarf_strict))
14513 {
14514 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14515 {
14516 expansion_failed (loc, NULL_RTX,
14517 "DWARF address size mismatch");
14518 return 0;
14519 }
14520 if (ret)
14521 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14522 else
14523 add_loc_descr_to_each (list_ret,
14524 new_loc_descr (DW_OP_stack_value, 0, 0));
14525 have_address = 1;
14526 }
14527 /* Show if we can't fill the request for an address. */
14528 if (want_address && !have_address)
14529 {
14530 expansion_failed (loc, NULL_RTX,
14531 "Want address and only have value");
14532 return 0;
14533 }
14534
14535 gcc_assert (!ret || !list_ret);
14536
14537 /* If we've got an address and don't want one, dereference. */
14538 if (!want_address && have_address)
14539 {
14540 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14541
14542 if (size > DWARF2_ADDR_SIZE || size == -1)
14543 {
14544 expansion_failed (loc, NULL_RTX,
14545 "DWARF address size mismatch");
14546 return 0;
14547 }
14548 else if (size == DWARF2_ADDR_SIZE)
14549 op = DW_OP_deref;
14550 else
14551 op = DW_OP_deref_size;
14552
14553 if (ret)
14554 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14555 else
14556 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14557 }
14558 if (ret)
14559 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14560
14561 return list_ret;
14562 }
14563
14564 /* Same as above but return only single location expression. */
14565 static dw_loc_descr_ref
14566 loc_descriptor_from_tree (tree loc, int want_address)
14567 {
14568 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14569 if (!ret)
14570 return NULL;
14571 if (ret->dw_loc_next)
14572 {
14573 expansion_failed (loc, NULL_RTX,
14574 "Location list where only loc descriptor needed");
14575 return NULL;
14576 }
14577 return ret->expr;
14578 }
14579
14580 /* Given a value, round it up to the lowest multiple of `boundary'
14581 which is not less than the value itself. */
14582
14583 static inline HOST_WIDE_INT
14584 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14585 {
14586 return (((value + boundary - 1) / boundary) * boundary);
14587 }
14588
14589 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14590 pointer to the declared type for the relevant field variable, or return
14591 `integer_type_node' if the given node turns out to be an
14592 ERROR_MARK node. */
14593
14594 static inline tree
14595 field_type (const_tree decl)
14596 {
14597 tree type;
14598
14599 if (TREE_CODE (decl) == ERROR_MARK)
14600 return integer_type_node;
14601
14602 type = DECL_BIT_FIELD_TYPE (decl);
14603 if (type == NULL_TREE)
14604 type = TREE_TYPE (decl);
14605
14606 return type;
14607 }
14608
14609 /* Given a pointer to a tree node, return the alignment in bits for
14610 it, or else return BITS_PER_WORD if the node actually turns out to
14611 be an ERROR_MARK node. */
14612
14613 static inline unsigned
14614 simple_type_align_in_bits (const_tree type)
14615 {
14616 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14617 }
14618
14619 static inline unsigned
14620 simple_decl_align_in_bits (const_tree decl)
14621 {
14622 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14623 }
14624
14625 /* Return the result of rounding T up to ALIGN. */
14626
14627 static inline double_int
14628 round_up_to_align (double_int t, unsigned int align)
14629 {
14630 double_int alignd = double_int::from_uhwi (align);
14631 t += alignd;
14632 t += double_int_minus_one;
14633 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14634 t *= alignd;
14635 return t;
14636 }
14637
14638 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14639 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14640 or return 0 if we are unable to determine what that offset is, either
14641 because the argument turns out to be a pointer to an ERROR_MARK node, or
14642 because the offset is actually variable. (We can't handle the latter case
14643 just yet). */
14644
14645 static HOST_WIDE_INT
14646 field_byte_offset (const_tree decl)
14647 {
14648 double_int object_offset_in_bits;
14649 double_int object_offset_in_bytes;
14650 double_int bitpos_int;
14651
14652 if (TREE_CODE (decl) == ERROR_MARK)
14653 return 0;
14654
14655 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14656
14657 /* We cannot yet cope with fields whose positions are variable, so
14658 for now, when we see such things, we simply return 0. Someday, we may
14659 be able to handle such cases, but it will be damn difficult. */
14660 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14661 return 0;
14662
14663 bitpos_int = tree_to_double_int (bit_position (decl));
14664
14665 #ifdef PCC_BITFIELD_TYPE_MATTERS
14666 if (PCC_BITFIELD_TYPE_MATTERS)
14667 {
14668 tree type;
14669 tree field_size_tree;
14670 double_int deepest_bitpos;
14671 double_int field_size_in_bits;
14672 unsigned int type_align_in_bits;
14673 unsigned int decl_align_in_bits;
14674 double_int type_size_in_bits;
14675
14676 type = field_type (decl);
14677 type_size_in_bits = double_int_type_size_in_bits (type);
14678 type_align_in_bits = simple_type_align_in_bits (type);
14679
14680 field_size_tree = DECL_SIZE (decl);
14681
14682 /* The size could be unspecified if there was an error, or for
14683 a flexible array member. */
14684 if (!field_size_tree)
14685 field_size_tree = bitsize_zero_node;
14686
14687 /* If the size of the field is not constant, use the type size. */
14688 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14689 field_size_in_bits = tree_to_double_int (field_size_tree);
14690 else
14691 field_size_in_bits = type_size_in_bits;
14692
14693 decl_align_in_bits = simple_decl_align_in_bits (decl);
14694
14695 /* The GCC front-end doesn't make any attempt to keep track of the
14696 starting bit offset (relative to the start of the containing
14697 structure type) of the hypothetical "containing object" for a
14698 bit-field. Thus, when computing the byte offset value for the
14699 start of the "containing object" of a bit-field, we must deduce
14700 this information on our own. This can be rather tricky to do in
14701 some cases. For example, handling the following structure type
14702 definition when compiling for an i386/i486 target (which only
14703 aligns long long's to 32-bit boundaries) can be very tricky:
14704
14705 struct S { int field1; long long field2:31; };
14706
14707 Fortunately, there is a simple rule-of-thumb which can be used
14708 in such cases. When compiling for an i386/i486, GCC will
14709 allocate 8 bytes for the structure shown above. It decides to
14710 do this based upon one simple rule for bit-field allocation.
14711 GCC allocates each "containing object" for each bit-field at
14712 the first (i.e. lowest addressed) legitimate alignment boundary
14713 (based upon the required minimum alignment for the declared
14714 type of the field) which it can possibly use, subject to the
14715 condition that there is still enough available space remaining
14716 in the containing object (when allocated at the selected point)
14717 to fully accommodate all of the bits of the bit-field itself.
14718
14719 This simple rule makes it obvious why GCC allocates 8 bytes for
14720 each object of the structure type shown above. When looking
14721 for a place to allocate the "containing object" for `field2',
14722 the compiler simply tries to allocate a 64-bit "containing
14723 object" at each successive 32-bit boundary (starting at zero)
14724 until it finds a place to allocate that 64- bit field such that
14725 at least 31 contiguous (and previously unallocated) bits remain
14726 within that selected 64 bit field. (As it turns out, for the
14727 example above, the compiler finds it is OK to allocate the
14728 "containing object" 64-bit field at bit-offset zero within the
14729 structure type.)
14730
14731 Here we attempt to work backwards from the limited set of facts
14732 we're given, and we try to deduce from those facts, where GCC
14733 must have believed that the containing object started (within
14734 the structure type). The value we deduce is then used (by the
14735 callers of this routine) to generate DW_AT_location and
14736 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14737 the case of DW_AT_location, regular fields as well). */
14738
14739 /* Figure out the bit-distance from the start of the structure to
14740 the "deepest" bit of the bit-field. */
14741 deepest_bitpos = bitpos_int + field_size_in_bits;
14742
14743 /* This is the tricky part. Use some fancy footwork to deduce
14744 where the lowest addressed bit of the containing object must
14745 be. */
14746 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14747
14748 /* Round up to type_align by default. This works best for
14749 bitfields. */
14750 object_offset_in_bits
14751 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14752
14753 if (object_offset_in_bits.ugt (bitpos_int))
14754 {
14755 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14756
14757 /* Round up to decl_align instead. */
14758 object_offset_in_bits
14759 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14760 }
14761 }
14762 else
14763 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14764 object_offset_in_bits = bitpos_int;
14765
14766 object_offset_in_bytes
14767 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14768 true, TRUNC_DIV_EXPR);
14769 return object_offset_in_bytes.to_shwi ();
14770 }
14771 \f
14772 /* The following routines define various Dwarf attributes and any data
14773 associated with them. */
14774
14775 /* Add a location description attribute value to a DIE.
14776
14777 This emits location attributes suitable for whole variables and
14778 whole parameters. Note that the location attributes for struct fields are
14779 generated by the routine `data_member_location_attribute' below. */
14780
14781 static inline void
14782 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14783 dw_loc_list_ref descr)
14784 {
14785 if (descr == 0)
14786 return;
14787 if (single_element_loc_list_p (descr))
14788 add_AT_loc (die, attr_kind, descr->expr);
14789 else
14790 add_AT_loc_list (die, attr_kind, descr);
14791 }
14792
14793 /* Add DW_AT_accessibility attribute to DIE if needed. */
14794
14795 static void
14796 add_accessibility_attribute (dw_die_ref die, tree decl)
14797 {
14798 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14799 children, otherwise the default is DW_ACCESS_public. In DWARF2
14800 the default has always been DW_ACCESS_public. */
14801 if (TREE_PROTECTED (decl))
14802 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14803 else if (TREE_PRIVATE (decl))
14804 {
14805 if (dwarf_version == 2
14806 || die->die_parent == NULL
14807 || die->die_parent->die_tag != DW_TAG_class_type)
14808 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14809 }
14810 else if (dwarf_version > 2
14811 && die->die_parent
14812 && die->die_parent->die_tag == DW_TAG_class_type)
14813 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14814 }
14815
14816 /* Attach the specialized form of location attribute used for data members of
14817 struct and union types. In the special case of a FIELD_DECL node which
14818 represents a bit-field, the "offset" part of this special location
14819 descriptor must indicate the distance in bytes from the lowest-addressed
14820 byte of the containing struct or union type to the lowest-addressed byte of
14821 the "containing object" for the bit-field. (See the `field_byte_offset'
14822 function above).
14823
14824 For any given bit-field, the "containing object" is a hypothetical object
14825 (of some integral or enum type) within which the given bit-field lives. The
14826 type of this hypothetical "containing object" is always the same as the
14827 declared type of the individual bit-field itself (for GCC anyway... the
14828 DWARF spec doesn't actually mandate this). Note that it is the size (in
14829 bytes) of the hypothetical "containing object" which will be given in the
14830 DW_AT_byte_size attribute for this bit-field. (See the
14831 `byte_size_attribute' function below.) It is also used when calculating the
14832 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14833 function below.) */
14834
14835 static void
14836 add_data_member_location_attribute (dw_die_ref die, tree decl)
14837 {
14838 HOST_WIDE_INT offset;
14839 dw_loc_descr_ref loc_descr = 0;
14840
14841 if (TREE_CODE (decl) == TREE_BINFO)
14842 {
14843 /* We're working on the TAG_inheritance for a base class. */
14844 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14845 {
14846 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14847 aren't at a fixed offset from all (sub)objects of the same
14848 type. We need to extract the appropriate offset from our
14849 vtable. The following dwarf expression means
14850
14851 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14852
14853 This is specific to the V3 ABI, of course. */
14854
14855 dw_loc_descr_ref tmp;
14856
14857 /* Make a copy of the object address. */
14858 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14859 add_loc_descr (&loc_descr, tmp);
14860
14861 /* Extract the vtable address. */
14862 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14863 add_loc_descr (&loc_descr, tmp);
14864
14865 /* Calculate the address of the offset. */
14866 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
14867 gcc_assert (offset < 0);
14868
14869 tmp = int_loc_descriptor (-offset);
14870 add_loc_descr (&loc_descr, tmp);
14871 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14872 add_loc_descr (&loc_descr, tmp);
14873
14874 /* Extract the offset. */
14875 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14876 add_loc_descr (&loc_descr, tmp);
14877
14878 /* Add it to the object address. */
14879 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14880 add_loc_descr (&loc_descr, tmp);
14881 }
14882 else
14883 offset = tree_to_shwi (BINFO_OFFSET (decl));
14884 }
14885 else
14886 offset = field_byte_offset (decl);
14887
14888 if (! loc_descr)
14889 {
14890 if (dwarf_version > 2)
14891 {
14892 /* Don't need to output a location expression, just the constant. */
14893 if (offset < 0)
14894 add_AT_int (die, DW_AT_data_member_location, offset);
14895 else
14896 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14897 return;
14898 }
14899 else
14900 {
14901 enum dwarf_location_atom op;
14902
14903 /* The DWARF2 standard says that we should assume that the structure
14904 address is already on the stack, so we can specify a structure
14905 field address by using DW_OP_plus_uconst. */
14906 op = DW_OP_plus_uconst;
14907 loc_descr = new_loc_descr (op, offset, 0);
14908 }
14909 }
14910
14911 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14912 }
14913
14914 /* Writes integer values to dw_vec_const array. */
14915
14916 static void
14917 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14918 {
14919 while (size != 0)
14920 {
14921 *dest++ = val & 0xff;
14922 val >>= 8;
14923 --size;
14924 }
14925 }
14926
14927 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14928
14929 static HOST_WIDE_INT
14930 extract_int (const unsigned char *src, unsigned int size)
14931 {
14932 HOST_WIDE_INT val = 0;
14933
14934 src += size;
14935 while (size != 0)
14936 {
14937 val <<= 8;
14938 val |= *--src & 0xff;
14939 --size;
14940 }
14941 return val;
14942 }
14943
14944 /* Writes double_int values to dw_vec_const array. */
14945
14946 static void
14947 insert_double (double_int val, unsigned char *dest)
14948 {
14949 unsigned char *p0 = dest;
14950 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14951
14952 if (WORDS_BIG_ENDIAN)
14953 {
14954 p0 = p1;
14955 p1 = dest;
14956 }
14957
14958 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14959 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14960 }
14961
14962 /* Writes floating point values to dw_vec_const array. */
14963
14964 static void
14965 insert_float (const_rtx rtl, unsigned char *array)
14966 {
14967 REAL_VALUE_TYPE rv;
14968 long val[4];
14969 int i;
14970
14971 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14972 real_to_target (val, &rv, GET_MODE (rtl));
14973
14974 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14975 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14976 {
14977 insert_int (val[i], 4, array);
14978 array += 4;
14979 }
14980 }
14981
14982 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14983 does not have a "location" either in memory or in a register. These
14984 things can arise in GNU C when a constant is passed as an actual parameter
14985 to an inlined function. They can also arise in C++ where declared
14986 constants do not necessarily get memory "homes". */
14987
14988 static bool
14989 add_const_value_attribute (dw_die_ref die, rtx rtl)
14990 {
14991 switch (GET_CODE (rtl))
14992 {
14993 case CONST_INT:
14994 {
14995 HOST_WIDE_INT val = INTVAL (rtl);
14996
14997 if (val < 0)
14998 add_AT_int (die, DW_AT_const_value, val);
14999 else
15000 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15001 }
15002 return true;
15003
15004 case CONST_DOUBLE:
15005 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15006 floating-point constant. A CONST_DOUBLE is used whenever the
15007 constant requires more than one word in order to be adequately
15008 represented. */
15009 {
15010 enum machine_mode mode = GET_MODE (rtl);
15011
15012 if (SCALAR_FLOAT_MODE_P (mode))
15013 {
15014 unsigned int length = GET_MODE_SIZE (mode);
15015 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15016
15017 insert_float (rtl, array);
15018 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15019 }
15020 else
15021 add_AT_double (die, DW_AT_const_value,
15022 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15023 }
15024 return true;
15025
15026 case CONST_VECTOR:
15027 {
15028 enum machine_mode mode = GET_MODE (rtl);
15029 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15030 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15031 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15032 (length * elt_size);
15033 unsigned int i;
15034 unsigned char *p;
15035
15036 switch (GET_MODE_CLASS (mode))
15037 {
15038 case MODE_VECTOR_INT:
15039 for (i = 0, p = array; i < length; i++, p += elt_size)
15040 {
15041 rtx elt = CONST_VECTOR_ELT (rtl, i);
15042 double_int val = rtx_to_double_int (elt);
15043
15044 if (elt_size <= sizeof (HOST_WIDE_INT))
15045 insert_int (val.to_shwi (), elt_size, p);
15046 else
15047 {
15048 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15049 insert_double (val, p);
15050 }
15051 }
15052 break;
15053
15054 case MODE_VECTOR_FLOAT:
15055 for (i = 0, p = array; i < length; i++, p += elt_size)
15056 {
15057 rtx elt = CONST_VECTOR_ELT (rtl, i);
15058 insert_float (elt, p);
15059 }
15060 break;
15061
15062 default:
15063 gcc_unreachable ();
15064 }
15065
15066 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15067 }
15068 return true;
15069
15070 case CONST_STRING:
15071 if (dwarf_version >= 4 || !dwarf_strict)
15072 {
15073 dw_loc_descr_ref loc_result;
15074 resolve_one_addr (&rtl, NULL);
15075 rtl_addr:
15076 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15077 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15078 add_AT_loc (die, DW_AT_location, loc_result);
15079 vec_safe_push (used_rtx_array, rtl);
15080 return true;
15081 }
15082 return false;
15083
15084 case CONST:
15085 if (CONSTANT_P (XEXP (rtl, 0)))
15086 return add_const_value_attribute (die, XEXP (rtl, 0));
15087 /* FALLTHROUGH */
15088 case SYMBOL_REF:
15089 if (!const_ok_for_output (rtl))
15090 return false;
15091 case LABEL_REF:
15092 if (dwarf_version >= 4 || !dwarf_strict)
15093 goto rtl_addr;
15094 return false;
15095
15096 case PLUS:
15097 /* In cases where an inlined instance of an inline function is passed
15098 the address of an `auto' variable (which is local to the caller) we
15099 can get a situation where the DECL_RTL of the artificial local
15100 variable (for the inlining) which acts as a stand-in for the
15101 corresponding formal parameter (of the inline function) will look
15102 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15103 exactly a compile-time constant expression, but it isn't the address
15104 of the (artificial) local variable either. Rather, it represents the
15105 *value* which the artificial local variable always has during its
15106 lifetime. We currently have no way to represent such quasi-constant
15107 values in Dwarf, so for now we just punt and generate nothing. */
15108 return false;
15109
15110 case HIGH:
15111 case CONST_FIXED:
15112 return false;
15113
15114 case MEM:
15115 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15116 && MEM_READONLY_P (rtl)
15117 && GET_MODE (rtl) == BLKmode)
15118 {
15119 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15120 return true;
15121 }
15122 return false;
15123
15124 default:
15125 /* No other kinds of rtx should be possible here. */
15126 gcc_unreachable ();
15127 }
15128 return false;
15129 }
15130
15131 /* Determine whether the evaluation of EXPR references any variables
15132 or functions which aren't otherwise used (and therefore may not be
15133 output). */
15134 static tree
15135 reference_to_unused (tree * tp, int * walk_subtrees,
15136 void * data ATTRIBUTE_UNUSED)
15137 {
15138 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15139 *walk_subtrees = 0;
15140
15141 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15142 && ! TREE_ASM_WRITTEN (*tp))
15143 return *tp;
15144 /* ??? The C++ FE emits debug information for using decls, so
15145 putting gcc_unreachable here falls over. See PR31899. For now
15146 be conservative. */
15147 else if (!cgraph_global_info_ready
15148 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15149 return *tp;
15150 else if (TREE_CODE (*tp) == VAR_DECL)
15151 {
15152 struct varpool_node *node = varpool_get_node (*tp);
15153 if (!node || !node->definition)
15154 return *tp;
15155 }
15156 else if (TREE_CODE (*tp) == FUNCTION_DECL
15157 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15158 {
15159 /* The call graph machinery must have finished analyzing,
15160 optimizing and gimplifying the CU by now.
15161 So if *TP has no call graph node associated
15162 to it, it means *TP will not be emitted. */
15163 if (!cgraph_get_node (*tp))
15164 return *tp;
15165 }
15166 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15167 return *tp;
15168
15169 return NULL_TREE;
15170 }
15171
15172 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15173 for use in a later add_const_value_attribute call. */
15174
15175 static rtx
15176 rtl_for_decl_init (tree init, tree type)
15177 {
15178 rtx rtl = NULL_RTX;
15179
15180 STRIP_NOPS (init);
15181
15182 /* If a variable is initialized with a string constant without embedded
15183 zeros, build CONST_STRING. */
15184 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15185 {
15186 tree enttype = TREE_TYPE (type);
15187 tree domain = TYPE_DOMAIN (type);
15188 enum machine_mode mode = TYPE_MODE (enttype);
15189
15190 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15191 && domain
15192 && integer_zerop (TYPE_MIN_VALUE (domain))
15193 && compare_tree_int (TYPE_MAX_VALUE (domain),
15194 TREE_STRING_LENGTH (init) - 1) == 0
15195 && ((size_t) TREE_STRING_LENGTH (init)
15196 == strlen (TREE_STRING_POINTER (init)) + 1))
15197 {
15198 rtl = gen_rtx_CONST_STRING (VOIDmode,
15199 ggc_strdup (TREE_STRING_POINTER (init)));
15200 rtl = gen_rtx_MEM (BLKmode, rtl);
15201 MEM_READONLY_P (rtl) = 1;
15202 }
15203 }
15204 /* Other aggregates, and complex values, could be represented using
15205 CONCAT: FIXME! */
15206 else if (AGGREGATE_TYPE_P (type)
15207 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15208 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15209 || TREE_CODE (type) == COMPLEX_TYPE)
15210 ;
15211 /* Vectors only work if their mode is supported by the target.
15212 FIXME: generic vectors ought to work too. */
15213 else if (TREE_CODE (type) == VECTOR_TYPE
15214 && !VECTOR_MODE_P (TYPE_MODE (type)))
15215 ;
15216 /* If the initializer is something that we know will expand into an
15217 immediate RTL constant, expand it now. We must be careful not to
15218 reference variables which won't be output. */
15219 else if (initializer_constant_valid_p (init, type)
15220 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15221 {
15222 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15223 possible. */
15224 if (TREE_CODE (type) == VECTOR_TYPE)
15225 switch (TREE_CODE (init))
15226 {
15227 case VECTOR_CST:
15228 break;
15229 case CONSTRUCTOR:
15230 if (TREE_CONSTANT (init))
15231 {
15232 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15233 bool constant_p = true;
15234 tree value;
15235 unsigned HOST_WIDE_INT ix;
15236
15237 /* Even when ctor is constant, it might contain non-*_CST
15238 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15239 belong into VECTOR_CST nodes. */
15240 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15241 if (!CONSTANT_CLASS_P (value))
15242 {
15243 constant_p = false;
15244 break;
15245 }
15246
15247 if (constant_p)
15248 {
15249 init = build_vector_from_ctor (type, elts);
15250 break;
15251 }
15252 }
15253 /* FALLTHRU */
15254
15255 default:
15256 return NULL;
15257 }
15258
15259 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15260
15261 /* If expand_expr returns a MEM, it wasn't immediate. */
15262 gcc_assert (!rtl || !MEM_P (rtl));
15263 }
15264
15265 return rtl;
15266 }
15267
15268 /* Generate RTL for the variable DECL to represent its location. */
15269
15270 static rtx
15271 rtl_for_decl_location (tree decl)
15272 {
15273 rtx rtl;
15274
15275 /* Here we have to decide where we are going to say the parameter "lives"
15276 (as far as the debugger is concerned). We only have a couple of
15277 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15278
15279 DECL_RTL normally indicates where the parameter lives during most of the
15280 activation of the function. If optimization is enabled however, this
15281 could be either NULL or else a pseudo-reg. Both of those cases indicate
15282 that the parameter doesn't really live anywhere (as far as the code
15283 generation parts of GCC are concerned) during most of the function's
15284 activation. That will happen (for example) if the parameter is never
15285 referenced within the function.
15286
15287 We could just generate a location descriptor here for all non-NULL
15288 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15289 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15290 where DECL_RTL is NULL or is a pseudo-reg.
15291
15292 Note however that we can only get away with using DECL_INCOMING_RTL as
15293 a backup substitute for DECL_RTL in certain limited cases. In cases
15294 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15295 we can be sure that the parameter was passed using the same type as it is
15296 declared to have within the function, and that its DECL_INCOMING_RTL
15297 points us to a place where a value of that type is passed.
15298
15299 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15300 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15301 because in these cases DECL_INCOMING_RTL points us to a value of some
15302 type which is *different* from the type of the parameter itself. Thus,
15303 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15304 such cases, the debugger would end up (for example) trying to fetch a
15305 `float' from a place which actually contains the first part of a
15306 `double'. That would lead to really incorrect and confusing
15307 output at debug-time.
15308
15309 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15310 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15311 are a couple of exceptions however. On little-endian machines we can
15312 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15313 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15314 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15315 when (on a little-endian machine) a non-prototyped function has a
15316 parameter declared to be of type `short' or `char'. In such cases,
15317 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15318 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15319 passed `int' value. If the debugger then uses that address to fetch
15320 a `short' or a `char' (on a little-endian machine) the result will be
15321 the correct data, so we allow for such exceptional cases below.
15322
15323 Note that our goal here is to describe the place where the given formal
15324 parameter lives during most of the function's activation (i.e. between the
15325 end of the prologue and the start of the epilogue). We'll do that as best
15326 as we can. Note however that if the given formal parameter is modified
15327 sometime during the execution of the function, then a stack backtrace (at
15328 debug-time) will show the function as having been called with the *new*
15329 value rather than the value which was originally passed in. This happens
15330 rarely enough that it is not a major problem, but it *is* a problem, and
15331 I'd like to fix it.
15332
15333 A future version of dwarf2out.c may generate two additional attributes for
15334 any given DW_TAG_formal_parameter DIE which will describe the "passed
15335 type" and the "passed location" for the given formal parameter in addition
15336 to the attributes we now generate to indicate the "declared type" and the
15337 "active location" for each parameter. This additional set of attributes
15338 could be used by debuggers for stack backtraces. Separately, note that
15339 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15340 This happens (for example) for inlined-instances of inline function formal
15341 parameters which are never referenced. This really shouldn't be
15342 happening. All PARM_DECL nodes should get valid non-NULL
15343 DECL_INCOMING_RTL values. FIXME. */
15344
15345 /* Use DECL_RTL as the "location" unless we find something better. */
15346 rtl = DECL_RTL_IF_SET (decl);
15347
15348 /* When generating abstract instances, ignore everything except
15349 constants, symbols living in memory, and symbols living in
15350 fixed registers. */
15351 if (! reload_completed)
15352 {
15353 if (rtl
15354 && (CONSTANT_P (rtl)
15355 || (MEM_P (rtl)
15356 && CONSTANT_P (XEXP (rtl, 0)))
15357 || (REG_P (rtl)
15358 && TREE_CODE (decl) == VAR_DECL
15359 && TREE_STATIC (decl))))
15360 {
15361 rtl = targetm.delegitimize_address (rtl);
15362 return rtl;
15363 }
15364 rtl = NULL_RTX;
15365 }
15366 else if (TREE_CODE (decl) == PARM_DECL)
15367 {
15368 if (rtl == NULL_RTX
15369 || is_pseudo_reg (rtl)
15370 || (MEM_P (rtl)
15371 && is_pseudo_reg (XEXP (rtl, 0))
15372 && DECL_INCOMING_RTL (decl)
15373 && MEM_P (DECL_INCOMING_RTL (decl))
15374 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15375 {
15376 tree declared_type = TREE_TYPE (decl);
15377 tree passed_type = DECL_ARG_TYPE (decl);
15378 enum machine_mode dmode = TYPE_MODE (declared_type);
15379 enum machine_mode pmode = TYPE_MODE (passed_type);
15380
15381 /* This decl represents a formal parameter which was optimized out.
15382 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15383 all cases where (rtl == NULL_RTX) just below. */
15384 if (dmode == pmode)
15385 rtl = DECL_INCOMING_RTL (decl);
15386 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15387 && SCALAR_INT_MODE_P (dmode)
15388 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15389 && DECL_INCOMING_RTL (decl))
15390 {
15391 rtx inc = DECL_INCOMING_RTL (decl);
15392 if (REG_P (inc))
15393 rtl = inc;
15394 else if (MEM_P (inc))
15395 {
15396 if (BYTES_BIG_ENDIAN)
15397 rtl = adjust_address_nv (inc, dmode,
15398 GET_MODE_SIZE (pmode)
15399 - GET_MODE_SIZE (dmode));
15400 else
15401 rtl = inc;
15402 }
15403 }
15404 }
15405
15406 /* If the parm was passed in registers, but lives on the stack, then
15407 make a big endian correction if the mode of the type of the
15408 parameter is not the same as the mode of the rtl. */
15409 /* ??? This is the same series of checks that are made in dbxout.c before
15410 we reach the big endian correction code there. It isn't clear if all
15411 of these checks are necessary here, but keeping them all is the safe
15412 thing to do. */
15413 else if (MEM_P (rtl)
15414 && XEXP (rtl, 0) != const0_rtx
15415 && ! CONSTANT_P (XEXP (rtl, 0))
15416 /* Not passed in memory. */
15417 && !MEM_P (DECL_INCOMING_RTL (decl))
15418 /* Not passed by invisible reference. */
15419 && (!REG_P (XEXP (rtl, 0))
15420 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15421 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15422 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15423 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15424 #endif
15425 )
15426 /* Big endian correction check. */
15427 && BYTES_BIG_ENDIAN
15428 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15429 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15430 < UNITS_PER_WORD))
15431 {
15432 enum machine_mode addr_mode = get_address_mode (rtl);
15433 int offset = (UNITS_PER_WORD
15434 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15435
15436 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15437 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15438 }
15439 }
15440 else if (TREE_CODE (decl) == VAR_DECL
15441 && rtl
15442 && MEM_P (rtl)
15443 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15444 && BYTES_BIG_ENDIAN)
15445 {
15446 enum machine_mode addr_mode = get_address_mode (rtl);
15447 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15448 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15449
15450 /* If a variable is declared "register" yet is smaller than
15451 a register, then if we store the variable to memory, it
15452 looks like we're storing a register-sized value, when in
15453 fact we are not. We need to adjust the offset of the
15454 storage location to reflect the actual value's bytes,
15455 else gdb will not be able to display it. */
15456 if (rsize > dsize)
15457 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15458 plus_constant (addr_mode, XEXP (rtl, 0),
15459 rsize - dsize));
15460 }
15461
15462 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15463 and will have been substituted directly into all expressions that use it.
15464 C does not have such a concept, but C++ and other languages do. */
15465 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15466 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15467
15468 if (rtl)
15469 rtl = targetm.delegitimize_address (rtl);
15470
15471 /* If we don't look past the constant pool, we risk emitting a
15472 reference to a constant pool entry that isn't referenced from
15473 code, and thus is not emitted. */
15474 if (rtl)
15475 rtl = avoid_constant_pool_reference (rtl);
15476
15477 /* Try harder to get a rtl. If this symbol ends up not being emitted
15478 in the current CU, resolve_addr will remove the expression referencing
15479 it. */
15480 if (rtl == NULL_RTX
15481 && TREE_CODE (decl) == VAR_DECL
15482 && !DECL_EXTERNAL (decl)
15483 && TREE_STATIC (decl)
15484 && DECL_NAME (decl)
15485 && !DECL_HARD_REGISTER (decl)
15486 && DECL_MODE (decl) != VOIDmode)
15487 {
15488 rtl = make_decl_rtl_for_debug (decl);
15489 if (!MEM_P (rtl)
15490 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15491 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15492 rtl = NULL_RTX;
15493 }
15494
15495 return rtl;
15496 }
15497
15498 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15499 returned. If so, the decl for the COMMON block is returned, and the
15500 value is the offset into the common block for the symbol. */
15501
15502 static tree
15503 fortran_common (tree decl, HOST_WIDE_INT *value)
15504 {
15505 tree val_expr, cvar;
15506 enum machine_mode mode;
15507 HOST_WIDE_INT bitsize, bitpos;
15508 tree offset;
15509 int unsignedp, volatilep = 0;
15510
15511 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15512 it does not have a value (the offset into the common area), or if it
15513 is thread local (as opposed to global) then it isn't common, and shouldn't
15514 be handled as such. */
15515 if (TREE_CODE (decl) != VAR_DECL
15516 || !TREE_STATIC (decl)
15517 || !DECL_HAS_VALUE_EXPR_P (decl)
15518 || !is_fortran ())
15519 return NULL_TREE;
15520
15521 val_expr = DECL_VALUE_EXPR (decl);
15522 if (TREE_CODE (val_expr) != COMPONENT_REF)
15523 return NULL_TREE;
15524
15525 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15526 &mode, &unsignedp, &volatilep, true);
15527
15528 if (cvar == NULL_TREE
15529 || TREE_CODE (cvar) != VAR_DECL
15530 || DECL_ARTIFICIAL (cvar)
15531 || !TREE_PUBLIC (cvar))
15532 return NULL_TREE;
15533
15534 *value = 0;
15535 if (offset != NULL)
15536 {
15537 if (!tree_fits_shwi_p (offset))
15538 return NULL_TREE;
15539 *value = tree_to_shwi (offset);
15540 }
15541 if (bitpos != 0)
15542 *value += bitpos / BITS_PER_UNIT;
15543
15544 return cvar;
15545 }
15546
15547 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15548 data attribute for a variable or a parameter. We generate the
15549 DW_AT_const_value attribute only in those cases where the given variable
15550 or parameter does not have a true "location" either in memory or in a
15551 register. This can happen (for example) when a constant is passed as an
15552 actual argument in a call to an inline function. (It's possible that
15553 these things can crop up in other ways also.) Note that one type of
15554 constant value which can be passed into an inlined function is a constant
15555 pointer. This can happen for example if an actual argument in an inlined
15556 function call evaluates to a compile-time constant address.
15557
15558 CACHE_P is true if it is worth caching the location list for DECL,
15559 so that future calls can reuse it rather than regenerate it from scratch.
15560 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15561 since we will need to refer to them each time the function is inlined. */
15562
15563 static bool
15564 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15565 enum dwarf_attribute attr)
15566 {
15567 rtx rtl;
15568 dw_loc_list_ref list;
15569 var_loc_list *loc_list;
15570 cached_dw_loc_list *cache;
15571 void **slot;
15572
15573 if (TREE_CODE (decl) == ERROR_MARK)
15574 return false;
15575
15576 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15577 || TREE_CODE (decl) == RESULT_DECL);
15578
15579 /* Try to get some constant RTL for this decl, and use that as the value of
15580 the location. */
15581
15582 rtl = rtl_for_decl_location (decl);
15583 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15584 && add_const_value_attribute (die, rtl))
15585 return true;
15586
15587 /* See if we have single element location list that is equivalent to
15588 a constant value. That way we are better to use add_const_value_attribute
15589 rather than expanding constant value equivalent. */
15590 loc_list = lookup_decl_loc (decl);
15591 if (loc_list
15592 && loc_list->first
15593 && loc_list->first->next == NULL
15594 && NOTE_P (loc_list->first->loc)
15595 && NOTE_VAR_LOCATION (loc_list->first->loc)
15596 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15597 {
15598 struct var_loc_node *node;
15599
15600 node = loc_list->first;
15601 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15602 if (GET_CODE (rtl) == EXPR_LIST)
15603 rtl = XEXP (rtl, 0);
15604 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15605 && add_const_value_attribute (die, rtl))
15606 return true;
15607 }
15608 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15609 list several times. See if we've already cached the contents. */
15610 list = NULL;
15611 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15612 cache_p = false;
15613 if (cache_p)
15614 {
15615 cache = (cached_dw_loc_list *)
15616 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15617 if (cache)
15618 list = cache->loc_list;
15619 }
15620 if (list == NULL)
15621 {
15622 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15623 /* It is usually worth caching this result if the decl is from
15624 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15625 if (cache_p && list && list->dw_loc_next)
15626 {
15627 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15628 DECL_UID (decl), INSERT);
15629 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15630 cache->decl_id = DECL_UID (decl);
15631 cache->loc_list = list;
15632 *slot = cache;
15633 }
15634 }
15635 if (list)
15636 {
15637 add_AT_location_description (die, attr, list);
15638 return true;
15639 }
15640 /* None of that worked, so it must not really have a location;
15641 try adding a constant value attribute from the DECL_INITIAL. */
15642 return tree_add_const_value_attribute_for_decl (die, decl);
15643 }
15644
15645 /* Add VARIABLE and DIE into deferred locations list. */
15646
15647 static void
15648 defer_location (tree variable, dw_die_ref die)
15649 {
15650 deferred_locations entry;
15651 entry.variable = variable;
15652 entry.die = die;
15653 vec_safe_push (deferred_locations_list, entry);
15654 }
15655
15656 /* Helper function for tree_add_const_value_attribute. Natively encode
15657 initializer INIT into an array. Return true if successful. */
15658
15659 static bool
15660 native_encode_initializer (tree init, unsigned char *array, int size)
15661 {
15662 tree type;
15663
15664 if (init == NULL_TREE)
15665 return false;
15666
15667 STRIP_NOPS (init);
15668 switch (TREE_CODE (init))
15669 {
15670 case STRING_CST:
15671 type = TREE_TYPE (init);
15672 if (TREE_CODE (type) == ARRAY_TYPE)
15673 {
15674 tree enttype = TREE_TYPE (type);
15675 enum machine_mode mode = TYPE_MODE (enttype);
15676
15677 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15678 return false;
15679 if (int_size_in_bytes (type) != size)
15680 return false;
15681 if (size > TREE_STRING_LENGTH (init))
15682 {
15683 memcpy (array, TREE_STRING_POINTER (init),
15684 TREE_STRING_LENGTH (init));
15685 memset (array + TREE_STRING_LENGTH (init),
15686 '\0', size - TREE_STRING_LENGTH (init));
15687 }
15688 else
15689 memcpy (array, TREE_STRING_POINTER (init), size);
15690 return true;
15691 }
15692 return false;
15693 case CONSTRUCTOR:
15694 type = TREE_TYPE (init);
15695 if (int_size_in_bytes (type) != size)
15696 return false;
15697 if (TREE_CODE (type) == ARRAY_TYPE)
15698 {
15699 HOST_WIDE_INT min_index;
15700 unsigned HOST_WIDE_INT cnt;
15701 int curpos = 0, fieldsize;
15702 constructor_elt *ce;
15703
15704 if (TYPE_DOMAIN (type) == NULL_TREE
15705 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15706 return false;
15707
15708 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15709 if (fieldsize <= 0)
15710 return false;
15711
15712 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15713 memset (array, '\0', size);
15714 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15715 {
15716 tree val = ce->value;
15717 tree index = ce->index;
15718 int pos = curpos;
15719 if (index && TREE_CODE (index) == RANGE_EXPR)
15720 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15721 * fieldsize;
15722 else if (index)
15723 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15724
15725 if (val)
15726 {
15727 STRIP_NOPS (val);
15728 if (!native_encode_initializer (val, array + pos, fieldsize))
15729 return false;
15730 }
15731 curpos = pos + fieldsize;
15732 if (index && TREE_CODE (index) == RANGE_EXPR)
15733 {
15734 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15735 - tree_to_shwi (TREE_OPERAND (index, 0));
15736 while (count-- > 0)
15737 {
15738 if (val)
15739 memcpy (array + curpos, array + pos, fieldsize);
15740 curpos += fieldsize;
15741 }
15742 }
15743 gcc_assert (curpos <= size);
15744 }
15745 return true;
15746 }
15747 else if (TREE_CODE (type) == RECORD_TYPE
15748 || TREE_CODE (type) == UNION_TYPE)
15749 {
15750 tree field = NULL_TREE;
15751 unsigned HOST_WIDE_INT cnt;
15752 constructor_elt *ce;
15753
15754 if (int_size_in_bytes (type) != size)
15755 return false;
15756
15757 if (TREE_CODE (type) == RECORD_TYPE)
15758 field = TYPE_FIELDS (type);
15759
15760 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15761 {
15762 tree val = ce->value;
15763 int pos, fieldsize;
15764
15765 if (ce->index != 0)
15766 field = ce->index;
15767
15768 if (val)
15769 STRIP_NOPS (val);
15770
15771 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15772 return false;
15773
15774 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15775 && TYPE_DOMAIN (TREE_TYPE (field))
15776 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15777 return false;
15778 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15779 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15780 return false;
15781 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15782 pos = int_byte_position (field);
15783 gcc_assert (pos + fieldsize <= size);
15784 if (val
15785 && !native_encode_initializer (val, array + pos, fieldsize))
15786 return false;
15787 }
15788 return true;
15789 }
15790 return false;
15791 case VIEW_CONVERT_EXPR:
15792 case NON_LVALUE_EXPR:
15793 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15794 default:
15795 return native_encode_expr (init, array, size) == size;
15796 }
15797 }
15798
15799 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15800 attribute is the const value T. */
15801
15802 static bool
15803 tree_add_const_value_attribute (dw_die_ref die, tree t)
15804 {
15805 tree init;
15806 tree type = TREE_TYPE (t);
15807 rtx rtl;
15808
15809 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15810 return false;
15811
15812 init = t;
15813 gcc_assert (!DECL_P (init));
15814
15815 rtl = rtl_for_decl_init (init, type);
15816 if (rtl)
15817 return add_const_value_attribute (die, rtl);
15818 /* If the host and target are sane, try harder. */
15819 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15820 && initializer_constant_valid_p (init, type))
15821 {
15822 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15823 if (size > 0 && (int) size == size)
15824 {
15825 unsigned char *array = (unsigned char *)
15826 ggc_alloc_cleared_atomic (size);
15827
15828 if (native_encode_initializer (init, array, size))
15829 {
15830 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15831 return true;
15832 }
15833 ggc_free (array);
15834 }
15835 }
15836 return false;
15837 }
15838
15839 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15840 attribute is the const value of T, where T is an integral constant
15841 variable with static storage duration
15842 (so it can't be a PARM_DECL or a RESULT_DECL). */
15843
15844 static bool
15845 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15846 {
15847
15848 if (!decl
15849 || (TREE_CODE (decl) != VAR_DECL
15850 && TREE_CODE (decl) != CONST_DECL)
15851 || (TREE_CODE (decl) == VAR_DECL
15852 && !TREE_STATIC (decl)))
15853 return false;
15854
15855 if (TREE_READONLY (decl)
15856 && ! TREE_THIS_VOLATILE (decl)
15857 && DECL_INITIAL (decl))
15858 /* OK */;
15859 else
15860 return false;
15861
15862 /* Don't add DW_AT_const_value if abstract origin already has one. */
15863 if (get_AT (var_die, DW_AT_const_value))
15864 return false;
15865
15866 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15867 }
15868
15869 /* Convert the CFI instructions for the current function into a
15870 location list. This is used for DW_AT_frame_base when we targeting
15871 a dwarf2 consumer that does not support the dwarf3
15872 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15873 expressions. */
15874
15875 static dw_loc_list_ref
15876 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15877 {
15878 int ix;
15879 dw_fde_ref fde;
15880 dw_loc_list_ref list, *list_tail;
15881 dw_cfi_ref cfi;
15882 dw_cfa_location last_cfa, next_cfa;
15883 const char *start_label, *last_label, *section;
15884 dw_cfa_location remember;
15885
15886 fde = cfun->fde;
15887 gcc_assert (fde != NULL);
15888
15889 section = secname_for_decl (current_function_decl);
15890 list_tail = &list;
15891 list = NULL;
15892
15893 memset (&next_cfa, 0, sizeof (next_cfa));
15894 next_cfa.reg = INVALID_REGNUM;
15895 remember = next_cfa;
15896
15897 start_label = fde->dw_fde_begin;
15898
15899 /* ??? Bald assumption that the CIE opcode list does not contain
15900 advance opcodes. */
15901 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15902 lookup_cfa_1 (cfi, &next_cfa, &remember);
15903
15904 last_cfa = next_cfa;
15905 last_label = start_label;
15906
15907 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15908 {
15909 /* If the first partition contained no CFI adjustments, the
15910 CIE opcodes apply to the whole first partition. */
15911 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15912 fde->dw_fde_begin, fde->dw_fde_end, section);
15913 list_tail =&(*list_tail)->dw_loc_next;
15914 start_label = last_label = fde->dw_fde_second_begin;
15915 }
15916
15917 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15918 {
15919 switch (cfi->dw_cfi_opc)
15920 {
15921 case DW_CFA_set_loc:
15922 case DW_CFA_advance_loc1:
15923 case DW_CFA_advance_loc2:
15924 case DW_CFA_advance_loc4:
15925 if (!cfa_equal_p (&last_cfa, &next_cfa))
15926 {
15927 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15928 start_label, last_label, section);
15929
15930 list_tail = &(*list_tail)->dw_loc_next;
15931 last_cfa = next_cfa;
15932 start_label = last_label;
15933 }
15934 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15935 break;
15936
15937 case DW_CFA_advance_loc:
15938 /* The encoding is complex enough that we should never emit this. */
15939 gcc_unreachable ();
15940
15941 default:
15942 lookup_cfa_1 (cfi, &next_cfa, &remember);
15943 break;
15944 }
15945 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15946 {
15947 if (!cfa_equal_p (&last_cfa, &next_cfa))
15948 {
15949 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15950 start_label, last_label, section);
15951
15952 list_tail = &(*list_tail)->dw_loc_next;
15953 last_cfa = next_cfa;
15954 start_label = last_label;
15955 }
15956 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15957 start_label, fde->dw_fde_end, section);
15958 list_tail = &(*list_tail)->dw_loc_next;
15959 start_label = last_label = fde->dw_fde_second_begin;
15960 }
15961 }
15962
15963 if (!cfa_equal_p (&last_cfa, &next_cfa))
15964 {
15965 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15966 start_label, last_label, section);
15967 list_tail = &(*list_tail)->dw_loc_next;
15968 start_label = last_label;
15969 }
15970
15971 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15972 start_label,
15973 fde->dw_fde_second_begin
15974 ? fde->dw_fde_second_end : fde->dw_fde_end,
15975 section);
15976
15977 if (list && list->dw_loc_next)
15978 gen_llsym (list);
15979
15980 return list;
15981 }
15982
15983 /* Compute a displacement from the "steady-state frame pointer" to the
15984 frame base (often the same as the CFA), and store it in
15985 frame_pointer_fb_offset. OFFSET is added to the displacement
15986 before the latter is negated. */
15987
15988 static void
15989 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15990 {
15991 rtx reg, elim;
15992
15993 #ifdef FRAME_POINTER_CFA_OFFSET
15994 reg = frame_pointer_rtx;
15995 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15996 #else
15997 reg = arg_pointer_rtx;
15998 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15999 #endif
16000
16001 elim = (ira_use_lra_p
16002 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16003 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16004 if (GET_CODE (elim) == PLUS)
16005 {
16006 offset += INTVAL (XEXP (elim, 1));
16007 elim = XEXP (elim, 0);
16008 }
16009
16010 frame_pointer_fb_offset = -offset;
16011
16012 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16013 in which to eliminate. This is because it's stack pointer isn't
16014 directly accessible as a register within the ISA. To work around
16015 this, assume that while we cannot provide a proper value for
16016 frame_pointer_fb_offset, we won't need one either. */
16017 frame_pointer_fb_offset_valid
16018 = ((SUPPORTS_STACK_ALIGNMENT
16019 && (elim == hard_frame_pointer_rtx
16020 || elim == stack_pointer_rtx))
16021 || elim == (frame_pointer_needed
16022 ? hard_frame_pointer_rtx
16023 : stack_pointer_rtx));
16024 }
16025
16026 /* Generate a DW_AT_name attribute given some string value to be included as
16027 the value of the attribute. */
16028
16029 static void
16030 add_name_attribute (dw_die_ref die, const char *name_string)
16031 {
16032 if (name_string != NULL && *name_string != 0)
16033 {
16034 if (demangle_name_func)
16035 name_string = (*demangle_name_func) (name_string);
16036
16037 add_AT_string (die, DW_AT_name, name_string);
16038 }
16039 }
16040
16041 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16042 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16043 of TYPE accordingly.
16044
16045 ??? This is a temporary measure until after we're able to generate
16046 regular DWARF for the complex Ada type system. */
16047
16048 static void
16049 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16050 dw_die_ref context_die)
16051 {
16052 tree dtype;
16053 dw_die_ref dtype_die;
16054
16055 if (!lang_hooks.types.descriptive_type)
16056 return;
16057
16058 dtype = lang_hooks.types.descriptive_type (type);
16059 if (!dtype)
16060 return;
16061
16062 dtype_die = lookup_type_die (dtype);
16063 if (!dtype_die)
16064 {
16065 gen_type_die (dtype, context_die);
16066 dtype_die = lookup_type_die (dtype);
16067 gcc_assert (dtype_die);
16068 }
16069
16070 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16071 }
16072
16073 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16074
16075 static const char *
16076 comp_dir_string (void)
16077 {
16078 const char *wd;
16079 char *wd1;
16080 static const char *cached_wd = NULL;
16081
16082 if (cached_wd != NULL)
16083 return cached_wd;
16084
16085 wd = get_src_pwd ();
16086 if (wd == NULL)
16087 return NULL;
16088
16089 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16090 {
16091 int wdlen;
16092
16093 wdlen = strlen (wd);
16094 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16095 strcpy (wd1, wd);
16096 wd1 [wdlen] = DIR_SEPARATOR;
16097 wd1 [wdlen + 1] = 0;
16098 wd = wd1;
16099 }
16100
16101 cached_wd = remap_debug_filename (wd);
16102 return cached_wd;
16103 }
16104
16105 /* Generate a DW_AT_comp_dir attribute for DIE. */
16106
16107 static void
16108 add_comp_dir_attribute (dw_die_ref die)
16109 {
16110 const char * wd = comp_dir_string ();
16111 if (wd != NULL)
16112 add_AT_string (die, DW_AT_comp_dir, wd);
16113 }
16114
16115 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16116 default. */
16117
16118 static int
16119 lower_bound_default (void)
16120 {
16121 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16122 {
16123 case DW_LANG_C:
16124 case DW_LANG_C89:
16125 case DW_LANG_C99:
16126 case DW_LANG_C_plus_plus:
16127 case DW_LANG_ObjC:
16128 case DW_LANG_ObjC_plus_plus:
16129 case DW_LANG_Java:
16130 return 0;
16131 case DW_LANG_Fortran77:
16132 case DW_LANG_Fortran90:
16133 case DW_LANG_Fortran95:
16134 return 1;
16135 case DW_LANG_UPC:
16136 case DW_LANG_D:
16137 case DW_LANG_Python:
16138 return dwarf_version >= 4 ? 0 : -1;
16139 case DW_LANG_Ada95:
16140 case DW_LANG_Ada83:
16141 case DW_LANG_Cobol74:
16142 case DW_LANG_Cobol85:
16143 case DW_LANG_Pascal83:
16144 case DW_LANG_Modula2:
16145 case DW_LANG_PLI:
16146 return dwarf_version >= 4 ? 1 : -1;
16147 default:
16148 return -1;
16149 }
16150 }
16151
16152 /* Given a tree node describing an array bound (either lower or upper) output
16153 a representation for that bound. */
16154
16155 static void
16156 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16157 {
16158 switch (TREE_CODE (bound))
16159 {
16160 case ERROR_MARK:
16161 return;
16162
16163 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16164 case INTEGER_CST:
16165 {
16166 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16167 int dflt;
16168
16169 /* Use the default if possible. */
16170 if (bound_attr == DW_AT_lower_bound
16171 && tree_fits_shwi_p (bound)
16172 && (dflt = lower_bound_default ()) != -1
16173 && tree_to_shwi (bound) == dflt)
16174 ;
16175
16176 /* Otherwise represent the bound as an unsigned value with the
16177 precision of its type. The precision and signedness of the
16178 type will be necessary to re-interpret it unambiguously. */
16179 else if (prec < HOST_BITS_PER_WIDE_INT)
16180 {
16181 unsigned HOST_WIDE_INT mask
16182 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16183 add_AT_unsigned (subrange_die, bound_attr,
16184 TREE_INT_CST_LOW (bound) & mask);
16185 }
16186 else if (prec == HOST_BITS_PER_WIDE_INT
16187 || TREE_INT_CST_HIGH (bound) == 0)
16188 add_AT_unsigned (subrange_die, bound_attr,
16189 TREE_INT_CST_LOW (bound));
16190 else
16191 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16192 TREE_INT_CST_LOW (bound));
16193 }
16194 break;
16195
16196 CASE_CONVERT:
16197 case VIEW_CONVERT_EXPR:
16198 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16199 break;
16200
16201 case SAVE_EXPR:
16202 break;
16203
16204 case VAR_DECL:
16205 case PARM_DECL:
16206 case RESULT_DECL:
16207 {
16208 dw_die_ref decl_die = lookup_decl_die (bound);
16209
16210 /* ??? Can this happen, or should the variable have been bound
16211 first? Probably it can, since I imagine that we try to create
16212 the types of parameters in the order in which they exist in
16213 the list, and won't have created a forward reference to a
16214 later parameter. */
16215 if (decl_die != NULL)
16216 {
16217 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16218 break;
16219 }
16220 }
16221 /* FALLTHRU */
16222
16223 default:
16224 {
16225 /* Otherwise try to create a stack operation procedure to
16226 evaluate the value of the array bound. */
16227
16228 dw_die_ref ctx, decl_die;
16229 dw_loc_list_ref list;
16230
16231 list = loc_list_from_tree (bound, 2);
16232 if (list == NULL || single_element_loc_list_p (list))
16233 {
16234 /* If DW_AT_*bound is not a reference nor constant, it is
16235 a DWARF expression rather than location description.
16236 For that loc_list_from_tree (bound, 0) is needed.
16237 If that fails to give a single element list,
16238 fall back to outputting this as a reference anyway. */
16239 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16240 if (list2 && single_element_loc_list_p (list2))
16241 {
16242 add_AT_loc (subrange_die, bound_attr, list2->expr);
16243 break;
16244 }
16245 }
16246 if (list == NULL)
16247 break;
16248
16249 if (current_function_decl == 0)
16250 ctx = comp_unit_die ();
16251 else
16252 ctx = lookup_decl_die (current_function_decl);
16253
16254 decl_die = new_die (DW_TAG_variable, ctx, bound);
16255 add_AT_flag (decl_die, DW_AT_artificial, 1);
16256 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16257 add_AT_location_description (decl_die, DW_AT_location, list);
16258 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16259 break;
16260 }
16261 }
16262 }
16263
16264 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16265 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16266 Note that the block of subscript information for an array type also
16267 includes information about the element type of the given array type. */
16268
16269 static void
16270 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16271 {
16272 unsigned dimension_number;
16273 tree lower, upper;
16274 dw_die_ref subrange_die;
16275
16276 for (dimension_number = 0;
16277 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16278 type = TREE_TYPE (type), dimension_number++)
16279 {
16280 tree domain = TYPE_DOMAIN (type);
16281
16282 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16283 break;
16284
16285 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16286 and (in GNU C only) variable bounds. Handle all three forms
16287 here. */
16288 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16289 if (domain)
16290 {
16291 /* We have an array type with specified bounds. */
16292 lower = TYPE_MIN_VALUE (domain);
16293 upper = TYPE_MAX_VALUE (domain);
16294
16295 /* Define the index type. */
16296 if (TREE_TYPE (domain))
16297 {
16298 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16299 TREE_TYPE field. We can't emit debug info for this
16300 because it is an unnamed integral type. */
16301 if (TREE_CODE (domain) == INTEGER_TYPE
16302 && TYPE_NAME (domain) == NULL_TREE
16303 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16304 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16305 ;
16306 else
16307 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16308 type_die);
16309 }
16310
16311 /* ??? If upper is NULL, the array has unspecified length,
16312 but it does have a lower bound. This happens with Fortran
16313 dimension arr(N:*)
16314 Since the debugger is definitely going to need to know N
16315 to produce useful results, go ahead and output the lower
16316 bound solo, and hope the debugger can cope. */
16317
16318 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16319 if (upper)
16320 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16321 }
16322
16323 /* Otherwise we have an array type with an unspecified length. The
16324 DWARF-2 spec does not say how to handle this; let's just leave out the
16325 bounds. */
16326 }
16327 }
16328
16329 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16330
16331 static void
16332 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16333 {
16334 dw_die_ref decl_die;
16335 HOST_WIDE_INT size;
16336
16337 switch (TREE_CODE (tree_node))
16338 {
16339 case ERROR_MARK:
16340 size = 0;
16341 break;
16342 case ENUMERAL_TYPE:
16343 case RECORD_TYPE:
16344 case UNION_TYPE:
16345 case QUAL_UNION_TYPE:
16346 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16347 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16348 {
16349 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16350 return;
16351 }
16352 size = int_size_in_bytes (tree_node);
16353 break;
16354 case FIELD_DECL:
16355 /* For a data member of a struct or union, the DW_AT_byte_size is
16356 generally given as the number of bytes normally allocated for an
16357 object of the *declared* type of the member itself. This is true
16358 even for bit-fields. */
16359 size = int_size_in_bytes (field_type (tree_node));
16360 break;
16361 default:
16362 gcc_unreachable ();
16363 }
16364
16365 /* Note that `size' might be -1 when we get to this point. If it is, that
16366 indicates that the byte size of the entity in question is variable. We
16367 have no good way of expressing this fact in Dwarf at the present time,
16368 when location description was not used by the caller code instead. */
16369 if (size >= 0)
16370 add_AT_unsigned (die, DW_AT_byte_size, size);
16371 }
16372
16373 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16374 which specifies the distance in bits from the highest order bit of the
16375 "containing object" for the bit-field to the highest order bit of the
16376 bit-field itself.
16377
16378 For any given bit-field, the "containing object" is a hypothetical object
16379 (of some integral or enum type) within which the given bit-field lives. The
16380 type of this hypothetical "containing object" is always the same as the
16381 declared type of the individual bit-field itself. The determination of the
16382 exact location of the "containing object" for a bit-field is rather
16383 complicated. It's handled by the `field_byte_offset' function (above).
16384
16385 Note that it is the size (in bytes) of the hypothetical "containing object"
16386 which will be given in the DW_AT_byte_size attribute for this bit-field.
16387 (See `byte_size_attribute' above). */
16388
16389 static inline void
16390 add_bit_offset_attribute (dw_die_ref die, tree decl)
16391 {
16392 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16393 tree type = DECL_BIT_FIELD_TYPE (decl);
16394 HOST_WIDE_INT bitpos_int;
16395 HOST_WIDE_INT highest_order_object_bit_offset;
16396 HOST_WIDE_INT highest_order_field_bit_offset;
16397 HOST_WIDE_INT bit_offset;
16398
16399 /* Must be a field and a bit field. */
16400 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16401
16402 /* We can't yet handle bit-fields whose offsets are variable, so if we
16403 encounter such things, just return without generating any attribute
16404 whatsoever. Likewise for variable or too large size. */
16405 if (! tree_fits_shwi_p (bit_position (decl))
16406 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16407 return;
16408
16409 bitpos_int = int_bit_position (decl);
16410
16411 /* Note that the bit offset is always the distance (in bits) from the
16412 highest-order bit of the "containing object" to the highest-order bit of
16413 the bit-field itself. Since the "high-order end" of any object or field
16414 is different on big-endian and little-endian machines, the computation
16415 below must take account of these differences. */
16416 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16417 highest_order_field_bit_offset = bitpos_int;
16418
16419 if (! BYTES_BIG_ENDIAN)
16420 {
16421 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16422 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16423 }
16424
16425 bit_offset
16426 = (! BYTES_BIG_ENDIAN
16427 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16428 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16429
16430 if (bit_offset < 0)
16431 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16432 else
16433 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16434 }
16435
16436 /* For a FIELD_DECL node which represents a bit field, output an attribute
16437 which specifies the length in bits of the given field. */
16438
16439 static inline void
16440 add_bit_size_attribute (dw_die_ref die, tree decl)
16441 {
16442 /* Must be a field and a bit field. */
16443 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16444 && DECL_BIT_FIELD_TYPE (decl));
16445
16446 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16447 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16448 }
16449
16450 /* If the compiled language is ANSI C, then add a 'prototyped'
16451 attribute, if arg types are given for the parameters of a function. */
16452
16453 static inline void
16454 add_prototyped_attribute (dw_die_ref die, tree func_type)
16455 {
16456 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16457 && prototype_p (func_type))
16458 add_AT_flag (die, DW_AT_prototyped, 1);
16459 }
16460
16461 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16462 by looking in either the type declaration or object declaration
16463 equate table. */
16464
16465 static inline dw_die_ref
16466 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16467 {
16468 dw_die_ref origin_die = NULL;
16469
16470 if (TREE_CODE (origin) != FUNCTION_DECL)
16471 {
16472 /* We may have gotten separated from the block for the inlined
16473 function, if we're in an exception handler or some such; make
16474 sure that the abstract function has been written out.
16475
16476 Doing this for nested functions is wrong, however; functions are
16477 distinct units, and our context might not even be inline. */
16478 tree fn = origin;
16479
16480 if (TYPE_P (fn))
16481 fn = TYPE_STUB_DECL (fn);
16482
16483 fn = decl_function_context (fn);
16484 if (fn)
16485 dwarf2out_abstract_function (fn);
16486 }
16487
16488 if (DECL_P (origin))
16489 origin_die = lookup_decl_die (origin);
16490 else if (TYPE_P (origin))
16491 origin_die = lookup_type_die (origin);
16492
16493 /* XXX: Functions that are never lowered don't always have correct block
16494 trees (in the case of java, they simply have no block tree, in some other
16495 languages). For these functions, there is nothing we can really do to
16496 output correct debug info for inlined functions in all cases. Rather
16497 than die, we'll just produce deficient debug info now, in that we will
16498 have variables without a proper abstract origin. In the future, when all
16499 functions are lowered, we should re-add a gcc_assert (origin_die)
16500 here. */
16501
16502 if (origin_die)
16503 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16504 return origin_die;
16505 }
16506
16507 /* We do not currently support the pure_virtual attribute. */
16508
16509 static inline void
16510 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16511 {
16512 if (DECL_VINDEX (func_decl))
16513 {
16514 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16515
16516 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16517 add_AT_loc (die, DW_AT_vtable_elem_location,
16518 new_loc_descr (DW_OP_constu,
16519 tree_to_shwi (DECL_VINDEX (func_decl)),
16520 0));
16521
16522 /* GNU extension: Record what type this method came from originally. */
16523 if (debug_info_level > DINFO_LEVEL_TERSE
16524 && DECL_CONTEXT (func_decl))
16525 add_AT_die_ref (die, DW_AT_containing_type,
16526 lookup_type_die (DECL_CONTEXT (func_decl)));
16527 }
16528 }
16529 \f
16530 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16531 given decl. This used to be a vendor extension until after DWARF 4
16532 standardized it. */
16533
16534 static void
16535 add_linkage_attr (dw_die_ref die, tree decl)
16536 {
16537 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16538
16539 /* Mimic what assemble_name_raw does with a leading '*'. */
16540 if (name[0] == '*')
16541 name = &name[1];
16542
16543 if (dwarf_version >= 4)
16544 add_AT_string (die, DW_AT_linkage_name, name);
16545 else
16546 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16547 }
16548
16549 /* Add source coordinate attributes for the given decl. */
16550
16551 static void
16552 add_src_coords_attributes (dw_die_ref die, tree decl)
16553 {
16554 expanded_location s;
16555
16556 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16557 return;
16558 s = expand_location (DECL_SOURCE_LOCATION (decl));
16559 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16560 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16561 }
16562
16563 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16564
16565 static void
16566 add_linkage_name (dw_die_ref die, tree decl)
16567 {
16568 if (debug_info_level > DINFO_LEVEL_TERSE
16569 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16570 && TREE_PUBLIC (decl)
16571 && !DECL_ABSTRACT (decl)
16572 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16573 && die->die_tag != DW_TAG_member)
16574 {
16575 /* Defer until we have an assembler name set. */
16576 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16577 {
16578 limbo_die_node *asm_name;
16579
16580 asm_name = ggc_alloc_cleared_limbo_die_node ();
16581 asm_name->die = die;
16582 asm_name->created_for = decl;
16583 asm_name->next = deferred_asm_name;
16584 deferred_asm_name = asm_name;
16585 }
16586 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16587 add_linkage_attr (die, decl);
16588 }
16589 }
16590
16591 /* Add a DW_AT_name attribute and source coordinate attribute for the
16592 given decl, but only if it actually has a name. */
16593
16594 static void
16595 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16596 {
16597 tree decl_name;
16598
16599 decl_name = DECL_NAME (decl);
16600 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16601 {
16602 const char *name = dwarf2_name (decl, 0);
16603 if (name)
16604 add_name_attribute (die, name);
16605 if (! DECL_ARTIFICIAL (decl))
16606 add_src_coords_attributes (die, decl);
16607
16608 add_linkage_name (die, decl);
16609 }
16610
16611 #ifdef VMS_DEBUGGING_INFO
16612 /* Get the function's name, as described by its RTL. This may be different
16613 from the DECL_NAME name used in the source file. */
16614 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16615 {
16616 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16617 XEXP (DECL_RTL (decl), 0), false);
16618 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16619 }
16620 #endif /* VMS_DEBUGGING_INFO */
16621 }
16622
16623 #ifdef VMS_DEBUGGING_INFO
16624 /* Output the debug main pointer die for VMS */
16625
16626 void
16627 dwarf2out_vms_debug_main_pointer (void)
16628 {
16629 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16630 dw_die_ref die;
16631
16632 /* Allocate the VMS debug main subprogram die. */
16633 die = ggc_alloc_cleared_die_node ();
16634 die->die_tag = DW_TAG_subprogram;
16635 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16636 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16637 current_function_funcdef_no);
16638 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16639
16640 /* Make it the first child of comp_unit_die (). */
16641 die->die_parent = comp_unit_die ();
16642 if (comp_unit_die ()->die_child)
16643 {
16644 die->die_sib = comp_unit_die ()->die_child->die_sib;
16645 comp_unit_die ()->die_child->die_sib = die;
16646 }
16647 else
16648 {
16649 die->die_sib = die;
16650 comp_unit_die ()->die_child = die;
16651 }
16652 }
16653 #endif /* VMS_DEBUGGING_INFO */
16654
16655 /* Push a new declaration scope. */
16656
16657 static void
16658 push_decl_scope (tree scope)
16659 {
16660 vec_safe_push (decl_scope_table, scope);
16661 }
16662
16663 /* Pop a declaration scope. */
16664
16665 static inline void
16666 pop_decl_scope (void)
16667 {
16668 decl_scope_table->pop ();
16669 }
16670
16671 /* walk_tree helper function for uses_local_type, below. */
16672
16673 static tree
16674 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16675 {
16676 if (!TYPE_P (*tp))
16677 *walk_subtrees = 0;
16678 else
16679 {
16680 tree name = TYPE_NAME (*tp);
16681 if (name && DECL_P (name) && decl_function_context (name))
16682 return *tp;
16683 }
16684 return NULL_TREE;
16685 }
16686
16687 /* If TYPE involves a function-local type (including a local typedef to a
16688 non-local type), returns that type; otherwise returns NULL_TREE. */
16689
16690 static tree
16691 uses_local_type (tree type)
16692 {
16693 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16694 return used;
16695 }
16696
16697 /* Return the DIE for the scope that immediately contains this type.
16698 Non-named types that do not involve a function-local type get global
16699 scope. Named types nested in namespaces or other types get their
16700 containing scope. All other types (i.e. function-local named types) get
16701 the current active scope. */
16702
16703 static dw_die_ref
16704 scope_die_for (tree t, dw_die_ref context_die)
16705 {
16706 dw_die_ref scope_die = NULL;
16707 tree containing_scope;
16708
16709 /* Non-types always go in the current scope. */
16710 gcc_assert (TYPE_P (t));
16711
16712 /* Use the scope of the typedef, rather than the scope of the type
16713 it refers to. */
16714 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16715 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16716 else
16717 containing_scope = TYPE_CONTEXT (t);
16718
16719 /* Use the containing namespace if there is one. */
16720 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16721 {
16722 if (context_die == lookup_decl_die (containing_scope))
16723 /* OK */;
16724 else if (debug_info_level > DINFO_LEVEL_TERSE)
16725 context_die = get_context_die (containing_scope);
16726 else
16727 containing_scope = NULL_TREE;
16728 }
16729
16730 /* Ignore function type "scopes" from the C frontend. They mean that
16731 a tagged type is local to a parmlist of a function declarator, but
16732 that isn't useful to DWARF. */
16733 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16734 containing_scope = NULL_TREE;
16735
16736 if (SCOPE_FILE_SCOPE_P (containing_scope))
16737 {
16738 /* If T uses a local type keep it local as well, to avoid references
16739 to function-local DIEs from outside the function. */
16740 if (current_function_decl && uses_local_type (t))
16741 scope_die = context_die;
16742 else
16743 scope_die = comp_unit_die ();
16744 }
16745 else if (TYPE_P (containing_scope))
16746 {
16747 /* For types, we can just look up the appropriate DIE. */
16748 if (debug_info_level > DINFO_LEVEL_TERSE)
16749 scope_die = get_context_die (containing_scope);
16750 else
16751 {
16752 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16753 if (scope_die == NULL)
16754 scope_die = comp_unit_die ();
16755 }
16756 }
16757 else
16758 scope_die = context_die;
16759
16760 return scope_die;
16761 }
16762
16763 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16764
16765 static inline int
16766 local_scope_p (dw_die_ref context_die)
16767 {
16768 for (; context_die; context_die = context_die->die_parent)
16769 if (context_die->die_tag == DW_TAG_inlined_subroutine
16770 || context_die->die_tag == DW_TAG_subprogram)
16771 return 1;
16772
16773 return 0;
16774 }
16775
16776 /* Returns nonzero if CONTEXT_DIE is a class. */
16777
16778 static inline int
16779 class_scope_p (dw_die_ref context_die)
16780 {
16781 return (context_die
16782 && (context_die->die_tag == DW_TAG_structure_type
16783 || context_die->die_tag == DW_TAG_class_type
16784 || context_die->die_tag == DW_TAG_interface_type
16785 || context_die->die_tag == DW_TAG_union_type));
16786 }
16787
16788 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16789 whether or not to treat a DIE in this context as a declaration. */
16790
16791 static inline int
16792 class_or_namespace_scope_p (dw_die_ref context_die)
16793 {
16794 return (class_scope_p (context_die)
16795 || (context_die && context_die->die_tag == DW_TAG_namespace));
16796 }
16797
16798 /* Many forms of DIEs require a "type description" attribute. This
16799 routine locates the proper "type descriptor" die for the type given
16800 by 'type', and adds a DW_AT_type attribute below the given die. */
16801
16802 static void
16803 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16804 int decl_volatile, dw_die_ref context_die)
16805 {
16806 enum tree_code code = TREE_CODE (type);
16807 dw_die_ref type_die = NULL;
16808
16809 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16810 or fixed-point type, use the inner type. This is because we have no
16811 support for unnamed types in base_type_die. This can happen if this is
16812 an Ada subrange type. Correct solution is emit a subrange type die. */
16813 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16814 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16815 type = TREE_TYPE (type), code = TREE_CODE (type);
16816
16817 if (code == ERROR_MARK
16818 /* Handle a special case. For functions whose return type is void, we
16819 generate *no* type attribute. (Note that no object may have type
16820 `void', so this only applies to function return types). */
16821 || code == VOID_TYPE)
16822 return;
16823
16824 type_die = modified_type_die (type,
16825 decl_const || TYPE_READONLY (type),
16826 decl_volatile || TYPE_VOLATILE (type),
16827 context_die);
16828
16829 if (type_die != NULL)
16830 add_AT_die_ref (object_die, DW_AT_type, type_die);
16831 }
16832
16833 /* Given an object die, add the calling convention attribute for the
16834 function call type. */
16835 static void
16836 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16837 {
16838 enum dwarf_calling_convention value = DW_CC_normal;
16839
16840 value = ((enum dwarf_calling_convention)
16841 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16842
16843 if (is_fortran ()
16844 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16845 {
16846 /* DWARF 2 doesn't provide a way to identify a program's source-level
16847 entry point. DW_AT_calling_convention attributes are only meant
16848 to describe functions' calling conventions. However, lacking a
16849 better way to signal the Fortran main program, we used this for
16850 a long time, following existing custom. Now, DWARF 4 has
16851 DW_AT_main_subprogram, which we add below, but some tools still
16852 rely on the old way, which we thus keep. */
16853 value = DW_CC_program;
16854
16855 if (dwarf_version >= 4 || !dwarf_strict)
16856 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16857 }
16858
16859 /* Only add the attribute if the backend requests it, and
16860 is not DW_CC_normal. */
16861 if (value && (value != DW_CC_normal))
16862 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16863 }
16864
16865 /* Given a tree pointer to a struct, class, union, or enum type node, return
16866 a pointer to the (string) tag name for the given type, or zero if the type
16867 was declared without a tag. */
16868
16869 static const char *
16870 type_tag (const_tree type)
16871 {
16872 const char *name = 0;
16873
16874 if (TYPE_NAME (type) != 0)
16875 {
16876 tree t = 0;
16877
16878 /* Find the IDENTIFIER_NODE for the type name. */
16879 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16880 && !TYPE_NAMELESS (type))
16881 t = TYPE_NAME (type);
16882
16883 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16884 a TYPE_DECL node, regardless of whether or not a `typedef' was
16885 involved. */
16886 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16887 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16888 {
16889 /* We want to be extra verbose. Don't call dwarf_name if
16890 DECL_NAME isn't set. The default hook for decl_printable_name
16891 doesn't like that, and in this context it's correct to return
16892 0, instead of "<anonymous>" or the like. */
16893 if (DECL_NAME (TYPE_NAME (type))
16894 && !DECL_NAMELESS (TYPE_NAME (type)))
16895 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16896 }
16897
16898 /* Now get the name as a string, or invent one. */
16899 if (!name && t != 0)
16900 name = IDENTIFIER_POINTER (t);
16901 }
16902
16903 return (name == 0 || *name == '\0') ? 0 : name;
16904 }
16905
16906 /* Return the type associated with a data member, make a special check
16907 for bit field types. */
16908
16909 static inline tree
16910 member_declared_type (const_tree member)
16911 {
16912 return (DECL_BIT_FIELD_TYPE (member)
16913 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16914 }
16915
16916 /* Get the decl's label, as described by its RTL. This may be different
16917 from the DECL_NAME name used in the source file. */
16918
16919 #if 0
16920 static const char *
16921 decl_start_label (tree decl)
16922 {
16923 rtx x;
16924 const char *fnname;
16925
16926 x = DECL_RTL (decl);
16927 gcc_assert (MEM_P (x));
16928
16929 x = XEXP (x, 0);
16930 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16931
16932 fnname = XSTR (x, 0);
16933 return fnname;
16934 }
16935 #endif
16936 \f
16937 /* These routines generate the internal representation of the DIE's for
16938 the compilation unit. Debugging information is collected by walking
16939 the declaration trees passed in from dwarf2out_decl(). */
16940
16941 static void
16942 gen_array_type_die (tree type, dw_die_ref context_die)
16943 {
16944 dw_die_ref scope_die = scope_die_for (type, context_die);
16945 dw_die_ref array_die;
16946
16947 /* GNU compilers represent multidimensional array types as sequences of one
16948 dimensional array types whose element types are themselves array types.
16949 We sometimes squish that down to a single array_type DIE with multiple
16950 subscripts in the Dwarf debugging info. The draft Dwarf specification
16951 say that we are allowed to do this kind of compression in C, because
16952 there is no difference between an array of arrays and a multidimensional
16953 array. We don't do this for Ada to remain as close as possible to the
16954 actual representation, which is especially important against the language
16955 flexibilty wrt arrays of variable size. */
16956
16957 bool collapse_nested_arrays = !is_ada ();
16958 tree element_type;
16959
16960 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16961 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16962 if (TYPE_STRING_FLAG (type)
16963 && TREE_CODE (type) == ARRAY_TYPE
16964 && is_fortran ()
16965 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16966 {
16967 HOST_WIDE_INT size;
16968
16969 array_die = new_die (DW_TAG_string_type, scope_die, type);
16970 add_name_attribute (array_die, type_tag (type));
16971 equate_type_number_to_die (type, array_die);
16972 size = int_size_in_bytes (type);
16973 if (size >= 0)
16974 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16975 else if (TYPE_DOMAIN (type) != NULL_TREE
16976 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16977 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16978 {
16979 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16980 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16981
16982 size = int_size_in_bytes (TREE_TYPE (szdecl));
16983 if (loc && size > 0)
16984 {
16985 add_AT_location_description (array_die, DW_AT_string_length, loc);
16986 if (size != DWARF2_ADDR_SIZE)
16987 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16988 }
16989 }
16990 return;
16991 }
16992
16993 array_die = new_die (DW_TAG_array_type, scope_die, type);
16994 add_name_attribute (array_die, type_tag (type));
16995 equate_type_number_to_die (type, array_die);
16996
16997 if (TREE_CODE (type) == VECTOR_TYPE)
16998 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16999
17000 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17001 if (is_fortran ()
17002 && TREE_CODE (type) == ARRAY_TYPE
17003 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17004 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17005 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17006
17007 #if 0
17008 /* We default the array ordering. SDB will probably do
17009 the right things even if DW_AT_ordering is not present. It's not even
17010 an issue until we start to get into multidimensional arrays anyway. If
17011 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17012 then we'll have to put the DW_AT_ordering attribute back in. (But if
17013 and when we find out that we need to put these in, we will only do so
17014 for multidimensional arrays. */
17015 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17016 #endif
17017
17018 if (TREE_CODE (type) == VECTOR_TYPE)
17019 {
17020 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17021 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17022 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17023 add_bound_info (subrange_die, DW_AT_upper_bound,
17024 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17025 }
17026 else
17027 add_subscript_info (array_die, type, collapse_nested_arrays);
17028
17029 /* Add representation of the type of the elements of this array type and
17030 emit the corresponding DIE if we haven't done it already. */
17031 element_type = TREE_TYPE (type);
17032 if (collapse_nested_arrays)
17033 while (TREE_CODE (element_type) == ARRAY_TYPE)
17034 {
17035 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17036 break;
17037 element_type = TREE_TYPE (element_type);
17038 }
17039
17040 add_type_attribute (array_die, element_type, 0, 0, context_die);
17041
17042 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17043 if (TYPE_ARTIFICIAL (type))
17044 add_AT_flag (array_die, DW_AT_artificial, 1);
17045
17046 if (get_AT (array_die, DW_AT_name))
17047 add_pubtype (type, array_die);
17048 }
17049
17050 static dw_loc_descr_ref
17051 descr_info_loc (tree val, tree base_decl)
17052 {
17053 HOST_WIDE_INT size;
17054 dw_loc_descr_ref loc, loc2;
17055 enum dwarf_location_atom op;
17056
17057 if (val == base_decl)
17058 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17059
17060 switch (TREE_CODE (val))
17061 {
17062 CASE_CONVERT:
17063 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17064 case VAR_DECL:
17065 return loc_descriptor_from_tree (val, 0);
17066 case INTEGER_CST:
17067 if (tree_fits_shwi_p (val))
17068 return int_loc_descriptor (tree_to_shwi (val));
17069 break;
17070 case INDIRECT_REF:
17071 size = int_size_in_bytes (TREE_TYPE (val));
17072 if (size < 0)
17073 break;
17074 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17075 if (!loc)
17076 break;
17077 if (size == DWARF2_ADDR_SIZE)
17078 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17079 else
17080 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17081 return loc;
17082 case POINTER_PLUS_EXPR:
17083 case PLUS_EXPR:
17084 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17085 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17086 {
17087 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17088 if (!loc)
17089 break;
17090 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17091 }
17092 else
17093 {
17094 op = DW_OP_plus;
17095 do_binop:
17096 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17097 if (!loc)
17098 break;
17099 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17100 if (!loc2)
17101 break;
17102 add_loc_descr (&loc, loc2);
17103 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17104 }
17105 return loc;
17106 case MINUS_EXPR:
17107 op = DW_OP_minus;
17108 goto do_binop;
17109 case MULT_EXPR:
17110 op = DW_OP_mul;
17111 goto do_binop;
17112 case EQ_EXPR:
17113 op = DW_OP_eq;
17114 goto do_binop;
17115 case NE_EXPR:
17116 op = DW_OP_ne;
17117 goto do_binop;
17118 default:
17119 break;
17120 }
17121 return NULL;
17122 }
17123
17124 static void
17125 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17126 tree val, tree base_decl)
17127 {
17128 dw_loc_descr_ref loc;
17129
17130 if (tree_fits_shwi_p (val))
17131 {
17132 add_AT_unsigned (die, attr, tree_to_shwi (val));
17133 return;
17134 }
17135
17136 loc = descr_info_loc (val, base_decl);
17137 if (!loc)
17138 return;
17139
17140 add_AT_loc (die, attr, loc);
17141 }
17142
17143 /* This routine generates DIE for array with hidden descriptor, details
17144 are filled into *info by a langhook. */
17145
17146 static void
17147 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17148 dw_die_ref context_die)
17149 {
17150 dw_die_ref scope_die = scope_die_for (type, context_die);
17151 dw_die_ref array_die;
17152 int dim;
17153
17154 array_die = new_die (DW_TAG_array_type, scope_die, type);
17155 add_name_attribute (array_die, type_tag (type));
17156 equate_type_number_to_die (type, array_die);
17157
17158 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17159 if (is_fortran ()
17160 && info->ndimensions >= 2)
17161 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17162
17163 if (info->data_location)
17164 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17165 info->base_decl);
17166 if (info->associated)
17167 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17168 info->base_decl);
17169 if (info->allocated)
17170 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17171 info->base_decl);
17172
17173 for (dim = 0; dim < info->ndimensions; dim++)
17174 {
17175 dw_die_ref subrange_die
17176 = new_die (DW_TAG_subrange_type, array_die, NULL);
17177
17178 if (info->dimen[dim].lower_bound)
17179 {
17180 /* If it is the default value, omit it. */
17181 int dflt;
17182
17183 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17184 && (dflt = lower_bound_default ()) != -1
17185 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17186 ;
17187 else
17188 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17189 info->dimen[dim].lower_bound,
17190 info->base_decl);
17191 }
17192 if (info->dimen[dim].upper_bound)
17193 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17194 info->dimen[dim].upper_bound,
17195 info->base_decl);
17196 if (info->dimen[dim].stride)
17197 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17198 info->dimen[dim].stride,
17199 info->base_decl);
17200 }
17201
17202 gen_type_die (info->element_type, context_die);
17203 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17204
17205 if (get_AT (array_die, DW_AT_name))
17206 add_pubtype (type, array_die);
17207 }
17208
17209 #if 0
17210 static void
17211 gen_entry_point_die (tree decl, dw_die_ref context_die)
17212 {
17213 tree origin = decl_ultimate_origin (decl);
17214 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17215
17216 if (origin != NULL)
17217 add_abstract_origin_attribute (decl_die, origin);
17218 else
17219 {
17220 add_name_and_src_coords_attributes (decl_die, decl);
17221 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17222 0, 0, context_die);
17223 }
17224
17225 if (DECL_ABSTRACT (decl))
17226 equate_decl_number_to_die (decl, decl_die);
17227 else
17228 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17229 }
17230 #endif
17231
17232 /* Walk through the list of incomplete types again, trying once more to
17233 emit full debugging info for them. */
17234
17235 static void
17236 retry_incomplete_types (void)
17237 {
17238 int i;
17239
17240 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17241 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17242 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17243 }
17244
17245 /* Determine what tag to use for a record type. */
17246
17247 static enum dwarf_tag
17248 record_type_tag (tree type)
17249 {
17250 if (! lang_hooks.types.classify_record)
17251 return DW_TAG_structure_type;
17252
17253 switch (lang_hooks.types.classify_record (type))
17254 {
17255 case RECORD_IS_STRUCT:
17256 return DW_TAG_structure_type;
17257
17258 case RECORD_IS_CLASS:
17259 return DW_TAG_class_type;
17260
17261 case RECORD_IS_INTERFACE:
17262 if (dwarf_version >= 3 || !dwarf_strict)
17263 return DW_TAG_interface_type;
17264 return DW_TAG_structure_type;
17265
17266 default:
17267 gcc_unreachable ();
17268 }
17269 }
17270
17271 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17272 include all of the information about the enumeration values also. Each
17273 enumerated type name/value is listed as a child of the enumerated type
17274 DIE. */
17275
17276 static dw_die_ref
17277 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17278 {
17279 dw_die_ref type_die = lookup_type_die (type);
17280
17281 if (type_die == NULL)
17282 {
17283 type_die = new_die (DW_TAG_enumeration_type,
17284 scope_die_for (type, context_die), type);
17285 equate_type_number_to_die (type, type_die);
17286 add_name_attribute (type_die, type_tag (type));
17287 if (dwarf_version >= 4 || !dwarf_strict)
17288 {
17289 if (ENUM_IS_SCOPED (type))
17290 add_AT_flag (type_die, DW_AT_enum_class, 1);
17291 if (ENUM_IS_OPAQUE (type))
17292 add_AT_flag (type_die, DW_AT_declaration, 1);
17293 }
17294 }
17295 else if (! TYPE_SIZE (type))
17296 return type_die;
17297 else
17298 remove_AT (type_die, DW_AT_declaration);
17299
17300 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17301 given enum type is incomplete, do not generate the DW_AT_byte_size
17302 attribute or the DW_AT_element_list attribute. */
17303 if (TYPE_SIZE (type))
17304 {
17305 tree link;
17306
17307 TREE_ASM_WRITTEN (type) = 1;
17308 add_byte_size_attribute (type_die, type);
17309 if (TYPE_STUB_DECL (type) != NULL_TREE)
17310 {
17311 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17312 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17313 }
17314
17315 /* If the first reference to this type was as the return type of an
17316 inline function, then it may not have a parent. Fix this now. */
17317 if (type_die->die_parent == NULL)
17318 add_child_die (scope_die_for (type, context_die), type_die);
17319
17320 for (link = TYPE_VALUES (type);
17321 link != NULL; link = TREE_CHAIN (link))
17322 {
17323 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17324 tree value = TREE_VALUE (link);
17325
17326 add_name_attribute (enum_die,
17327 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17328
17329 if (TREE_CODE (value) == CONST_DECL)
17330 value = DECL_INITIAL (value);
17331
17332 if (simple_type_size_in_bits (TREE_TYPE (value))
17333 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17334 /* DWARF2 does not provide a way of indicating whether or
17335 not enumeration constants are signed or unsigned. GDB
17336 always assumes the values are signed, so we output all
17337 values as if they were signed. That means that
17338 enumeration constants with very large unsigned values
17339 will appear to have negative values in the debugger.
17340
17341 TODO: the above comment is wrong, DWARF2 does provide
17342 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17343 This should be re-worked to use correct signed/unsigned
17344 int/double tags for all cases, instead of always treating as
17345 signed. */
17346 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17347 else
17348 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17349 that here. */
17350 add_AT_double (enum_die, DW_AT_const_value,
17351 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17352 }
17353
17354 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17355 if (TYPE_ARTIFICIAL (type))
17356 add_AT_flag (type_die, DW_AT_artificial, 1);
17357 }
17358 else
17359 add_AT_flag (type_die, DW_AT_declaration, 1);
17360
17361 add_pubtype (type, type_die);
17362
17363 return type_die;
17364 }
17365
17366 /* Generate a DIE to represent either a real live formal parameter decl or to
17367 represent just the type of some formal parameter position in some function
17368 type.
17369
17370 Note that this routine is a bit unusual because its argument may be a
17371 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17372 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17373 node. If it's the former then this function is being called to output a
17374 DIE to represent a formal parameter object (or some inlining thereof). If
17375 it's the latter, then this function is only being called to output a
17376 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17377 argument type of some subprogram type.
17378 If EMIT_NAME_P is true, name and source coordinate attributes
17379 are emitted. */
17380
17381 static dw_die_ref
17382 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17383 dw_die_ref context_die)
17384 {
17385 tree node_or_origin = node ? node : origin;
17386 tree ultimate_origin;
17387 dw_die_ref parm_die
17388 = new_die (DW_TAG_formal_parameter, context_die, node);
17389
17390 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17391 {
17392 case tcc_declaration:
17393 ultimate_origin = decl_ultimate_origin (node_or_origin);
17394 if (node || ultimate_origin)
17395 origin = ultimate_origin;
17396 if (origin != NULL)
17397 add_abstract_origin_attribute (parm_die, origin);
17398 else if (emit_name_p)
17399 add_name_and_src_coords_attributes (parm_die, node);
17400 if (origin == NULL
17401 || (! DECL_ABSTRACT (node_or_origin)
17402 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17403 decl_function_context
17404 (node_or_origin))))
17405 {
17406 tree type = TREE_TYPE (node_or_origin);
17407 if (decl_by_reference_p (node_or_origin))
17408 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17409 context_die);
17410 else
17411 add_type_attribute (parm_die, type,
17412 TREE_READONLY (node_or_origin),
17413 TREE_THIS_VOLATILE (node_or_origin),
17414 context_die);
17415 }
17416 if (origin == NULL && DECL_ARTIFICIAL (node))
17417 add_AT_flag (parm_die, DW_AT_artificial, 1);
17418
17419 if (node && node != origin)
17420 equate_decl_number_to_die (node, parm_die);
17421 if (! DECL_ABSTRACT (node_or_origin))
17422 add_location_or_const_value_attribute (parm_die, node_or_origin,
17423 node == NULL, DW_AT_location);
17424
17425 break;
17426
17427 case tcc_type:
17428 /* We were called with some kind of a ..._TYPE node. */
17429 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17430 break;
17431
17432 default:
17433 gcc_unreachable ();
17434 }
17435
17436 return parm_die;
17437 }
17438
17439 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17440 children DW_TAG_formal_parameter DIEs representing the arguments of the
17441 parameter pack.
17442
17443 PARM_PACK must be a function parameter pack.
17444 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17445 must point to the subsequent arguments of the function PACK_ARG belongs to.
17446 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17447 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17448 following the last one for which a DIE was generated. */
17449
17450 static dw_die_ref
17451 gen_formal_parameter_pack_die (tree parm_pack,
17452 tree pack_arg,
17453 dw_die_ref subr_die,
17454 tree *next_arg)
17455 {
17456 tree arg;
17457 dw_die_ref parm_pack_die;
17458
17459 gcc_assert (parm_pack
17460 && lang_hooks.function_parameter_pack_p (parm_pack)
17461 && subr_die);
17462
17463 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17464 add_src_coords_attributes (parm_pack_die, parm_pack);
17465
17466 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17467 {
17468 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17469 parm_pack))
17470 break;
17471 gen_formal_parameter_die (arg, NULL,
17472 false /* Don't emit name attribute. */,
17473 parm_pack_die);
17474 }
17475 if (next_arg)
17476 *next_arg = arg;
17477 return parm_pack_die;
17478 }
17479
17480 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17481 at the end of an (ANSI prototyped) formal parameters list. */
17482
17483 static void
17484 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17485 {
17486 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17487 }
17488
17489 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17490 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17491 parameters as specified in some function type specification (except for
17492 those which appear as part of a function *definition*). */
17493
17494 static void
17495 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17496 {
17497 tree link;
17498 tree formal_type = NULL;
17499 tree first_parm_type;
17500 tree arg;
17501
17502 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17503 {
17504 arg = DECL_ARGUMENTS (function_or_method_type);
17505 function_or_method_type = TREE_TYPE (function_or_method_type);
17506 }
17507 else
17508 arg = NULL_TREE;
17509
17510 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17511
17512 /* Make our first pass over the list of formal parameter types and output a
17513 DW_TAG_formal_parameter DIE for each one. */
17514 for (link = first_parm_type; link; )
17515 {
17516 dw_die_ref parm_die;
17517
17518 formal_type = TREE_VALUE (link);
17519 if (formal_type == void_type_node)
17520 break;
17521
17522 /* Output a (nameless) DIE to represent the formal parameter itself. */
17523 parm_die = gen_formal_parameter_die (formal_type, NULL,
17524 true /* Emit name attribute. */,
17525 context_die);
17526 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17527 && link == first_parm_type)
17528 {
17529 add_AT_flag (parm_die, DW_AT_artificial, 1);
17530 if (dwarf_version >= 3 || !dwarf_strict)
17531 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17532 }
17533 else if (arg && DECL_ARTIFICIAL (arg))
17534 add_AT_flag (parm_die, DW_AT_artificial, 1);
17535
17536 link = TREE_CHAIN (link);
17537 if (arg)
17538 arg = DECL_CHAIN (arg);
17539 }
17540
17541 /* If this function type has an ellipsis, add a
17542 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17543 if (formal_type != void_type_node)
17544 gen_unspecified_parameters_die (function_or_method_type, context_die);
17545
17546 /* Make our second (and final) pass over the list of formal parameter types
17547 and output DIEs to represent those types (as necessary). */
17548 for (link = TYPE_ARG_TYPES (function_or_method_type);
17549 link && TREE_VALUE (link);
17550 link = TREE_CHAIN (link))
17551 gen_type_die (TREE_VALUE (link), context_die);
17552 }
17553
17554 /* We want to generate the DIE for TYPE so that we can generate the
17555 die for MEMBER, which has been defined; we will need to refer back
17556 to the member declaration nested within TYPE. If we're trying to
17557 generate minimal debug info for TYPE, processing TYPE won't do the
17558 trick; we need to attach the member declaration by hand. */
17559
17560 static void
17561 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17562 {
17563 gen_type_die (type, context_die);
17564
17565 /* If we're trying to avoid duplicate debug info, we may not have
17566 emitted the member decl for this function. Emit it now. */
17567 if (TYPE_STUB_DECL (type)
17568 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17569 && ! lookup_decl_die (member))
17570 {
17571 dw_die_ref type_die;
17572 gcc_assert (!decl_ultimate_origin (member));
17573
17574 push_decl_scope (type);
17575 type_die = lookup_type_die_strip_naming_typedef (type);
17576 if (TREE_CODE (member) == FUNCTION_DECL)
17577 gen_subprogram_die (member, type_die);
17578 else if (TREE_CODE (member) == FIELD_DECL)
17579 {
17580 /* Ignore the nameless fields that are used to skip bits but handle
17581 C++ anonymous unions and structs. */
17582 if (DECL_NAME (member) != NULL_TREE
17583 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17584 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17585 {
17586 gen_type_die (member_declared_type (member), type_die);
17587 gen_field_die (member, type_die);
17588 }
17589 }
17590 else
17591 gen_variable_die (member, NULL_TREE, type_die);
17592
17593 pop_decl_scope ();
17594 }
17595 }
17596 \f
17597 /* Forward declare these functions, because they are mutually recursive
17598 with their set_block_* pairing functions. */
17599 static void set_decl_origin_self (tree);
17600 static void set_decl_abstract_flags (tree, int);
17601
17602 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17603 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17604 that it points to the node itself, thus indicating that the node is its
17605 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17606 the given node is NULL, recursively descend the decl/block tree which
17607 it is the root of, and for each other ..._DECL or BLOCK node contained
17608 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17609 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17610 values to point to themselves. */
17611
17612 static void
17613 set_block_origin_self (tree stmt)
17614 {
17615 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17616 {
17617 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17618
17619 {
17620 tree local_decl;
17621
17622 for (local_decl = BLOCK_VARS (stmt);
17623 local_decl != NULL_TREE;
17624 local_decl = DECL_CHAIN (local_decl))
17625 if (! DECL_EXTERNAL (local_decl))
17626 set_decl_origin_self (local_decl); /* Potential recursion. */
17627 }
17628
17629 {
17630 tree subblock;
17631
17632 for (subblock = BLOCK_SUBBLOCKS (stmt);
17633 subblock != NULL_TREE;
17634 subblock = BLOCK_CHAIN (subblock))
17635 set_block_origin_self (subblock); /* Recurse. */
17636 }
17637 }
17638 }
17639
17640 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17641 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17642 node to so that it points to the node itself, thus indicating that the
17643 node represents its own (abstract) origin. Additionally, if the
17644 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17645 the decl/block tree of which the given node is the root of, and for
17646 each other ..._DECL or BLOCK node contained therein whose
17647 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17648 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17649 point to themselves. */
17650
17651 static void
17652 set_decl_origin_self (tree decl)
17653 {
17654 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17655 {
17656 DECL_ABSTRACT_ORIGIN (decl) = decl;
17657 if (TREE_CODE (decl) == FUNCTION_DECL)
17658 {
17659 tree arg;
17660
17661 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17662 DECL_ABSTRACT_ORIGIN (arg) = arg;
17663 if (DECL_INITIAL (decl) != NULL_TREE
17664 && DECL_INITIAL (decl) != error_mark_node)
17665 set_block_origin_self (DECL_INITIAL (decl));
17666 }
17667 }
17668 }
17669 \f
17670 /* Given a pointer to some BLOCK node, and a boolean value to set the
17671 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17672 the given block, and for all local decls and all local sub-blocks
17673 (recursively) which are contained therein. */
17674
17675 static void
17676 set_block_abstract_flags (tree stmt, int setting)
17677 {
17678 tree local_decl;
17679 tree subblock;
17680 unsigned int i;
17681
17682 BLOCK_ABSTRACT (stmt) = setting;
17683
17684 for (local_decl = BLOCK_VARS (stmt);
17685 local_decl != NULL_TREE;
17686 local_decl = DECL_CHAIN (local_decl))
17687 if (! DECL_EXTERNAL (local_decl))
17688 set_decl_abstract_flags (local_decl, setting);
17689
17690 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17691 {
17692 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17693 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17694 || TREE_CODE (local_decl) == PARM_DECL)
17695 set_decl_abstract_flags (local_decl, setting);
17696 }
17697
17698 for (subblock = BLOCK_SUBBLOCKS (stmt);
17699 subblock != NULL_TREE;
17700 subblock = BLOCK_CHAIN (subblock))
17701 set_block_abstract_flags (subblock, setting);
17702 }
17703
17704 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17705 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17706 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17707 set the abstract flags for all of the parameters, local vars, local
17708 blocks and sub-blocks (recursively) to the same setting. */
17709
17710 static void
17711 set_decl_abstract_flags (tree decl, int setting)
17712 {
17713 DECL_ABSTRACT (decl) = setting;
17714 if (TREE_CODE (decl) == FUNCTION_DECL)
17715 {
17716 tree arg;
17717
17718 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17719 DECL_ABSTRACT (arg) = setting;
17720 if (DECL_INITIAL (decl) != NULL_TREE
17721 && DECL_INITIAL (decl) != error_mark_node)
17722 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17723 }
17724 }
17725
17726 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17727 may later generate inlined and/or out-of-line instances of. */
17728
17729 static void
17730 dwarf2out_abstract_function (tree decl)
17731 {
17732 dw_die_ref old_die;
17733 tree save_fn;
17734 tree context;
17735 int was_abstract;
17736 htab_t old_decl_loc_table;
17737 htab_t old_cached_dw_loc_list_table;
17738 int old_call_site_count, old_tail_call_site_count;
17739 struct call_arg_loc_node *old_call_arg_locations;
17740
17741 /* Make sure we have the actual abstract inline, not a clone. */
17742 decl = DECL_ORIGIN (decl);
17743
17744 old_die = lookup_decl_die (decl);
17745 if (old_die && get_AT (old_die, DW_AT_inline))
17746 /* We've already generated the abstract instance. */
17747 return;
17748
17749 /* We can be called while recursively when seeing block defining inlined subroutine
17750 DIE. Be sure to not clobber the outer location table nor use it or we would
17751 get locations in abstract instantces. */
17752 old_decl_loc_table = decl_loc_table;
17753 decl_loc_table = NULL;
17754 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17755 cached_dw_loc_list_table = NULL;
17756 old_call_arg_locations = call_arg_locations;
17757 call_arg_locations = NULL;
17758 old_call_site_count = call_site_count;
17759 call_site_count = -1;
17760 old_tail_call_site_count = tail_call_site_count;
17761 tail_call_site_count = -1;
17762
17763 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17764 we don't get confused by DECL_ABSTRACT. */
17765 if (debug_info_level > DINFO_LEVEL_TERSE)
17766 {
17767 context = decl_class_context (decl);
17768 if (context)
17769 gen_type_die_for_member
17770 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17771 }
17772
17773 /* Pretend we've just finished compiling this function. */
17774 save_fn = current_function_decl;
17775 current_function_decl = decl;
17776
17777 was_abstract = DECL_ABSTRACT (decl);
17778 set_decl_abstract_flags (decl, 1);
17779 dwarf2out_decl (decl);
17780 if (! was_abstract)
17781 set_decl_abstract_flags (decl, 0);
17782
17783 current_function_decl = save_fn;
17784 decl_loc_table = old_decl_loc_table;
17785 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17786 call_arg_locations = old_call_arg_locations;
17787 call_site_count = old_call_site_count;
17788 tail_call_site_count = old_tail_call_site_count;
17789 }
17790
17791 /* Helper function of premark_used_types() which gets called through
17792 htab_traverse.
17793
17794 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17795 marked as unused by prune_unused_types. */
17796
17797 static int
17798 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17799 {
17800 tree type;
17801 dw_die_ref die;
17802
17803 type = (tree) *slot;
17804 die = lookup_type_die (type);
17805 if (die != NULL)
17806 die->die_perennial_p = 1;
17807 return 1;
17808 }
17809
17810 /* Helper function of premark_types_used_by_global_vars which gets called
17811 through htab_traverse.
17812
17813 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17814 marked as unused by prune_unused_types. The DIE of the type is marked
17815 only if the global variable using the type will actually be emitted. */
17816
17817 static int
17818 premark_types_used_by_global_vars_helper (void **slot,
17819 void *data ATTRIBUTE_UNUSED)
17820 {
17821 struct types_used_by_vars_entry *entry;
17822 dw_die_ref die;
17823
17824 entry = (struct types_used_by_vars_entry *) *slot;
17825 gcc_assert (entry->type != NULL
17826 && entry->var_decl != NULL);
17827 die = lookup_type_die (entry->type);
17828 if (die)
17829 {
17830 /* Ask cgraph if the global variable really is to be emitted.
17831 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17832 struct varpool_node *node = varpool_get_node (entry->var_decl);
17833 if (node && node->definition)
17834 {
17835 die->die_perennial_p = 1;
17836 /* Keep the parent DIEs as well. */
17837 while ((die = die->die_parent) && die->die_perennial_p == 0)
17838 die->die_perennial_p = 1;
17839 }
17840 }
17841 return 1;
17842 }
17843
17844 /* Mark all members of used_types_hash as perennial. */
17845
17846 static void
17847 premark_used_types (struct function *fun)
17848 {
17849 if (fun && fun->used_types_hash)
17850 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17851 }
17852
17853 /* Mark all members of types_used_by_vars_entry as perennial. */
17854
17855 static void
17856 premark_types_used_by_global_vars (void)
17857 {
17858 if (types_used_by_vars_hash)
17859 htab_traverse (types_used_by_vars_hash,
17860 premark_types_used_by_global_vars_helper, NULL);
17861 }
17862
17863 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17864 for CA_LOC call arg loc node. */
17865
17866 static dw_die_ref
17867 gen_call_site_die (tree decl, dw_die_ref subr_die,
17868 struct call_arg_loc_node *ca_loc)
17869 {
17870 dw_die_ref stmt_die = NULL, die;
17871 tree block = ca_loc->block;
17872
17873 while (block
17874 && block != DECL_INITIAL (decl)
17875 && TREE_CODE (block) == BLOCK)
17876 {
17877 if (block_map.length () > BLOCK_NUMBER (block))
17878 stmt_die = block_map[BLOCK_NUMBER (block)];
17879 if (stmt_die)
17880 break;
17881 block = BLOCK_SUPERCONTEXT (block);
17882 }
17883 if (stmt_die == NULL)
17884 stmt_die = subr_die;
17885 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17886 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17887 if (ca_loc->tail_call_p)
17888 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17889 if (ca_loc->symbol_ref)
17890 {
17891 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17892 if (tdie)
17893 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17894 else
17895 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17896 }
17897 return die;
17898 }
17899
17900 /* Generate a DIE to represent a declared function (either file-scope or
17901 block-local). */
17902
17903 static void
17904 gen_subprogram_die (tree decl, dw_die_ref context_die)
17905 {
17906 tree origin = decl_ultimate_origin (decl);
17907 dw_die_ref subr_die;
17908 tree outer_scope;
17909 dw_die_ref old_die = lookup_decl_die (decl);
17910 int declaration = (current_function_decl != decl
17911 || class_or_namespace_scope_p (context_die));
17912
17913 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17914
17915 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17916 started to generate the abstract instance of an inline, decided to output
17917 its containing class, and proceeded to emit the declaration of the inline
17918 from the member list for the class. If so, DECLARATION takes priority;
17919 we'll get back to the abstract instance when done with the class. */
17920
17921 /* The class-scope declaration DIE must be the primary DIE. */
17922 if (origin && declaration && class_or_namespace_scope_p (context_die))
17923 {
17924 origin = NULL;
17925 gcc_assert (!old_die);
17926 }
17927
17928 /* Now that the C++ front end lazily declares artificial member fns, we
17929 might need to retrofit the declaration into its class. */
17930 if (!declaration && !origin && !old_die
17931 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17932 && !class_or_namespace_scope_p (context_die)
17933 && debug_info_level > DINFO_LEVEL_TERSE)
17934 old_die = force_decl_die (decl);
17935
17936 if (origin != NULL)
17937 {
17938 gcc_assert (!declaration || local_scope_p (context_die));
17939
17940 /* Fixup die_parent for the abstract instance of a nested
17941 inline function. */
17942 if (old_die && old_die->die_parent == NULL)
17943 add_child_die (context_die, old_die);
17944
17945 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17946 add_abstract_origin_attribute (subr_die, origin);
17947 /* This is where the actual code for a cloned function is.
17948 Let's emit linkage name attribute for it. This helps
17949 debuggers to e.g, set breakpoints into
17950 constructors/destructors when the user asks "break
17951 K::K". */
17952 add_linkage_name (subr_die, decl);
17953 }
17954 else if (old_die)
17955 {
17956 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17957 struct dwarf_file_data * file_index = lookup_filename (s.file);
17958
17959 if (!get_AT_flag (old_die, DW_AT_declaration)
17960 /* We can have a normal definition following an inline one in the
17961 case of redefinition of GNU C extern inlines.
17962 It seems reasonable to use AT_specification in this case. */
17963 && !get_AT (old_die, DW_AT_inline))
17964 {
17965 /* Detect and ignore this case, where we are trying to output
17966 something we have already output. */
17967 return;
17968 }
17969
17970 /* If the definition comes from the same place as the declaration,
17971 maybe use the old DIE. We always want the DIE for this function
17972 that has the *_pc attributes to be under comp_unit_die so the
17973 debugger can find it. We also need to do this for abstract
17974 instances of inlines, since the spec requires the out-of-line copy
17975 to have the same parent. For local class methods, this doesn't
17976 apply; we just use the old DIE. */
17977 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17978 && (DECL_ARTIFICIAL (decl)
17979 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17980 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17981 == (unsigned) s.line))))
17982 {
17983 subr_die = old_die;
17984
17985 /* Clear out the declaration attribute and the formal parameters.
17986 Do not remove all children, because it is possible that this
17987 declaration die was forced using force_decl_die(). In such
17988 cases die that forced declaration die (e.g. TAG_imported_module)
17989 is one of the children that we do not want to remove. */
17990 remove_AT (subr_die, DW_AT_declaration);
17991 remove_AT (subr_die, DW_AT_object_pointer);
17992 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17993 }
17994 else
17995 {
17996 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17997 add_AT_specification (subr_die, old_die);
17998 add_pubname (decl, subr_die);
17999 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18000 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18001 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18002 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18003 }
18004 }
18005 else
18006 {
18007 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18008
18009 if (TREE_PUBLIC (decl))
18010 add_AT_flag (subr_die, DW_AT_external, 1);
18011
18012 add_name_and_src_coords_attributes (subr_die, decl);
18013 add_pubname (decl, subr_die);
18014 if (debug_info_level > DINFO_LEVEL_TERSE)
18015 {
18016 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18017 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18018 0, 0, context_die);
18019 }
18020
18021 add_pure_or_virtual_attribute (subr_die, decl);
18022 if (DECL_ARTIFICIAL (decl))
18023 add_AT_flag (subr_die, DW_AT_artificial, 1);
18024
18025 add_accessibility_attribute (subr_die, decl);
18026 }
18027
18028 if (declaration)
18029 {
18030 if (!old_die || !get_AT (old_die, DW_AT_inline))
18031 {
18032 add_AT_flag (subr_die, DW_AT_declaration, 1);
18033
18034 /* If this is an explicit function declaration then generate
18035 a DW_AT_explicit attribute. */
18036 if (lang_hooks.decls.function_decl_explicit_p (decl)
18037 && (dwarf_version >= 3 || !dwarf_strict))
18038 add_AT_flag (subr_die, DW_AT_explicit, 1);
18039
18040 /* The first time we see a member function, it is in the context of
18041 the class to which it belongs. We make sure of this by emitting
18042 the class first. The next time is the definition, which is
18043 handled above. The two may come from the same source text.
18044
18045 Note that force_decl_die() forces function declaration die. It is
18046 later reused to represent definition. */
18047 equate_decl_number_to_die (decl, subr_die);
18048 }
18049 }
18050 else if (DECL_ABSTRACT (decl))
18051 {
18052 if (DECL_DECLARED_INLINE_P (decl))
18053 {
18054 if (cgraph_function_possibly_inlined_p (decl))
18055 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18056 else
18057 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18058 }
18059 else
18060 {
18061 if (cgraph_function_possibly_inlined_p (decl))
18062 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18063 else
18064 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18065 }
18066
18067 if (DECL_DECLARED_INLINE_P (decl)
18068 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18069 add_AT_flag (subr_die, DW_AT_artificial, 1);
18070
18071 equate_decl_number_to_die (decl, subr_die);
18072 }
18073 else if (!DECL_EXTERNAL (decl))
18074 {
18075 HOST_WIDE_INT cfa_fb_offset;
18076 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18077
18078 if (!old_die || !get_AT (old_die, DW_AT_inline))
18079 equate_decl_number_to_die (decl, subr_die);
18080
18081 gcc_checking_assert (fun);
18082 if (!flag_reorder_blocks_and_partition)
18083 {
18084 dw_fde_ref fde = fun->fde;
18085 if (fde->dw_fde_begin)
18086 {
18087 /* We have already generated the labels. */
18088 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18089 fde->dw_fde_end, false);
18090 }
18091 else
18092 {
18093 /* Create start/end labels and add the range. */
18094 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18095 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18096 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18097 current_function_funcdef_no);
18098 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18099 current_function_funcdef_no);
18100 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18101 false);
18102 }
18103
18104 #if VMS_DEBUGGING_INFO
18105 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18106 Section 2.3 Prologue and Epilogue Attributes:
18107 When a breakpoint is set on entry to a function, it is generally
18108 desirable for execution to be suspended, not on the very first
18109 instruction of the function, but rather at a point after the
18110 function's frame has been set up, after any language defined local
18111 declaration processing has been completed, and before execution of
18112 the first statement of the function begins. Debuggers generally
18113 cannot properly determine where this point is. Similarly for a
18114 breakpoint set on exit from a function. The prologue and epilogue
18115 attributes allow a compiler to communicate the location(s) to use. */
18116
18117 {
18118 if (fde->dw_fde_vms_end_prologue)
18119 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18120 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18121
18122 if (fde->dw_fde_vms_begin_epilogue)
18123 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18124 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18125 }
18126 #endif
18127
18128 }
18129 else
18130 {
18131 /* Generate pubnames entries for the split function code ranges. */
18132 dw_fde_ref fde = fun->fde;
18133
18134 if (fde->dw_fde_second_begin)
18135 {
18136 if (dwarf_version >= 3 || !dwarf_strict)
18137 {
18138 /* We should use ranges for non-contiguous code section
18139 addresses. Use the actual code range for the initial
18140 section, since the HOT/COLD labels might precede an
18141 alignment offset. */
18142 bool range_list_added = false;
18143 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18144 fde->dw_fde_end, &range_list_added,
18145 false);
18146 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18147 fde->dw_fde_second_end,
18148 &range_list_added, false);
18149 if (range_list_added)
18150 add_ranges (NULL);
18151 }
18152 else
18153 {
18154 /* There is no real support in DW2 for this .. so we make
18155 a work-around. First, emit the pub name for the segment
18156 containing the function label. Then make and emit a
18157 simplified subprogram DIE for the second segment with the
18158 name pre-fixed by __hot/cold_sect_of_. We use the same
18159 linkage name for the second die so that gdb will find both
18160 sections when given "b foo". */
18161 const char *name = NULL;
18162 tree decl_name = DECL_NAME (decl);
18163 dw_die_ref seg_die;
18164
18165 /* Do the 'primary' section. */
18166 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18167 fde->dw_fde_end, false);
18168
18169 /* Build a minimal DIE for the secondary section. */
18170 seg_die = new_die (DW_TAG_subprogram,
18171 subr_die->die_parent, decl);
18172
18173 if (TREE_PUBLIC (decl))
18174 add_AT_flag (seg_die, DW_AT_external, 1);
18175
18176 if (decl_name != NULL
18177 && IDENTIFIER_POINTER (decl_name) != NULL)
18178 {
18179 name = dwarf2_name (decl, 1);
18180 if (! DECL_ARTIFICIAL (decl))
18181 add_src_coords_attributes (seg_die, decl);
18182
18183 add_linkage_name (seg_die, decl);
18184 }
18185 gcc_assert (name != NULL);
18186 add_pure_or_virtual_attribute (seg_die, decl);
18187 if (DECL_ARTIFICIAL (decl))
18188 add_AT_flag (seg_die, DW_AT_artificial, 1);
18189
18190 name = concat ("__second_sect_of_", name, NULL);
18191 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18192 fde->dw_fde_second_end, false);
18193 add_name_attribute (seg_die, name);
18194 if (want_pubnames ())
18195 add_pubname_string (name, seg_die);
18196 }
18197 }
18198 else
18199 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18200 false);
18201 }
18202
18203 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18204
18205 /* We define the "frame base" as the function's CFA. This is more
18206 convenient for several reasons: (1) It's stable across the prologue
18207 and epilogue, which makes it better than just a frame pointer,
18208 (2) With dwarf3, there exists a one-byte encoding that allows us
18209 to reference the .debug_frame data by proxy, but failing that,
18210 (3) We can at least reuse the code inspection and interpretation
18211 code that determines the CFA position at various points in the
18212 function. */
18213 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18214 {
18215 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18216 add_AT_loc (subr_die, DW_AT_frame_base, op);
18217 }
18218 else
18219 {
18220 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18221 if (list->dw_loc_next)
18222 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18223 else
18224 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18225 }
18226
18227 /* Compute a displacement from the "steady-state frame pointer" to
18228 the CFA. The former is what all stack slots and argument slots
18229 will reference in the rtl; the latter is what we've told the
18230 debugger about. We'll need to adjust all frame_base references
18231 by this displacement. */
18232 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18233
18234 if (fun->static_chain_decl)
18235 add_AT_location_description (subr_die, DW_AT_static_link,
18236 loc_list_from_tree (fun->static_chain_decl, 2));
18237 }
18238
18239 /* Generate child dies for template paramaters. */
18240 if (debug_info_level > DINFO_LEVEL_TERSE)
18241 gen_generic_params_dies (decl);
18242
18243 /* Now output descriptions of the arguments for this function. This gets
18244 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18245 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18246 `...' at the end of the formal parameter list. In order to find out if
18247 there was a trailing ellipsis or not, we must instead look at the type
18248 associated with the FUNCTION_DECL. This will be a node of type
18249 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18250 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18251 an ellipsis at the end. */
18252
18253 /* In the case where we are describing a mere function declaration, all we
18254 need to do here (and all we *can* do here) is to describe the *types* of
18255 its formal parameters. */
18256 if (debug_info_level <= DINFO_LEVEL_TERSE)
18257 ;
18258 else if (declaration)
18259 gen_formal_types_die (decl, subr_die);
18260 else
18261 {
18262 /* Generate DIEs to represent all known formal parameters. */
18263 tree parm = DECL_ARGUMENTS (decl);
18264 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18265 tree generic_decl_parm = generic_decl
18266 ? DECL_ARGUMENTS (generic_decl)
18267 : NULL;
18268
18269 /* Now we want to walk the list of parameters of the function and
18270 emit their relevant DIEs.
18271
18272 We consider the case of DECL being an instance of a generic function
18273 as well as it being a normal function.
18274
18275 If DECL is an instance of a generic function we walk the
18276 parameters of the generic function declaration _and_ the parameters of
18277 DECL itself. This is useful because we want to emit specific DIEs for
18278 function parameter packs and those are declared as part of the
18279 generic function declaration. In that particular case,
18280 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18281 That DIE has children DIEs representing the set of arguments
18282 of the pack. Note that the set of pack arguments can be empty.
18283 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18284 children DIE.
18285
18286 Otherwise, we just consider the parameters of DECL. */
18287 while (generic_decl_parm || parm)
18288 {
18289 if (generic_decl_parm
18290 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18291 gen_formal_parameter_pack_die (generic_decl_parm,
18292 parm, subr_die,
18293 &parm);
18294 else if (parm && !POINTER_BOUNDS_P (parm))
18295 {
18296 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18297
18298 if (parm == DECL_ARGUMENTS (decl)
18299 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18300 && parm_die
18301 && (dwarf_version >= 3 || !dwarf_strict))
18302 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18303
18304 parm = DECL_CHAIN (parm);
18305 }
18306 else if (parm)
18307 parm = DECL_CHAIN (parm);
18308
18309 if (generic_decl_parm)
18310 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18311 }
18312
18313 /* Decide whether we need an unspecified_parameters DIE at the end.
18314 There are 2 more cases to do this for: 1) the ansi ... declaration -
18315 this is detectable when the end of the arg list is not a
18316 void_type_node 2) an unprototyped function declaration (not a
18317 definition). This just means that we have no info about the
18318 parameters at all. */
18319 if (prototype_p (TREE_TYPE (decl)))
18320 {
18321 /* This is the prototyped case, check for.... */
18322 if (stdarg_p (TREE_TYPE (decl)))
18323 gen_unspecified_parameters_die (decl, subr_die);
18324 }
18325 else if (DECL_INITIAL (decl) == NULL_TREE)
18326 gen_unspecified_parameters_die (decl, subr_die);
18327 }
18328
18329 /* Output Dwarf info for all of the stuff within the body of the function
18330 (if it has one - it may be just a declaration). */
18331 outer_scope = DECL_INITIAL (decl);
18332
18333 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18334 a function. This BLOCK actually represents the outermost binding contour
18335 for the function, i.e. the contour in which the function's formal
18336 parameters and labels get declared. Curiously, it appears that the front
18337 end doesn't actually put the PARM_DECL nodes for the current function onto
18338 the BLOCK_VARS list for this outer scope, but are strung off of the
18339 DECL_ARGUMENTS list for the function instead.
18340
18341 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18342 the LABEL_DECL nodes for the function however, and we output DWARF info
18343 for those in decls_for_scope. Just within the `outer_scope' there will be
18344 a BLOCK node representing the function's outermost pair of curly braces,
18345 and any blocks used for the base and member initializers of a C++
18346 constructor function. */
18347 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18348 {
18349 int call_site_note_count = 0;
18350 int tail_call_site_note_count = 0;
18351
18352 /* Emit a DW_TAG_variable DIE for a named return value. */
18353 if (DECL_NAME (DECL_RESULT (decl)))
18354 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18355
18356 current_function_has_inlines = 0;
18357 decls_for_scope (outer_scope, subr_die, 0);
18358
18359 if (call_arg_locations && !dwarf_strict)
18360 {
18361 struct call_arg_loc_node *ca_loc;
18362 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18363 {
18364 dw_die_ref die = NULL;
18365 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18366 rtx arg, next_arg;
18367
18368 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18369 arg; arg = next_arg)
18370 {
18371 dw_loc_descr_ref reg, val;
18372 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18373 dw_die_ref cdie, tdie = NULL;
18374
18375 next_arg = XEXP (arg, 1);
18376 if (REG_P (XEXP (XEXP (arg, 0), 0))
18377 && next_arg
18378 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18379 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18380 && REGNO (XEXP (XEXP (arg, 0), 0))
18381 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18382 next_arg = XEXP (next_arg, 1);
18383 if (mode == VOIDmode)
18384 {
18385 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18386 if (mode == VOIDmode)
18387 mode = GET_MODE (XEXP (arg, 0));
18388 }
18389 if (mode == VOIDmode || mode == BLKmode)
18390 continue;
18391 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18392 {
18393 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18394 tloc = XEXP (XEXP (arg, 0), 1);
18395 continue;
18396 }
18397 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18398 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18399 {
18400 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18401 tlocc = XEXP (XEXP (arg, 0), 1);
18402 continue;
18403 }
18404 reg = NULL;
18405 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18406 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18407 VAR_INIT_STATUS_INITIALIZED);
18408 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18409 {
18410 rtx mem = XEXP (XEXP (arg, 0), 0);
18411 reg = mem_loc_descriptor (XEXP (mem, 0),
18412 get_address_mode (mem),
18413 GET_MODE (mem),
18414 VAR_INIT_STATUS_INITIALIZED);
18415 }
18416 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18417 == DEBUG_PARAMETER_REF)
18418 {
18419 tree tdecl
18420 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18421 tdie = lookup_decl_die (tdecl);
18422 if (tdie == NULL)
18423 continue;
18424 }
18425 else
18426 continue;
18427 if (reg == NULL
18428 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18429 != DEBUG_PARAMETER_REF)
18430 continue;
18431 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18432 VOIDmode,
18433 VAR_INIT_STATUS_INITIALIZED);
18434 if (val == NULL)
18435 continue;
18436 if (die == NULL)
18437 die = gen_call_site_die (decl, subr_die, ca_loc);
18438 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18439 NULL_TREE);
18440 if (reg != NULL)
18441 add_AT_loc (cdie, DW_AT_location, reg);
18442 else if (tdie != NULL)
18443 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18444 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18445 if (next_arg != XEXP (arg, 1))
18446 {
18447 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18448 if (mode == VOIDmode)
18449 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18450 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18451 0), 1),
18452 mode, VOIDmode,
18453 VAR_INIT_STATUS_INITIALIZED);
18454 if (val != NULL)
18455 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18456 }
18457 }
18458 if (die == NULL
18459 && (ca_loc->symbol_ref || tloc))
18460 die = gen_call_site_die (decl, subr_die, ca_loc);
18461 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18462 {
18463 dw_loc_descr_ref tval = NULL;
18464
18465 if (tloc != NULL_RTX)
18466 tval = mem_loc_descriptor (tloc,
18467 GET_MODE (tloc) == VOIDmode
18468 ? Pmode : GET_MODE (tloc),
18469 VOIDmode,
18470 VAR_INIT_STATUS_INITIALIZED);
18471 if (tval)
18472 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18473 else if (tlocc != NULL_RTX)
18474 {
18475 tval = mem_loc_descriptor (tlocc,
18476 GET_MODE (tlocc) == VOIDmode
18477 ? Pmode : GET_MODE (tlocc),
18478 VOIDmode,
18479 VAR_INIT_STATUS_INITIALIZED);
18480 if (tval)
18481 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18482 tval);
18483 }
18484 }
18485 if (die != NULL)
18486 {
18487 call_site_note_count++;
18488 if (ca_loc->tail_call_p)
18489 tail_call_site_note_count++;
18490 }
18491 }
18492 }
18493 call_arg_locations = NULL;
18494 call_arg_loc_last = NULL;
18495 if (tail_call_site_count >= 0
18496 && tail_call_site_count == tail_call_site_note_count
18497 && !dwarf_strict)
18498 {
18499 if (call_site_count >= 0
18500 && call_site_count == call_site_note_count)
18501 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18502 else
18503 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18504 }
18505 call_site_count = -1;
18506 tail_call_site_count = -1;
18507 }
18508 /* Add the calling convention attribute if requested. */
18509 add_calling_convention_attribute (subr_die, decl);
18510
18511 }
18512
18513 /* Returns a hash value for X (which really is a die_struct). */
18514
18515 static hashval_t
18516 common_block_die_table_hash (const void *x)
18517 {
18518 const_dw_die_ref d = (const_dw_die_ref) x;
18519 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18520 }
18521
18522 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18523 as decl_id and die_parent of die_struct Y. */
18524
18525 static int
18526 common_block_die_table_eq (const void *x, const void *y)
18527 {
18528 const_dw_die_ref d = (const_dw_die_ref) x;
18529 const_dw_die_ref e = (const_dw_die_ref) y;
18530 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18531 }
18532
18533 /* Generate a DIE to represent a declared data object.
18534 Either DECL or ORIGIN must be non-null. */
18535
18536 static void
18537 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18538 {
18539 HOST_WIDE_INT off = 0;
18540 tree com_decl;
18541 tree decl_or_origin = decl ? decl : origin;
18542 tree ultimate_origin;
18543 dw_die_ref var_die;
18544 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18545 dw_die_ref origin_die;
18546 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18547 || class_or_namespace_scope_p (context_die));
18548 bool specialization_p = false;
18549
18550 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18551 if (decl || ultimate_origin)
18552 origin = ultimate_origin;
18553 com_decl = fortran_common (decl_or_origin, &off);
18554
18555 /* Symbol in common gets emitted as a child of the common block, in the form
18556 of a data member. */
18557 if (com_decl)
18558 {
18559 dw_die_ref com_die;
18560 dw_loc_list_ref loc;
18561 die_node com_die_arg;
18562
18563 var_die = lookup_decl_die (decl_or_origin);
18564 if (var_die)
18565 {
18566 if (get_AT (var_die, DW_AT_location) == NULL)
18567 {
18568 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18569 if (loc)
18570 {
18571 if (off)
18572 {
18573 /* Optimize the common case. */
18574 if (single_element_loc_list_p (loc)
18575 && loc->expr->dw_loc_opc == DW_OP_addr
18576 && loc->expr->dw_loc_next == NULL
18577 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18578 == SYMBOL_REF)
18579 {
18580 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18581 loc->expr->dw_loc_oprnd1.v.val_addr
18582 = plus_constant (GET_MODE (x), x , off);
18583 }
18584 else
18585 loc_list_plus_const (loc, off);
18586 }
18587 add_AT_location_description (var_die, DW_AT_location, loc);
18588 remove_AT (var_die, DW_AT_declaration);
18589 }
18590 }
18591 return;
18592 }
18593
18594 if (common_block_die_table == NULL)
18595 common_block_die_table
18596 = htab_create_ggc (10, common_block_die_table_hash,
18597 common_block_die_table_eq, NULL);
18598
18599 com_die_arg.decl_id = DECL_UID (com_decl);
18600 com_die_arg.die_parent = context_die;
18601 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18602 loc = loc_list_from_tree (com_decl, 2);
18603 if (com_die == NULL)
18604 {
18605 const char *cnam
18606 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18607 void **slot;
18608
18609 com_die = new_die (DW_TAG_common_block, context_die, decl);
18610 add_name_and_src_coords_attributes (com_die, com_decl);
18611 if (loc)
18612 {
18613 add_AT_location_description (com_die, DW_AT_location, loc);
18614 /* Avoid sharing the same loc descriptor between
18615 DW_TAG_common_block and DW_TAG_variable. */
18616 loc = loc_list_from_tree (com_decl, 2);
18617 }
18618 else if (DECL_EXTERNAL (decl))
18619 add_AT_flag (com_die, DW_AT_declaration, 1);
18620 if (want_pubnames ())
18621 add_pubname_string (cnam, com_die); /* ??? needed? */
18622 com_die->decl_id = DECL_UID (com_decl);
18623 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18624 *slot = (void *) com_die;
18625 }
18626 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18627 {
18628 add_AT_location_description (com_die, DW_AT_location, loc);
18629 loc = loc_list_from_tree (com_decl, 2);
18630 remove_AT (com_die, DW_AT_declaration);
18631 }
18632 var_die = new_die (DW_TAG_variable, com_die, decl);
18633 add_name_and_src_coords_attributes (var_die, decl);
18634 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18635 TREE_THIS_VOLATILE (decl), context_die);
18636 add_AT_flag (var_die, DW_AT_external, 1);
18637 if (loc)
18638 {
18639 if (off)
18640 {
18641 /* Optimize the common case. */
18642 if (single_element_loc_list_p (loc)
18643 && loc->expr->dw_loc_opc == DW_OP_addr
18644 && loc->expr->dw_loc_next == NULL
18645 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18646 {
18647 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18648 loc->expr->dw_loc_oprnd1.v.val_addr
18649 = plus_constant (GET_MODE (x), x, off);
18650 }
18651 else
18652 loc_list_plus_const (loc, off);
18653 }
18654 add_AT_location_description (var_die, DW_AT_location, loc);
18655 }
18656 else if (DECL_EXTERNAL (decl))
18657 add_AT_flag (var_die, DW_AT_declaration, 1);
18658 equate_decl_number_to_die (decl, var_die);
18659 return;
18660 }
18661
18662 /* If the compiler emitted a definition for the DECL declaration
18663 and if we already emitted a DIE for it, don't emit a second
18664 DIE for it again. Allow re-declarations of DECLs that are
18665 inside functions, though. */
18666 if (old_die && declaration && !local_scope_p (context_die))
18667 return;
18668
18669 /* For static data members, the declaration in the class is supposed
18670 to have DW_TAG_member tag; the specification should still be
18671 DW_TAG_variable referencing the DW_TAG_member DIE. */
18672 if (declaration && class_scope_p (context_die))
18673 var_die = new_die (DW_TAG_member, context_die, decl);
18674 else
18675 var_die = new_die (DW_TAG_variable, context_die, decl);
18676
18677 origin_die = NULL;
18678 if (origin != NULL)
18679 origin_die = add_abstract_origin_attribute (var_die, origin);
18680
18681 /* Loop unrolling can create multiple blocks that refer to the same
18682 static variable, so we must test for the DW_AT_declaration flag.
18683
18684 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18685 copy decls and set the DECL_ABSTRACT flag on them instead of
18686 sharing them.
18687
18688 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18689
18690 ??? The declare_in_namespace support causes us to get two DIEs for one
18691 variable, both of which are declarations. We want to avoid considering
18692 one to be a specification, so we must test that this DIE is not a
18693 declaration. */
18694 else if (old_die && TREE_STATIC (decl) && ! declaration
18695 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18696 {
18697 /* This is a definition of a C++ class level static. */
18698 add_AT_specification (var_die, old_die);
18699 specialization_p = true;
18700 if (DECL_NAME (decl))
18701 {
18702 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18703 struct dwarf_file_data * file_index = lookup_filename (s.file);
18704
18705 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18706 add_AT_file (var_die, DW_AT_decl_file, file_index);
18707
18708 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18709 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18710
18711 if (old_die->die_tag == DW_TAG_member)
18712 add_linkage_name (var_die, decl);
18713 }
18714 }
18715 else
18716 add_name_and_src_coords_attributes (var_die, decl);
18717
18718 if ((origin == NULL && !specialization_p)
18719 || (origin != NULL
18720 && !DECL_ABSTRACT (decl_or_origin)
18721 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18722 decl_function_context
18723 (decl_or_origin))))
18724 {
18725 tree type = TREE_TYPE (decl_or_origin);
18726
18727 if (decl_by_reference_p (decl_or_origin))
18728 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18729 else
18730 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18731 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18732 }
18733
18734 if (origin == NULL && !specialization_p)
18735 {
18736 if (TREE_PUBLIC (decl))
18737 add_AT_flag (var_die, DW_AT_external, 1);
18738
18739 if (DECL_ARTIFICIAL (decl))
18740 add_AT_flag (var_die, DW_AT_artificial, 1);
18741
18742 add_accessibility_attribute (var_die, decl);
18743 }
18744
18745 if (declaration)
18746 add_AT_flag (var_die, DW_AT_declaration, 1);
18747
18748 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18749 equate_decl_number_to_die (decl, var_die);
18750
18751 if (! declaration
18752 && (! DECL_ABSTRACT (decl_or_origin)
18753 /* Local static vars are shared between all clones/inlines,
18754 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18755 already set. */
18756 || (TREE_CODE (decl_or_origin) == VAR_DECL
18757 && TREE_STATIC (decl_or_origin)
18758 && DECL_RTL_SET_P (decl_or_origin)))
18759 /* When abstract origin already has DW_AT_location attribute, no need
18760 to add it again. */
18761 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18762 {
18763 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18764 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18765 defer_location (decl_or_origin, var_die);
18766 else
18767 add_location_or_const_value_attribute (var_die, decl_or_origin,
18768 decl == NULL, DW_AT_location);
18769 add_pubname (decl_or_origin, var_die);
18770 }
18771 else
18772 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18773 }
18774
18775 /* Generate a DIE to represent a named constant. */
18776
18777 static void
18778 gen_const_die (tree decl, dw_die_ref context_die)
18779 {
18780 dw_die_ref const_die;
18781 tree type = TREE_TYPE (decl);
18782
18783 const_die = new_die (DW_TAG_constant, context_die, decl);
18784 add_name_and_src_coords_attributes (const_die, decl);
18785 add_type_attribute (const_die, type, 1, 0, context_die);
18786 if (TREE_PUBLIC (decl))
18787 add_AT_flag (const_die, DW_AT_external, 1);
18788 if (DECL_ARTIFICIAL (decl))
18789 add_AT_flag (const_die, DW_AT_artificial, 1);
18790 tree_add_const_value_attribute_for_decl (const_die, decl);
18791 }
18792
18793 /* Generate a DIE to represent a label identifier. */
18794
18795 static void
18796 gen_label_die (tree decl, dw_die_ref context_die)
18797 {
18798 tree origin = decl_ultimate_origin (decl);
18799 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18800 rtx insn;
18801 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18802
18803 if (origin != NULL)
18804 add_abstract_origin_attribute (lbl_die, origin);
18805 else
18806 add_name_and_src_coords_attributes (lbl_die, decl);
18807
18808 if (DECL_ABSTRACT (decl))
18809 equate_decl_number_to_die (decl, lbl_die);
18810 else
18811 {
18812 insn = DECL_RTL_IF_SET (decl);
18813
18814 /* Deleted labels are programmer specified labels which have been
18815 eliminated because of various optimizations. We still emit them
18816 here so that it is possible to put breakpoints on them. */
18817 if (insn
18818 && (LABEL_P (insn)
18819 || ((NOTE_P (insn)
18820 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18821 {
18822 /* When optimization is enabled (via -O) some parts of the compiler
18823 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18824 represent source-level labels which were explicitly declared by
18825 the user. This really shouldn't be happening though, so catch
18826 it if it ever does happen. */
18827 gcc_assert (!INSN_DELETED_P (insn));
18828
18829 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18830 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18831 }
18832 else if (insn
18833 && NOTE_P (insn)
18834 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18835 && CODE_LABEL_NUMBER (insn) != -1)
18836 {
18837 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18838 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18839 }
18840 }
18841 }
18842
18843 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18844 attributes to the DIE for a block STMT, to describe where the inlined
18845 function was called from. This is similar to add_src_coords_attributes. */
18846
18847 static inline void
18848 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18849 {
18850 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18851
18852 if (dwarf_version >= 3 || !dwarf_strict)
18853 {
18854 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18855 add_AT_unsigned (die, DW_AT_call_line, s.line);
18856 }
18857 }
18858
18859
18860 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18861 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18862
18863 static inline void
18864 add_high_low_attributes (tree stmt, dw_die_ref die)
18865 {
18866 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18867
18868 if (BLOCK_FRAGMENT_CHAIN (stmt)
18869 && (dwarf_version >= 3 || !dwarf_strict))
18870 {
18871 tree chain, superblock = NULL_TREE;
18872 dw_die_ref pdie;
18873 dw_attr_ref attr = NULL;
18874
18875 if (inlined_function_outer_scope_p (stmt))
18876 {
18877 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18878 BLOCK_NUMBER (stmt));
18879 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18880 }
18881
18882 /* Optimize duplicate .debug_ranges lists or even tails of
18883 lists. If this BLOCK has same ranges as its supercontext,
18884 lookup DW_AT_ranges attribute in the supercontext (and
18885 recursively so), verify that the ranges_table contains the
18886 right values and use it instead of adding a new .debug_range. */
18887 for (chain = stmt, pdie = die;
18888 BLOCK_SAME_RANGE (chain);
18889 chain = BLOCK_SUPERCONTEXT (chain))
18890 {
18891 dw_attr_ref new_attr;
18892
18893 pdie = pdie->die_parent;
18894 if (pdie == NULL)
18895 break;
18896 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18897 break;
18898 new_attr = get_AT (pdie, DW_AT_ranges);
18899 if (new_attr == NULL
18900 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18901 break;
18902 attr = new_attr;
18903 superblock = BLOCK_SUPERCONTEXT (chain);
18904 }
18905 if (attr != NULL
18906 && (ranges_table[attr->dw_attr_val.v.val_offset
18907 / 2 / DWARF2_ADDR_SIZE].num
18908 == BLOCK_NUMBER (superblock))
18909 && BLOCK_FRAGMENT_CHAIN (superblock))
18910 {
18911 unsigned long off = attr->dw_attr_val.v.val_offset
18912 / 2 / DWARF2_ADDR_SIZE;
18913 unsigned long supercnt = 0, thiscnt = 0;
18914 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18915 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18916 {
18917 ++supercnt;
18918 gcc_checking_assert (ranges_table[off + supercnt].num
18919 == BLOCK_NUMBER (chain));
18920 }
18921 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18922 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18923 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18924 ++thiscnt;
18925 gcc_assert (supercnt >= thiscnt);
18926 add_AT_range_list (die, DW_AT_ranges,
18927 ((off + supercnt - thiscnt)
18928 * 2 * DWARF2_ADDR_SIZE),
18929 false);
18930 return;
18931 }
18932
18933 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18934
18935 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18936 do
18937 {
18938 add_ranges (chain);
18939 chain = BLOCK_FRAGMENT_CHAIN (chain);
18940 }
18941 while (chain);
18942 add_ranges (NULL);
18943 }
18944 else
18945 {
18946 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18947 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18948 BLOCK_NUMBER (stmt));
18949 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18950 BLOCK_NUMBER (stmt));
18951 add_AT_low_high_pc (die, label, label_high, false);
18952 }
18953 }
18954
18955 /* Generate a DIE for a lexical block. */
18956
18957 static void
18958 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18959 {
18960 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18961
18962 if (call_arg_locations)
18963 {
18964 if (block_map.length () <= BLOCK_NUMBER (stmt))
18965 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18966 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18967 }
18968
18969 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18970 add_high_low_attributes (stmt, stmt_die);
18971
18972 decls_for_scope (stmt, stmt_die, depth);
18973 }
18974
18975 /* Generate a DIE for an inlined subprogram. */
18976
18977 static void
18978 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18979 {
18980 tree decl;
18981
18982 /* The instance of function that is effectively being inlined shall not
18983 be abstract. */
18984 gcc_assert (! BLOCK_ABSTRACT (stmt));
18985
18986 decl = block_ultimate_origin (stmt);
18987
18988 /* Emit info for the abstract instance first, if we haven't yet. We
18989 must emit this even if the block is abstract, otherwise when we
18990 emit the block below (or elsewhere), we may end up trying to emit
18991 a die whose origin die hasn't been emitted, and crashing. */
18992 dwarf2out_abstract_function (decl);
18993
18994 if (! BLOCK_ABSTRACT (stmt))
18995 {
18996 dw_die_ref subr_die
18997 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18998
18999 if (call_arg_locations)
19000 {
19001 if (block_map.length () <= BLOCK_NUMBER (stmt))
19002 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19003 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19004 }
19005 add_abstract_origin_attribute (subr_die, decl);
19006 if (TREE_ASM_WRITTEN (stmt))
19007 add_high_low_attributes (stmt, subr_die);
19008 add_call_src_coords_attributes (stmt, subr_die);
19009
19010 decls_for_scope (stmt, subr_die, depth);
19011 current_function_has_inlines = 1;
19012 }
19013 }
19014
19015 /* Generate a DIE for a field in a record, or structure. */
19016
19017 static void
19018 gen_field_die (tree decl, dw_die_ref context_die)
19019 {
19020 dw_die_ref decl_die;
19021
19022 if (TREE_TYPE (decl) == error_mark_node)
19023 return;
19024
19025 decl_die = new_die (DW_TAG_member, context_die, decl);
19026 add_name_and_src_coords_attributes (decl_die, decl);
19027 add_type_attribute (decl_die, member_declared_type (decl),
19028 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19029 context_die);
19030
19031 if (DECL_BIT_FIELD_TYPE (decl))
19032 {
19033 add_byte_size_attribute (decl_die, decl);
19034 add_bit_size_attribute (decl_die, decl);
19035 add_bit_offset_attribute (decl_die, decl);
19036 }
19037
19038 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19039 add_data_member_location_attribute (decl_die, decl);
19040
19041 if (DECL_ARTIFICIAL (decl))
19042 add_AT_flag (decl_die, DW_AT_artificial, 1);
19043
19044 add_accessibility_attribute (decl_die, decl);
19045
19046 /* Equate decl number to die, so that we can look up this decl later on. */
19047 equate_decl_number_to_die (decl, decl_die);
19048 }
19049
19050 #if 0
19051 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19052 Use modified_type_die instead.
19053 We keep this code here just in case these types of DIEs may be needed to
19054 represent certain things in other languages (e.g. Pascal) someday. */
19055
19056 static void
19057 gen_pointer_type_die (tree type, dw_die_ref context_die)
19058 {
19059 dw_die_ref ptr_die
19060 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19061
19062 equate_type_number_to_die (type, ptr_die);
19063 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19064 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19065 }
19066
19067 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19068 Use modified_type_die instead.
19069 We keep this code here just in case these types of DIEs may be needed to
19070 represent certain things in other languages (e.g. Pascal) someday. */
19071
19072 static void
19073 gen_reference_type_die (tree type, dw_die_ref context_die)
19074 {
19075 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19076
19077 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19078 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19079 else
19080 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19081
19082 equate_type_number_to_die (type, ref_die);
19083 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19084 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19085 }
19086 #endif
19087
19088 /* Generate a DIE for a pointer to a member type. */
19089
19090 static void
19091 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19092 {
19093 dw_die_ref ptr_die
19094 = new_die (DW_TAG_ptr_to_member_type,
19095 scope_die_for (type, context_die), type);
19096
19097 equate_type_number_to_die (type, ptr_die);
19098 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19099 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19100 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19101 }
19102
19103 typedef const char *dchar_p; /* For DEF_VEC_P. */
19104
19105 static char *producer_string;
19106
19107 /* Return a heap allocated producer string including command line options
19108 if -grecord-gcc-switches. */
19109
19110 static char *
19111 gen_producer_string (void)
19112 {
19113 size_t j;
19114 auto_vec<dchar_p> switches;
19115 const char *language_string = lang_hooks.name;
19116 char *producer, *tail;
19117 const char *p;
19118 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19119 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19120
19121 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19122 switch (save_decoded_options[j].opt_index)
19123 {
19124 case OPT_o:
19125 case OPT_d:
19126 case OPT_dumpbase:
19127 case OPT_dumpdir:
19128 case OPT_auxbase:
19129 case OPT_auxbase_strip:
19130 case OPT_quiet:
19131 case OPT_version:
19132 case OPT_v:
19133 case OPT_w:
19134 case OPT_L:
19135 case OPT_D:
19136 case OPT_I:
19137 case OPT_U:
19138 case OPT_SPECIAL_unknown:
19139 case OPT_SPECIAL_ignore:
19140 case OPT_SPECIAL_program_name:
19141 case OPT_SPECIAL_input_file:
19142 case OPT_grecord_gcc_switches:
19143 case OPT_gno_record_gcc_switches:
19144 case OPT__output_pch_:
19145 case OPT_fdiagnostics_show_location_:
19146 case OPT_fdiagnostics_show_option:
19147 case OPT_fdiagnostics_show_caret:
19148 case OPT_fdiagnostics_color_:
19149 case OPT_fverbose_asm:
19150 case OPT____:
19151 case OPT__sysroot_:
19152 case OPT_nostdinc:
19153 case OPT_nostdinc__:
19154 /* Ignore these. */
19155 continue;
19156 default:
19157 if (cl_options[save_decoded_options[j].opt_index].flags
19158 & CL_NO_DWARF_RECORD)
19159 continue;
19160 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19161 == '-');
19162 switch (save_decoded_options[j].canonical_option[0][1])
19163 {
19164 case 'M':
19165 case 'i':
19166 case 'W':
19167 continue;
19168 case 'f':
19169 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19170 "dump", 4) == 0)
19171 continue;
19172 break;
19173 default:
19174 break;
19175 }
19176 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19177 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19178 break;
19179 }
19180
19181 producer = XNEWVEC (char, plen + 1 + len + 1);
19182 tail = producer;
19183 sprintf (tail, "%s %s", language_string, version_string);
19184 tail += plen;
19185
19186 FOR_EACH_VEC_ELT (switches, j, p)
19187 {
19188 len = strlen (p);
19189 *tail = ' ';
19190 memcpy (tail + 1, p, len);
19191 tail += len + 1;
19192 }
19193
19194 *tail = '\0';
19195 return producer;
19196 }
19197
19198 /* Generate the DIE for the compilation unit. */
19199
19200 static dw_die_ref
19201 gen_compile_unit_die (const char *filename)
19202 {
19203 dw_die_ref die;
19204 const char *language_string = lang_hooks.name;
19205 int language;
19206
19207 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19208
19209 if (filename)
19210 {
19211 add_name_attribute (die, filename);
19212 /* Don't add cwd for <built-in>. */
19213 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19214 add_comp_dir_attribute (die);
19215 }
19216
19217 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19218
19219 /* If our producer is LTO try to figure out a common language to use
19220 from the global list of translation units. */
19221 if (strcmp (language_string, "GNU GIMPLE") == 0)
19222 {
19223 unsigned i;
19224 tree t;
19225 const char *common_lang = NULL;
19226
19227 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19228 {
19229 if (!TRANSLATION_UNIT_LANGUAGE (t))
19230 continue;
19231 if (!common_lang)
19232 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19233 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19234 ;
19235 else if (strncmp (common_lang, "GNU C", 5) == 0
19236 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19237 /* Mixing C and C++ is ok, use C++ in that case. */
19238 common_lang = "GNU C++";
19239 else
19240 {
19241 /* Fall back to C. */
19242 common_lang = NULL;
19243 break;
19244 }
19245 }
19246
19247 if (common_lang)
19248 language_string = common_lang;
19249 }
19250
19251 language = DW_LANG_C89;
19252 if (strcmp (language_string, "GNU C++") == 0)
19253 language = DW_LANG_C_plus_plus;
19254 else if (strcmp (language_string, "GNU F77") == 0)
19255 language = DW_LANG_Fortran77;
19256 else if (strcmp (language_string, "GNU Pascal") == 0)
19257 language = DW_LANG_Pascal83;
19258 else if (dwarf_version >= 3 || !dwarf_strict)
19259 {
19260 if (strcmp (language_string, "GNU Ada") == 0)
19261 language = DW_LANG_Ada95;
19262 else if (strcmp (language_string, "GNU Fortran") == 0)
19263 language = DW_LANG_Fortran95;
19264 else if (strcmp (language_string, "GNU Java") == 0)
19265 language = DW_LANG_Java;
19266 else if (strcmp (language_string, "GNU Objective-C") == 0)
19267 language = DW_LANG_ObjC;
19268 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19269 language = DW_LANG_ObjC_plus_plus;
19270 else if (dwarf_version >= 5 || !dwarf_strict)
19271 {
19272 if (strcmp (language_string, "GNU Go") == 0)
19273 language = DW_LANG_Go;
19274 }
19275 }
19276 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19277 else if (strcmp (language_string, "GNU Fortran") == 0)
19278 language = DW_LANG_Fortran90;
19279
19280 add_AT_unsigned (die, DW_AT_language, language);
19281
19282 switch (language)
19283 {
19284 case DW_LANG_Fortran77:
19285 case DW_LANG_Fortran90:
19286 case DW_LANG_Fortran95:
19287 /* Fortran has case insensitive identifiers and the front-end
19288 lowercases everything. */
19289 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19290 break;
19291 default:
19292 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19293 break;
19294 }
19295 return die;
19296 }
19297
19298 /* Generate the DIE for a base class. */
19299
19300 static void
19301 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19302 {
19303 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19304
19305 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19306 add_data_member_location_attribute (die, binfo);
19307
19308 if (BINFO_VIRTUAL_P (binfo))
19309 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19310
19311 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19312 children, otherwise the default is DW_ACCESS_public. In DWARF2
19313 the default has always been DW_ACCESS_private. */
19314 if (access == access_public_node)
19315 {
19316 if (dwarf_version == 2
19317 || context_die->die_tag == DW_TAG_class_type)
19318 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19319 }
19320 else if (access == access_protected_node)
19321 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19322 else if (dwarf_version > 2
19323 && context_die->die_tag != DW_TAG_class_type)
19324 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19325 }
19326
19327 /* Generate a DIE for a class member. */
19328
19329 static void
19330 gen_member_die (tree type, dw_die_ref context_die)
19331 {
19332 tree member;
19333 tree binfo = TYPE_BINFO (type);
19334 dw_die_ref child;
19335
19336 /* If this is not an incomplete type, output descriptions of each of its
19337 members. Note that as we output the DIEs necessary to represent the
19338 members of this record or union type, we will also be trying to output
19339 DIEs to represent the *types* of those members. However the `type'
19340 function (above) will specifically avoid generating type DIEs for member
19341 types *within* the list of member DIEs for this (containing) type except
19342 for those types (of members) which are explicitly marked as also being
19343 members of this (containing) type themselves. The g++ front- end can
19344 force any given type to be treated as a member of some other (containing)
19345 type by setting the TYPE_CONTEXT of the given (member) type to point to
19346 the TREE node representing the appropriate (containing) type. */
19347
19348 /* First output info about the base classes. */
19349 if (binfo)
19350 {
19351 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19352 int i;
19353 tree base;
19354
19355 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19356 gen_inheritance_die (base,
19357 (accesses ? (*accesses)[i] : access_public_node),
19358 context_die);
19359 }
19360
19361 /* Now output info about the data members and type members. */
19362 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19363 {
19364 /* If we thought we were generating minimal debug info for TYPE
19365 and then changed our minds, some of the member declarations
19366 may have already been defined. Don't define them again, but
19367 do put them in the right order. */
19368
19369 child = lookup_decl_die (member);
19370 if (child)
19371 splice_child_die (context_die, child);
19372 else
19373 gen_decl_die (member, NULL, context_die);
19374 }
19375
19376 /* Now output info about the function members (if any). */
19377 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19378 {
19379 /* Don't include clones in the member list. */
19380 if (DECL_ABSTRACT_ORIGIN (member))
19381 continue;
19382
19383 child = lookup_decl_die (member);
19384 if (child)
19385 splice_child_die (context_die, child);
19386 else
19387 gen_decl_die (member, NULL, context_die);
19388 }
19389 }
19390
19391 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19392 is set, we pretend that the type was never defined, so we only get the
19393 member DIEs needed by later specification DIEs. */
19394
19395 static void
19396 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19397 enum debug_info_usage usage)
19398 {
19399 dw_die_ref type_die = lookup_type_die (type);
19400 dw_die_ref scope_die = 0;
19401 int nested = 0;
19402 int complete = (TYPE_SIZE (type)
19403 && (! TYPE_STUB_DECL (type)
19404 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19405 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19406 complete = complete && should_emit_struct_debug (type, usage);
19407
19408 if (type_die && ! complete)
19409 return;
19410
19411 if (TYPE_CONTEXT (type) != NULL_TREE
19412 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19413 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19414 nested = 1;
19415
19416 scope_die = scope_die_for (type, context_die);
19417
19418 /* Generate child dies for template paramaters. */
19419 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19420 schedule_generic_params_dies_gen (type);
19421
19422 if (! type_die || (nested && is_cu_die (scope_die)))
19423 /* First occurrence of type or toplevel definition of nested class. */
19424 {
19425 dw_die_ref old_die = type_die;
19426
19427 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19428 ? record_type_tag (type) : DW_TAG_union_type,
19429 scope_die, type);
19430 equate_type_number_to_die (type, type_die);
19431 if (old_die)
19432 add_AT_specification (type_die, old_die);
19433 else
19434 add_name_attribute (type_die, type_tag (type));
19435 }
19436 else
19437 remove_AT (type_die, DW_AT_declaration);
19438
19439 /* If this type has been completed, then give it a byte_size attribute and
19440 then give a list of members. */
19441 if (complete && !ns_decl)
19442 {
19443 /* Prevent infinite recursion in cases where the type of some member of
19444 this type is expressed in terms of this type itself. */
19445 TREE_ASM_WRITTEN (type) = 1;
19446 add_byte_size_attribute (type_die, type);
19447 if (TYPE_STUB_DECL (type) != NULL_TREE)
19448 {
19449 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19450 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19451 }
19452
19453 /* If the first reference to this type was as the return type of an
19454 inline function, then it may not have a parent. Fix this now. */
19455 if (type_die->die_parent == NULL)
19456 add_child_die (scope_die, type_die);
19457
19458 push_decl_scope (type);
19459 gen_member_die (type, type_die);
19460 pop_decl_scope ();
19461
19462 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19463 if (TYPE_ARTIFICIAL (type))
19464 add_AT_flag (type_die, DW_AT_artificial, 1);
19465
19466 /* GNU extension: Record what type our vtable lives in. */
19467 if (TYPE_VFIELD (type))
19468 {
19469 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19470
19471 gen_type_die (vtype, context_die);
19472 add_AT_die_ref (type_die, DW_AT_containing_type,
19473 lookup_type_die (vtype));
19474 }
19475 }
19476 else
19477 {
19478 add_AT_flag (type_die, DW_AT_declaration, 1);
19479
19480 /* We don't need to do this for function-local types. */
19481 if (TYPE_STUB_DECL (type)
19482 && ! decl_function_context (TYPE_STUB_DECL (type)))
19483 vec_safe_push (incomplete_types, type);
19484 }
19485
19486 if (get_AT (type_die, DW_AT_name))
19487 add_pubtype (type, type_die);
19488 }
19489
19490 /* Generate a DIE for a subroutine _type_. */
19491
19492 static void
19493 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19494 {
19495 tree return_type = TREE_TYPE (type);
19496 dw_die_ref subr_die
19497 = new_die (DW_TAG_subroutine_type,
19498 scope_die_for (type, context_die), type);
19499
19500 equate_type_number_to_die (type, subr_die);
19501 add_prototyped_attribute (subr_die, type);
19502 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19503 gen_formal_types_die (type, subr_die);
19504
19505 if (get_AT (subr_die, DW_AT_name))
19506 add_pubtype (type, subr_die);
19507 }
19508
19509 /* Generate a DIE for a type definition. */
19510
19511 static void
19512 gen_typedef_die (tree decl, dw_die_ref context_die)
19513 {
19514 dw_die_ref type_die;
19515 tree origin;
19516
19517 if (TREE_ASM_WRITTEN (decl))
19518 return;
19519
19520 TREE_ASM_WRITTEN (decl) = 1;
19521 type_die = new_die (DW_TAG_typedef, context_die, decl);
19522 origin = decl_ultimate_origin (decl);
19523 if (origin != NULL)
19524 add_abstract_origin_attribute (type_die, origin);
19525 else
19526 {
19527 tree type;
19528
19529 add_name_and_src_coords_attributes (type_die, decl);
19530 if (DECL_ORIGINAL_TYPE (decl))
19531 {
19532 type = DECL_ORIGINAL_TYPE (decl);
19533
19534 gcc_assert (type != TREE_TYPE (decl));
19535 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19536 }
19537 else
19538 {
19539 type = TREE_TYPE (decl);
19540
19541 if (is_naming_typedef_decl (TYPE_NAME (type)))
19542 {
19543 /* Here, we are in the case of decl being a typedef naming
19544 an anonymous type, e.g:
19545 typedef struct {...} foo;
19546 In that case TREE_TYPE (decl) is not a typedef variant
19547 type and TYPE_NAME of the anonymous type is set to the
19548 TYPE_DECL of the typedef. This construct is emitted by
19549 the C++ FE.
19550
19551 TYPE is the anonymous struct named by the typedef
19552 DECL. As we need the DW_AT_type attribute of the
19553 DW_TAG_typedef to point to the DIE of TYPE, let's
19554 generate that DIE right away. add_type_attribute
19555 called below will then pick (via lookup_type_die) that
19556 anonymous struct DIE. */
19557 if (!TREE_ASM_WRITTEN (type))
19558 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19559
19560 /* This is a GNU Extension. We are adding a
19561 DW_AT_linkage_name attribute to the DIE of the
19562 anonymous struct TYPE. The value of that attribute
19563 is the name of the typedef decl naming the anonymous
19564 struct. This greatly eases the work of consumers of
19565 this debug info. */
19566 add_linkage_attr (lookup_type_die (type), decl);
19567 }
19568 }
19569
19570 add_type_attribute (type_die, type, TREE_READONLY (decl),
19571 TREE_THIS_VOLATILE (decl), context_die);
19572
19573 if (is_naming_typedef_decl (decl))
19574 /* We want that all subsequent calls to lookup_type_die with
19575 TYPE in argument yield the DW_TAG_typedef we have just
19576 created. */
19577 equate_type_number_to_die (type, type_die);
19578
19579 add_accessibility_attribute (type_die, decl);
19580 }
19581
19582 if (DECL_ABSTRACT (decl))
19583 equate_decl_number_to_die (decl, type_die);
19584
19585 if (get_AT (type_die, DW_AT_name))
19586 add_pubtype (decl, type_die);
19587 }
19588
19589 /* Generate a DIE for a struct, class, enum or union type. */
19590
19591 static void
19592 gen_tagged_type_die (tree type,
19593 dw_die_ref context_die,
19594 enum debug_info_usage usage)
19595 {
19596 int need_pop;
19597
19598 if (type == NULL_TREE
19599 || !is_tagged_type (type))
19600 return;
19601
19602 /* If this is a nested type whose containing class hasn't been written
19603 out yet, writing it out will cover this one, too. This does not apply
19604 to instantiations of member class templates; they need to be added to
19605 the containing class as they are generated. FIXME: This hurts the
19606 idea of combining type decls from multiple TUs, since we can't predict
19607 what set of template instantiations we'll get. */
19608 if (TYPE_CONTEXT (type)
19609 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19610 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19611 {
19612 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19613
19614 if (TREE_ASM_WRITTEN (type))
19615 return;
19616
19617 /* If that failed, attach ourselves to the stub. */
19618 push_decl_scope (TYPE_CONTEXT (type));
19619 context_die = lookup_type_die (TYPE_CONTEXT (type));
19620 need_pop = 1;
19621 }
19622 else if (TYPE_CONTEXT (type) != NULL_TREE
19623 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19624 {
19625 /* If this type is local to a function that hasn't been written
19626 out yet, use a NULL context for now; it will be fixed up in
19627 decls_for_scope. */
19628 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19629 /* A declaration DIE doesn't count; nested types need to go in the
19630 specification. */
19631 if (context_die && is_declaration_die (context_die))
19632 context_die = NULL;
19633 need_pop = 0;
19634 }
19635 else
19636 {
19637 context_die = declare_in_namespace (type, context_die);
19638 need_pop = 0;
19639 }
19640
19641 if (TREE_CODE (type) == ENUMERAL_TYPE)
19642 {
19643 /* This might have been written out by the call to
19644 declare_in_namespace. */
19645 if (!TREE_ASM_WRITTEN (type))
19646 gen_enumeration_type_die (type, context_die);
19647 }
19648 else
19649 gen_struct_or_union_type_die (type, context_die, usage);
19650
19651 if (need_pop)
19652 pop_decl_scope ();
19653
19654 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19655 it up if it is ever completed. gen_*_type_die will set it for us
19656 when appropriate. */
19657 }
19658
19659 /* Generate a type description DIE. */
19660
19661 static void
19662 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19663 enum debug_info_usage usage)
19664 {
19665 struct array_descr_info info;
19666
19667 if (type == NULL_TREE || type == error_mark_node)
19668 return;
19669
19670 if (TYPE_NAME (type) != NULL_TREE
19671 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19672 && is_redundant_typedef (TYPE_NAME (type))
19673 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19674 /* The DECL of this type is a typedef we don't want to emit debug
19675 info for but we want debug info for its underlying typedef.
19676 This can happen for e.g, the injected-class-name of a C++
19677 type. */
19678 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19679
19680 /* If TYPE is a typedef type variant, let's generate debug info
19681 for the parent typedef which TYPE is a type of. */
19682 if (typedef_variant_p (type))
19683 {
19684 if (TREE_ASM_WRITTEN (type))
19685 return;
19686
19687 /* Prevent broken recursion; we can't hand off to the same type. */
19688 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19689
19690 /* Give typedefs the right scope. */
19691 context_die = scope_die_for (type, context_die);
19692
19693 TREE_ASM_WRITTEN (type) = 1;
19694
19695 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19696 return;
19697 }
19698
19699 /* If type is an anonymous tagged type named by a typedef, let's
19700 generate debug info for the typedef. */
19701 if (is_naming_typedef_decl (TYPE_NAME (type)))
19702 {
19703 /* Use the DIE of the containing namespace as the parent DIE of
19704 the type description DIE we want to generate. */
19705 if (DECL_CONTEXT (TYPE_NAME (type))
19706 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19707 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19708
19709 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19710 return;
19711 }
19712
19713 /* If this is an array type with hidden descriptor, handle it first. */
19714 if (!TREE_ASM_WRITTEN (type)
19715 && lang_hooks.types.get_array_descr_info
19716 && lang_hooks.types.get_array_descr_info (type, &info)
19717 && (dwarf_version >= 3 || !dwarf_strict))
19718 {
19719 gen_descr_array_type_die (type, &info, context_die);
19720 TREE_ASM_WRITTEN (type) = 1;
19721 return;
19722 }
19723
19724 /* We are going to output a DIE to represent the unqualified version
19725 of this type (i.e. without any const or volatile qualifiers) so
19726 get the main variant (i.e. the unqualified version) of this type
19727 now. (Vectors are special because the debugging info is in the
19728 cloned type itself). */
19729 if (TREE_CODE (type) != VECTOR_TYPE)
19730 type = type_main_variant (type);
19731
19732 if (TREE_ASM_WRITTEN (type))
19733 return;
19734
19735 switch (TREE_CODE (type))
19736 {
19737 case ERROR_MARK:
19738 break;
19739
19740 case POINTER_TYPE:
19741 case REFERENCE_TYPE:
19742 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19743 ensures that the gen_type_die recursion will terminate even if the
19744 type is recursive. Recursive types are possible in Ada. */
19745 /* ??? We could perhaps do this for all types before the switch
19746 statement. */
19747 TREE_ASM_WRITTEN (type) = 1;
19748
19749 /* For these types, all that is required is that we output a DIE (or a
19750 set of DIEs) to represent the "basis" type. */
19751 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19752 DINFO_USAGE_IND_USE);
19753 break;
19754
19755 case OFFSET_TYPE:
19756 /* This code is used for C++ pointer-to-data-member types.
19757 Output a description of the relevant class type. */
19758 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19759 DINFO_USAGE_IND_USE);
19760
19761 /* Output a description of the type of the object pointed to. */
19762 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19763 DINFO_USAGE_IND_USE);
19764
19765 /* Now output a DIE to represent this pointer-to-data-member type
19766 itself. */
19767 gen_ptr_to_mbr_type_die (type, context_die);
19768 break;
19769
19770 case FUNCTION_TYPE:
19771 /* Force out return type (in case it wasn't forced out already). */
19772 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19773 DINFO_USAGE_DIR_USE);
19774 gen_subroutine_type_die (type, context_die);
19775 break;
19776
19777 case METHOD_TYPE:
19778 /* Force out return type (in case it wasn't forced out already). */
19779 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19780 DINFO_USAGE_DIR_USE);
19781 gen_subroutine_type_die (type, context_die);
19782 break;
19783
19784 case ARRAY_TYPE:
19785 gen_array_type_die (type, context_die);
19786 break;
19787
19788 case VECTOR_TYPE:
19789 gen_array_type_die (type, context_die);
19790 break;
19791
19792 case ENUMERAL_TYPE:
19793 case RECORD_TYPE:
19794 case UNION_TYPE:
19795 case QUAL_UNION_TYPE:
19796 gen_tagged_type_die (type, context_die, usage);
19797 return;
19798
19799 case VOID_TYPE:
19800 case INTEGER_TYPE:
19801 case REAL_TYPE:
19802 case FIXED_POINT_TYPE:
19803 case COMPLEX_TYPE:
19804 case BOOLEAN_TYPE:
19805 case POINTER_BOUNDS_TYPE:
19806 /* No DIEs needed for fundamental types. */
19807 break;
19808
19809 case NULLPTR_TYPE:
19810 case LANG_TYPE:
19811 /* Just use DW_TAG_unspecified_type. */
19812 {
19813 dw_die_ref type_die = lookup_type_die (type);
19814 if (type_die == NULL)
19815 {
19816 tree name = TYPE_NAME (type);
19817 if (TREE_CODE (name) == TYPE_DECL)
19818 name = DECL_NAME (name);
19819 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19820 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19821 equate_type_number_to_die (type, type_die);
19822 }
19823 }
19824 break;
19825
19826 default:
19827 gcc_unreachable ();
19828 }
19829
19830 TREE_ASM_WRITTEN (type) = 1;
19831 }
19832
19833 static void
19834 gen_type_die (tree type, dw_die_ref context_die)
19835 {
19836 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19837 }
19838
19839 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19840 things which are local to the given block. */
19841
19842 static void
19843 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19844 {
19845 int must_output_die = 0;
19846 bool inlined_func;
19847
19848 /* Ignore blocks that are NULL. */
19849 if (stmt == NULL_TREE)
19850 return;
19851
19852 inlined_func = inlined_function_outer_scope_p (stmt);
19853
19854 /* If the block is one fragment of a non-contiguous block, do not
19855 process the variables, since they will have been done by the
19856 origin block. Do process subblocks. */
19857 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19858 {
19859 tree sub;
19860
19861 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19862 gen_block_die (sub, context_die, depth + 1);
19863
19864 return;
19865 }
19866
19867 /* Determine if we need to output any Dwarf DIEs at all to represent this
19868 block. */
19869 if (inlined_func)
19870 /* The outer scopes for inlinings *must* always be represented. We
19871 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19872 must_output_die = 1;
19873 else
19874 {
19875 /* Determine if this block directly contains any "significant"
19876 local declarations which we will need to output DIEs for. */
19877 if (debug_info_level > DINFO_LEVEL_TERSE)
19878 /* We are not in terse mode so *any* local declaration counts
19879 as being a "significant" one. */
19880 must_output_die = ((BLOCK_VARS (stmt) != NULL
19881 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19882 && (TREE_USED (stmt)
19883 || TREE_ASM_WRITTEN (stmt)
19884 || BLOCK_ABSTRACT (stmt)));
19885 else if ((TREE_USED (stmt)
19886 || TREE_ASM_WRITTEN (stmt)
19887 || BLOCK_ABSTRACT (stmt))
19888 && !dwarf2out_ignore_block (stmt))
19889 must_output_die = 1;
19890 }
19891
19892 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19893 DIE for any block which contains no significant local declarations at
19894 all. Rather, in such cases we just call `decls_for_scope' so that any
19895 needed Dwarf info for any sub-blocks will get properly generated. Note
19896 that in terse mode, our definition of what constitutes a "significant"
19897 local declaration gets restricted to include only inlined function
19898 instances and local (nested) function definitions. */
19899 if (must_output_die)
19900 {
19901 if (inlined_func)
19902 {
19903 /* If STMT block is abstract, that means we have been called
19904 indirectly from dwarf2out_abstract_function.
19905 That function rightfully marks the descendent blocks (of
19906 the abstract function it is dealing with) as being abstract,
19907 precisely to prevent us from emitting any
19908 DW_TAG_inlined_subroutine DIE as a descendent
19909 of an abstract function instance. So in that case, we should
19910 not call gen_inlined_subroutine_die.
19911
19912 Later though, when cgraph asks dwarf2out to emit info
19913 for the concrete instance of the function decl into which
19914 the concrete instance of STMT got inlined, the later will lead
19915 to the generation of a DW_TAG_inlined_subroutine DIE. */
19916 if (! BLOCK_ABSTRACT (stmt))
19917 gen_inlined_subroutine_die (stmt, context_die, depth);
19918 }
19919 else
19920 gen_lexical_block_die (stmt, context_die, depth);
19921 }
19922 else
19923 decls_for_scope (stmt, context_die, depth);
19924 }
19925
19926 /* Process variable DECL (or variable with origin ORIGIN) within
19927 block STMT and add it to CONTEXT_DIE. */
19928 static void
19929 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19930 {
19931 dw_die_ref die;
19932 tree decl_or_origin = decl ? decl : origin;
19933
19934 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19935 die = lookup_decl_die (decl_or_origin);
19936 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19937 && TYPE_DECL_IS_STUB (decl_or_origin))
19938 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19939 else
19940 die = NULL;
19941
19942 if (die != NULL && die->die_parent == NULL)
19943 add_child_die (context_die, die);
19944 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19945 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19946 stmt, context_die);
19947 else
19948 gen_decl_die (decl, origin, context_die);
19949 }
19950
19951 /* Generate all of the decls declared within a given scope and (recursively)
19952 all of its sub-blocks. */
19953
19954 static void
19955 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19956 {
19957 tree decl;
19958 unsigned int i;
19959 tree subblocks;
19960
19961 /* Ignore NULL blocks. */
19962 if (stmt == NULL_TREE)
19963 return;
19964
19965 /* Output the DIEs to represent all of the data objects and typedefs
19966 declared directly within this block but not within any nested
19967 sub-blocks. Also, nested function and tag DIEs have been
19968 generated with a parent of NULL; fix that up now. We don't
19969 have to do this if we're at -g1. */
19970 if (debug_info_level > DINFO_LEVEL_TERSE)
19971 {
19972 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19973 process_scope_var (stmt, decl, NULL_TREE, context_die);
19974 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19975 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19976 context_die);
19977 }
19978
19979 /* Even if we're at -g1, we need to process the subblocks in order to get
19980 inlined call information. */
19981
19982 /* Output the DIEs to represent all sub-blocks (and the items declared
19983 therein) of this block. */
19984 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19985 subblocks != NULL;
19986 subblocks = BLOCK_CHAIN (subblocks))
19987 gen_block_die (subblocks, context_die, depth + 1);
19988 }
19989
19990 /* Is this a typedef we can avoid emitting? */
19991
19992 static inline int
19993 is_redundant_typedef (const_tree decl)
19994 {
19995 if (TYPE_DECL_IS_STUB (decl))
19996 return 1;
19997
19998 if (DECL_ARTIFICIAL (decl)
19999 && DECL_CONTEXT (decl)
20000 && is_tagged_type (DECL_CONTEXT (decl))
20001 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20002 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20003 /* Also ignore the artificial member typedef for the class name. */
20004 return 1;
20005
20006 return 0;
20007 }
20008
20009 /* Return TRUE if TYPE is a typedef that names a type for linkage
20010 purposes. This kind of typedefs is produced by the C++ FE for
20011 constructs like:
20012
20013 typedef struct {...} foo;
20014
20015 In that case, there is no typedef variant type produced for foo.
20016 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20017 struct type. */
20018
20019 static bool
20020 is_naming_typedef_decl (const_tree decl)
20021 {
20022 if (decl == NULL_TREE
20023 || TREE_CODE (decl) != TYPE_DECL
20024 || !is_tagged_type (TREE_TYPE (decl))
20025 || DECL_IS_BUILTIN (decl)
20026 || is_redundant_typedef (decl)
20027 /* It looks like Ada produces TYPE_DECLs that are very similar
20028 to C++ naming typedefs but that have different
20029 semantics. Let's be specific to c++ for now. */
20030 || !is_cxx ())
20031 return FALSE;
20032
20033 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20034 && TYPE_NAME (TREE_TYPE (decl)) == decl
20035 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20036 != TYPE_NAME (TREE_TYPE (decl))));
20037 }
20038
20039 /* Returns the DIE for a context. */
20040
20041 static inline dw_die_ref
20042 get_context_die (tree context)
20043 {
20044 if (context)
20045 {
20046 /* Find die that represents this context. */
20047 if (TYPE_P (context))
20048 {
20049 context = TYPE_MAIN_VARIANT (context);
20050 return strip_naming_typedef (context, force_type_die (context));
20051 }
20052 else
20053 return force_decl_die (context);
20054 }
20055 return comp_unit_die ();
20056 }
20057
20058 /* Returns the DIE for decl. A DIE will always be returned. */
20059
20060 static dw_die_ref
20061 force_decl_die (tree decl)
20062 {
20063 dw_die_ref decl_die;
20064 unsigned saved_external_flag;
20065 tree save_fn = NULL_TREE;
20066 decl_die = lookup_decl_die (decl);
20067 if (!decl_die)
20068 {
20069 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20070
20071 decl_die = lookup_decl_die (decl);
20072 if (decl_die)
20073 return decl_die;
20074
20075 switch (TREE_CODE (decl))
20076 {
20077 case FUNCTION_DECL:
20078 /* Clear current_function_decl, so that gen_subprogram_die thinks
20079 that this is a declaration. At this point, we just want to force
20080 declaration die. */
20081 save_fn = current_function_decl;
20082 current_function_decl = NULL_TREE;
20083 gen_subprogram_die (decl, context_die);
20084 current_function_decl = save_fn;
20085 break;
20086
20087 case VAR_DECL:
20088 /* Set external flag to force declaration die. Restore it after
20089 gen_decl_die() call. */
20090 saved_external_flag = DECL_EXTERNAL (decl);
20091 DECL_EXTERNAL (decl) = 1;
20092 gen_decl_die (decl, NULL, context_die);
20093 DECL_EXTERNAL (decl) = saved_external_flag;
20094 break;
20095
20096 case NAMESPACE_DECL:
20097 if (dwarf_version >= 3 || !dwarf_strict)
20098 dwarf2out_decl (decl);
20099 else
20100 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20101 decl_die = comp_unit_die ();
20102 break;
20103
20104 case TRANSLATION_UNIT_DECL:
20105 decl_die = comp_unit_die ();
20106 break;
20107
20108 default:
20109 gcc_unreachable ();
20110 }
20111
20112 /* We should be able to find the DIE now. */
20113 if (!decl_die)
20114 decl_die = lookup_decl_die (decl);
20115 gcc_assert (decl_die);
20116 }
20117
20118 return decl_die;
20119 }
20120
20121 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20122 always returned. */
20123
20124 static dw_die_ref
20125 force_type_die (tree type)
20126 {
20127 dw_die_ref type_die;
20128
20129 type_die = lookup_type_die (type);
20130 if (!type_die)
20131 {
20132 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20133
20134 type_die = modified_type_die (type, TYPE_READONLY (type),
20135 TYPE_VOLATILE (type), context_die);
20136 gcc_assert (type_die);
20137 }
20138 return type_die;
20139 }
20140
20141 /* Force out any required namespaces to be able to output DECL,
20142 and return the new context_die for it, if it's changed. */
20143
20144 static dw_die_ref
20145 setup_namespace_context (tree thing, dw_die_ref context_die)
20146 {
20147 tree context = (DECL_P (thing)
20148 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20149 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20150 /* Force out the namespace. */
20151 context_die = force_decl_die (context);
20152
20153 return context_die;
20154 }
20155
20156 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20157 type) within its namespace, if appropriate.
20158
20159 For compatibility with older debuggers, namespace DIEs only contain
20160 declarations; all definitions are emitted at CU scope. */
20161
20162 static dw_die_ref
20163 declare_in_namespace (tree thing, dw_die_ref context_die)
20164 {
20165 dw_die_ref ns_context;
20166
20167 if (debug_info_level <= DINFO_LEVEL_TERSE)
20168 return context_die;
20169
20170 /* If this decl is from an inlined function, then don't try to emit it in its
20171 namespace, as we will get confused. It would have already been emitted
20172 when the abstract instance of the inline function was emitted anyways. */
20173 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20174 return context_die;
20175
20176 ns_context = setup_namespace_context (thing, context_die);
20177
20178 if (ns_context != context_die)
20179 {
20180 if (is_fortran ())
20181 return ns_context;
20182 if (DECL_P (thing))
20183 gen_decl_die (thing, NULL, ns_context);
20184 else
20185 gen_type_die (thing, ns_context);
20186 }
20187 return context_die;
20188 }
20189
20190 /* Generate a DIE for a namespace or namespace alias. */
20191
20192 static void
20193 gen_namespace_die (tree decl, dw_die_ref context_die)
20194 {
20195 dw_die_ref namespace_die;
20196
20197 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20198 they are an alias of. */
20199 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20200 {
20201 /* Output a real namespace or module. */
20202 context_die = setup_namespace_context (decl, comp_unit_die ());
20203 namespace_die = new_die (is_fortran ()
20204 ? DW_TAG_module : DW_TAG_namespace,
20205 context_die, decl);
20206 /* For Fortran modules defined in different CU don't add src coords. */
20207 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20208 {
20209 const char *name = dwarf2_name (decl, 0);
20210 if (name)
20211 add_name_attribute (namespace_die, name);
20212 }
20213 else
20214 add_name_and_src_coords_attributes (namespace_die, decl);
20215 if (DECL_EXTERNAL (decl))
20216 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20217 equate_decl_number_to_die (decl, namespace_die);
20218 }
20219 else
20220 {
20221 /* Output a namespace alias. */
20222
20223 /* Force out the namespace we are an alias of, if necessary. */
20224 dw_die_ref origin_die
20225 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20226
20227 if (DECL_FILE_SCOPE_P (decl)
20228 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20229 context_die = setup_namespace_context (decl, comp_unit_die ());
20230 /* Now create the namespace alias DIE. */
20231 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20232 add_name_and_src_coords_attributes (namespace_die, decl);
20233 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20234 equate_decl_number_to_die (decl, namespace_die);
20235 }
20236 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20237 if (want_pubnames ())
20238 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20239 }
20240
20241 /* Generate Dwarf debug information for a decl described by DECL.
20242 The return value is currently only meaningful for PARM_DECLs,
20243 for all other decls it returns NULL. */
20244
20245 static dw_die_ref
20246 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20247 {
20248 tree decl_or_origin = decl ? decl : origin;
20249 tree class_origin = NULL, ultimate_origin;
20250
20251 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20252 return NULL;
20253
20254 switch (TREE_CODE (decl_or_origin))
20255 {
20256 case ERROR_MARK:
20257 break;
20258
20259 case CONST_DECL:
20260 if (!is_fortran () && !is_ada ())
20261 {
20262 /* The individual enumerators of an enum type get output when we output
20263 the Dwarf representation of the relevant enum type itself. */
20264 break;
20265 }
20266
20267 /* Emit its type. */
20268 gen_type_die (TREE_TYPE (decl), context_die);
20269
20270 /* And its containing namespace. */
20271 context_die = declare_in_namespace (decl, context_die);
20272
20273 gen_const_die (decl, context_die);
20274 break;
20275
20276 case FUNCTION_DECL:
20277 /* Don't output any DIEs to represent mere function declarations,
20278 unless they are class members or explicit block externs. */
20279 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20280 && DECL_FILE_SCOPE_P (decl_or_origin)
20281 && (current_function_decl == NULL_TREE
20282 || DECL_ARTIFICIAL (decl_or_origin)))
20283 break;
20284
20285 #if 0
20286 /* FIXME */
20287 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20288 on local redeclarations of global functions. That seems broken. */
20289 if (current_function_decl != decl)
20290 /* This is only a declaration. */;
20291 #endif
20292
20293 /* If we're emitting a clone, emit info for the abstract instance. */
20294 if (origin || DECL_ORIGIN (decl) != decl)
20295 dwarf2out_abstract_function (origin
20296 ? DECL_ORIGIN (origin)
20297 : DECL_ABSTRACT_ORIGIN (decl));
20298
20299 /* If we're emitting an out-of-line copy of an inline function,
20300 emit info for the abstract instance and set up to refer to it. */
20301 else if (cgraph_function_possibly_inlined_p (decl)
20302 && ! DECL_ABSTRACT (decl)
20303 && ! class_or_namespace_scope_p (context_die)
20304 /* dwarf2out_abstract_function won't emit a die if this is just
20305 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20306 that case, because that works only if we have a die. */
20307 && DECL_INITIAL (decl) != NULL_TREE)
20308 {
20309 dwarf2out_abstract_function (decl);
20310 set_decl_origin_self (decl);
20311 }
20312
20313 /* Otherwise we're emitting the primary DIE for this decl. */
20314 else if (debug_info_level > DINFO_LEVEL_TERSE)
20315 {
20316 /* Before we describe the FUNCTION_DECL itself, make sure that we
20317 have its containing type. */
20318 if (!origin)
20319 origin = decl_class_context (decl);
20320 if (origin != NULL_TREE)
20321 gen_type_die (origin, context_die);
20322
20323 /* And its return type. */
20324 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20325
20326 /* And its virtual context. */
20327 if (DECL_VINDEX (decl) != NULL_TREE)
20328 gen_type_die (DECL_CONTEXT (decl), context_die);
20329
20330 /* Make sure we have a member DIE for decl. */
20331 if (origin != NULL_TREE)
20332 gen_type_die_for_member (origin, decl, context_die);
20333
20334 /* And its containing namespace. */
20335 context_die = declare_in_namespace (decl, context_die);
20336 }
20337
20338 /* Now output a DIE to represent the function itself. */
20339 if (decl)
20340 gen_subprogram_die (decl, context_die);
20341 break;
20342
20343 case TYPE_DECL:
20344 /* If we are in terse mode, don't generate any DIEs to represent any
20345 actual typedefs. */
20346 if (debug_info_level <= DINFO_LEVEL_TERSE)
20347 break;
20348
20349 /* In the special case of a TYPE_DECL node representing the declaration
20350 of some type tag, if the given TYPE_DECL is marked as having been
20351 instantiated from some other (original) TYPE_DECL node (e.g. one which
20352 was generated within the original definition of an inline function) we
20353 used to generate a special (abbreviated) DW_TAG_structure_type,
20354 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20355 should be actually referencing those DIEs, as variable DIEs with that
20356 type would be emitted already in the abstract origin, so it was always
20357 removed during unused type prunning. Don't add anything in this
20358 case. */
20359 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20360 break;
20361
20362 if (is_redundant_typedef (decl))
20363 gen_type_die (TREE_TYPE (decl), context_die);
20364 else
20365 /* Output a DIE to represent the typedef itself. */
20366 gen_typedef_die (decl, context_die);
20367 break;
20368
20369 case LABEL_DECL:
20370 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20371 gen_label_die (decl, context_die);
20372 break;
20373
20374 case VAR_DECL:
20375 case RESULT_DECL:
20376 /* If we are in terse mode, don't generate any DIEs to represent any
20377 variable declarations or definitions. */
20378 if (debug_info_level <= DINFO_LEVEL_TERSE)
20379 break;
20380
20381 /* Output any DIEs that are needed to specify the type of this data
20382 object. */
20383 if (decl_by_reference_p (decl_or_origin))
20384 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20385 else
20386 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20387
20388 /* And its containing type. */
20389 class_origin = decl_class_context (decl_or_origin);
20390 if (class_origin != NULL_TREE)
20391 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20392
20393 /* And its containing namespace. */
20394 context_die = declare_in_namespace (decl_or_origin, context_die);
20395
20396 /* Now output the DIE to represent the data object itself. This gets
20397 complicated because of the possibility that the VAR_DECL really
20398 represents an inlined instance of a formal parameter for an inline
20399 function. */
20400 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20401 if (ultimate_origin != NULL_TREE
20402 && TREE_CODE (ultimate_origin) == PARM_DECL)
20403 gen_formal_parameter_die (decl, origin,
20404 true /* Emit name attribute. */,
20405 context_die);
20406 else
20407 gen_variable_die (decl, origin, context_die);
20408 break;
20409
20410 case FIELD_DECL:
20411 /* Ignore the nameless fields that are used to skip bits but handle C++
20412 anonymous unions and structs. */
20413 if (DECL_NAME (decl) != NULL_TREE
20414 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20415 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20416 {
20417 gen_type_die (member_declared_type (decl), context_die);
20418 gen_field_die (decl, context_die);
20419 }
20420 break;
20421
20422 case PARM_DECL:
20423 if (DECL_BY_REFERENCE (decl_or_origin))
20424 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20425 else
20426 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20427 return gen_formal_parameter_die (decl, origin,
20428 true /* Emit name attribute. */,
20429 context_die);
20430
20431 case NAMESPACE_DECL:
20432 case IMPORTED_DECL:
20433 if (dwarf_version >= 3 || !dwarf_strict)
20434 gen_namespace_die (decl, context_die);
20435 break;
20436
20437 default:
20438 /* Probably some frontend-internal decl. Assume we don't care. */
20439 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20440 break;
20441 }
20442
20443 return NULL;
20444 }
20445 \f
20446 /* Output debug information for global decl DECL. Called from toplev.c after
20447 compilation proper has finished. */
20448
20449 static void
20450 dwarf2out_global_decl (tree decl)
20451 {
20452 /* Output DWARF2 information for file-scope tentative data object
20453 declarations, file-scope (extern) function declarations (which
20454 had no corresponding body) and file-scope tagged type declarations
20455 and definitions which have not yet been forced out. */
20456 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20457 && !POINTER_BOUNDS_P (decl))
20458 dwarf2out_decl (decl);
20459 }
20460
20461 /* Output debug information for type decl DECL. Called from toplev.c
20462 and from language front ends (to record built-in types). */
20463 static void
20464 dwarf2out_type_decl (tree decl, int local)
20465 {
20466 if (!local)
20467 dwarf2out_decl (decl);
20468 }
20469
20470 /* Output debug information for imported module or decl DECL.
20471 NAME is non-NULL name in the lexical block if the decl has been renamed.
20472 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20473 that DECL belongs to.
20474 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20475 static void
20476 dwarf2out_imported_module_or_decl_1 (tree decl,
20477 tree name,
20478 tree lexical_block,
20479 dw_die_ref lexical_block_die)
20480 {
20481 expanded_location xloc;
20482 dw_die_ref imported_die = NULL;
20483 dw_die_ref at_import_die;
20484
20485 if (TREE_CODE (decl) == IMPORTED_DECL)
20486 {
20487 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20488 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20489 gcc_assert (decl);
20490 }
20491 else
20492 xloc = expand_location (input_location);
20493
20494 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20495 {
20496 at_import_die = force_type_die (TREE_TYPE (decl));
20497 /* For namespace N { typedef void T; } using N::T; base_type_die
20498 returns NULL, but DW_TAG_imported_declaration requires
20499 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20500 if (!at_import_die)
20501 {
20502 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20503 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20504 at_import_die = lookup_type_die (TREE_TYPE (decl));
20505 gcc_assert (at_import_die);
20506 }
20507 }
20508 else
20509 {
20510 at_import_die = lookup_decl_die (decl);
20511 if (!at_import_die)
20512 {
20513 /* If we're trying to avoid duplicate debug info, we may not have
20514 emitted the member decl for this field. Emit it now. */
20515 if (TREE_CODE (decl) == FIELD_DECL)
20516 {
20517 tree type = DECL_CONTEXT (decl);
20518
20519 if (TYPE_CONTEXT (type)
20520 && TYPE_P (TYPE_CONTEXT (type))
20521 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20522 DINFO_USAGE_DIR_USE))
20523 return;
20524 gen_type_die_for_member (type, decl,
20525 get_context_die (TYPE_CONTEXT (type)));
20526 }
20527 at_import_die = force_decl_die (decl);
20528 }
20529 }
20530
20531 if (TREE_CODE (decl) == NAMESPACE_DECL)
20532 {
20533 if (dwarf_version >= 3 || !dwarf_strict)
20534 imported_die = new_die (DW_TAG_imported_module,
20535 lexical_block_die,
20536 lexical_block);
20537 else
20538 return;
20539 }
20540 else
20541 imported_die = new_die (DW_TAG_imported_declaration,
20542 lexical_block_die,
20543 lexical_block);
20544
20545 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20546 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20547 if (name)
20548 add_AT_string (imported_die, DW_AT_name,
20549 IDENTIFIER_POINTER (name));
20550 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20551 }
20552
20553 /* Output debug information for imported module or decl DECL.
20554 NAME is non-NULL name in context if the decl has been renamed.
20555 CHILD is true if decl is one of the renamed decls as part of
20556 importing whole module. */
20557
20558 static void
20559 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20560 bool child)
20561 {
20562 /* dw_die_ref at_import_die; */
20563 dw_die_ref scope_die;
20564
20565 if (debug_info_level <= DINFO_LEVEL_TERSE)
20566 return;
20567
20568 gcc_assert (decl);
20569
20570 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20571 We need decl DIE for reference and scope die. First, get DIE for the decl
20572 itself. */
20573
20574 /* Get the scope die for decl context. Use comp_unit_die for global module
20575 or decl. If die is not found for non globals, force new die. */
20576 if (context
20577 && TYPE_P (context)
20578 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20579 return;
20580
20581 if (!(dwarf_version >= 3 || !dwarf_strict))
20582 return;
20583
20584 scope_die = get_context_die (context);
20585
20586 if (child)
20587 {
20588 gcc_assert (scope_die->die_child);
20589 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20590 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20591 scope_die = scope_die->die_child;
20592 }
20593
20594 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20595 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20596
20597 }
20598
20599 /* Write the debugging output for DECL. */
20600
20601 void
20602 dwarf2out_decl (tree decl)
20603 {
20604 dw_die_ref context_die = comp_unit_die ();
20605
20606 switch (TREE_CODE (decl))
20607 {
20608 case ERROR_MARK:
20609 return;
20610
20611 case FUNCTION_DECL:
20612 /* What we would really like to do here is to filter out all mere
20613 file-scope declarations of file-scope functions which are never
20614 referenced later within this translation unit (and keep all of ones
20615 that *are* referenced later on) but we aren't clairvoyant, so we have
20616 no idea which functions will be referenced in the future (i.e. later
20617 on within the current translation unit). So here we just ignore all
20618 file-scope function declarations which are not also definitions. If
20619 and when the debugger needs to know something about these functions,
20620 it will have to hunt around and find the DWARF information associated
20621 with the definition of the function.
20622
20623 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20624 nodes represent definitions and which ones represent mere
20625 declarations. We have to check DECL_INITIAL instead. That's because
20626 the C front-end supports some weird semantics for "extern inline"
20627 function definitions. These can get inlined within the current
20628 translation unit (and thus, we need to generate Dwarf info for their
20629 abstract instances so that the Dwarf info for the concrete inlined
20630 instances can have something to refer to) but the compiler never
20631 generates any out-of-lines instances of such things (despite the fact
20632 that they *are* definitions).
20633
20634 The important point is that the C front-end marks these "extern
20635 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20636 them anyway. Note that the C++ front-end also plays some similar games
20637 for inline function definitions appearing within include files which
20638 also contain `#pragma interface' pragmas.
20639
20640 If we are called from dwarf2out_abstract_function output a DIE
20641 anyway. We can end up here this way with early inlining and LTO
20642 where the inlined function is output in a different LTRANS unit
20643 or not at all. */
20644 if (DECL_INITIAL (decl) == NULL_TREE
20645 && ! DECL_ABSTRACT (decl))
20646 return;
20647
20648 /* If we're a nested function, initially use a parent of NULL; if we're
20649 a plain function, this will be fixed up in decls_for_scope. If
20650 we're a method, it will be ignored, since we already have a DIE. */
20651 if (decl_function_context (decl)
20652 /* But if we're in terse mode, we don't care about scope. */
20653 && debug_info_level > DINFO_LEVEL_TERSE)
20654 context_die = NULL;
20655 break;
20656
20657 case VAR_DECL:
20658 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20659 declaration and if the declaration was never even referenced from
20660 within this entire compilation unit. We suppress these DIEs in
20661 order to save space in the .debug section (by eliminating entries
20662 which are probably useless). Note that we must not suppress
20663 block-local extern declarations (whether used or not) because that
20664 would screw-up the debugger's name lookup mechanism and cause it to
20665 miss things which really ought to be in scope at a given point. */
20666 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20667 return;
20668
20669 /* For local statics lookup proper context die. */
20670 if (TREE_STATIC (decl)
20671 && DECL_CONTEXT (decl)
20672 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20673 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20674
20675 /* If we are in terse mode, don't generate any DIEs to represent any
20676 variable declarations or definitions. */
20677 if (debug_info_level <= DINFO_LEVEL_TERSE)
20678 return;
20679 break;
20680
20681 case CONST_DECL:
20682 if (debug_info_level <= DINFO_LEVEL_TERSE)
20683 return;
20684 if (!is_fortran () && !is_ada ())
20685 return;
20686 if (TREE_STATIC (decl) && decl_function_context (decl))
20687 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20688 break;
20689
20690 case NAMESPACE_DECL:
20691 case IMPORTED_DECL:
20692 if (debug_info_level <= DINFO_LEVEL_TERSE)
20693 return;
20694 if (lookup_decl_die (decl) != NULL)
20695 return;
20696 break;
20697
20698 case TYPE_DECL:
20699 /* Don't emit stubs for types unless they are needed by other DIEs. */
20700 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20701 return;
20702
20703 /* Don't bother trying to generate any DIEs to represent any of the
20704 normal built-in types for the language we are compiling. */
20705 if (DECL_IS_BUILTIN (decl))
20706 return;
20707
20708 /* If we are in terse mode, don't generate any DIEs for types. */
20709 if (debug_info_level <= DINFO_LEVEL_TERSE)
20710 return;
20711
20712 /* If we're a function-scope tag, initially use a parent of NULL;
20713 this will be fixed up in decls_for_scope. */
20714 if (decl_function_context (decl))
20715 context_die = NULL;
20716
20717 break;
20718
20719 default:
20720 return;
20721 }
20722
20723 gen_decl_die (decl, NULL, context_die);
20724 }
20725
20726 /* Write the debugging output for DECL. */
20727
20728 static void
20729 dwarf2out_function_decl (tree decl)
20730 {
20731 dwarf2out_decl (decl);
20732 call_arg_locations = NULL;
20733 call_arg_loc_last = NULL;
20734 call_site_count = -1;
20735 tail_call_site_count = -1;
20736 block_map.release ();
20737 htab_empty (decl_loc_table);
20738 htab_empty (cached_dw_loc_list_table);
20739 }
20740
20741 /* Output a marker (i.e. a label) for the beginning of the generated code for
20742 a lexical block. */
20743
20744 static void
20745 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20746 unsigned int blocknum)
20747 {
20748 switch_to_section (current_function_section ());
20749 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20750 }
20751
20752 /* Output a marker (i.e. a label) for the end of the generated code for a
20753 lexical block. */
20754
20755 static void
20756 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20757 {
20758 switch_to_section (current_function_section ());
20759 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20760 }
20761
20762 /* Returns nonzero if it is appropriate not to emit any debugging
20763 information for BLOCK, because it doesn't contain any instructions.
20764
20765 Don't allow this for blocks with nested functions or local classes
20766 as we would end up with orphans, and in the presence of scheduling
20767 we may end up calling them anyway. */
20768
20769 static bool
20770 dwarf2out_ignore_block (const_tree block)
20771 {
20772 tree decl;
20773 unsigned int i;
20774
20775 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20776 if (TREE_CODE (decl) == FUNCTION_DECL
20777 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20778 return 0;
20779 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20780 {
20781 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20782 if (TREE_CODE (decl) == FUNCTION_DECL
20783 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20784 return 0;
20785 }
20786
20787 return 1;
20788 }
20789
20790 /* Hash table routines for file_hash. */
20791
20792 static int
20793 file_table_eq (const void *p1_p, const void *p2_p)
20794 {
20795 const struct dwarf_file_data *const p1 =
20796 (const struct dwarf_file_data *) p1_p;
20797 const char *const p2 = (const char *) p2_p;
20798 return filename_cmp (p1->filename, p2) == 0;
20799 }
20800
20801 static hashval_t
20802 file_table_hash (const void *p_p)
20803 {
20804 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20805 return htab_hash_string (p->filename);
20806 }
20807
20808 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20809 dwarf2out.c) and return its "index". The index of each (known) filename is
20810 just a unique number which is associated with only that one filename. We
20811 need such numbers for the sake of generating labels (in the .debug_sfnames
20812 section) and references to those files numbers (in the .debug_srcinfo
20813 and.debug_macinfo sections). If the filename given as an argument is not
20814 found in our current list, add it to the list and assign it the next
20815 available unique index number. In order to speed up searches, we remember
20816 the index of the filename was looked up last. This handles the majority of
20817 all searches. */
20818
20819 static struct dwarf_file_data *
20820 lookup_filename (const char *file_name)
20821 {
20822 void ** slot;
20823 struct dwarf_file_data * created;
20824
20825 /* Check to see if the file name that was searched on the previous
20826 call matches this file name. If so, return the index. */
20827 if (file_table_last_lookup
20828 && (file_name == file_table_last_lookup->filename
20829 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20830 return file_table_last_lookup;
20831
20832 /* Didn't match the previous lookup, search the table. */
20833 slot = htab_find_slot_with_hash (file_table, file_name,
20834 htab_hash_string (file_name), INSERT);
20835 if (*slot)
20836 return (struct dwarf_file_data *) *slot;
20837
20838 created = ggc_alloc_dwarf_file_data ();
20839 created->filename = file_name;
20840 created->emitted_number = 0;
20841 *slot = created;
20842 return created;
20843 }
20844
20845 /* If the assembler will construct the file table, then translate the compiler
20846 internal file table number into the assembler file table number, and emit
20847 a .file directive if we haven't already emitted one yet. The file table
20848 numbers are different because we prune debug info for unused variables and
20849 types, which may include filenames. */
20850
20851 static int
20852 maybe_emit_file (struct dwarf_file_data * fd)
20853 {
20854 if (! fd->emitted_number)
20855 {
20856 if (last_emitted_file)
20857 fd->emitted_number = last_emitted_file->emitted_number + 1;
20858 else
20859 fd->emitted_number = 1;
20860 last_emitted_file = fd;
20861
20862 if (DWARF2_ASM_LINE_DEBUG_INFO)
20863 {
20864 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20865 output_quoted_string (asm_out_file,
20866 remap_debug_filename (fd->filename));
20867 fputc ('\n', asm_out_file);
20868 }
20869 }
20870
20871 return fd->emitted_number;
20872 }
20873
20874 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20875 That generation should happen after function debug info has been
20876 generated. The value of the attribute is the constant value of ARG. */
20877
20878 static void
20879 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20880 {
20881 die_arg_entry entry;
20882
20883 if (!die || !arg)
20884 return;
20885
20886 if (!tmpl_value_parm_die_table)
20887 vec_alloc (tmpl_value_parm_die_table, 32);
20888
20889 entry.die = die;
20890 entry.arg = arg;
20891 vec_safe_push (tmpl_value_parm_die_table, entry);
20892 }
20893
20894 /* Return TRUE if T is an instance of generic type, FALSE
20895 otherwise. */
20896
20897 static bool
20898 generic_type_p (tree t)
20899 {
20900 if (t == NULL_TREE || !TYPE_P (t))
20901 return false;
20902 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20903 }
20904
20905 /* Schedule the generation of the generic parameter dies for the
20906 instance of generic type T. The proper generation itself is later
20907 done by gen_scheduled_generic_parms_dies. */
20908
20909 static void
20910 schedule_generic_params_dies_gen (tree t)
20911 {
20912 if (!generic_type_p (t))
20913 return;
20914
20915 if (!generic_type_instances)
20916 vec_alloc (generic_type_instances, 256);
20917
20918 vec_safe_push (generic_type_instances, t);
20919 }
20920
20921 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20922 by append_entry_to_tmpl_value_parm_die_table. This function must
20923 be called after function DIEs have been generated. */
20924
20925 static void
20926 gen_remaining_tmpl_value_param_die_attribute (void)
20927 {
20928 if (tmpl_value_parm_die_table)
20929 {
20930 unsigned i;
20931 die_arg_entry *e;
20932
20933 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20934 tree_add_const_value_attribute (e->die, e->arg);
20935 }
20936 }
20937
20938 /* Generate generic parameters DIEs for instances of generic types
20939 that have been previously scheduled by
20940 schedule_generic_params_dies_gen. This function must be called
20941 after all the types of the CU have been laid out. */
20942
20943 static void
20944 gen_scheduled_generic_parms_dies (void)
20945 {
20946 unsigned i;
20947 tree t;
20948
20949 if (!generic_type_instances)
20950 return;
20951
20952 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20953 if (COMPLETE_TYPE_P (t))
20954 gen_generic_params_dies (t);
20955 }
20956
20957
20958 /* Replace DW_AT_name for the decl with name. */
20959
20960 static void
20961 dwarf2out_set_name (tree decl, tree name)
20962 {
20963 dw_die_ref die;
20964 dw_attr_ref attr;
20965 const char *dname;
20966
20967 die = TYPE_SYMTAB_DIE (decl);
20968 if (!die)
20969 return;
20970
20971 dname = dwarf2_name (name, 0);
20972 if (!dname)
20973 return;
20974
20975 attr = get_AT (die, DW_AT_name);
20976 if (attr)
20977 {
20978 struct indirect_string_node *node;
20979
20980 node = find_AT_string (dname);
20981 /* replace the string. */
20982 attr->dw_attr_val.v.val_str = node;
20983 }
20984
20985 else
20986 add_name_attribute (die, dname);
20987 }
20988
20989 /* True if before or during processing of the first function being emitted. */
20990 static bool in_first_function_p = true;
20991 /* True if loc_note during dwarf2out_var_location call might still be
20992 before first real instruction at address equal to .Ltext0. */
20993 static bool maybe_at_text_label_p = true;
20994 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20995 static unsigned int first_loclabel_num_not_at_text_label;
20996
20997 /* Called by the final INSN scan whenever we see a var location. We
20998 use it to drop labels in the right places, and throw the location in
20999 our lookup table. */
21000
21001 static void
21002 dwarf2out_var_location (rtx loc_note)
21003 {
21004 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21005 struct var_loc_node *newloc;
21006 rtx next_real, next_note;
21007 static const char *last_label;
21008 static const char *last_postcall_label;
21009 static bool last_in_cold_section_p;
21010 static rtx expected_next_loc_note;
21011 tree decl;
21012 bool var_loc_p;
21013
21014 if (!NOTE_P (loc_note))
21015 {
21016 if (CALL_P (loc_note))
21017 {
21018 call_site_count++;
21019 if (SIBLING_CALL_P (loc_note))
21020 tail_call_site_count++;
21021 }
21022 return;
21023 }
21024
21025 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21026 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21027 return;
21028
21029 /* Optimize processing a large consecutive sequence of location
21030 notes so we don't spend too much time in next_real_insn. If the
21031 next insn is another location note, remember the next_real_insn
21032 calculation for next time. */
21033 next_real = cached_next_real_insn;
21034 if (next_real)
21035 {
21036 if (expected_next_loc_note != loc_note)
21037 next_real = NULL_RTX;
21038 }
21039
21040 next_note = NEXT_INSN (loc_note);
21041 if (! next_note
21042 || INSN_DELETED_P (next_note)
21043 || ! NOTE_P (next_note)
21044 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21045 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21046 next_note = NULL_RTX;
21047
21048 if (! next_real)
21049 next_real = next_real_insn (loc_note);
21050
21051 if (next_note)
21052 {
21053 expected_next_loc_note = next_note;
21054 cached_next_real_insn = next_real;
21055 }
21056 else
21057 cached_next_real_insn = NULL_RTX;
21058
21059 /* If there are no instructions which would be affected by this note,
21060 don't do anything. */
21061 if (var_loc_p
21062 && next_real == NULL_RTX
21063 && !NOTE_DURING_CALL_P (loc_note))
21064 return;
21065
21066 if (next_real == NULL_RTX)
21067 next_real = get_last_insn ();
21068
21069 /* If there were any real insns between note we processed last time
21070 and this note (or if it is the first note), clear
21071 last_{,postcall_}label so that they are not reused this time. */
21072 if (last_var_location_insn == NULL_RTX
21073 || last_var_location_insn != next_real
21074 || last_in_cold_section_p != in_cold_section_p)
21075 {
21076 last_label = NULL;
21077 last_postcall_label = NULL;
21078 }
21079
21080 if (var_loc_p)
21081 {
21082 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21083 newloc = add_var_loc_to_decl (decl, loc_note,
21084 NOTE_DURING_CALL_P (loc_note)
21085 ? last_postcall_label : last_label);
21086 if (newloc == NULL)
21087 return;
21088 }
21089 else
21090 {
21091 decl = NULL_TREE;
21092 newloc = NULL;
21093 }
21094
21095 /* If there were no real insns between note we processed last time
21096 and this note, use the label we emitted last time. Otherwise
21097 create a new label and emit it. */
21098 if (last_label == NULL)
21099 {
21100 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21102 loclabel_num++;
21103 last_label = ggc_strdup (loclabel);
21104 /* See if loclabel might be equal to .Ltext0. If yes,
21105 bump first_loclabel_num_not_at_text_label. */
21106 if (!have_multiple_function_sections
21107 && in_first_function_p
21108 && maybe_at_text_label_p)
21109 {
21110 static rtx last_start;
21111 rtx insn;
21112 for (insn = loc_note; insn; insn = previous_insn (insn))
21113 if (insn == last_start)
21114 break;
21115 else if (!NONDEBUG_INSN_P (insn))
21116 continue;
21117 else
21118 {
21119 rtx body = PATTERN (insn);
21120 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21121 continue;
21122 /* Inline asm could occupy zero bytes. */
21123 else if (GET_CODE (body) == ASM_INPUT
21124 || asm_noperands (body) >= 0)
21125 continue;
21126 #ifdef HAVE_attr_length
21127 else if (get_attr_min_length (insn) == 0)
21128 continue;
21129 #endif
21130 else
21131 {
21132 /* Assume insn has non-zero length. */
21133 maybe_at_text_label_p = false;
21134 break;
21135 }
21136 }
21137 if (maybe_at_text_label_p)
21138 {
21139 last_start = loc_note;
21140 first_loclabel_num_not_at_text_label = loclabel_num;
21141 }
21142 }
21143 }
21144
21145 if (!var_loc_p)
21146 {
21147 struct call_arg_loc_node *ca_loc
21148 = ggc_alloc_cleared_call_arg_loc_node ();
21149 rtx prev = prev_real_insn (loc_note), x;
21150 ca_loc->call_arg_loc_note = loc_note;
21151 ca_loc->next = NULL;
21152 ca_loc->label = last_label;
21153 gcc_assert (prev
21154 && (CALL_P (prev)
21155 || (NONJUMP_INSN_P (prev)
21156 && GET_CODE (PATTERN (prev)) == SEQUENCE
21157 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21158 if (!CALL_P (prev))
21159 prev = XVECEXP (PATTERN (prev), 0, 0);
21160 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21161 x = get_call_rtx_from (PATTERN (prev));
21162 if (x)
21163 {
21164 x = XEXP (XEXP (x, 0), 0);
21165 if (GET_CODE (x) == SYMBOL_REF
21166 && SYMBOL_REF_DECL (x)
21167 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21168 ca_loc->symbol_ref = x;
21169 }
21170 ca_loc->block = insn_scope (prev);
21171 if (call_arg_locations)
21172 call_arg_loc_last->next = ca_loc;
21173 else
21174 call_arg_locations = ca_loc;
21175 call_arg_loc_last = ca_loc;
21176 }
21177 else if (!NOTE_DURING_CALL_P (loc_note))
21178 newloc->label = last_label;
21179 else
21180 {
21181 if (!last_postcall_label)
21182 {
21183 sprintf (loclabel, "%s-1", last_label);
21184 last_postcall_label = ggc_strdup (loclabel);
21185 }
21186 newloc->label = last_postcall_label;
21187 }
21188
21189 last_var_location_insn = next_real;
21190 last_in_cold_section_p = in_cold_section_p;
21191 }
21192
21193 /* Note in one location list that text section has changed. */
21194
21195 static int
21196 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21197 {
21198 var_loc_list *list = (var_loc_list *) *slot;
21199 if (list->first)
21200 list->last_before_switch
21201 = list->last->next ? list->last->next : list->last;
21202 return 1;
21203 }
21204
21205 /* Note in all location lists that text section has changed. */
21206
21207 static void
21208 var_location_switch_text_section (void)
21209 {
21210 if (decl_loc_table == NULL)
21211 return;
21212
21213 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21214 }
21215
21216 /* Create a new line number table. */
21217
21218 static dw_line_info_table *
21219 new_line_info_table (void)
21220 {
21221 dw_line_info_table *table;
21222
21223 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21224 table->file_num = 1;
21225 table->line_num = 1;
21226 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21227
21228 return table;
21229 }
21230
21231 /* Lookup the "current" table into which we emit line info, so
21232 that we don't have to do it for every source line. */
21233
21234 static void
21235 set_cur_line_info_table (section *sec)
21236 {
21237 dw_line_info_table *table;
21238
21239 if (sec == text_section)
21240 table = text_section_line_info;
21241 else if (sec == cold_text_section)
21242 {
21243 table = cold_text_section_line_info;
21244 if (!table)
21245 {
21246 cold_text_section_line_info = table = new_line_info_table ();
21247 table->end_label = cold_end_label;
21248 }
21249 }
21250 else
21251 {
21252 const char *end_label;
21253
21254 if (flag_reorder_blocks_and_partition)
21255 {
21256 if (in_cold_section_p)
21257 end_label = crtl->subsections.cold_section_end_label;
21258 else
21259 end_label = crtl->subsections.hot_section_end_label;
21260 }
21261 else
21262 {
21263 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21264 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21265 current_function_funcdef_no);
21266 end_label = ggc_strdup (label);
21267 }
21268
21269 table = new_line_info_table ();
21270 table->end_label = end_label;
21271
21272 vec_safe_push (separate_line_info, table);
21273 }
21274
21275 if (DWARF2_ASM_LINE_DEBUG_INFO)
21276 table->is_stmt = (cur_line_info_table
21277 ? cur_line_info_table->is_stmt
21278 : DWARF_LINE_DEFAULT_IS_STMT_START);
21279 cur_line_info_table = table;
21280 }
21281
21282
21283 /* We need to reset the locations at the beginning of each
21284 function. We can't do this in the end_function hook, because the
21285 declarations that use the locations won't have been output when
21286 that hook is called. Also compute have_multiple_function_sections here. */
21287
21288 static void
21289 dwarf2out_begin_function (tree fun)
21290 {
21291 section *sec = function_section (fun);
21292
21293 if (sec != text_section)
21294 have_multiple_function_sections = true;
21295
21296 if (flag_reorder_blocks_and_partition && !cold_text_section)
21297 {
21298 gcc_assert (current_function_decl == fun);
21299 cold_text_section = unlikely_text_section ();
21300 switch_to_section (cold_text_section);
21301 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21302 switch_to_section (sec);
21303 }
21304
21305 dwarf2out_note_section_used ();
21306 call_site_count = 0;
21307 tail_call_site_count = 0;
21308
21309 set_cur_line_info_table (sec);
21310 }
21311
21312 /* Helper function of dwarf2out_end_function, called only after emitting
21313 the very first function into assembly. Check if some .debug_loc range
21314 might end with a .LVL* label that could be equal to .Ltext0.
21315 In that case we must force using absolute addresses in .debug_loc ranges,
21316 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21317 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21318 list terminator.
21319 Set have_multiple_function_sections to true in that case and
21320 terminate htab traversal. */
21321
21322 static int
21323 find_empty_loc_ranges_at_text_label (void **slot, void *)
21324 {
21325 var_loc_list *entry;
21326 struct var_loc_node *node;
21327
21328 entry = (var_loc_list *) *slot;
21329 node = entry->first;
21330 if (node && node->next && node->next->label)
21331 {
21332 unsigned int i;
21333 const char *label = node->next->label;
21334 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21335
21336 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21337 {
21338 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21339 if (strcmp (label, loclabel) == 0)
21340 {
21341 have_multiple_function_sections = true;
21342 return 0;
21343 }
21344 }
21345 }
21346 return 1;
21347 }
21348
21349 /* Hook called after emitting a function into assembly.
21350 This does something only for the very first function emitted. */
21351
21352 static void
21353 dwarf2out_end_function (unsigned int)
21354 {
21355 if (in_first_function_p
21356 && !have_multiple_function_sections
21357 && first_loclabel_num_not_at_text_label
21358 && decl_loc_table)
21359 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21360 NULL);
21361 in_first_function_p = false;
21362 maybe_at_text_label_p = false;
21363 }
21364
21365 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21366
21367 static void
21368 push_dw_line_info_entry (dw_line_info_table *table,
21369 enum dw_line_info_opcode opcode, unsigned int val)
21370 {
21371 dw_line_info_entry e;
21372 e.opcode = opcode;
21373 e.val = val;
21374 vec_safe_push (table->entries, e);
21375 }
21376
21377 /* Output a label to mark the beginning of a source code line entry
21378 and record information relating to this source line, in
21379 'line_info_table' for later output of the .debug_line section. */
21380 /* ??? The discriminator parameter ought to be unsigned. */
21381
21382 static void
21383 dwarf2out_source_line (unsigned int line, const char *filename,
21384 int discriminator, bool is_stmt)
21385 {
21386 unsigned int file_num;
21387 dw_line_info_table *table;
21388
21389 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21390 return;
21391
21392 /* The discriminator column was added in dwarf4. Simplify the below
21393 by simply removing it if we're not supposed to output it. */
21394 if (dwarf_version < 4 && dwarf_strict)
21395 discriminator = 0;
21396
21397 table = cur_line_info_table;
21398 file_num = maybe_emit_file (lookup_filename (filename));
21399
21400 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21401 the debugger has used the second (possibly duplicate) line number
21402 at the beginning of the function to mark the end of the prologue.
21403 We could eliminate any other duplicates within the function. For
21404 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21405 that second line number entry. */
21406 /* Recall that this end-of-prologue indication is *not* the same thing
21407 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21408 to which the hook corresponds, follows the last insn that was
21409 emitted by gen_prologue. What we need is to precede the first insn
21410 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21411 insn that corresponds to something the user wrote. These may be
21412 very different locations once scheduling is enabled. */
21413
21414 if (0 && file_num == table->file_num
21415 && line == table->line_num
21416 && discriminator == table->discrim_num
21417 && is_stmt == table->is_stmt)
21418 return;
21419
21420 switch_to_section (current_function_section ());
21421
21422 /* If requested, emit something human-readable. */
21423 if (flag_debug_asm)
21424 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21425
21426 if (DWARF2_ASM_LINE_DEBUG_INFO)
21427 {
21428 /* Emit the .loc directive understood by GNU as. */
21429 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21430 file_num, line, is_stmt, discriminator */
21431 fputs ("\t.loc ", asm_out_file);
21432 fprint_ul (asm_out_file, file_num);
21433 putc (' ', asm_out_file);
21434 fprint_ul (asm_out_file, line);
21435 putc (' ', asm_out_file);
21436 putc ('0', asm_out_file);
21437
21438 if (is_stmt != table->is_stmt)
21439 {
21440 fputs (" is_stmt ", asm_out_file);
21441 putc (is_stmt ? '1' : '0', asm_out_file);
21442 }
21443 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21444 {
21445 gcc_assert (discriminator > 0);
21446 fputs (" discriminator ", asm_out_file);
21447 fprint_ul (asm_out_file, (unsigned long) discriminator);
21448 }
21449 putc ('\n', asm_out_file);
21450 }
21451 else
21452 {
21453 unsigned int label_num = ++line_info_label_num;
21454
21455 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21456
21457 push_dw_line_info_entry (table, LI_set_address, label_num);
21458 if (file_num != table->file_num)
21459 push_dw_line_info_entry (table, LI_set_file, file_num);
21460 if (discriminator != table->discrim_num)
21461 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21462 if (is_stmt != table->is_stmt)
21463 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21464 push_dw_line_info_entry (table, LI_set_line, line);
21465 }
21466
21467 table->file_num = file_num;
21468 table->line_num = line;
21469 table->discrim_num = discriminator;
21470 table->is_stmt = is_stmt;
21471 table->in_use = true;
21472 }
21473
21474 /* Record the beginning of a new source file. */
21475
21476 static void
21477 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21478 {
21479 if (flag_eliminate_dwarf2_dups)
21480 {
21481 /* Record the beginning of the file for break_out_includes. */
21482 dw_die_ref bincl_die;
21483
21484 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21485 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21486 }
21487
21488 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21489 {
21490 macinfo_entry e;
21491 e.code = DW_MACINFO_start_file;
21492 e.lineno = lineno;
21493 e.info = ggc_strdup (filename);
21494 vec_safe_push (macinfo_table, e);
21495 }
21496 }
21497
21498 /* Record the end of a source file. */
21499
21500 static void
21501 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21502 {
21503 if (flag_eliminate_dwarf2_dups)
21504 /* Record the end of the file for break_out_includes. */
21505 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21506
21507 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21508 {
21509 macinfo_entry e;
21510 e.code = DW_MACINFO_end_file;
21511 e.lineno = lineno;
21512 e.info = NULL;
21513 vec_safe_push (macinfo_table, e);
21514 }
21515 }
21516
21517 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21518 the tail part of the directive line, i.e. the part which is past the
21519 initial whitespace, #, whitespace, directive-name, whitespace part. */
21520
21521 static void
21522 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21523 const char *buffer ATTRIBUTE_UNUSED)
21524 {
21525 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21526 {
21527 macinfo_entry e;
21528 /* Insert a dummy first entry to be able to optimize the whole
21529 predefined macro block using DW_MACRO_GNU_transparent_include. */
21530 if (macinfo_table->is_empty () && lineno <= 1)
21531 {
21532 e.code = 0;
21533 e.lineno = 0;
21534 e.info = NULL;
21535 vec_safe_push (macinfo_table, e);
21536 }
21537 e.code = DW_MACINFO_define;
21538 e.lineno = lineno;
21539 e.info = ggc_strdup (buffer);
21540 vec_safe_push (macinfo_table, e);
21541 }
21542 }
21543
21544 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21545 the tail part of the directive line, i.e. the part which is past the
21546 initial whitespace, #, whitespace, directive-name, whitespace part. */
21547
21548 static void
21549 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21550 const char *buffer ATTRIBUTE_UNUSED)
21551 {
21552 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21553 {
21554 macinfo_entry e;
21555 /* Insert a dummy first entry to be able to optimize the whole
21556 predefined macro block using DW_MACRO_GNU_transparent_include. */
21557 if (macinfo_table->is_empty () && lineno <= 1)
21558 {
21559 e.code = 0;
21560 e.lineno = 0;
21561 e.info = NULL;
21562 vec_safe_push (macinfo_table, e);
21563 }
21564 e.code = DW_MACINFO_undef;
21565 e.lineno = lineno;
21566 e.info = ggc_strdup (buffer);
21567 vec_safe_push (macinfo_table, e);
21568 }
21569 }
21570
21571 /* Helpers to manipulate hash table of CUs. */
21572
21573 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21574 {
21575 typedef macinfo_entry value_type;
21576 typedef macinfo_entry compare_type;
21577 static inline hashval_t hash (const value_type *);
21578 static inline bool equal (const value_type *, const compare_type *);
21579 };
21580
21581 inline hashval_t
21582 macinfo_entry_hasher::hash (const value_type *entry)
21583 {
21584 return htab_hash_string (entry->info);
21585 }
21586
21587 inline bool
21588 macinfo_entry_hasher::equal (const value_type *entry1,
21589 const compare_type *entry2)
21590 {
21591 return !strcmp (entry1->info, entry2->info);
21592 }
21593
21594 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21595
21596 /* Output a single .debug_macinfo entry. */
21597
21598 static void
21599 output_macinfo_op (macinfo_entry *ref)
21600 {
21601 int file_num;
21602 size_t len;
21603 struct indirect_string_node *node;
21604 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21605 struct dwarf_file_data *fd;
21606
21607 switch (ref->code)
21608 {
21609 case DW_MACINFO_start_file:
21610 fd = lookup_filename (ref->info);
21611 file_num = maybe_emit_file (fd);
21612 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21613 dw2_asm_output_data_uleb128 (ref->lineno,
21614 "Included from line number %lu",
21615 (unsigned long) ref->lineno);
21616 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21617 break;
21618 case DW_MACINFO_end_file:
21619 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21620 break;
21621 case DW_MACINFO_define:
21622 case DW_MACINFO_undef:
21623 len = strlen (ref->info) + 1;
21624 if (!dwarf_strict
21625 && len > DWARF_OFFSET_SIZE
21626 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21627 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21628 {
21629 ref->code = ref->code == DW_MACINFO_define
21630 ? DW_MACRO_GNU_define_indirect
21631 : DW_MACRO_GNU_undef_indirect;
21632 output_macinfo_op (ref);
21633 return;
21634 }
21635 dw2_asm_output_data (1, ref->code,
21636 ref->code == DW_MACINFO_define
21637 ? "Define macro" : "Undefine macro");
21638 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21639 (unsigned long) ref->lineno);
21640 dw2_asm_output_nstring (ref->info, -1, "The macro");
21641 break;
21642 case DW_MACRO_GNU_define_indirect:
21643 case DW_MACRO_GNU_undef_indirect:
21644 node = find_AT_string (ref->info);
21645 gcc_assert (node
21646 && ((node->form == DW_FORM_strp)
21647 || (node->form == DW_FORM_GNU_str_index)));
21648 dw2_asm_output_data (1, ref->code,
21649 ref->code == DW_MACRO_GNU_define_indirect
21650 ? "Define macro indirect"
21651 : "Undefine macro indirect");
21652 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21653 (unsigned long) ref->lineno);
21654 if (node->form == DW_FORM_strp)
21655 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21656 debug_str_section, "The macro: \"%s\"",
21657 ref->info);
21658 else
21659 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21660 ref->info);
21661 break;
21662 case DW_MACRO_GNU_transparent_include:
21663 dw2_asm_output_data (1, ref->code, "Transparent include");
21664 ASM_GENERATE_INTERNAL_LABEL (label,
21665 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21666 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21667 break;
21668 default:
21669 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21670 ASM_COMMENT_START, (unsigned long) ref->code);
21671 break;
21672 }
21673 }
21674
21675 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21676 other compilation unit .debug_macinfo sections. IDX is the first
21677 index of a define/undef, return the number of ops that should be
21678 emitted in a comdat .debug_macinfo section and emit
21679 a DW_MACRO_GNU_transparent_include entry referencing it.
21680 If the define/undef entry should be emitted normally, return 0. */
21681
21682 static unsigned
21683 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21684 macinfo_hash_type *macinfo_htab)
21685 {
21686 macinfo_entry *first, *second, *cur, *inc;
21687 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21688 unsigned char checksum[16];
21689 struct md5_ctx ctx;
21690 char *grp_name, *tail;
21691 const char *base;
21692 unsigned int i, count, encoded_filename_len, linebuf_len;
21693 macinfo_entry **slot;
21694
21695 first = &(*macinfo_table)[idx];
21696 second = &(*macinfo_table)[idx + 1];
21697
21698 /* Optimize only if there are at least two consecutive define/undef ops,
21699 and either all of them are before first DW_MACINFO_start_file
21700 with lineno {0,1} (i.e. predefined macro block), or all of them are
21701 in some included header file. */
21702 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21703 return 0;
21704 if (vec_safe_is_empty (files))
21705 {
21706 if (first->lineno > 1 || second->lineno > 1)
21707 return 0;
21708 }
21709 else if (first->lineno == 0)
21710 return 0;
21711
21712 /* Find the last define/undef entry that can be grouped together
21713 with first and at the same time compute md5 checksum of their
21714 codes, linenumbers and strings. */
21715 md5_init_ctx (&ctx);
21716 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21717 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21718 break;
21719 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21720 break;
21721 else
21722 {
21723 unsigned char code = cur->code;
21724 md5_process_bytes (&code, 1, &ctx);
21725 checksum_uleb128 (cur->lineno, &ctx);
21726 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21727 }
21728 md5_finish_ctx (&ctx, checksum);
21729 count = i - idx;
21730
21731 /* From the containing include filename (if any) pick up just
21732 usable characters from its basename. */
21733 if (vec_safe_is_empty (files))
21734 base = "";
21735 else
21736 base = lbasename (files->last ().info);
21737 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21738 if (ISIDNUM (base[i]) || base[i] == '.')
21739 encoded_filename_len++;
21740 /* Count . at the end. */
21741 if (encoded_filename_len)
21742 encoded_filename_len++;
21743
21744 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21745 linebuf_len = strlen (linebuf);
21746
21747 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21748 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21749 + 16 * 2 + 1);
21750 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21751 tail = grp_name + 4;
21752 if (encoded_filename_len)
21753 {
21754 for (i = 0; base[i]; i++)
21755 if (ISIDNUM (base[i]) || base[i] == '.')
21756 *tail++ = base[i];
21757 *tail++ = '.';
21758 }
21759 memcpy (tail, linebuf, linebuf_len);
21760 tail += linebuf_len;
21761 *tail++ = '.';
21762 for (i = 0; i < 16; i++)
21763 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21764
21765 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21766 in the empty vector entry before the first define/undef. */
21767 inc = &(*macinfo_table)[idx - 1];
21768 inc->code = DW_MACRO_GNU_transparent_include;
21769 inc->lineno = 0;
21770 inc->info = ggc_strdup (grp_name);
21771 if (!macinfo_htab->is_created ())
21772 macinfo_htab->create (10);
21773 /* Avoid emitting duplicates. */
21774 slot = macinfo_htab->find_slot (inc, INSERT);
21775 if (*slot != NULL)
21776 {
21777 inc->code = 0;
21778 inc->info = NULL;
21779 /* If such an entry has been used before, just emit
21780 a DW_MACRO_GNU_transparent_include op. */
21781 inc = *slot;
21782 output_macinfo_op (inc);
21783 /* And clear all macinfo_entry in the range to avoid emitting them
21784 in the second pass. */
21785 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21786 {
21787 cur->code = 0;
21788 cur->info = NULL;
21789 }
21790 }
21791 else
21792 {
21793 *slot = inc;
21794 inc->lineno = macinfo_htab->elements ();
21795 output_macinfo_op (inc);
21796 }
21797 return count;
21798 }
21799
21800 /* Save any strings needed by the macinfo table in the debug str
21801 table. All strings must be collected into the table by the time
21802 index_string is called. */
21803
21804 static void
21805 save_macinfo_strings (void)
21806 {
21807 unsigned len;
21808 unsigned i;
21809 macinfo_entry *ref;
21810
21811 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21812 {
21813 switch (ref->code)
21814 {
21815 /* Match the logic in output_macinfo_op to decide on
21816 indirect strings. */
21817 case DW_MACINFO_define:
21818 case DW_MACINFO_undef:
21819 len = strlen (ref->info) + 1;
21820 if (!dwarf_strict
21821 && len > DWARF_OFFSET_SIZE
21822 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21823 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21824 set_indirect_string (find_AT_string (ref->info));
21825 break;
21826 case DW_MACRO_GNU_define_indirect:
21827 case DW_MACRO_GNU_undef_indirect:
21828 set_indirect_string (find_AT_string (ref->info));
21829 break;
21830 default:
21831 break;
21832 }
21833 }
21834 }
21835
21836 /* Output macinfo section(s). */
21837
21838 static void
21839 output_macinfo (void)
21840 {
21841 unsigned i;
21842 unsigned long length = vec_safe_length (macinfo_table);
21843 macinfo_entry *ref;
21844 vec<macinfo_entry, va_gc> *files = NULL;
21845 macinfo_hash_type macinfo_htab;
21846
21847 if (! length)
21848 return;
21849
21850 /* output_macinfo* uses these interchangeably. */
21851 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21852 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21853 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21854 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21855
21856 /* For .debug_macro emit the section header. */
21857 if (!dwarf_strict)
21858 {
21859 dw2_asm_output_data (2, 4, "DWARF macro version number");
21860 if (DWARF_OFFSET_SIZE == 8)
21861 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21862 else
21863 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21864 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21865 (!dwarf_split_debug_info ? debug_line_section_label
21866 : debug_skeleton_line_section_label),
21867 debug_line_section, NULL);
21868 }
21869
21870 /* In the first loop, it emits the primary .debug_macinfo section
21871 and after each emitted op the macinfo_entry is cleared.
21872 If a longer range of define/undef ops can be optimized using
21873 DW_MACRO_GNU_transparent_include, the
21874 DW_MACRO_GNU_transparent_include op is emitted and kept in
21875 the vector before the first define/undef in the range and the
21876 whole range of define/undef ops is not emitted and kept. */
21877 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21878 {
21879 switch (ref->code)
21880 {
21881 case DW_MACINFO_start_file:
21882 vec_safe_push (files, *ref);
21883 break;
21884 case DW_MACINFO_end_file:
21885 if (!vec_safe_is_empty (files))
21886 files->pop ();
21887 break;
21888 case DW_MACINFO_define:
21889 case DW_MACINFO_undef:
21890 if (!dwarf_strict
21891 && HAVE_COMDAT_GROUP
21892 && vec_safe_length (files) != 1
21893 && i > 0
21894 && i + 1 < length
21895 && (*macinfo_table)[i - 1].code == 0)
21896 {
21897 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21898 if (count)
21899 {
21900 i += count - 1;
21901 continue;
21902 }
21903 }
21904 break;
21905 case 0:
21906 /* A dummy entry may be inserted at the beginning to be able
21907 to optimize the whole block of predefined macros. */
21908 if (i == 0)
21909 continue;
21910 default:
21911 break;
21912 }
21913 output_macinfo_op (ref);
21914 ref->info = NULL;
21915 ref->code = 0;
21916 }
21917
21918 if (!macinfo_htab.is_created ())
21919 return;
21920
21921 macinfo_htab.dispose ();
21922
21923 /* If any DW_MACRO_GNU_transparent_include were used, on those
21924 DW_MACRO_GNU_transparent_include entries terminate the
21925 current chain and switch to a new comdat .debug_macinfo
21926 section and emit the define/undef entries within it. */
21927 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21928 switch (ref->code)
21929 {
21930 case 0:
21931 continue;
21932 case DW_MACRO_GNU_transparent_include:
21933 {
21934 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21935 tree comdat_key = get_identifier (ref->info);
21936 /* Terminate the previous .debug_macinfo section. */
21937 dw2_asm_output_data (1, 0, "End compilation unit");
21938 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21939 SECTION_DEBUG
21940 | SECTION_LINKONCE,
21941 comdat_key);
21942 ASM_GENERATE_INTERNAL_LABEL (label,
21943 DEBUG_MACRO_SECTION_LABEL,
21944 ref->lineno);
21945 ASM_OUTPUT_LABEL (asm_out_file, label);
21946 ref->code = 0;
21947 ref->info = NULL;
21948 dw2_asm_output_data (2, 4, "DWARF macro version number");
21949 if (DWARF_OFFSET_SIZE == 8)
21950 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21951 else
21952 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21953 }
21954 break;
21955 case DW_MACINFO_define:
21956 case DW_MACINFO_undef:
21957 output_macinfo_op (ref);
21958 ref->code = 0;
21959 ref->info = NULL;
21960 break;
21961 default:
21962 gcc_unreachable ();
21963 }
21964 }
21965
21966 /* Set up for Dwarf output at the start of compilation. */
21967
21968 static void
21969 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21970 {
21971 /* Allocate the file_table. */
21972 file_table = htab_create_ggc (50, file_table_hash,
21973 file_table_eq, NULL);
21974
21975 /* Allocate the decl_die_table. */
21976 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21977 decl_die_table_eq, NULL);
21978
21979 /* Allocate the decl_loc_table. */
21980 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21981 decl_loc_table_eq, NULL);
21982
21983 /* Allocate the cached_dw_loc_list_table. */
21984 cached_dw_loc_list_table
21985 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21986 cached_dw_loc_list_table_eq, NULL);
21987
21988 /* Allocate the initial hunk of the decl_scope_table. */
21989 vec_alloc (decl_scope_table, 256);
21990
21991 /* Allocate the initial hunk of the abbrev_die_table. */
21992 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21993 (ABBREV_DIE_TABLE_INCREMENT);
21994 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21995 /* Zero-th entry is allocated, but unused. */
21996 abbrev_die_table_in_use = 1;
21997
21998 /* Allocate the pubtypes and pubnames vectors. */
21999 vec_alloc (pubname_table, 32);
22000 vec_alloc (pubtype_table, 32);
22001
22002 vec_alloc (incomplete_types, 64);
22003
22004 vec_alloc (used_rtx_array, 32);
22005
22006 if (!dwarf_split_debug_info)
22007 {
22008 debug_info_section = get_section (DEBUG_INFO_SECTION,
22009 SECTION_DEBUG, NULL);
22010 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22011 SECTION_DEBUG, NULL);
22012 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22013 SECTION_DEBUG, NULL);
22014 }
22015 else
22016 {
22017 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22018 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22019 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22020 SECTION_DEBUG | SECTION_EXCLUDE,
22021 NULL);
22022 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22023 SECTION_DEBUG, NULL);
22024 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22025 SECTION_DEBUG, NULL);
22026 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22027 SECTION_DEBUG, NULL);
22028 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22029 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22030
22031 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22032 the main .o, but the skeleton_line goes into the split off dwo. */
22033 debug_skeleton_line_section
22034 = get_section (DEBUG_DWO_LINE_SECTION,
22035 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22036 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22037 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22038 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22039 SECTION_DEBUG | SECTION_EXCLUDE,
22040 NULL);
22041 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22042 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22043 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22044 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22045 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22046 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22047 }
22048 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22049 SECTION_DEBUG, NULL);
22050 debug_macinfo_section = get_section (dwarf_strict
22051 ? DEBUG_MACINFO_SECTION
22052 : DEBUG_MACRO_SECTION,
22053 DEBUG_MACRO_SECTION_FLAGS, NULL);
22054 debug_line_section = get_section (DEBUG_LINE_SECTION,
22055 SECTION_DEBUG, NULL);
22056 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22057 SECTION_DEBUG, NULL);
22058 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22059 SECTION_DEBUG, NULL);
22060 debug_str_section = get_section (DEBUG_STR_SECTION,
22061 DEBUG_STR_SECTION_FLAGS, NULL);
22062 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22063 SECTION_DEBUG, NULL);
22064 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22065 SECTION_DEBUG, NULL);
22066
22067 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22068 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22069 DEBUG_ABBREV_SECTION_LABEL, 0);
22070 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22071 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22072 COLD_TEXT_SECTION_LABEL, 0);
22073 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22074
22075 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22076 DEBUG_INFO_SECTION_LABEL, 0);
22077 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22078 DEBUG_LINE_SECTION_LABEL, 0);
22079 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22080 DEBUG_RANGES_SECTION_LABEL, 0);
22081 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22082 DEBUG_ADDR_SECTION_LABEL, 0);
22083 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22084 dwarf_strict
22085 ? DEBUG_MACINFO_SECTION_LABEL
22086 : DEBUG_MACRO_SECTION_LABEL, 0);
22087 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22088
22089 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22090 vec_alloc (macinfo_table, 64);
22091
22092 switch_to_section (text_section);
22093 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22094
22095 /* Make sure the line number table for .text always exists. */
22096 text_section_line_info = new_line_info_table ();
22097 text_section_line_info->end_label = text_end_label;
22098 }
22099
22100 /* Called before compile () starts outputtting functions, variables
22101 and toplevel asms into assembly. */
22102
22103 static void
22104 dwarf2out_assembly_start (void)
22105 {
22106 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22107 && dwarf2out_do_cfi_asm ()
22108 && (!(flag_unwind_tables || flag_exceptions)
22109 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22110 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22111 }
22112
22113 /* A helper function for dwarf2out_finish called through
22114 htab_traverse. Assign a string its index. All strings must be
22115 collected into the table by the time index_string is called,
22116 because the indexing code relies on htab_traverse to traverse nodes
22117 in the same order for each run. */
22118
22119 static int
22120 index_string (void **h, void *v)
22121 {
22122 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22123 unsigned int *index = (unsigned int *) v;
22124
22125 find_string_form (node);
22126 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22127 {
22128 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22129 node->index = *index;
22130 *index += 1;
22131 }
22132 return 1;
22133 }
22134
22135 /* A helper function for output_indirect_strings called through
22136 htab_traverse. Output the offset to a string and update the
22137 current offset. */
22138
22139 static int
22140 output_index_string_offset (void **h, void *v)
22141 {
22142 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22143 unsigned int *offset = (unsigned int *) v;
22144
22145 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22146 {
22147 /* Assert that this node has been assigned an index. */
22148 gcc_assert (node->index != NO_INDEX_ASSIGNED
22149 && node->index != NOT_INDEXED);
22150 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22151 "indexed string 0x%x: %s", node->index, node->str);
22152 *offset += strlen (node->str) + 1;
22153 }
22154 return 1;
22155 }
22156
22157 /* A helper function for dwarf2out_finish called through
22158 htab_traverse. Output the indexed string. */
22159
22160 static int
22161 output_index_string (void **h, void *v)
22162 {
22163 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22164 unsigned int *cur_idx = (unsigned int *) v;
22165
22166 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22167 {
22168 /* Assert that the strings are output in the same order as their
22169 indexes were assigned. */
22170 gcc_assert (*cur_idx == node->index);
22171 assemble_string (node->str, strlen (node->str) + 1);
22172 *cur_idx += 1;
22173 }
22174 return 1;
22175 }
22176
22177 /* A helper function for dwarf2out_finish called through
22178 htab_traverse. Emit one queued .debug_str string. */
22179
22180 static int
22181 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22182 {
22183 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22184
22185 node->form = find_string_form (node);
22186 if (node->form == DW_FORM_strp && node->refcount > 0)
22187 {
22188 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22189 assemble_string (node->str, strlen (node->str) + 1);
22190 }
22191
22192 return 1;
22193 }
22194
22195 /* Output the indexed string table. */
22196
22197 static void
22198 output_indirect_strings (void)
22199 {
22200 switch_to_section (debug_str_section);
22201 if (!dwarf_split_debug_info)
22202 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22203 else
22204 {
22205 unsigned int offset = 0;
22206 unsigned int cur_idx = 0;
22207
22208 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22209
22210 switch_to_section (debug_str_offsets_section);
22211 htab_traverse_noresize (debug_str_hash,
22212 output_index_string_offset,
22213 &offset);
22214 switch_to_section (debug_str_dwo_section);
22215 htab_traverse_noresize (debug_str_hash,
22216 output_index_string,
22217 &cur_idx);
22218 }
22219 }
22220
22221 /* Callback for htab_traverse to assign an index to an entry in the
22222 table, and to write that entry to the .debug_addr section. */
22223
22224 static int
22225 output_addr_table_entry (void **slot, void *data)
22226 {
22227 addr_table_entry *entry = (addr_table_entry *) *slot;
22228 unsigned int *cur_index = (unsigned int *)data;
22229
22230 if (entry->refcount == 0)
22231 {
22232 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22233 || entry->index == NOT_INDEXED);
22234 return 1;
22235 }
22236
22237 gcc_assert (entry->index == *cur_index);
22238 (*cur_index)++;
22239
22240 switch (entry->kind)
22241 {
22242 case ate_kind_rtx:
22243 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22244 "0x%x", entry->index);
22245 break;
22246 case ate_kind_rtx_dtprel:
22247 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22248 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22249 DWARF2_ADDR_SIZE,
22250 entry->addr.rtl);
22251 fputc ('\n', asm_out_file);
22252 break;
22253 case ate_kind_label:
22254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22255 "0x%x", entry->index);
22256 break;
22257 default:
22258 gcc_unreachable ();
22259 }
22260 return 1;
22261 }
22262
22263 /* Produce the .debug_addr section. */
22264
22265 static void
22266 output_addr_table (void)
22267 {
22268 unsigned int index = 0;
22269 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22270 return;
22271
22272 switch_to_section (debug_addr_section);
22273 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22274 }
22275
22276 #if ENABLE_ASSERT_CHECKING
22277 /* Verify that all marks are clear. */
22278
22279 static void
22280 verify_marks_clear (dw_die_ref die)
22281 {
22282 dw_die_ref c;
22283
22284 gcc_assert (! die->die_mark);
22285 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22286 }
22287 #endif /* ENABLE_ASSERT_CHECKING */
22288
22289 /* Clear the marks for a die and its children.
22290 Be cool if the mark isn't set. */
22291
22292 static void
22293 prune_unmark_dies (dw_die_ref die)
22294 {
22295 dw_die_ref c;
22296
22297 if (die->die_mark)
22298 die->die_mark = 0;
22299 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22300 }
22301
22302 /* Given DIE that we're marking as used, find any other dies
22303 it references as attributes and mark them as used. */
22304
22305 static void
22306 prune_unused_types_walk_attribs (dw_die_ref die)
22307 {
22308 dw_attr_ref a;
22309 unsigned ix;
22310
22311 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22312 {
22313 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22314 {
22315 /* A reference to another DIE.
22316 Make sure that it will get emitted.
22317 If it was broken out into a comdat group, don't follow it. */
22318 if (! AT_ref (a)->comdat_type_p
22319 || a->dw_attr == DW_AT_specification)
22320 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22321 }
22322 /* Set the string's refcount to 0 so that prune_unused_types_mark
22323 accounts properly for it. */
22324 if (AT_class (a) == dw_val_class_str)
22325 a->dw_attr_val.v.val_str->refcount = 0;
22326 }
22327 }
22328
22329 /* Mark the generic parameters and arguments children DIEs of DIE. */
22330
22331 static void
22332 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22333 {
22334 dw_die_ref c;
22335
22336 if (die == NULL || die->die_child == NULL)
22337 return;
22338 c = die->die_child;
22339 do
22340 {
22341 if (is_template_parameter (c))
22342 prune_unused_types_mark (c, 1);
22343 c = c->die_sib;
22344 } while (c && c != die->die_child);
22345 }
22346
22347 /* Mark DIE as being used. If DOKIDS is true, then walk down
22348 to DIE's children. */
22349
22350 static void
22351 prune_unused_types_mark (dw_die_ref die, int dokids)
22352 {
22353 dw_die_ref c;
22354
22355 if (die->die_mark == 0)
22356 {
22357 /* We haven't done this node yet. Mark it as used. */
22358 die->die_mark = 1;
22359 /* If this is the DIE of a generic type instantiation,
22360 mark the children DIEs that describe its generic parms and
22361 args. */
22362 prune_unused_types_mark_generic_parms_dies (die);
22363
22364 /* We also have to mark its parents as used.
22365 (But we don't want to mark our parent's kids due to this,
22366 unless it is a class.) */
22367 if (die->die_parent)
22368 prune_unused_types_mark (die->die_parent,
22369 class_scope_p (die->die_parent));
22370
22371 /* Mark any referenced nodes. */
22372 prune_unused_types_walk_attribs (die);
22373
22374 /* If this node is a specification,
22375 also mark the definition, if it exists. */
22376 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22377 prune_unused_types_mark (die->die_definition, 1);
22378 }
22379
22380 if (dokids && die->die_mark != 2)
22381 {
22382 /* We need to walk the children, but haven't done so yet.
22383 Remember that we've walked the kids. */
22384 die->die_mark = 2;
22385
22386 /* If this is an array type, we need to make sure our
22387 kids get marked, even if they're types. If we're
22388 breaking out types into comdat sections, do this
22389 for all type definitions. */
22390 if (die->die_tag == DW_TAG_array_type
22391 || (use_debug_types
22392 && is_type_die (die) && ! is_declaration_die (die)))
22393 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22394 else
22395 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22396 }
22397 }
22398
22399 /* For local classes, look if any static member functions were emitted
22400 and if so, mark them. */
22401
22402 static void
22403 prune_unused_types_walk_local_classes (dw_die_ref die)
22404 {
22405 dw_die_ref c;
22406
22407 if (die->die_mark == 2)
22408 return;
22409
22410 switch (die->die_tag)
22411 {
22412 case DW_TAG_structure_type:
22413 case DW_TAG_union_type:
22414 case DW_TAG_class_type:
22415 break;
22416
22417 case DW_TAG_subprogram:
22418 if (!get_AT_flag (die, DW_AT_declaration)
22419 || die->die_definition != NULL)
22420 prune_unused_types_mark (die, 1);
22421 return;
22422
22423 default:
22424 return;
22425 }
22426
22427 /* Mark children. */
22428 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22429 }
22430
22431 /* Walk the tree DIE and mark types that we actually use. */
22432
22433 static void
22434 prune_unused_types_walk (dw_die_ref die)
22435 {
22436 dw_die_ref c;
22437
22438 /* Don't do anything if this node is already marked and
22439 children have been marked as well. */
22440 if (die->die_mark == 2)
22441 return;
22442
22443 switch (die->die_tag)
22444 {
22445 case DW_TAG_structure_type:
22446 case DW_TAG_union_type:
22447 case DW_TAG_class_type:
22448 if (die->die_perennial_p)
22449 break;
22450
22451 for (c = die->die_parent; c; c = c->die_parent)
22452 if (c->die_tag == DW_TAG_subprogram)
22453 break;
22454
22455 /* Finding used static member functions inside of classes
22456 is needed just for local classes, because for other classes
22457 static member function DIEs with DW_AT_specification
22458 are emitted outside of the DW_TAG_*_type. If we ever change
22459 it, we'd need to call this even for non-local classes. */
22460 if (c)
22461 prune_unused_types_walk_local_classes (die);
22462
22463 /* It's a type node --- don't mark it. */
22464 return;
22465
22466 case DW_TAG_const_type:
22467 case DW_TAG_packed_type:
22468 case DW_TAG_pointer_type:
22469 case DW_TAG_reference_type:
22470 case DW_TAG_rvalue_reference_type:
22471 case DW_TAG_volatile_type:
22472 case DW_TAG_typedef:
22473 case DW_TAG_array_type:
22474 case DW_TAG_interface_type:
22475 case DW_TAG_friend:
22476 case DW_TAG_variant_part:
22477 case DW_TAG_enumeration_type:
22478 case DW_TAG_subroutine_type:
22479 case DW_TAG_string_type:
22480 case DW_TAG_set_type:
22481 case DW_TAG_subrange_type:
22482 case DW_TAG_ptr_to_member_type:
22483 case DW_TAG_file_type:
22484 if (die->die_perennial_p)
22485 break;
22486
22487 /* It's a type node --- don't mark it. */
22488 return;
22489
22490 default:
22491 /* Mark everything else. */
22492 break;
22493 }
22494
22495 if (die->die_mark == 0)
22496 {
22497 die->die_mark = 1;
22498
22499 /* Now, mark any dies referenced from here. */
22500 prune_unused_types_walk_attribs (die);
22501 }
22502
22503 die->die_mark = 2;
22504
22505 /* Mark children. */
22506 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22507 }
22508
22509 /* Increment the string counts on strings referred to from DIE's
22510 attributes. */
22511
22512 static void
22513 prune_unused_types_update_strings (dw_die_ref die)
22514 {
22515 dw_attr_ref a;
22516 unsigned ix;
22517
22518 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22519 if (AT_class (a) == dw_val_class_str)
22520 {
22521 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22522 s->refcount++;
22523 /* Avoid unnecessarily putting strings that are used less than
22524 twice in the hash table. */
22525 if (s->refcount
22526 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22527 {
22528 void ** slot;
22529 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22530 htab_hash_string (s->str),
22531 INSERT);
22532 gcc_assert (*slot == NULL);
22533 *slot = s;
22534 }
22535 }
22536 }
22537
22538 /* Remove from the tree DIE any dies that aren't marked. */
22539
22540 static void
22541 prune_unused_types_prune (dw_die_ref die)
22542 {
22543 dw_die_ref c;
22544
22545 gcc_assert (die->die_mark);
22546 prune_unused_types_update_strings (die);
22547
22548 if (! die->die_child)
22549 return;
22550
22551 c = die->die_child;
22552 do {
22553 dw_die_ref prev = c;
22554 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22555 if (c == die->die_child)
22556 {
22557 /* No marked children between 'prev' and the end of the list. */
22558 if (prev == c)
22559 /* No marked children at all. */
22560 die->die_child = NULL;
22561 else
22562 {
22563 prev->die_sib = c->die_sib;
22564 die->die_child = prev;
22565 }
22566 return;
22567 }
22568
22569 if (c != prev->die_sib)
22570 prev->die_sib = c;
22571 prune_unused_types_prune (c);
22572 } while (c != die->die_child);
22573 }
22574
22575 /* Remove dies representing declarations that we never use. */
22576
22577 static void
22578 prune_unused_types (void)
22579 {
22580 unsigned int i;
22581 limbo_die_node *node;
22582 comdat_type_node *ctnode;
22583 pubname_ref pub;
22584 dw_die_ref base_type;
22585
22586 #if ENABLE_ASSERT_CHECKING
22587 /* All the marks should already be clear. */
22588 verify_marks_clear (comp_unit_die ());
22589 for (node = limbo_die_list; node; node = node->next)
22590 verify_marks_clear (node->die);
22591 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22592 verify_marks_clear (ctnode->root_die);
22593 #endif /* ENABLE_ASSERT_CHECKING */
22594
22595 /* Mark types that are used in global variables. */
22596 premark_types_used_by_global_vars ();
22597
22598 /* Set the mark on nodes that are actually used. */
22599 prune_unused_types_walk (comp_unit_die ());
22600 for (node = limbo_die_list; node; node = node->next)
22601 prune_unused_types_walk (node->die);
22602 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22603 {
22604 prune_unused_types_walk (ctnode->root_die);
22605 prune_unused_types_mark (ctnode->type_die, 1);
22606 }
22607
22608 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22609 are unusual in that they are pubnames that are the children of pubtypes.
22610 They should only be marked via their parent DW_TAG_enumeration_type die,
22611 not as roots in themselves. */
22612 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22613 if (pub->die->die_tag != DW_TAG_enumerator)
22614 prune_unused_types_mark (pub->die, 1);
22615 for (i = 0; base_types.iterate (i, &base_type); i++)
22616 prune_unused_types_mark (base_type, 1);
22617
22618 if (debug_str_hash)
22619 htab_empty (debug_str_hash);
22620 if (skeleton_debug_str_hash)
22621 htab_empty (skeleton_debug_str_hash);
22622 prune_unused_types_prune (comp_unit_die ());
22623 for (node = limbo_die_list; node; node = node->next)
22624 prune_unused_types_prune (node->die);
22625 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22626 prune_unused_types_prune (ctnode->root_die);
22627
22628 /* Leave the marks clear. */
22629 prune_unmark_dies (comp_unit_die ());
22630 for (node = limbo_die_list; node; node = node->next)
22631 prune_unmark_dies (node->die);
22632 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22633 prune_unmark_dies (ctnode->root_die);
22634 }
22635
22636 /* Set the parameter to true if there are any relative pathnames in
22637 the file table. */
22638 static int
22639 file_table_relative_p (void ** slot, void *param)
22640 {
22641 bool *p = (bool *) param;
22642 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22643 if (!IS_ABSOLUTE_PATH (d->filename))
22644 {
22645 *p = true;
22646 return 0;
22647 }
22648 return 1;
22649 }
22650
22651 /* Helpers to manipulate hash table of comdat type units. */
22652
22653 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22654 {
22655 typedef comdat_type_node value_type;
22656 typedef comdat_type_node compare_type;
22657 static inline hashval_t hash (const value_type *);
22658 static inline bool equal (const value_type *, const compare_type *);
22659 };
22660
22661 inline hashval_t
22662 comdat_type_hasher::hash (const value_type *type_node)
22663 {
22664 hashval_t h;
22665 memcpy (&h, type_node->signature, sizeof (h));
22666 return h;
22667 }
22668
22669 inline bool
22670 comdat_type_hasher::equal (const value_type *type_node_1,
22671 const compare_type *type_node_2)
22672 {
22673 return (! memcmp (type_node_1->signature, type_node_2->signature,
22674 DWARF_TYPE_SIGNATURE_SIZE));
22675 }
22676
22677 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22678 to the location it would have been added, should we know its
22679 DECL_ASSEMBLER_NAME when we added other attributes. This will
22680 probably improve compactness of debug info, removing equivalent
22681 abbrevs, and hide any differences caused by deferring the
22682 computation of the assembler name, triggered by e.g. PCH. */
22683
22684 static inline void
22685 move_linkage_attr (dw_die_ref die)
22686 {
22687 unsigned ix = vec_safe_length (die->die_attr);
22688 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22689
22690 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22691 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22692
22693 while (--ix > 0)
22694 {
22695 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22696
22697 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22698 break;
22699 }
22700
22701 if (ix != vec_safe_length (die->die_attr) - 1)
22702 {
22703 die->die_attr->pop ();
22704 die->die_attr->quick_insert (ix, linkage);
22705 }
22706 }
22707
22708 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22709 referenced from typed stack ops and count how often they are used. */
22710
22711 static void
22712 mark_base_types (dw_loc_descr_ref loc)
22713 {
22714 dw_die_ref base_type = NULL;
22715
22716 for (; loc; loc = loc->dw_loc_next)
22717 {
22718 switch (loc->dw_loc_opc)
22719 {
22720 case DW_OP_GNU_regval_type:
22721 case DW_OP_GNU_deref_type:
22722 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22723 break;
22724 case DW_OP_GNU_convert:
22725 case DW_OP_GNU_reinterpret:
22726 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22727 continue;
22728 /* FALLTHRU */
22729 case DW_OP_GNU_const_type:
22730 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22731 break;
22732 case DW_OP_GNU_entry_value:
22733 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22734 continue;
22735 default:
22736 continue;
22737 }
22738 gcc_assert (base_type->die_parent == comp_unit_die ());
22739 if (base_type->die_mark)
22740 base_type->die_mark++;
22741 else
22742 {
22743 base_types.safe_push (base_type);
22744 base_type->die_mark = 1;
22745 }
22746 }
22747 }
22748
22749 /* Comparison function for sorting marked base types. */
22750
22751 static int
22752 base_type_cmp (const void *x, const void *y)
22753 {
22754 dw_die_ref dx = *(const dw_die_ref *) x;
22755 dw_die_ref dy = *(const dw_die_ref *) y;
22756 unsigned int byte_size1, byte_size2;
22757 unsigned int encoding1, encoding2;
22758 if (dx->die_mark > dy->die_mark)
22759 return -1;
22760 if (dx->die_mark < dy->die_mark)
22761 return 1;
22762 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22763 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22764 if (byte_size1 < byte_size2)
22765 return 1;
22766 if (byte_size1 > byte_size2)
22767 return -1;
22768 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22769 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22770 if (encoding1 < encoding2)
22771 return 1;
22772 if (encoding1 > encoding2)
22773 return -1;
22774 return 0;
22775 }
22776
22777 /* Move base types marked by mark_base_types as early as possible
22778 in the CU, sorted by decreasing usage count both to make the
22779 uleb128 references as small as possible and to make sure they
22780 will have die_offset already computed by calc_die_sizes when
22781 sizes of typed stack loc ops is computed. */
22782
22783 static void
22784 move_marked_base_types (void)
22785 {
22786 unsigned int i;
22787 dw_die_ref base_type, die, c;
22788
22789 if (base_types.is_empty ())
22790 return;
22791
22792 /* Sort by decreasing usage count, they will be added again in that
22793 order later on. */
22794 base_types.qsort (base_type_cmp);
22795 die = comp_unit_die ();
22796 c = die->die_child;
22797 do
22798 {
22799 dw_die_ref prev = c;
22800 c = c->die_sib;
22801 while (c->die_mark)
22802 {
22803 remove_child_with_prev (c, prev);
22804 /* As base types got marked, there must be at least
22805 one node other than DW_TAG_base_type. */
22806 gcc_assert (c != c->die_sib);
22807 c = c->die_sib;
22808 }
22809 }
22810 while (c != die->die_child);
22811 gcc_assert (die->die_child);
22812 c = die->die_child;
22813 for (i = 0; base_types.iterate (i, &base_type); i++)
22814 {
22815 base_type->die_mark = 0;
22816 base_type->die_sib = c->die_sib;
22817 c->die_sib = base_type;
22818 c = base_type;
22819 }
22820 }
22821
22822 /* Helper function for resolve_addr, attempt to resolve
22823 one CONST_STRING, return non-zero if not successful. Similarly verify that
22824 SYMBOL_REFs refer to variables emitted in the current CU. */
22825
22826 static int
22827 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22828 {
22829 rtx rtl = *addr;
22830
22831 if (GET_CODE (rtl) == CONST_STRING)
22832 {
22833 size_t len = strlen (XSTR (rtl, 0)) + 1;
22834 tree t = build_string (len, XSTR (rtl, 0));
22835 tree tlen = size_int (len - 1);
22836 TREE_TYPE (t)
22837 = build_array_type (char_type_node, build_index_type (tlen));
22838 rtl = lookup_constant_def (t);
22839 if (!rtl || !MEM_P (rtl))
22840 return 1;
22841 rtl = XEXP (rtl, 0);
22842 if (GET_CODE (rtl) == SYMBOL_REF
22843 && SYMBOL_REF_DECL (rtl)
22844 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22845 return 1;
22846 vec_safe_push (used_rtx_array, rtl);
22847 *addr = rtl;
22848 return 0;
22849 }
22850
22851 if (GET_CODE (rtl) == SYMBOL_REF
22852 && SYMBOL_REF_DECL (rtl))
22853 {
22854 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22855 {
22856 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22857 return 1;
22858 }
22859 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22860 return 1;
22861 }
22862
22863 if (GET_CODE (rtl) == CONST
22864 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22865 return 1;
22866
22867 return 0;
22868 }
22869
22870 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22871 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22872 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22873
22874 static rtx
22875 string_cst_pool_decl (tree t)
22876 {
22877 rtx rtl = output_constant_def (t, 1);
22878 unsigned char *array;
22879 dw_loc_descr_ref l;
22880 tree decl;
22881 size_t len;
22882 dw_die_ref ref;
22883
22884 if (!rtl || !MEM_P (rtl))
22885 return NULL_RTX;
22886 rtl = XEXP (rtl, 0);
22887 if (GET_CODE (rtl) != SYMBOL_REF
22888 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22889 return NULL_RTX;
22890
22891 decl = SYMBOL_REF_DECL (rtl);
22892 if (!lookup_decl_die (decl))
22893 {
22894 len = TREE_STRING_LENGTH (t);
22895 vec_safe_push (used_rtx_array, rtl);
22896 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22897 array = (unsigned char *) ggc_alloc_atomic (len);
22898 memcpy (array, TREE_STRING_POINTER (t), len);
22899 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22900 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22901 l->dw_loc_oprnd2.v.val_vec.length = len;
22902 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22903 l->dw_loc_oprnd2.v.val_vec.array = array;
22904 add_AT_loc (ref, DW_AT_location, l);
22905 equate_decl_number_to_die (decl, ref);
22906 }
22907 return rtl;
22908 }
22909
22910 /* Helper function of resolve_addr_in_expr. LOC is
22911 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22912 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22913 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22914 with DW_OP_GNU_implicit_pointer if possible
22915 and return true, if unsuccessful, return false. */
22916
22917 static bool
22918 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22919 {
22920 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22921 HOST_WIDE_INT offset = 0;
22922 dw_die_ref ref = NULL;
22923 tree decl;
22924
22925 if (GET_CODE (rtl) == CONST
22926 && GET_CODE (XEXP (rtl, 0)) == PLUS
22927 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22928 {
22929 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22930 rtl = XEXP (XEXP (rtl, 0), 0);
22931 }
22932 if (GET_CODE (rtl) == CONST_STRING)
22933 {
22934 size_t len = strlen (XSTR (rtl, 0)) + 1;
22935 tree t = build_string (len, XSTR (rtl, 0));
22936 tree tlen = size_int (len - 1);
22937
22938 TREE_TYPE (t)
22939 = build_array_type (char_type_node, build_index_type (tlen));
22940 rtl = string_cst_pool_decl (t);
22941 if (!rtl)
22942 return false;
22943 }
22944 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22945 {
22946 decl = SYMBOL_REF_DECL (rtl);
22947 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22948 {
22949 ref = lookup_decl_die (decl);
22950 if (ref && (get_AT (ref, DW_AT_location)
22951 || get_AT (ref, DW_AT_const_value)))
22952 {
22953 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22954 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22955 loc->dw_loc_oprnd1.val_entry = NULL;
22956 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22957 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22958 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22959 loc->dw_loc_oprnd2.v.val_int = offset;
22960 return true;
22961 }
22962 }
22963 }
22964 return false;
22965 }
22966
22967 /* Helper function for resolve_addr, handle one location
22968 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22969 the location list couldn't be resolved. */
22970
22971 static bool
22972 resolve_addr_in_expr (dw_loc_descr_ref loc)
22973 {
22974 dw_loc_descr_ref keep = NULL;
22975 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22976 switch (loc->dw_loc_opc)
22977 {
22978 case DW_OP_addr:
22979 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22980 {
22981 if ((prev == NULL
22982 || prev->dw_loc_opc == DW_OP_piece
22983 || prev->dw_loc_opc == DW_OP_bit_piece)
22984 && loc->dw_loc_next
22985 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22986 && !dwarf_strict
22987 && optimize_one_addr_into_implicit_ptr (loc))
22988 break;
22989 return false;
22990 }
22991 break;
22992 case DW_OP_GNU_addr_index:
22993 case DW_OP_GNU_const_index:
22994 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22995 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22996 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22997 NULL))
22998 return false;
22999 break;
23000 case DW_OP_const4u:
23001 case DW_OP_const8u:
23002 if (loc->dtprel
23003 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23004 return false;
23005 break;
23006 case DW_OP_plus_uconst:
23007 if (size_of_loc_descr (loc)
23008 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23009 + 1
23010 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23011 {
23012 dw_loc_descr_ref repl
23013 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23014 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23015 add_loc_descr (&repl, loc->dw_loc_next);
23016 *loc = *repl;
23017 }
23018 break;
23019 case DW_OP_implicit_value:
23020 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23021 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23022 return false;
23023 break;
23024 case DW_OP_GNU_implicit_pointer:
23025 case DW_OP_GNU_parameter_ref:
23026 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23027 {
23028 dw_die_ref ref
23029 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23030 if (ref == NULL)
23031 return false;
23032 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23033 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23034 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23035 }
23036 break;
23037 case DW_OP_GNU_const_type:
23038 case DW_OP_GNU_regval_type:
23039 case DW_OP_GNU_deref_type:
23040 case DW_OP_GNU_convert:
23041 case DW_OP_GNU_reinterpret:
23042 while (loc->dw_loc_next
23043 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23044 {
23045 dw_die_ref base1, base2;
23046 unsigned enc1, enc2, size1, size2;
23047 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23048 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23049 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23050 else if (loc->dw_loc_oprnd1.val_class
23051 == dw_val_class_unsigned_const)
23052 break;
23053 else
23054 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23055 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23056 == dw_val_class_unsigned_const)
23057 break;
23058 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23059 gcc_assert (base1->die_tag == DW_TAG_base_type
23060 && base2->die_tag == DW_TAG_base_type);
23061 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23062 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23063 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23064 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23065 if (size1 == size2
23066 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23067 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23068 && loc != keep)
23069 || enc1 == enc2))
23070 {
23071 /* Optimize away next DW_OP_GNU_convert after
23072 adjusting LOC's base type die reference. */
23073 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23074 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23075 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23076 else
23077 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23078 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23079 continue;
23080 }
23081 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23082 point typed stack entry. */
23083 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23084 keep = loc->dw_loc_next;
23085 break;
23086 }
23087 break;
23088 default:
23089 break;
23090 }
23091 return true;
23092 }
23093
23094 /* Helper function of resolve_addr. DIE had DW_AT_location of
23095 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23096 and DW_OP_addr couldn't be resolved. resolve_addr has already
23097 removed the DW_AT_location attribute. This function attempts to
23098 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23099 to it or DW_AT_const_value attribute, if possible. */
23100
23101 static void
23102 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23103 {
23104 if (TREE_CODE (decl) != VAR_DECL
23105 || lookup_decl_die (decl) != die
23106 || DECL_EXTERNAL (decl)
23107 || !TREE_STATIC (decl)
23108 || DECL_INITIAL (decl) == NULL_TREE
23109 || DECL_P (DECL_INITIAL (decl))
23110 || get_AT (die, DW_AT_const_value))
23111 return;
23112
23113 tree init = DECL_INITIAL (decl);
23114 HOST_WIDE_INT offset = 0;
23115 /* For variables that have been optimized away and thus
23116 don't have a memory location, see if we can emit
23117 DW_AT_const_value instead. */
23118 if (tree_add_const_value_attribute (die, init))
23119 return;
23120 if (dwarf_strict)
23121 return;
23122 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23123 and ADDR_EXPR refers to a decl that has DW_AT_location or
23124 DW_AT_const_value (but isn't addressable, otherwise
23125 resolving the original DW_OP_addr wouldn't fail), see if
23126 we can add DW_OP_GNU_implicit_pointer. */
23127 STRIP_NOPS (init);
23128 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23129 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23130 {
23131 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23132 init = TREE_OPERAND (init, 0);
23133 STRIP_NOPS (init);
23134 }
23135 if (TREE_CODE (init) != ADDR_EXPR)
23136 return;
23137 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23138 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23139 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23140 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23141 && TREE_OPERAND (init, 0) != decl))
23142 {
23143 dw_die_ref ref;
23144 dw_loc_descr_ref l;
23145
23146 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23147 {
23148 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23149 if (!rtl)
23150 return;
23151 decl = SYMBOL_REF_DECL (rtl);
23152 }
23153 else
23154 decl = TREE_OPERAND (init, 0);
23155 ref = lookup_decl_die (decl);
23156 if (ref == NULL
23157 || (!get_AT (ref, DW_AT_location)
23158 && !get_AT (ref, DW_AT_const_value)))
23159 return;
23160 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23161 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23162 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23163 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23164 add_AT_loc (die, DW_AT_location, l);
23165 }
23166 }
23167
23168 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23169 an address in .rodata section if the string literal is emitted there,
23170 or remove the containing location list or replace DW_AT_const_value
23171 with DW_AT_location and empty location expression, if it isn't found
23172 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23173 to something that has been emitted in the current CU. */
23174
23175 static void
23176 resolve_addr (dw_die_ref die)
23177 {
23178 dw_die_ref c;
23179 dw_attr_ref a;
23180 dw_loc_list_ref *curr, *start, loc;
23181 unsigned ix;
23182
23183 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23184 switch (AT_class (a))
23185 {
23186 case dw_val_class_loc_list:
23187 start = curr = AT_loc_list_ptr (a);
23188 loc = *curr;
23189 gcc_assert (loc);
23190 /* The same list can be referenced more than once. See if we have
23191 already recorded the result from a previous pass. */
23192 if (loc->replaced)
23193 *curr = loc->dw_loc_next;
23194 else if (!loc->resolved_addr)
23195 {
23196 /* As things stand, we do not expect or allow one die to
23197 reference a suffix of another die's location list chain.
23198 References must be identical or completely separate.
23199 There is therefore no need to cache the result of this
23200 pass on any list other than the first; doing so
23201 would lead to unnecessary writes. */
23202 while (*curr)
23203 {
23204 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23205 if (!resolve_addr_in_expr ((*curr)->expr))
23206 {
23207 dw_loc_list_ref next = (*curr)->dw_loc_next;
23208 dw_loc_descr_ref l = (*curr)->expr;
23209
23210 if (next && (*curr)->ll_symbol)
23211 {
23212 gcc_assert (!next->ll_symbol);
23213 next->ll_symbol = (*curr)->ll_symbol;
23214 }
23215 if (dwarf_split_debug_info)
23216 remove_loc_list_addr_table_entries (l);
23217 *curr = next;
23218 }
23219 else
23220 {
23221 mark_base_types ((*curr)->expr);
23222 curr = &(*curr)->dw_loc_next;
23223 }
23224 }
23225 if (loc == *start)
23226 loc->resolved_addr = 1;
23227 else
23228 {
23229 loc->replaced = 1;
23230 loc->dw_loc_next = *start;
23231 }
23232 }
23233 if (!*start)
23234 {
23235 remove_AT (die, a->dw_attr);
23236 ix--;
23237 }
23238 break;
23239 case dw_val_class_loc:
23240 {
23241 dw_loc_descr_ref l = AT_loc (a);
23242 /* For -gdwarf-2 don't attempt to optimize
23243 DW_AT_data_member_location containing
23244 DW_OP_plus_uconst - older consumers might
23245 rely on it being that op instead of a more complex,
23246 but shorter, location description. */
23247 if ((dwarf_version > 2
23248 || a->dw_attr != DW_AT_data_member_location
23249 || l == NULL
23250 || l->dw_loc_opc != DW_OP_plus_uconst
23251 || l->dw_loc_next != NULL)
23252 && !resolve_addr_in_expr (l))
23253 {
23254 if (dwarf_split_debug_info)
23255 remove_loc_list_addr_table_entries (l);
23256 if (l != NULL
23257 && l->dw_loc_next == NULL
23258 && l->dw_loc_opc == DW_OP_addr
23259 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23260 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23261 && a->dw_attr == DW_AT_location)
23262 {
23263 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23264 remove_AT (die, a->dw_attr);
23265 ix--;
23266 optimize_location_into_implicit_ptr (die, decl);
23267 break;
23268 }
23269 remove_AT (die, a->dw_attr);
23270 ix--;
23271 }
23272 else
23273 mark_base_types (l);
23274 }
23275 break;
23276 case dw_val_class_addr:
23277 if (a->dw_attr == DW_AT_const_value
23278 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23279 {
23280 if (AT_index (a) != NOT_INDEXED)
23281 remove_addr_table_entry (a->dw_attr_val.val_entry);
23282 remove_AT (die, a->dw_attr);
23283 ix--;
23284 }
23285 if (die->die_tag == DW_TAG_GNU_call_site
23286 && a->dw_attr == DW_AT_abstract_origin)
23287 {
23288 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23289 dw_die_ref tdie = lookup_decl_die (tdecl);
23290 if (tdie == NULL
23291 && DECL_EXTERNAL (tdecl)
23292 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23293 {
23294 force_decl_die (tdecl);
23295 tdie = lookup_decl_die (tdecl);
23296 }
23297 if (tdie)
23298 {
23299 a->dw_attr_val.val_class = dw_val_class_die_ref;
23300 a->dw_attr_val.v.val_die_ref.die = tdie;
23301 a->dw_attr_val.v.val_die_ref.external = 0;
23302 }
23303 else
23304 {
23305 if (AT_index (a) != NOT_INDEXED)
23306 remove_addr_table_entry (a->dw_attr_val.val_entry);
23307 remove_AT (die, a->dw_attr);
23308 ix--;
23309 }
23310 }
23311 break;
23312 default:
23313 break;
23314 }
23315
23316 FOR_EACH_CHILD (die, c, resolve_addr (c));
23317 }
23318 \f
23319 /* Helper routines for optimize_location_lists.
23320 This pass tries to share identical local lists in .debug_loc
23321 section. */
23322
23323 /* Iteratively hash operands of LOC opcode. */
23324
23325 static hashval_t
23326 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23327 {
23328 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23329 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23330
23331 switch (loc->dw_loc_opc)
23332 {
23333 case DW_OP_const4u:
23334 case DW_OP_const8u:
23335 if (loc->dtprel)
23336 goto hash_addr;
23337 /* FALLTHRU */
23338 case DW_OP_const1u:
23339 case DW_OP_const1s:
23340 case DW_OP_const2u:
23341 case DW_OP_const2s:
23342 case DW_OP_const4s:
23343 case DW_OP_const8s:
23344 case DW_OP_constu:
23345 case DW_OP_consts:
23346 case DW_OP_pick:
23347 case DW_OP_plus_uconst:
23348 case DW_OP_breg0:
23349 case DW_OP_breg1:
23350 case DW_OP_breg2:
23351 case DW_OP_breg3:
23352 case DW_OP_breg4:
23353 case DW_OP_breg5:
23354 case DW_OP_breg6:
23355 case DW_OP_breg7:
23356 case DW_OP_breg8:
23357 case DW_OP_breg9:
23358 case DW_OP_breg10:
23359 case DW_OP_breg11:
23360 case DW_OP_breg12:
23361 case DW_OP_breg13:
23362 case DW_OP_breg14:
23363 case DW_OP_breg15:
23364 case DW_OP_breg16:
23365 case DW_OP_breg17:
23366 case DW_OP_breg18:
23367 case DW_OP_breg19:
23368 case DW_OP_breg20:
23369 case DW_OP_breg21:
23370 case DW_OP_breg22:
23371 case DW_OP_breg23:
23372 case DW_OP_breg24:
23373 case DW_OP_breg25:
23374 case DW_OP_breg26:
23375 case DW_OP_breg27:
23376 case DW_OP_breg28:
23377 case DW_OP_breg29:
23378 case DW_OP_breg30:
23379 case DW_OP_breg31:
23380 case DW_OP_regx:
23381 case DW_OP_fbreg:
23382 case DW_OP_piece:
23383 case DW_OP_deref_size:
23384 case DW_OP_xderef_size:
23385 hash = iterative_hash_object (val1->v.val_int, hash);
23386 break;
23387 case DW_OP_skip:
23388 case DW_OP_bra:
23389 {
23390 int offset;
23391
23392 gcc_assert (val1->val_class == dw_val_class_loc);
23393 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23394 hash = iterative_hash_object (offset, hash);
23395 }
23396 break;
23397 case DW_OP_implicit_value:
23398 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23399 switch (val2->val_class)
23400 {
23401 case dw_val_class_const:
23402 hash = iterative_hash_object (val2->v.val_int, hash);
23403 break;
23404 case dw_val_class_vec:
23405 {
23406 unsigned int elt_size = val2->v.val_vec.elt_size;
23407 unsigned int len = val2->v.val_vec.length;
23408
23409 hash = iterative_hash_object (elt_size, hash);
23410 hash = iterative_hash_object (len, hash);
23411 hash = iterative_hash (val2->v.val_vec.array,
23412 len * elt_size, hash);
23413 }
23414 break;
23415 case dw_val_class_const_double:
23416 hash = iterative_hash_object (val2->v.val_double.low, hash);
23417 hash = iterative_hash_object (val2->v.val_double.high, hash);
23418 break;
23419 case dw_val_class_addr:
23420 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23421 break;
23422 default:
23423 gcc_unreachable ();
23424 }
23425 break;
23426 case DW_OP_bregx:
23427 case DW_OP_bit_piece:
23428 hash = iterative_hash_object (val1->v.val_int, hash);
23429 hash = iterative_hash_object (val2->v.val_int, hash);
23430 break;
23431 case DW_OP_addr:
23432 hash_addr:
23433 if (loc->dtprel)
23434 {
23435 unsigned char dtprel = 0xd1;
23436 hash = iterative_hash_object (dtprel, hash);
23437 }
23438 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23439 break;
23440 case DW_OP_GNU_addr_index:
23441 case DW_OP_GNU_const_index:
23442 {
23443 if (loc->dtprel)
23444 {
23445 unsigned char dtprel = 0xd1;
23446 hash = iterative_hash_object (dtprel, hash);
23447 }
23448 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23449 }
23450 break;
23451 case DW_OP_GNU_implicit_pointer:
23452 hash = iterative_hash_object (val2->v.val_int, hash);
23453 break;
23454 case DW_OP_GNU_entry_value:
23455 hash = hash_loc_operands (val1->v.val_loc, hash);
23456 break;
23457 case DW_OP_GNU_regval_type:
23458 case DW_OP_GNU_deref_type:
23459 {
23460 unsigned int byte_size
23461 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23462 unsigned int encoding
23463 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23464 hash = iterative_hash_object (val1->v.val_int, hash);
23465 hash = iterative_hash_object (byte_size, hash);
23466 hash = iterative_hash_object (encoding, hash);
23467 }
23468 break;
23469 case DW_OP_GNU_convert:
23470 case DW_OP_GNU_reinterpret:
23471 if (val1->val_class == dw_val_class_unsigned_const)
23472 {
23473 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23474 break;
23475 }
23476 /* FALLTHRU */
23477 case DW_OP_GNU_const_type:
23478 {
23479 unsigned int byte_size
23480 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23481 unsigned int encoding
23482 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23483 hash = iterative_hash_object (byte_size, hash);
23484 hash = iterative_hash_object (encoding, hash);
23485 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23486 break;
23487 hash = iterative_hash_object (val2->val_class, hash);
23488 switch (val2->val_class)
23489 {
23490 case dw_val_class_const:
23491 hash = iterative_hash_object (val2->v.val_int, hash);
23492 break;
23493 case dw_val_class_vec:
23494 {
23495 unsigned int elt_size = val2->v.val_vec.elt_size;
23496 unsigned int len = val2->v.val_vec.length;
23497
23498 hash = iterative_hash_object (elt_size, hash);
23499 hash = iterative_hash_object (len, hash);
23500 hash = iterative_hash (val2->v.val_vec.array,
23501 len * elt_size, hash);
23502 }
23503 break;
23504 case dw_val_class_const_double:
23505 hash = iterative_hash_object (val2->v.val_double.low, hash);
23506 hash = iterative_hash_object (val2->v.val_double.high, hash);
23507 break;
23508 default:
23509 gcc_unreachable ();
23510 }
23511 }
23512 break;
23513
23514 default:
23515 /* Other codes have no operands. */
23516 break;
23517 }
23518 return hash;
23519 }
23520
23521 /* Iteratively hash the whole DWARF location expression LOC. */
23522
23523 static inline hashval_t
23524 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23525 {
23526 dw_loc_descr_ref l;
23527 bool sizes_computed = false;
23528 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23529 size_of_locs (loc);
23530
23531 for (l = loc; l != NULL; l = l->dw_loc_next)
23532 {
23533 enum dwarf_location_atom opc = l->dw_loc_opc;
23534 hash = iterative_hash_object (opc, hash);
23535 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23536 {
23537 size_of_locs (loc);
23538 sizes_computed = true;
23539 }
23540 hash = hash_loc_operands (l, hash);
23541 }
23542 return hash;
23543 }
23544
23545 /* Compute hash of the whole location list LIST_HEAD. */
23546
23547 static inline void
23548 hash_loc_list (dw_loc_list_ref list_head)
23549 {
23550 dw_loc_list_ref curr = list_head;
23551 hashval_t hash = 0;
23552
23553 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23554 {
23555 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23556 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23557 if (curr->section)
23558 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23559 hash);
23560 hash = hash_locs (curr->expr, hash);
23561 }
23562 list_head->hash = hash;
23563 }
23564
23565 /* Return true if X and Y opcodes have the same operands. */
23566
23567 static inline bool
23568 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23569 {
23570 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23571 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23572 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23573 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23574
23575 switch (x->dw_loc_opc)
23576 {
23577 case DW_OP_const4u:
23578 case DW_OP_const8u:
23579 if (x->dtprel)
23580 goto hash_addr;
23581 /* FALLTHRU */
23582 case DW_OP_const1u:
23583 case DW_OP_const1s:
23584 case DW_OP_const2u:
23585 case DW_OP_const2s:
23586 case DW_OP_const4s:
23587 case DW_OP_const8s:
23588 case DW_OP_constu:
23589 case DW_OP_consts:
23590 case DW_OP_pick:
23591 case DW_OP_plus_uconst:
23592 case DW_OP_breg0:
23593 case DW_OP_breg1:
23594 case DW_OP_breg2:
23595 case DW_OP_breg3:
23596 case DW_OP_breg4:
23597 case DW_OP_breg5:
23598 case DW_OP_breg6:
23599 case DW_OP_breg7:
23600 case DW_OP_breg8:
23601 case DW_OP_breg9:
23602 case DW_OP_breg10:
23603 case DW_OP_breg11:
23604 case DW_OP_breg12:
23605 case DW_OP_breg13:
23606 case DW_OP_breg14:
23607 case DW_OP_breg15:
23608 case DW_OP_breg16:
23609 case DW_OP_breg17:
23610 case DW_OP_breg18:
23611 case DW_OP_breg19:
23612 case DW_OP_breg20:
23613 case DW_OP_breg21:
23614 case DW_OP_breg22:
23615 case DW_OP_breg23:
23616 case DW_OP_breg24:
23617 case DW_OP_breg25:
23618 case DW_OP_breg26:
23619 case DW_OP_breg27:
23620 case DW_OP_breg28:
23621 case DW_OP_breg29:
23622 case DW_OP_breg30:
23623 case DW_OP_breg31:
23624 case DW_OP_regx:
23625 case DW_OP_fbreg:
23626 case DW_OP_piece:
23627 case DW_OP_deref_size:
23628 case DW_OP_xderef_size:
23629 return valx1->v.val_int == valy1->v.val_int;
23630 case DW_OP_skip:
23631 case DW_OP_bra:
23632 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23633 can cause irrelevant differences in dw_loc_addr. */
23634 gcc_assert (valx1->val_class == dw_val_class_loc
23635 && valy1->val_class == dw_val_class_loc
23636 && (dwarf_split_debug_info
23637 || x->dw_loc_addr == y->dw_loc_addr));
23638 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23639 case DW_OP_implicit_value:
23640 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23641 || valx2->val_class != valy2->val_class)
23642 return false;
23643 switch (valx2->val_class)
23644 {
23645 case dw_val_class_const:
23646 return valx2->v.val_int == valy2->v.val_int;
23647 case dw_val_class_vec:
23648 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23649 && valx2->v.val_vec.length == valy2->v.val_vec.length
23650 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23651 valx2->v.val_vec.elt_size
23652 * valx2->v.val_vec.length) == 0;
23653 case dw_val_class_const_double:
23654 return valx2->v.val_double.low == valy2->v.val_double.low
23655 && valx2->v.val_double.high == valy2->v.val_double.high;
23656 case dw_val_class_addr:
23657 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23658 default:
23659 gcc_unreachable ();
23660 }
23661 case DW_OP_bregx:
23662 case DW_OP_bit_piece:
23663 return valx1->v.val_int == valy1->v.val_int
23664 && valx2->v.val_int == valy2->v.val_int;
23665 case DW_OP_addr:
23666 hash_addr:
23667 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23668 case DW_OP_GNU_addr_index:
23669 case DW_OP_GNU_const_index:
23670 {
23671 rtx ax1 = valx1->val_entry->addr.rtl;
23672 rtx ay1 = valy1->val_entry->addr.rtl;
23673 return rtx_equal_p (ax1, ay1);
23674 }
23675 case DW_OP_GNU_implicit_pointer:
23676 return valx1->val_class == dw_val_class_die_ref
23677 && valx1->val_class == valy1->val_class
23678 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23679 && valx2->v.val_int == valy2->v.val_int;
23680 case DW_OP_GNU_entry_value:
23681 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23682 case DW_OP_GNU_const_type:
23683 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23684 || valx2->val_class != valy2->val_class)
23685 return false;
23686 switch (valx2->val_class)
23687 {
23688 case dw_val_class_const:
23689 return valx2->v.val_int == valy2->v.val_int;
23690 case dw_val_class_vec:
23691 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23692 && valx2->v.val_vec.length == valy2->v.val_vec.length
23693 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23694 valx2->v.val_vec.elt_size
23695 * valx2->v.val_vec.length) == 0;
23696 case dw_val_class_const_double:
23697 return valx2->v.val_double.low == valy2->v.val_double.low
23698 && valx2->v.val_double.high == valy2->v.val_double.high;
23699 default:
23700 gcc_unreachable ();
23701 }
23702 case DW_OP_GNU_regval_type:
23703 case DW_OP_GNU_deref_type:
23704 return valx1->v.val_int == valy1->v.val_int
23705 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23706 case DW_OP_GNU_convert:
23707 case DW_OP_GNU_reinterpret:
23708 if (valx1->val_class != valy1->val_class)
23709 return false;
23710 if (valx1->val_class == dw_val_class_unsigned_const)
23711 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23712 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23713 case DW_OP_GNU_parameter_ref:
23714 return valx1->val_class == dw_val_class_die_ref
23715 && valx1->val_class == valy1->val_class
23716 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23717 default:
23718 /* Other codes have no operands. */
23719 return true;
23720 }
23721 }
23722
23723 /* Return true if DWARF location expressions X and Y are the same. */
23724
23725 static inline bool
23726 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23727 {
23728 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23729 if (x->dw_loc_opc != y->dw_loc_opc
23730 || x->dtprel != y->dtprel
23731 || !compare_loc_operands (x, y))
23732 break;
23733 return x == NULL && y == NULL;
23734 }
23735
23736 /* Hashtable helpers. */
23737
23738 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23739 {
23740 typedef dw_loc_list_struct value_type;
23741 typedef dw_loc_list_struct compare_type;
23742 static inline hashval_t hash (const value_type *);
23743 static inline bool equal (const value_type *, const compare_type *);
23744 };
23745
23746 /* Return precomputed hash of location list X. */
23747
23748 inline hashval_t
23749 loc_list_hasher::hash (const value_type *x)
23750 {
23751 return x->hash;
23752 }
23753
23754 /* Return true if location lists A and B are the same. */
23755
23756 inline bool
23757 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23758 {
23759 if (a == b)
23760 return 1;
23761 if (a->hash != b->hash)
23762 return 0;
23763 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23764 if (strcmp (a->begin, b->begin) != 0
23765 || strcmp (a->end, b->end) != 0
23766 || (a->section == NULL) != (b->section == NULL)
23767 || (a->section && strcmp (a->section, b->section) != 0)
23768 || !compare_locs (a->expr, b->expr))
23769 break;
23770 return a == NULL && b == NULL;
23771 }
23772
23773 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23774
23775
23776 /* Recursively optimize location lists referenced from DIE
23777 children and share them whenever possible. */
23778
23779 static void
23780 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23781 {
23782 dw_die_ref c;
23783 dw_attr_ref a;
23784 unsigned ix;
23785 dw_loc_list_struct **slot;
23786
23787 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23788 if (AT_class (a) == dw_val_class_loc_list)
23789 {
23790 dw_loc_list_ref list = AT_loc_list (a);
23791 /* TODO: perform some optimizations here, before hashing
23792 it and storing into the hash table. */
23793 hash_loc_list (list);
23794 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23795 if (*slot == NULL)
23796 *slot = list;
23797 else
23798 a->dw_attr_val.v.val_loc_list = *slot;
23799 }
23800
23801 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23802 }
23803
23804
23805 /* Recursively assign each location list a unique index into the debug_addr
23806 section. */
23807
23808 static void
23809 index_location_lists (dw_die_ref die)
23810 {
23811 dw_die_ref c;
23812 dw_attr_ref a;
23813 unsigned ix;
23814
23815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23816 if (AT_class (a) == dw_val_class_loc_list)
23817 {
23818 dw_loc_list_ref list = AT_loc_list (a);
23819 dw_loc_list_ref curr;
23820 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23821 {
23822 /* Don't index an entry that has already been indexed
23823 or won't be output. */
23824 if (curr->begin_entry != NULL
23825 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23826 continue;
23827
23828 curr->begin_entry
23829 = add_addr_table_entry (xstrdup (curr->begin),
23830 ate_kind_label);
23831 }
23832 }
23833
23834 FOR_EACH_CHILD (die, c, index_location_lists (c));
23835 }
23836
23837 /* Optimize location lists referenced from DIE
23838 children and share them whenever possible. */
23839
23840 static void
23841 optimize_location_lists (dw_die_ref die)
23842 {
23843 loc_list_hash_type htab;
23844 htab.create (500);
23845 optimize_location_lists_1 (die, htab);
23846 htab.dispose ();
23847 }
23848 \f
23849 /* Output stuff that dwarf requires at the end of every file,
23850 and generate the DWARF-2 debugging info. */
23851
23852 static void
23853 dwarf2out_finish (const char *filename)
23854 {
23855 limbo_die_node *node, *next_node;
23856 comdat_type_node *ctnode;
23857 hash_table <comdat_type_hasher> comdat_type_table;
23858 unsigned int i;
23859 dw_die_ref main_comp_unit_die;
23860
23861 /* PCH might result in DW_AT_producer string being restored from the
23862 header compilation, so always fill it with empty string initially
23863 and overwrite only here. */
23864 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23865 producer_string = gen_producer_string ();
23866 producer->dw_attr_val.v.val_str->refcount--;
23867 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23868
23869 gen_scheduled_generic_parms_dies ();
23870 gen_remaining_tmpl_value_param_die_attribute ();
23871
23872 /* Add the name for the main input file now. We delayed this from
23873 dwarf2out_init to avoid complications with PCH. */
23874 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23875 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23876 add_comp_dir_attribute (comp_unit_die ());
23877 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23878 {
23879 bool p = false;
23880 htab_traverse (file_table, file_table_relative_p, &p);
23881 if (p)
23882 add_comp_dir_attribute (comp_unit_die ());
23883 }
23884
23885 if (deferred_locations_list)
23886 for (i = 0; i < deferred_locations_list->length (); i++)
23887 {
23888 add_location_or_const_value_attribute (
23889 (*deferred_locations_list)[i].die,
23890 (*deferred_locations_list)[i].variable,
23891 false,
23892 DW_AT_location);
23893 }
23894
23895 /* Traverse the limbo die list, and add parent/child links. The only
23896 dies without parents that should be here are concrete instances of
23897 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23898 For concrete instances, we can get the parent die from the abstract
23899 instance. */
23900 for (node = limbo_die_list; node; node = next_node)
23901 {
23902 dw_die_ref die = node->die;
23903 next_node = node->next;
23904
23905 if (die->die_parent == NULL)
23906 {
23907 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23908
23909 if (origin && origin->die_parent)
23910 add_child_die (origin->die_parent, die);
23911 else if (is_cu_die (die))
23912 ;
23913 else if (seen_error ())
23914 /* It's OK to be confused by errors in the input. */
23915 add_child_die (comp_unit_die (), die);
23916 else
23917 {
23918 /* In certain situations, the lexical block containing a
23919 nested function can be optimized away, which results
23920 in the nested function die being orphaned. Likewise
23921 with the return type of that nested function. Force
23922 this to be a child of the containing function.
23923
23924 It may happen that even the containing function got fully
23925 inlined and optimized out. In that case we are lost and
23926 assign the empty child. This should not be big issue as
23927 the function is likely unreachable too. */
23928 gcc_assert (node->created_for);
23929
23930 if (DECL_P (node->created_for))
23931 origin = get_context_die (DECL_CONTEXT (node->created_for));
23932 else if (TYPE_P (node->created_for))
23933 origin = scope_die_for (node->created_for, comp_unit_die ());
23934 else
23935 origin = comp_unit_die ();
23936
23937 add_child_die (origin, die);
23938 }
23939 }
23940 }
23941
23942 limbo_die_list = NULL;
23943
23944 #if ENABLE_ASSERT_CHECKING
23945 {
23946 dw_die_ref die = comp_unit_die (), c;
23947 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23948 }
23949 #endif
23950 resolve_addr (comp_unit_die ());
23951 move_marked_base_types ();
23952
23953 for (node = deferred_asm_name; node; node = node->next)
23954 {
23955 tree decl = node->created_for;
23956 /* When generating LTO bytecode we can not generate new assembler
23957 names at this point and all important decls got theirs via
23958 free-lang-data. */
23959 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23960 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23961 {
23962 add_linkage_attr (node->die, decl);
23963 move_linkage_attr (node->die);
23964 }
23965 }
23966
23967 deferred_asm_name = NULL;
23968
23969 /* Walk through the list of incomplete types again, trying once more to
23970 emit full debugging info for them. */
23971 retry_incomplete_types ();
23972
23973 if (flag_eliminate_unused_debug_types)
23974 prune_unused_types ();
23975
23976 /* Generate separate COMDAT sections for type DIEs. */
23977 if (use_debug_types)
23978 {
23979 break_out_comdat_types (comp_unit_die ());
23980
23981 /* Each new type_unit DIE was added to the limbo die list when created.
23982 Since these have all been added to comdat_type_list, clear the
23983 limbo die list. */
23984 limbo_die_list = NULL;
23985
23986 /* For each new comdat type unit, copy declarations for incomplete
23987 types to make the new unit self-contained (i.e., no direct
23988 references to the main compile unit). */
23989 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23990 copy_decls_for_unworthy_types (ctnode->root_die);
23991 copy_decls_for_unworthy_types (comp_unit_die ());
23992
23993 /* In the process of copying declarations from one unit to another,
23994 we may have left some declarations behind that are no longer
23995 referenced. Prune them. */
23996 prune_unused_types ();
23997 }
23998
23999 /* Generate separate CUs for each of the include files we've seen.
24000 They will go into limbo_die_list. */
24001 if (flag_eliminate_dwarf2_dups)
24002 break_out_includes (comp_unit_die ());
24003
24004 /* Traverse the DIE's and add add sibling attributes to those DIE's
24005 that have children. */
24006 add_sibling_attributes (comp_unit_die ());
24007 for (node = limbo_die_list; node; node = node->next)
24008 add_sibling_attributes (node->die);
24009 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24010 add_sibling_attributes (ctnode->root_die);
24011
24012 /* When splitting DWARF info, we put some attributes in the
24013 skeleton compile_unit DIE that remains in the .o, while
24014 most attributes go in the DWO compile_unit_die. */
24015 if (dwarf_split_debug_info)
24016 main_comp_unit_die = gen_compile_unit_die (NULL);
24017 else
24018 main_comp_unit_die = comp_unit_die ();
24019
24020 /* Output a terminator label for the .text section. */
24021 switch_to_section (text_section);
24022 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24023 if (cold_text_section)
24024 {
24025 switch_to_section (cold_text_section);
24026 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24027 }
24028
24029 /* We can only use the low/high_pc attributes if all of the code was
24030 in .text. */
24031 if (!have_multiple_function_sections
24032 || (dwarf_version < 3 && dwarf_strict))
24033 {
24034 /* Don't add if the CU has no associated code. */
24035 if (text_section_used)
24036 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24037 text_end_label, true);
24038 }
24039 else
24040 {
24041 unsigned fde_idx;
24042 dw_fde_ref fde;
24043 bool range_list_added = false;
24044
24045 if (text_section_used)
24046 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24047 text_end_label, &range_list_added, true);
24048 if (cold_text_section_used)
24049 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24050 cold_end_label, &range_list_added, true);
24051
24052 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24053 {
24054 if (DECL_IGNORED_P (fde->decl))
24055 continue;
24056 if (!fde->in_std_section)
24057 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24058 fde->dw_fde_end, &range_list_added,
24059 true);
24060 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24061 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24062 fde->dw_fde_second_end, &range_list_added,
24063 true);
24064 }
24065
24066 if (range_list_added)
24067 {
24068 /* We need to give .debug_loc and .debug_ranges an appropriate
24069 "base address". Use zero so that these addresses become
24070 absolute. Historically, we've emitted the unexpected
24071 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24072 Emit both to give time for other tools to adapt. */
24073 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24074 if (! dwarf_strict && dwarf_version < 4)
24075 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24076
24077 add_ranges (NULL);
24078 }
24079 }
24080
24081 if (debug_info_level >= DINFO_LEVEL_TERSE)
24082 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24083 debug_line_section_label);
24084
24085 if (have_macinfo)
24086 add_AT_macptr (comp_unit_die (),
24087 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24088 macinfo_section_label);
24089
24090 if (dwarf_split_debug_info && addr_index_table != NULL)
24091 {
24092 /* optimize_location_lists calculates the size of the lists,
24093 so index them first, and assign indices to the entries.
24094 Although optimize_location_lists will remove entries from
24095 the table, it only does so for duplicates, and therefore
24096 only reduces ref_counts to 1. */
24097 unsigned int index = 0;
24098 index_location_lists (comp_unit_die ());
24099 htab_traverse_noresize (addr_index_table,
24100 index_addr_table_entry, &index);
24101 }
24102 if (have_location_lists)
24103 optimize_location_lists (comp_unit_die ());
24104
24105 save_macinfo_strings ();
24106
24107 if (dwarf_split_debug_info)
24108 {
24109 unsigned int index = 0;
24110
24111 /* Add attributes common to skeleton compile_units and
24112 type_units. Because these attributes include strings, it
24113 must be done before freezing the string table. Top-level
24114 skeleton die attrs are added when the skeleton type unit is
24115 created, so ensure it is created by this point. */
24116 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24117 (void) get_skeleton_type_unit ();
24118 htab_traverse_noresize (debug_str_hash, index_string, &index);
24119 }
24120
24121 /* Output all of the compilation units. We put the main one last so that
24122 the offsets are available to output_pubnames. */
24123 for (node = limbo_die_list; node; node = node->next)
24124 output_comp_unit (node->die, 0);
24125
24126 comdat_type_table.create (100);
24127 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24128 {
24129 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24130
24131 /* Don't output duplicate types. */
24132 if (*slot != HTAB_EMPTY_ENTRY)
24133 continue;
24134
24135 /* Add a pointer to the line table for the main compilation unit
24136 so that the debugger can make sense of DW_AT_decl_file
24137 attributes. */
24138 if (debug_info_level >= DINFO_LEVEL_TERSE)
24139 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24140 (!dwarf_split_debug_info
24141 ? debug_line_section_label
24142 : debug_skeleton_line_section_label));
24143
24144 output_comdat_type_unit (ctnode);
24145 *slot = ctnode;
24146 }
24147 comdat_type_table.dispose ();
24148
24149 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24150 both the main_cu and all skeleton TUs. Making this call unconditional
24151 would end up either adding a second copy of the AT_pubnames attribute, or
24152 requiring a special case in add_top_level_skeleton_die_attrs. */
24153 if (!dwarf_split_debug_info)
24154 add_AT_pubnames (comp_unit_die ());
24155
24156 if (dwarf_split_debug_info)
24157 {
24158 int mark;
24159 unsigned char checksum[16];
24160 struct md5_ctx ctx;
24161
24162 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24163 md5_init_ctx (&ctx);
24164 mark = 0;
24165 die_checksum (comp_unit_die (), &ctx, &mark);
24166 unmark_all_dies (comp_unit_die ());
24167 md5_finish_ctx (&ctx, checksum);
24168
24169 /* Use the first 8 bytes of the checksum as the dwo_id,
24170 and add it to both comp-unit DIEs. */
24171 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24172 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24173
24174 /* Add the base offset of the ranges table to the skeleton
24175 comp-unit DIE. */
24176 if (ranges_table_in_use)
24177 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24178 ranges_section_label);
24179
24180 switch_to_section (debug_addr_section);
24181 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24182 output_addr_table ();
24183 }
24184
24185 /* Output the main compilation unit if non-empty or if .debug_macinfo
24186 or .debug_macro will be emitted. */
24187 output_comp_unit (comp_unit_die (), have_macinfo);
24188
24189 if (dwarf_split_debug_info && info_section_emitted)
24190 output_skeleton_debug_sections (main_comp_unit_die);
24191
24192 /* Output the abbreviation table. */
24193 if (abbrev_die_table_in_use != 1)
24194 {
24195 switch_to_section (debug_abbrev_section);
24196 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24197 output_abbrev_section ();
24198 }
24199
24200 /* Output location list section if necessary. */
24201 if (have_location_lists)
24202 {
24203 /* Output the location lists info. */
24204 switch_to_section (debug_loc_section);
24205 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24206 output_location_lists (comp_unit_die ());
24207 }
24208
24209 output_pubtables ();
24210
24211 /* Output the address range information if a CU (.debug_info section)
24212 was emitted. We output an empty table even if we had no functions
24213 to put in it. This because the consumer has no way to tell the
24214 difference between an empty table that we omitted and failure to
24215 generate a table that would have contained data. */
24216 if (info_section_emitted)
24217 {
24218 unsigned long aranges_length = size_of_aranges ();
24219
24220 switch_to_section (debug_aranges_section);
24221 output_aranges (aranges_length);
24222 }
24223
24224 /* Output ranges section if necessary. */
24225 if (ranges_table_in_use)
24226 {
24227 switch_to_section (debug_ranges_section);
24228 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24229 output_ranges ();
24230 }
24231
24232 /* Have to end the macro section. */
24233 if (have_macinfo)
24234 {
24235 switch_to_section (debug_macinfo_section);
24236 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24237 output_macinfo ();
24238 dw2_asm_output_data (1, 0, "End compilation unit");
24239 }
24240
24241 /* Output the source line correspondence table. We must do this
24242 even if there is no line information. Otherwise, on an empty
24243 translation unit, we will generate a present, but empty,
24244 .debug_info section. IRIX 6.5 `nm' will then complain when
24245 examining the file. This is done late so that any filenames
24246 used by the debug_info section are marked as 'used'. */
24247 switch_to_section (debug_line_section);
24248 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24249 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24250 output_line_info (false);
24251
24252 if (dwarf_split_debug_info && info_section_emitted)
24253 {
24254 switch_to_section (debug_skeleton_line_section);
24255 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24256 output_line_info (true);
24257 }
24258
24259 /* If we emitted any indirect strings, output the string table too. */
24260 if (debug_str_hash || skeleton_debug_str_hash)
24261 output_indirect_strings ();
24262 }
24263
24264 #include "gt-dwarf2out.h"