re PR ada/35998 (debug info invalid x86_64 DW_AT_byte_size 0xffffffff)
[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 "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hash-table.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "ira.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99 static rtx cached_next_real_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105 which are output in the debug info should end with a separator. */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108 from generating indirect strings in DWARF2 debug information, for instance
109 if your target is stuck with an old version of GDB that is unable to
110 process them properly or uses VMS Debug. */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically. They've been
118 totally replaced in this file; make sure it stays that way. */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 /* The size of the target's pointer type. */
126 #ifndef PTR_SIZE
127 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
128 #endif
129
130 /* Array of RTXes referenced by the debugging information, which therefore
131 must be kept around forever. */
132 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
133
134 /* A pointer to the base of a list of incomplete types which might be
135 completed at some later time. incomplete_types_list needs to be a
136 vec<tree, va_gc> *because we want to tell the garbage collector about
137 it. */
138 static GTY(()) vec<tree, va_gc> *incomplete_types;
139
140 /* A pointer to the base of a table of references to declaration
141 scopes. This table is a display which tracks the nesting
142 of declaration scopes at the current scope and containing
143 scopes. This table is used to find the proper place to
144 define type declaration DIE's. */
145 static GTY(()) vec<tree, va_gc> *decl_scope_table;
146
147 /* Pointers to various DWARF2 sections. */
148 static GTY(()) section *debug_info_section;
149 static GTY(()) section *debug_skeleton_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_skeleton_abbrev_section;
152 static GTY(()) section *debug_aranges_section;
153 static GTY(()) section *debug_addr_section;
154 static GTY(()) section *debug_macinfo_section;
155 static GTY(()) section *debug_line_section;
156 static GTY(()) section *debug_skeleton_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_str_dwo_section;
162 static GTY(()) section *debug_str_offsets_section;
163 static GTY(()) section *debug_ranges_section;
164 static GTY(()) section *debug_frame_section;
165
166 /* Maximum size (in bytes) of an artificially generated label. */
167 #define MAX_ARTIFICIAL_LABEL_BYTES 30
168
169 /* According to the (draft) DWARF 3 specification, the initial length
170 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
171 bytes are 0xffffffff, followed by the length stored in the next 8
172 bytes.
173
174 However, the SGI/MIPS ABI uses an initial length which is equal to
175 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
176
177 #ifndef DWARF_INITIAL_LENGTH_SIZE
178 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
179 #endif
180
181 /* Round SIZE up to the nearest BOUNDARY. */
182 #define DWARF_ROUND(SIZE,BOUNDARY) \
183 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
184
185 /* CIE identifier. */
186 #if HOST_BITS_PER_WIDE_INT >= 64
187 #define DWARF_CIE_ID \
188 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
189 #else
190 #define DWARF_CIE_ID DW_CIE_ID
191 #endif
192
193
194 /* A vector for a table that contains frame description
195 information for each routine. */
196 #define NOT_INDEXED (-1U)
197 #define NO_INDEX_ASSIGNED (-2U)
198
199 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
200
201 struct GTY(()) indirect_string_node {
202 const char *str;
203 unsigned int refcount;
204 enum dwarf_form form;
205 char *label;
206 unsigned int index;
207 };
208
209 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
210
211 /* With split_debug_info, both the comp_dir and dwo_name go in the
212 main object file, rather than the dwo, similar to the force_direct
213 parameter elsewhere but with additional complications:
214
215 1) The string is needed in both the main object file and the dwo.
216 That is, the comp_dir and dwo_name will appear in both places.
217
218 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
219 DW_FORM_GNU_str_index.
220
221 3) GCC chooses the form to use late, depending on the size and
222 reference count.
223
224 Rather than forcing the all debug string handling functions and
225 callers to deal with these complications, simply use a separate,
226 special-cased string table for any attribute that should go in the
227 main object file. This limits the complexity to just the places
228 that need it. */
229
230 static GTY ((param_is (struct indirect_string_node)))
231 htab_t skeleton_debug_str_hash;
232
233 static GTY(()) int dw2_string_counter;
234
235 /* True if the compilation unit places functions in more than one section. */
236 static GTY(()) bool have_multiple_function_sections = false;
237
238 /* Whether the default text and cold text sections have been used at all. */
239
240 static GTY(()) bool text_section_used = false;
241 static GTY(()) bool cold_text_section_used = false;
242
243 /* The default cold text section. */
244 static GTY(()) section *cold_text_section;
245
246 /* Forward declarations for functions defined in this file. */
247
248 static char *stripattributes (const char *);
249 static void output_call_frame_info (int);
250 static void dwarf2out_note_section_used (void);
251
252 /* Personality decl of current unit. Used only when assembler does not support
253 personality CFI. */
254 static GTY(()) rtx current_unit_personality;
255
256 /* Data and reference forms for relocatable data. */
257 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
258 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
259
260 #ifndef DEBUG_FRAME_SECTION
261 #define DEBUG_FRAME_SECTION ".debug_frame"
262 #endif
263
264 #ifndef FUNC_BEGIN_LABEL
265 #define FUNC_BEGIN_LABEL "LFB"
266 #endif
267
268 #ifndef FUNC_END_LABEL
269 #define FUNC_END_LABEL "LFE"
270 #endif
271
272 #ifndef PROLOGUE_END_LABEL
273 #define PROLOGUE_END_LABEL "LPE"
274 #endif
275
276 #ifndef EPILOGUE_BEGIN_LABEL
277 #define EPILOGUE_BEGIN_LABEL "LEB"
278 #endif
279
280 #ifndef FRAME_BEGIN_LABEL
281 #define FRAME_BEGIN_LABEL "Lframe"
282 #endif
283 #define CIE_AFTER_SIZE_LABEL "LSCIE"
284 #define CIE_END_LABEL "LECIE"
285 #define FDE_LABEL "LSFDE"
286 #define FDE_AFTER_SIZE_LABEL "LASFDE"
287 #define FDE_END_LABEL "LEFDE"
288 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
289 #define LINE_NUMBER_END_LABEL "LELT"
290 #define LN_PROLOG_AS_LABEL "LASLTP"
291 #define LN_PROLOG_END_LABEL "LELTP"
292 #define DIE_LABEL_PREFIX "DW"
293 \f
294 /* Match the base name of a file to the base name of a compilation unit. */
295
296 static int
297 matches_main_base (const char *path)
298 {
299 /* Cache the last query. */
300 static const char *last_path = NULL;
301 static int last_match = 0;
302 if (path != last_path)
303 {
304 const char *base;
305 int length = base_of_path (path, &base);
306 last_path = path;
307 last_match = (length == main_input_baselength
308 && memcmp (base, main_input_basename, length) == 0);
309 }
310 return last_match;
311 }
312
313 #ifdef DEBUG_DEBUG_STRUCT
314
315 static int
316 dump_struct_debug (tree type, enum debug_info_usage usage,
317 enum debug_struct_file criterion, int generic,
318 int matches, int result)
319 {
320 /* Find the type name. */
321 tree type_decl = TYPE_STUB_DECL (type);
322 tree t = type_decl;
323 const char *name = 0;
324 if (TREE_CODE (t) == TYPE_DECL)
325 t = DECL_NAME (t);
326 if (t)
327 name = IDENTIFIER_POINTER (t);
328
329 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
330 criterion,
331 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
332 matches ? "bas" : "hdr",
333 generic ? "gen" : "ord",
334 usage == DINFO_USAGE_DFN ? ";" :
335 usage == DINFO_USAGE_DIR_USE ? "." : "*",
336 result,
337 (void*) type_decl, name);
338 return result;
339 }
340 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
341 dump_struct_debug (type, usage, criterion, generic, matches, result)
342
343 #else
344
345 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
346 (result)
347
348 #endif
349
350 static bool
351 should_emit_struct_debug (tree type, enum debug_info_usage usage)
352 {
353 enum debug_struct_file criterion;
354 tree type_decl;
355 bool generic = lang_hooks.types.generic_p (type);
356
357 if (generic)
358 criterion = debug_struct_generic[usage];
359 else
360 criterion = debug_struct_ordinary[usage];
361
362 if (criterion == DINFO_STRUCT_FILE_NONE)
363 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
364 if (criterion == DINFO_STRUCT_FILE_ANY)
365 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
366
367 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
368
369 if (type_decl != NULL)
370 {
371 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
372 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
373
374 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
375 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
376 }
377
378 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
379 }
380 \f
381 /* Return a pointer to a copy of the section string name S with all
382 attributes stripped off, and an asterisk prepended (for assemble_name). */
383
384 static inline char *
385 stripattributes (const char *s)
386 {
387 char *stripped = XNEWVEC (char, strlen (s) + 2);
388 char *p = stripped;
389
390 *p++ = '*';
391
392 while (*s && *s != ',')
393 *p++ = *s++;
394
395 *p = '\0';
396 return stripped;
397 }
398
399 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
400 switch to the data section instead, and write out a synthetic start label
401 for collect2 the first time around. */
402
403 static void
404 switch_to_eh_frame_section (bool back)
405 {
406 tree label;
407
408 #ifdef EH_FRAME_SECTION_NAME
409 if (eh_frame_section == 0)
410 {
411 int flags;
412
413 if (EH_TABLES_CAN_BE_READ_ONLY)
414 {
415 int fde_encoding;
416 int per_encoding;
417 int lsda_encoding;
418
419 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
420 /*global=*/0);
421 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
422 /*global=*/1);
423 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
424 /*global=*/0);
425 flags = ((! flag_pic
426 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
427 && (fde_encoding & 0x70) != DW_EH_PE_aligned
428 && (per_encoding & 0x70) != DW_EH_PE_absptr
429 && (per_encoding & 0x70) != DW_EH_PE_aligned
430 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
431 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
432 ? 0 : SECTION_WRITE);
433 }
434 else
435 flags = SECTION_WRITE;
436 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
437 }
438 #endif /* EH_FRAME_SECTION_NAME */
439
440 if (eh_frame_section)
441 switch_to_section (eh_frame_section);
442 else
443 {
444 /* We have no special eh_frame section. Put the information in
445 the data section and emit special labels to guide collect2. */
446 switch_to_section (data_section);
447
448 if (!back)
449 {
450 label = get_file_function_name ("F");
451 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
452 targetm.asm_out.globalize_label (asm_out_file,
453 IDENTIFIER_POINTER (label));
454 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
455 }
456 }
457 }
458
459 /* Switch [BACK] to the eh or debug frame table section, depending on
460 FOR_EH. */
461
462 static void
463 switch_to_frame_table_section (int for_eh, bool back)
464 {
465 if (for_eh)
466 switch_to_eh_frame_section (back);
467 else
468 {
469 if (!debug_frame_section)
470 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
471 SECTION_DEBUG, NULL);
472 switch_to_section (debug_frame_section);
473 }
474 }
475
476 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
477
478 enum dw_cfi_oprnd_type
479 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
480 {
481 switch (cfi)
482 {
483 case DW_CFA_nop:
484 case DW_CFA_GNU_window_save:
485 case DW_CFA_remember_state:
486 case DW_CFA_restore_state:
487 return dw_cfi_oprnd_unused;
488
489 case DW_CFA_set_loc:
490 case DW_CFA_advance_loc1:
491 case DW_CFA_advance_loc2:
492 case DW_CFA_advance_loc4:
493 case DW_CFA_MIPS_advance_loc8:
494 return dw_cfi_oprnd_addr;
495
496 case DW_CFA_offset:
497 case DW_CFA_offset_extended:
498 case DW_CFA_def_cfa:
499 case DW_CFA_offset_extended_sf:
500 case DW_CFA_def_cfa_sf:
501 case DW_CFA_restore:
502 case DW_CFA_restore_extended:
503 case DW_CFA_undefined:
504 case DW_CFA_same_value:
505 case DW_CFA_def_cfa_register:
506 case DW_CFA_register:
507 case DW_CFA_expression:
508 return dw_cfi_oprnd_reg_num;
509
510 case DW_CFA_def_cfa_offset:
511 case DW_CFA_GNU_args_size:
512 case DW_CFA_def_cfa_offset_sf:
513 return dw_cfi_oprnd_offset;
514
515 case DW_CFA_def_cfa_expression:
516 return dw_cfi_oprnd_loc;
517
518 default:
519 gcc_unreachable ();
520 }
521 }
522
523 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
524
525 enum dw_cfi_oprnd_type
526 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
527 {
528 switch (cfi)
529 {
530 case DW_CFA_def_cfa:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_offset:
533 case DW_CFA_offset_extended_sf:
534 case DW_CFA_offset_extended:
535 return dw_cfi_oprnd_offset;
536
537 case DW_CFA_register:
538 return dw_cfi_oprnd_reg_num;
539
540 case DW_CFA_expression:
541 return dw_cfi_oprnd_loc;
542
543 default:
544 return dw_cfi_oprnd_unused;
545 }
546 }
547
548 /* Output one FDE. */
549
550 static void
551 output_fde (dw_fde_ref fde, bool for_eh, bool second,
552 char *section_start_label, int fde_encoding, char *augmentation,
553 bool any_lsda_needed, int lsda_encoding)
554 {
555 const char *begin, *end;
556 static unsigned int j;
557 char l1[20], l2[20];
558
559 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
560 /* empty */ 0);
561 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
562 for_eh + j);
563 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
564 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
565 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
566 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
567 " indicating 64-bit DWARF extension");
568 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
569 "FDE Length");
570 ASM_OUTPUT_LABEL (asm_out_file, l1);
571
572 if (for_eh)
573 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
574 else
575 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
576 debug_frame_section, "FDE CIE offset");
577
578 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
579 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
580
581 if (for_eh)
582 {
583 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
584 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
585 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
586 "FDE initial location");
587 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
588 end, begin, "FDE address range");
589 }
590 else
591 {
592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
593 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
594 }
595
596 if (augmentation[0])
597 {
598 if (any_lsda_needed)
599 {
600 int size = size_of_encoded_value (lsda_encoding);
601
602 if (lsda_encoding == DW_EH_PE_aligned)
603 {
604 int offset = ( 4 /* Length */
605 + 4 /* CIE offset */
606 + 2 * size_of_encoded_value (fde_encoding)
607 + 1 /* Augmentation size */ );
608 int pad = -offset & (PTR_SIZE - 1);
609
610 size += pad;
611 gcc_assert (size_of_uleb128 (size) == 1);
612 }
613
614 dw2_asm_output_data_uleb128 (size, "Augmentation size");
615
616 if (fde->uses_eh_lsda)
617 {
618 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
619 fde->funcdef_number);
620 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
621 gen_rtx_SYMBOL_REF (Pmode, l1),
622 false,
623 "Language Specific Data Area");
624 }
625 else
626 {
627 if (lsda_encoding == DW_EH_PE_aligned)
628 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
629 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
630 "Language Specific Data Area (none)");
631 }
632 }
633 else
634 dw2_asm_output_data_uleb128 (0, "Augmentation size");
635 }
636
637 /* Loop through the Call Frame Instructions associated with this FDE. */
638 fde->dw_fde_current_label = begin;
639 {
640 size_t from, until, i;
641
642 from = 0;
643 until = vec_safe_length (fde->dw_fde_cfi);
644
645 if (fde->dw_fde_second_begin == NULL)
646 ;
647 else if (!second)
648 until = fde->dw_fde_switch_cfi_index;
649 else
650 from = fde->dw_fde_switch_cfi_index;
651
652 for (i = from; i < until; i++)
653 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
654 }
655
656 /* If we are to emit a ref/link from function bodies to their frame tables,
657 do it now. This is typically performed to make sure that tables
658 associated with functions are dragged with them and not discarded in
659 garbage collecting links. We need to do this on a per function basis to
660 cope with -ffunction-sections. */
661
662 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
663 /* Switch to the function section, emit the ref to the tables, and
664 switch *back* into the table section. */
665 switch_to_section (function_section (fde->decl));
666 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
667 switch_to_frame_table_section (for_eh, true);
668 #endif
669
670 /* Pad the FDE out to an address sized boundary. */
671 ASM_OUTPUT_ALIGN (asm_out_file,
672 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
673 ASM_OUTPUT_LABEL (asm_out_file, l2);
674
675 j += 2;
676 }
677
678 /* Return true if frame description entry FDE is needed for EH. */
679
680 static bool
681 fde_needed_for_eh_p (dw_fde_ref fde)
682 {
683 if (flag_asynchronous_unwind_tables)
684 return true;
685
686 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
687 return true;
688
689 if (fde->uses_eh_lsda)
690 return true;
691
692 /* If exceptions are enabled, we have collected nothrow info. */
693 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
694 return false;
695
696 return true;
697 }
698
699 /* Output the call frame information used to record information
700 that relates to calculating the frame pointer, and records the
701 location of saved registers. */
702
703 static void
704 output_call_frame_info (int for_eh)
705 {
706 unsigned int i;
707 dw_fde_ref fde;
708 dw_cfi_ref cfi;
709 char l1[20], l2[20], section_start_label[20];
710 bool any_lsda_needed = false;
711 char augmentation[6];
712 int augmentation_size;
713 int fde_encoding = DW_EH_PE_absptr;
714 int per_encoding = DW_EH_PE_absptr;
715 int lsda_encoding = DW_EH_PE_absptr;
716 int return_reg;
717 rtx personality = NULL;
718 int dw_cie_version;
719
720 /* Don't emit a CIE if there won't be any FDEs. */
721 if (!fde_vec)
722 return;
723
724 /* Nothing to do if the assembler's doing it all. */
725 if (dwarf2out_do_cfi_asm ())
726 return;
727
728 /* If we don't have any functions we'll want to unwind out of, don't emit
729 any EH unwind information. If we make FDEs linkonce, we may have to
730 emit an empty label for an FDE that wouldn't otherwise be emitted. We
731 want to avoid having an FDE kept around when the function it refers to
732 is discarded. Example where this matters: a primary function template
733 in C++ requires EH information, an explicit specialization doesn't. */
734 if (for_eh)
735 {
736 bool any_eh_needed = false;
737
738 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
739 {
740 if (fde->uses_eh_lsda)
741 any_eh_needed = any_lsda_needed = true;
742 else if (fde_needed_for_eh_p (fde))
743 any_eh_needed = true;
744 else if (TARGET_USES_WEAK_UNWIND_INFO)
745 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
746 }
747
748 if (!any_eh_needed)
749 return;
750 }
751
752 /* We're going to be generating comments, so turn on app. */
753 if (flag_debug_asm)
754 app_enable ();
755
756 /* Switch to the proper frame section, first time. */
757 switch_to_frame_table_section (for_eh, false);
758
759 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
760 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
761
762 /* Output the CIE. */
763 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
764 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
765 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
766 dw2_asm_output_data (4, 0xffffffff,
767 "Initial length escape value indicating 64-bit DWARF extension");
768 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
769 "Length of Common Information Entry");
770 ASM_OUTPUT_LABEL (asm_out_file, l1);
771
772 /* Now that the CIE pointer is PC-relative for EH,
773 use 0 to identify the CIE. */
774 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
775 (for_eh ? 0 : DWARF_CIE_ID),
776 "CIE Identifier Tag");
777
778 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
779 use CIE version 1, unless that would produce incorrect results
780 due to overflowing the return register column. */
781 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
782 dw_cie_version = 1;
783 if (return_reg >= 256 || dwarf_version > 2)
784 dw_cie_version = 3;
785 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
786
787 augmentation[0] = 0;
788 augmentation_size = 0;
789
790 personality = current_unit_personality;
791 if (for_eh)
792 {
793 char *p;
794
795 /* Augmentation:
796 z Indicates that a uleb128 is present to size the
797 augmentation section.
798 L Indicates the encoding (and thus presence) of
799 an LSDA pointer in the FDE augmentation.
800 R Indicates a non-default pointer encoding for
801 FDE code pointers.
802 P Indicates the presence of an encoding + language
803 personality routine in the CIE augmentation. */
804
805 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
806 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
807 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
808
809 p = augmentation + 1;
810 if (personality)
811 {
812 *p++ = 'P';
813 augmentation_size += 1 + size_of_encoded_value (per_encoding);
814 assemble_external_libcall (personality);
815 }
816 if (any_lsda_needed)
817 {
818 *p++ = 'L';
819 augmentation_size += 1;
820 }
821 if (fde_encoding != DW_EH_PE_absptr)
822 {
823 *p++ = 'R';
824 augmentation_size += 1;
825 }
826 if (p > augmentation + 1)
827 {
828 augmentation[0] = 'z';
829 *p = '\0';
830 }
831
832 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
833 if (personality && per_encoding == DW_EH_PE_aligned)
834 {
835 int offset = ( 4 /* Length */
836 + 4 /* CIE Id */
837 + 1 /* CIE version */
838 + strlen (augmentation) + 1 /* Augmentation */
839 + size_of_uleb128 (1) /* Code alignment */
840 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
841 + 1 /* RA column */
842 + 1 /* Augmentation size */
843 + 1 /* Personality encoding */ );
844 int pad = -offset & (PTR_SIZE - 1);
845
846 augmentation_size += pad;
847
848 /* Augmentations should be small, so there's scarce need to
849 iterate for a solution. Die if we exceed one uleb128 byte. */
850 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
851 }
852 }
853
854 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
855 if (dw_cie_version >= 4)
856 {
857 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
858 dw2_asm_output_data (1, 0, "CIE Segment Size");
859 }
860 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
861 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
862 "CIE Data Alignment Factor");
863
864 if (dw_cie_version == 1)
865 dw2_asm_output_data (1, return_reg, "CIE RA Column");
866 else
867 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
868
869 if (augmentation[0])
870 {
871 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
872 if (personality)
873 {
874 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
875 eh_data_format_name (per_encoding));
876 dw2_asm_output_encoded_addr_rtx (per_encoding,
877 personality,
878 true, NULL);
879 }
880
881 if (any_lsda_needed)
882 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
883 eh_data_format_name (lsda_encoding));
884
885 if (fde_encoding != DW_EH_PE_absptr)
886 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
887 eh_data_format_name (fde_encoding));
888 }
889
890 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
891 output_cfi (cfi, NULL, for_eh);
892
893 /* Pad the CIE out to an address sized boundary. */
894 ASM_OUTPUT_ALIGN (asm_out_file,
895 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
896 ASM_OUTPUT_LABEL (asm_out_file, l2);
897
898 /* Loop through all of the FDE's. */
899 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
900 {
901 unsigned int k;
902
903 /* Don't emit EH unwind info for leaf functions that don't need it. */
904 if (for_eh && !fde_needed_for_eh_p (fde))
905 continue;
906
907 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
908 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
909 augmentation, any_lsda_needed, lsda_encoding);
910 }
911
912 if (for_eh && targetm.terminate_dw2_eh_frame_info)
913 dw2_asm_output_data (4, 0, "End of Table");
914
915 /* Turn off app to make assembly quicker. */
916 if (flag_debug_asm)
917 app_disable ();
918 }
919
920 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
921
922 static void
923 dwarf2out_do_cfi_startproc (bool second)
924 {
925 int enc;
926 rtx ref;
927 rtx personality = get_personality_function (current_function_decl);
928
929 fprintf (asm_out_file, "\t.cfi_startproc\n");
930
931 if (personality)
932 {
933 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
934 ref = personality;
935
936 /* ??? The GAS support isn't entirely consistent. We have to
937 handle indirect support ourselves, but PC-relative is done
938 in the assembler. Further, the assembler can't handle any
939 of the weirder relocation types. */
940 if (enc & DW_EH_PE_indirect)
941 ref = dw2_force_const_mem (ref, true);
942
943 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
944 output_addr_const (asm_out_file, ref);
945 fputc ('\n', asm_out_file);
946 }
947
948 if (crtl->uses_eh_lsda)
949 {
950 char lab[20];
951
952 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
953 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
954 current_function_funcdef_no);
955 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
956 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
957
958 if (enc & DW_EH_PE_indirect)
959 ref = dw2_force_const_mem (ref, true);
960
961 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
962 output_addr_const (asm_out_file, ref);
963 fputc ('\n', asm_out_file);
964 }
965 }
966
967 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
968 this allocation may be done before pass_final. */
969
970 dw_fde_ref
971 dwarf2out_alloc_current_fde (void)
972 {
973 dw_fde_ref fde;
974
975 fde = ggc_alloc_cleared_dw_fde_node ();
976 fde->decl = current_function_decl;
977 fde->funcdef_number = current_function_funcdef_no;
978 fde->fde_index = vec_safe_length (fde_vec);
979 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
980 fde->uses_eh_lsda = crtl->uses_eh_lsda;
981 fde->nothrow = crtl->nothrow;
982 fde->drap_reg = INVALID_REGNUM;
983 fde->vdrap_reg = INVALID_REGNUM;
984
985 /* Record the FDE associated with this function. */
986 cfun->fde = fde;
987 vec_safe_push (fde_vec, fde);
988
989 return fde;
990 }
991
992 /* Output a marker (i.e. a label) for the beginning of a function, before
993 the prologue. */
994
995 void
996 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
997 const char *file ATTRIBUTE_UNUSED)
998 {
999 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1000 char * dup_label;
1001 dw_fde_ref fde;
1002 section *fnsec;
1003 bool do_frame;
1004
1005 current_function_func_begin_label = NULL;
1006
1007 do_frame = dwarf2out_do_frame ();
1008
1009 /* ??? current_function_func_begin_label is also used by except.c for
1010 call-site information. We must emit this label if it might be used. */
1011 if (!do_frame
1012 && (!flag_exceptions
1013 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1014 return;
1015
1016 fnsec = function_section (current_function_decl);
1017 switch_to_section (fnsec);
1018 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1019 current_function_funcdef_no);
1020 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1021 current_function_funcdef_no);
1022 dup_label = xstrdup (label);
1023 current_function_func_begin_label = dup_label;
1024
1025 /* We can elide the fde allocation if we're not emitting debug info. */
1026 if (!do_frame)
1027 return;
1028
1029 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1030 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1031 would include pass_dwarf2_frame. If we've not created the FDE yet,
1032 do so now. */
1033 fde = cfun->fde;
1034 if (fde == NULL)
1035 fde = dwarf2out_alloc_current_fde ();
1036
1037 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1038 fde->dw_fde_begin = dup_label;
1039 fde->dw_fde_current_label = dup_label;
1040 fde->in_std_section = (fnsec == text_section
1041 || (cold_text_section && fnsec == cold_text_section));
1042
1043 /* We only want to output line number information for the genuine dwarf2
1044 prologue case, not the eh frame case. */
1045 #ifdef DWARF2_DEBUGGING_INFO
1046 if (file)
1047 dwarf2out_source_line (line, file, 0, true);
1048 #endif
1049
1050 if (dwarf2out_do_cfi_asm ())
1051 dwarf2out_do_cfi_startproc (false);
1052 else
1053 {
1054 rtx personality = get_personality_function (current_function_decl);
1055 if (!current_unit_personality)
1056 current_unit_personality = personality;
1057
1058 /* We cannot keep a current personality per function as without CFI
1059 asm, at the point where we emit the CFI data, there is no current
1060 function anymore. */
1061 if (personality && current_unit_personality != personality)
1062 sorry ("multiple EH personalities are supported only with assemblers "
1063 "supporting .cfi_personality directive");
1064 }
1065 }
1066
1067 /* Output a marker (i.e. a label) for the end of the generated code
1068 for a function prologue. This gets called *after* the prologue code has
1069 been generated. */
1070
1071 void
1072 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1073 const char *file ATTRIBUTE_UNUSED)
1074 {
1075 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1076
1077 /* Output a label to mark the endpoint of the code generated for this
1078 function. */
1079 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1080 current_function_funcdef_no);
1081 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1082 current_function_funcdef_no);
1083 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1084 }
1085
1086 /* Output a marker (i.e. a label) for the beginning of the generated code
1087 for a function epilogue. This gets called *before* the prologue code has
1088 been generated. */
1089
1090 void
1091 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1092 const char *file ATTRIBUTE_UNUSED)
1093 {
1094 dw_fde_ref fde = cfun->fde;
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1096
1097 if (fde->dw_fde_vms_begin_epilogue)
1098 return;
1099
1100 /* Output a label to mark the endpoint of the code generated for this
1101 function. */
1102 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1103 current_function_funcdef_no);
1104 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1105 current_function_funcdef_no);
1106 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1107 }
1108
1109 /* Output a marker (i.e. a label) for the absolute end of the generated code
1110 for a function definition. This gets called *after* the epilogue code has
1111 been generated. */
1112
1113 void
1114 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1115 const char *file ATTRIBUTE_UNUSED)
1116 {
1117 dw_fde_ref fde;
1118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1119
1120 last_var_location_insn = NULL_RTX;
1121 cached_next_real_insn = NULL_RTX;
1122
1123 if (dwarf2out_do_cfi_asm ())
1124 fprintf (asm_out_file, "\t.cfi_endproc\n");
1125
1126 /* Output a label to mark the endpoint of the code generated for this
1127 function. */
1128 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1129 current_function_funcdef_no);
1130 ASM_OUTPUT_LABEL (asm_out_file, label);
1131 fde = cfun->fde;
1132 gcc_assert (fde != NULL);
1133 if (fde->dw_fde_second_begin == NULL)
1134 fde->dw_fde_end = xstrdup (label);
1135 }
1136
1137 void
1138 dwarf2out_frame_finish (void)
1139 {
1140 /* Output call frame information. */
1141 if (targetm.debug_unwind_info () == UI_DWARF2)
1142 output_call_frame_info (0);
1143
1144 /* Output another copy for the unwinder. */
1145 if ((flag_unwind_tables || flag_exceptions)
1146 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1147 output_call_frame_info (1);
1148 }
1149
1150 /* Note that the current function section is being used for code. */
1151
1152 static void
1153 dwarf2out_note_section_used (void)
1154 {
1155 section *sec = current_function_section ();
1156 if (sec == text_section)
1157 text_section_used = true;
1158 else if (sec == cold_text_section)
1159 cold_text_section_used = true;
1160 }
1161
1162 static void var_location_switch_text_section (void);
1163 static void set_cur_line_info_table (section *);
1164
1165 void
1166 dwarf2out_switch_text_section (void)
1167 {
1168 section *sect;
1169 dw_fde_ref fde = cfun->fde;
1170
1171 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1172
1173 if (!in_cold_section_p)
1174 {
1175 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1176 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1177 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1178 }
1179 else
1180 {
1181 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1182 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1183 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1184 }
1185 have_multiple_function_sections = true;
1186
1187 /* There is no need to mark used sections when not debugging. */
1188 if (cold_text_section != NULL)
1189 dwarf2out_note_section_used ();
1190
1191 if (dwarf2out_do_cfi_asm ())
1192 fprintf (asm_out_file, "\t.cfi_endproc\n");
1193
1194 /* Now do the real section switch. */
1195 sect = current_function_section ();
1196 switch_to_section (sect);
1197
1198 fde->second_in_std_section
1199 = (sect == text_section
1200 || (cold_text_section && sect == cold_text_section));
1201
1202 if (dwarf2out_do_cfi_asm ())
1203 dwarf2out_do_cfi_startproc (true);
1204
1205 var_location_switch_text_section ();
1206
1207 if (cold_text_section != NULL)
1208 set_cur_line_info_table (sect);
1209 }
1210 \f
1211 /* And now, the subset of the debugging information support code necessary
1212 for emitting location expressions. */
1213
1214 /* Data about a single source file. */
1215 struct GTY(()) dwarf_file_data {
1216 const char * filename;
1217 int emitted_number;
1218 };
1219
1220 typedef struct GTY(()) deferred_locations_struct
1221 {
1222 tree variable;
1223 dw_die_ref die;
1224 } deferred_locations;
1225
1226
1227 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1228
1229
1230 /* Describe an entry into the .debug_addr section. */
1231
1232 enum ate_kind {
1233 ate_kind_rtx,
1234 ate_kind_rtx_dtprel,
1235 ate_kind_label
1236 };
1237
1238 typedef struct GTY(()) addr_table_entry_struct {
1239 enum ate_kind kind;
1240 unsigned int refcount;
1241 unsigned int index;
1242 union addr_table_entry_struct_union
1243 {
1244 rtx GTY ((tag ("0"))) rtl;
1245 char * GTY ((tag ("1"))) label;
1246 }
1247 GTY ((desc ("%1.kind"))) addr;
1248 }
1249 addr_table_entry;
1250
1251 /* Location lists are ranges + location descriptions for that range,
1252 so you can track variables that are in different places over
1253 their entire life. */
1254 typedef struct GTY(()) dw_loc_list_struct {
1255 dw_loc_list_ref dw_loc_next;
1256 const char *begin; /* Label and addr_entry for start of range */
1257 addr_table_entry *begin_entry;
1258 const char *end; /* Label for end of range */
1259 char *ll_symbol; /* Label for beginning of location list.
1260 Only on head of list */
1261 const char *section; /* Section this loclist is relative to */
1262 dw_loc_descr_ref expr;
1263 hashval_t hash;
1264 /* True if all addresses in this and subsequent lists are known to be
1265 resolved. */
1266 bool resolved_addr;
1267 /* True if this list has been replaced by dw_loc_next. */
1268 bool replaced;
1269 bool emitted;
1270 /* True if the range should be emitted even if begin and end
1271 are the same. */
1272 bool force;
1273 } dw_loc_list_node;
1274
1275 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1276
1277 /* Convert a DWARF stack opcode into its string name. */
1278
1279 static const char *
1280 dwarf_stack_op_name (unsigned int op)
1281 {
1282 const char *name = get_DW_OP_name (op);
1283
1284 if (name != NULL)
1285 return name;
1286
1287 return "OP_<unknown>";
1288 }
1289
1290 /* Return a pointer to a newly allocated location description. Location
1291 descriptions are simple expression terms that can be strung
1292 together to form more complicated location (address) descriptions. */
1293
1294 static inline dw_loc_descr_ref
1295 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1296 unsigned HOST_WIDE_INT oprnd2)
1297 {
1298 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1299
1300 descr->dw_loc_opc = op;
1301 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1302 descr->dw_loc_oprnd1.val_entry = NULL;
1303 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1304 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1305 descr->dw_loc_oprnd2.val_entry = NULL;
1306 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1307
1308 return descr;
1309 }
1310
1311 /* Return a pointer to a newly allocated location description for
1312 REG and OFFSET. */
1313
1314 static inline dw_loc_descr_ref
1315 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1316 {
1317 if (reg <= 31)
1318 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1319 offset, 0);
1320 else
1321 return new_loc_descr (DW_OP_bregx, reg, offset);
1322 }
1323
1324 /* Add a location description term to a location description expression. */
1325
1326 static inline void
1327 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1328 {
1329 dw_loc_descr_ref *d;
1330
1331 /* Find the end of the chain. */
1332 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1333 ;
1334
1335 *d = descr;
1336 }
1337
1338 /* Compare two location operands for exact equality. */
1339
1340 static bool
1341 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1342 {
1343 if (a->val_class != b->val_class)
1344 return false;
1345 switch (a->val_class)
1346 {
1347 case dw_val_class_none:
1348 return true;
1349 case dw_val_class_addr:
1350 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1351
1352 case dw_val_class_offset:
1353 case dw_val_class_unsigned_const:
1354 case dw_val_class_const:
1355 case dw_val_class_range_list:
1356 case dw_val_class_lineptr:
1357 case dw_val_class_macptr:
1358 /* These are all HOST_WIDE_INT, signed or unsigned. */
1359 return a->v.val_unsigned == b->v.val_unsigned;
1360
1361 case dw_val_class_loc:
1362 return a->v.val_loc == b->v.val_loc;
1363 case dw_val_class_loc_list:
1364 return a->v.val_loc_list == b->v.val_loc_list;
1365 case dw_val_class_die_ref:
1366 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1367 case dw_val_class_fde_ref:
1368 return a->v.val_fde_index == b->v.val_fde_index;
1369 case dw_val_class_lbl_id:
1370 case dw_val_class_high_pc:
1371 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1372 case dw_val_class_str:
1373 return a->v.val_str == b->v.val_str;
1374 case dw_val_class_flag:
1375 return a->v.val_flag == b->v.val_flag;
1376 case dw_val_class_file:
1377 return a->v.val_file == b->v.val_file;
1378 case dw_val_class_decl_ref:
1379 return a->v.val_decl_ref == b->v.val_decl_ref;
1380
1381 case dw_val_class_const_double:
1382 return (a->v.val_double.high == b->v.val_double.high
1383 && a->v.val_double.low == b->v.val_double.low);
1384
1385 case dw_val_class_vec:
1386 {
1387 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1388 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1389
1390 return (a_len == b_len
1391 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1392 }
1393
1394 case dw_val_class_data8:
1395 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1396
1397 case dw_val_class_vms_delta:
1398 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1399 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1400 }
1401 gcc_unreachable ();
1402 }
1403
1404 /* Compare two location atoms for exact equality. */
1405
1406 static bool
1407 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1408 {
1409 if (a->dw_loc_opc != b->dw_loc_opc)
1410 return false;
1411
1412 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1413 address size, but since we always allocate cleared storage it
1414 should be zero for other types of locations. */
1415 if (a->dtprel != b->dtprel)
1416 return false;
1417
1418 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1419 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1420 }
1421
1422 /* Compare two complete location expressions for exact equality. */
1423
1424 bool
1425 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1426 {
1427 while (1)
1428 {
1429 if (a == b)
1430 return true;
1431 if (a == NULL || b == NULL)
1432 return false;
1433 if (!loc_descr_equal_p_1 (a, b))
1434 return false;
1435
1436 a = a->dw_loc_next;
1437 b = b->dw_loc_next;
1438 }
1439 }
1440
1441
1442 /* Add a constant OFFSET to a location expression. */
1443
1444 static void
1445 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1446 {
1447 dw_loc_descr_ref loc;
1448 HOST_WIDE_INT *p;
1449
1450 gcc_assert (*list_head != NULL);
1451
1452 if (!offset)
1453 return;
1454
1455 /* Find the end of the chain. */
1456 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1457 ;
1458
1459 p = NULL;
1460 if (loc->dw_loc_opc == DW_OP_fbreg
1461 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1462 p = &loc->dw_loc_oprnd1.v.val_int;
1463 else if (loc->dw_loc_opc == DW_OP_bregx)
1464 p = &loc->dw_loc_oprnd2.v.val_int;
1465
1466 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1467 offset. Don't optimize if an signed integer overflow would happen. */
1468 if (p != NULL
1469 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1470 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1471 *p += offset;
1472
1473 else if (offset > 0)
1474 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1475
1476 else
1477 {
1478 loc->dw_loc_next = int_loc_descriptor (-offset);
1479 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1480 }
1481 }
1482
1483 /* Add a constant OFFSET to a location list. */
1484
1485 static void
1486 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1487 {
1488 dw_loc_list_ref d;
1489 for (d = list_head; d != NULL; d = d->dw_loc_next)
1490 loc_descr_plus_const (&d->expr, offset);
1491 }
1492
1493 #define DWARF_REF_SIZE \
1494 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1495
1496 static unsigned long int get_base_type_offset (dw_die_ref);
1497
1498 /* Return the size of a location descriptor. */
1499
1500 static unsigned long
1501 size_of_loc_descr (dw_loc_descr_ref loc)
1502 {
1503 unsigned long size = 1;
1504
1505 switch (loc->dw_loc_opc)
1506 {
1507 case DW_OP_addr:
1508 size += DWARF2_ADDR_SIZE;
1509 break;
1510 case DW_OP_GNU_addr_index:
1511 case DW_OP_GNU_const_index:
1512 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1513 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1514 break;
1515 case DW_OP_const1u:
1516 case DW_OP_const1s:
1517 size += 1;
1518 break;
1519 case DW_OP_const2u:
1520 case DW_OP_const2s:
1521 size += 2;
1522 break;
1523 case DW_OP_const4u:
1524 case DW_OP_const4s:
1525 size += 4;
1526 break;
1527 case DW_OP_const8u:
1528 case DW_OP_const8s:
1529 size += 8;
1530 break;
1531 case DW_OP_constu:
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1533 break;
1534 case DW_OP_consts:
1535 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1536 break;
1537 case DW_OP_pick:
1538 size += 1;
1539 break;
1540 case DW_OP_plus_uconst:
1541 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1542 break;
1543 case DW_OP_skip:
1544 case DW_OP_bra:
1545 size += 2;
1546 break;
1547 case DW_OP_breg0:
1548 case DW_OP_breg1:
1549 case DW_OP_breg2:
1550 case DW_OP_breg3:
1551 case DW_OP_breg4:
1552 case DW_OP_breg5:
1553 case DW_OP_breg6:
1554 case DW_OP_breg7:
1555 case DW_OP_breg8:
1556 case DW_OP_breg9:
1557 case DW_OP_breg10:
1558 case DW_OP_breg11:
1559 case DW_OP_breg12:
1560 case DW_OP_breg13:
1561 case DW_OP_breg14:
1562 case DW_OP_breg15:
1563 case DW_OP_breg16:
1564 case DW_OP_breg17:
1565 case DW_OP_breg18:
1566 case DW_OP_breg19:
1567 case DW_OP_breg20:
1568 case DW_OP_breg21:
1569 case DW_OP_breg22:
1570 case DW_OP_breg23:
1571 case DW_OP_breg24:
1572 case DW_OP_breg25:
1573 case DW_OP_breg26:
1574 case DW_OP_breg27:
1575 case DW_OP_breg28:
1576 case DW_OP_breg29:
1577 case DW_OP_breg30:
1578 case DW_OP_breg31:
1579 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1580 break;
1581 case DW_OP_regx:
1582 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1583 break;
1584 case DW_OP_fbreg:
1585 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1586 break;
1587 case DW_OP_bregx:
1588 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1589 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1590 break;
1591 case DW_OP_piece:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 break;
1594 case DW_OP_bit_piece:
1595 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1597 break;
1598 case DW_OP_deref_size:
1599 case DW_OP_xderef_size:
1600 size += 1;
1601 break;
1602 case DW_OP_call2:
1603 size += 2;
1604 break;
1605 case DW_OP_call4:
1606 size += 4;
1607 break;
1608 case DW_OP_call_ref:
1609 size += DWARF_REF_SIZE;
1610 break;
1611 case DW_OP_implicit_value:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1613 + loc->dw_loc_oprnd1.v.val_unsigned;
1614 break;
1615 case DW_OP_GNU_implicit_pointer:
1616 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1617 break;
1618 case DW_OP_GNU_entry_value:
1619 {
1620 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1621 size += size_of_uleb128 (op_size) + op_size;
1622 break;
1623 }
1624 case DW_OP_GNU_const_type:
1625 {
1626 unsigned long o
1627 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1628 size += size_of_uleb128 (o) + 1;
1629 switch (loc->dw_loc_oprnd2.val_class)
1630 {
1631 case dw_val_class_vec:
1632 size += loc->dw_loc_oprnd2.v.val_vec.length
1633 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1634 break;
1635 case dw_val_class_const:
1636 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1637 break;
1638 case dw_val_class_const_double:
1639 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1640 break;
1641 default:
1642 gcc_unreachable ();
1643 }
1644 break;
1645 }
1646 case DW_OP_GNU_regval_type:
1647 {
1648 unsigned long o
1649 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1650 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1651 + size_of_uleb128 (o);
1652 }
1653 break;
1654 case DW_OP_GNU_deref_type:
1655 {
1656 unsigned long o
1657 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1658 size += 1 + size_of_uleb128 (o);
1659 }
1660 break;
1661 case DW_OP_GNU_convert:
1662 case DW_OP_GNU_reinterpret:
1663 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1664 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1665 else
1666 {
1667 unsigned long o
1668 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1669 size += size_of_uleb128 (o);
1670 }
1671 break;
1672 case DW_OP_GNU_parameter_ref:
1673 size += 4;
1674 break;
1675 default:
1676 break;
1677 }
1678
1679 return size;
1680 }
1681
1682 /* Return the size of a series of location descriptors. */
1683
1684 unsigned long
1685 size_of_locs (dw_loc_descr_ref loc)
1686 {
1687 dw_loc_descr_ref l;
1688 unsigned long size;
1689
1690 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1691 field, to avoid writing to a PCH file. */
1692 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1693 {
1694 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1695 break;
1696 size += size_of_loc_descr (l);
1697 }
1698 if (! l)
1699 return size;
1700
1701 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1702 {
1703 l->dw_loc_addr = size;
1704 size += size_of_loc_descr (l);
1705 }
1706
1707 return size;
1708 }
1709
1710 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1711 static void get_ref_die_offset_label (char *, dw_die_ref);
1712 static unsigned long int get_ref_die_offset (dw_die_ref);
1713
1714 /* Output location description stack opcode's operands (if any).
1715 The for_eh_or_skip parameter controls whether register numbers are
1716 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1717 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1718 info). This should be suppressed for the cases that have not been converted
1719 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1720
1721 static void
1722 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1723 {
1724 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1725 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1726
1727 switch (loc->dw_loc_opc)
1728 {
1729 #ifdef DWARF2_DEBUGGING_INFO
1730 case DW_OP_const2u:
1731 case DW_OP_const2s:
1732 dw2_asm_output_data (2, val1->v.val_int, NULL);
1733 break;
1734 case DW_OP_const4u:
1735 if (loc->dtprel)
1736 {
1737 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1738 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1739 val1->v.val_addr);
1740 fputc ('\n', asm_out_file);
1741 break;
1742 }
1743 /* FALLTHRU */
1744 case DW_OP_const4s:
1745 dw2_asm_output_data (4, val1->v.val_int, NULL);
1746 break;
1747 case DW_OP_const8u:
1748 if (loc->dtprel)
1749 {
1750 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1751 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1752 val1->v.val_addr);
1753 fputc ('\n', asm_out_file);
1754 break;
1755 }
1756 /* FALLTHRU */
1757 case DW_OP_const8s:
1758 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1759 dw2_asm_output_data (8, val1->v.val_int, NULL);
1760 break;
1761 case DW_OP_skip:
1762 case DW_OP_bra:
1763 {
1764 int offset;
1765
1766 gcc_assert (val1->val_class == dw_val_class_loc);
1767 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1768
1769 dw2_asm_output_data (2, offset, NULL);
1770 }
1771 break;
1772 case DW_OP_implicit_value:
1773 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1774 switch (val2->val_class)
1775 {
1776 case dw_val_class_const:
1777 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1778 break;
1779 case dw_val_class_vec:
1780 {
1781 unsigned int elt_size = val2->v.val_vec.elt_size;
1782 unsigned int len = val2->v.val_vec.length;
1783 unsigned int i;
1784 unsigned char *p;
1785
1786 if (elt_size > sizeof (HOST_WIDE_INT))
1787 {
1788 elt_size /= 2;
1789 len *= 2;
1790 }
1791 for (i = 0, p = val2->v.val_vec.array;
1792 i < len;
1793 i++, p += elt_size)
1794 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1795 "fp or vector constant word %u", i);
1796 }
1797 break;
1798 case dw_val_class_const_double:
1799 {
1800 unsigned HOST_WIDE_INT first, second;
1801
1802 if (WORDS_BIG_ENDIAN)
1803 {
1804 first = val2->v.val_double.high;
1805 second = val2->v.val_double.low;
1806 }
1807 else
1808 {
1809 first = val2->v.val_double.low;
1810 second = val2->v.val_double.high;
1811 }
1812 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1813 first, NULL);
1814 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1815 second, NULL);
1816 }
1817 break;
1818 case dw_val_class_addr:
1819 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1820 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1821 break;
1822 default:
1823 gcc_unreachable ();
1824 }
1825 break;
1826 #else
1827 case DW_OP_const2u:
1828 case DW_OP_const2s:
1829 case DW_OP_const4u:
1830 case DW_OP_const4s:
1831 case DW_OP_const8u:
1832 case DW_OP_const8s:
1833 case DW_OP_skip:
1834 case DW_OP_bra:
1835 case DW_OP_implicit_value:
1836 /* We currently don't make any attempt to make sure these are
1837 aligned properly like we do for the main unwind info, so
1838 don't support emitting things larger than a byte if we're
1839 only doing unwinding. */
1840 gcc_unreachable ();
1841 #endif
1842 case DW_OP_const1u:
1843 case DW_OP_const1s:
1844 dw2_asm_output_data (1, val1->v.val_int, NULL);
1845 break;
1846 case DW_OP_constu:
1847 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1848 break;
1849 case DW_OP_consts:
1850 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1851 break;
1852 case DW_OP_pick:
1853 dw2_asm_output_data (1, val1->v.val_int, NULL);
1854 break;
1855 case DW_OP_plus_uconst:
1856 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1857 break;
1858 case DW_OP_breg0:
1859 case DW_OP_breg1:
1860 case DW_OP_breg2:
1861 case DW_OP_breg3:
1862 case DW_OP_breg4:
1863 case DW_OP_breg5:
1864 case DW_OP_breg6:
1865 case DW_OP_breg7:
1866 case DW_OP_breg8:
1867 case DW_OP_breg9:
1868 case DW_OP_breg10:
1869 case DW_OP_breg11:
1870 case DW_OP_breg12:
1871 case DW_OP_breg13:
1872 case DW_OP_breg14:
1873 case DW_OP_breg15:
1874 case DW_OP_breg16:
1875 case DW_OP_breg17:
1876 case DW_OP_breg18:
1877 case DW_OP_breg19:
1878 case DW_OP_breg20:
1879 case DW_OP_breg21:
1880 case DW_OP_breg22:
1881 case DW_OP_breg23:
1882 case DW_OP_breg24:
1883 case DW_OP_breg25:
1884 case DW_OP_breg26:
1885 case DW_OP_breg27:
1886 case DW_OP_breg28:
1887 case DW_OP_breg29:
1888 case DW_OP_breg30:
1889 case DW_OP_breg31:
1890 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1891 break;
1892 case DW_OP_regx:
1893 {
1894 unsigned r = val1->v.val_unsigned;
1895 if (for_eh_or_skip >= 0)
1896 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1897 gcc_assert (size_of_uleb128 (r)
1898 == size_of_uleb128 (val1->v.val_unsigned));
1899 dw2_asm_output_data_uleb128 (r, NULL);
1900 }
1901 break;
1902 case DW_OP_fbreg:
1903 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1904 break;
1905 case DW_OP_bregx:
1906 {
1907 unsigned r = val1->v.val_unsigned;
1908 if (for_eh_or_skip >= 0)
1909 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1910 gcc_assert (size_of_uleb128 (r)
1911 == size_of_uleb128 (val1->v.val_unsigned));
1912 dw2_asm_output_data_uleb128 (r, NULL);
1913 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1914 }
1915 break;
1916 case DW_OP_piece:
1917 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1918 break;
1919 case DW_OP_bit_piece:
1920 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1921 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1922 break;
1923 case DW_OP_deref_size:
1924 case DW_OP_xderef_size:
1925 dw2_asm_output_data (1, val1->v.val_int, NULL);
1926 break;
1927
1928 case DW_OP_addr:
1929 if (loc->dtprel)
1930 {
1931 if (targetm.asm_out.output_dwarf_dtprel)
1932 {
1933 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1934 DWARF2_ADDR_SIZE,
1935 val1->v.val_addr);
1936 fputc ('\n', asm_out_file);
1937 }
1938 else
1939 gcc_unreachable ();
1940 }
1941 else
1942 {
1943 #ifdef DWARF2_DEBUGGING_INFO
1944 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1945 #else
1946 gcc_unreachable ();
1947 #endif
1948 }
1949 break;
1950
1951 case DW_OP_GNU_addr_index:
1952 case DW_OP_GNU_const_index:
1953 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1954 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1955 "(index into .debug_addr)");
1956 break;
1957
1958 case DW_OP_GNU_implicit_pointer:
1959 {
1960 char label[MAX_ARTIFICIAL_LABEL_BYTES
1961 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1962 gcc_assert (val1->val_class == dw_val_class_die_ref);
1963 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1964 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1965 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1966 }
1967 break;
1968
1969 case DW_OP_GNU_entry_value:
1970 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1971 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1972 break;
1973
1974 case DW_OP_GNU_const_type:
1975 {
1976 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1977 gcc_assert (o);
1978 dw2_asm_output_data_uleb128 (o, NULL);
1979 switch (val2->val_class)
1980 {
1981 case dw_val_class_const:
1982 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1983 dw2_asm_output_data (1, l, NULL);
1984 dw2_asm_output_data (l, val2->v.val_int, NULL);
1985 break;
1986 case dw_val_class_vec:
1987 {
1988 unsigned int elt_size = val2->v.val_vec.elt_size;
1989 unsigned int len = val2->v.val_vec.length;
1990 unsigned int i;
1991 unsigned char *p;
1992
1993 l = len * elt_size;
1994 dw2_asm_output_data (1, l, NULL);
1995 if (elt_size > sizeof (HOST_WIDE_INT))
1996 {
1997 elt_size /= 2;
1998 len *= 2;
1999 }
2000 for (i = 0, p = val2->v.val_vec.array;
2001 i < len;
2002 i++, p += elt_size)
2003 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2004 "fp or vector constant word %u", i);
2005 }
2006 break;
2007 case dw_val_class_const_double:
2008 {
2009 unsigned HOST_WIDE_INT first, second;
2010 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2011
2012 dw2_asm_output_data (1, 2 * l, NULL);
2013 if (WORDS_BIG_ENDIAN)
2014 {
2015 first = val2->v.val_double.high;
2016 second = val2->v.val_double.low;
2017 }
2018 else
2019 {
2020 first = val2->v.val_double.low;
2021 second = val2->v.val_double.high;
2022 }
2023 dw2_asm_output_data (l, first, NULL);
2024 dw2_asm_output_data (l, second, NULL);
2025 }
2026 break;
2027 default:
2028 gcc_unreachable ();
2029 }
2030 }
2031 break;
2032 case DW_OP_GNU_regval_type:
2033 {
2034 unsigned r = val1->v.val_unsigned;
2035 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2036 gcc_assert (o);
2037 if (for_eh_or_skip >= 0)
2038 {
2039 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2040 gcc_assert (size_of_uleb128 (r)
2041 == size_of_uleb128 (val1->v.val_unsigned));
2042 }
2043 dw2_asm_output_data_uleb128 (r, NULL);
2044 dw2_asm_output_data_uleb128 (o, NULL);
2045 }
2046 break;
2047 case DW_OP_GNU_deref_type:
2048 {
2049 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2050 gcc_assert (o);
2051 dw2_asm_output_data (1, val1->v.val_int, NULL);
2052 dw2_asm_output_data_uleb128 (o, NULL);
2053 }
2054 break;
2055 case DW_OP_GNU_convert:
2056 case DW_OP_GNU_reinterpret:
2057 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2058 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2059 else
2060 {
2061 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2062 gcc_assert (o);
2063 dw2_asm_output_data_uleb128 (o, NULL);
2064 }
2065 break;
2066
2067 case DW_OP_GNU_parameter_ref:
2068 {
2069 unsigned long o;
2070 gcc_assert (val1->val_class == dw_val_class_die_ref);
2071 o = get_ref_die_offset (val1->v.val_die_ref.die);
2072 dw2_asm_output_data (4, o, NULL);
2073 }
2074 break;
2075
2076 default:
2077 /* Other codes have no operands. */
2078 break;
2079 }
2080 }
2081
2082 /* Output a sequence of location operations.
2083 The for_eh_or_skip parameter controls whether register numbers are
2084 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2085 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2086 info). This should be suppressed for the cases that have not been converted
2087 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2088
2089 void
2090 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2091 {
2092 for (; loc != NULL; loc = loc->dw_loc_next)
2093 {
2094 enum dwarf_location_atom opc = loc->dw_loc_opc;
2095 /* Output the opcode. */
2096 if (for_eh_or_skip >= 0
2097 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2098 {
2099 unsigned r = (opc - DW_OP_breg0);
2100 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2101 gcc_assert (r <= 31);
2102 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2103 }
2104 else if (for_eh_or_skip >= 0
2105 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2106 {
2107 unsigned r = (opc - DW_OP_reg0);
2108 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2109 gcc_assert (r <= 31);
2110 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2111 }
2112
2113 dw2_asm_output_data (1, opc,
2114 "%s", dwarf_stack_op_name (opc));
2115
2116 /* Output the operand(s) (if any). */
2117 output_loc_operands (loc, for_eh_or_skip);
2118 }
2119 }
2120
2121 /* Output location description stack opcode's operands (if any).
2122 The output is single bytes on a line, suitable for .cfi_escape. */
2123
2124 static void
2125 output_loc_operands_raw (dw_loc_descr_ref loc)
2126 {
2127 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2128 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2129
2130 switch (loc->dw_loc_opc)
2131 {
2132 case DW_OP_addr:
2133 case DW_OP_GNU_addr_index:
2134 case DW_OP_GNU_const_index:
2135 case DW_OP_implicit_value:
2136 /* We cannot output addresses in .cfi_escape, only bytes. */
2137 gcc_unreachable ();
2138
2139 case DW_OP_const1u:
2140 case DW_OP_const1s:
2141 case DW_OP_pick:
2142 case DW_OP_deref_size:
2143 case DW_OP_xderef_size:
2144 fputc (',', asm_out_file);
2145 dw2_asm_output_data_raw (1, val1->v.val_int);
2146 break;
2147
2148 case DW_OP_const2u:
2149 case DW_OP_const2s:
2150 fputc (',', asm_out_file);
2151 dw2_asm_output_data_raw (2, val1->v.val_int);
2152 break;
2153
2154 case DW_OP_const4u:
2155 case DW_OP_const4s:
2156 fputc (',', asm_out_file);
2157 dw2_asm_output_data_raw (4, val1->v.val_int);
2158 break;
2159
2160 case DW_OP_const8u:
2161 case DW_OP_const8s:
2162 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2163 fputc (',', asm_out_file);
2164 dw2_asm_output_data_raw (8, val1->v.val_int);
2165 break;
2166
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2169 {
2170 int offset;
2171
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2174
2175 fputc (',', asm_out_file);
2176 dw2_asm_output_data_raw (2, offset);
2177 }
2178 break;
2179
2180 case DW_OP_regx:
2181 {
2182 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2183 gcc_assert (size_of_uleb128 (r)
2184 == size_of_uleb128 (val1->v.val_unsigned));
2185 fputc (',', asm_out_file);
2186 dw2_asm_output_data_uleb128_raw (r);
2187 }
2188 break;
2189
2190 case DW_OP_constu:
2191 case DW_OP_plus_uconst:
2192 case DW_OP_piece:
2193 fputc (',', asm_out_file);
2194 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2195 break;
2196
2197 case DW_OP_bit_piece:
2198 fputc (',', asm_out_file);
2199 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2200 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2201 break;
2202
2203 case DW_OP_consts:
2204 case DW_OP_breg0:
2205 case DW_OP_breg1:
2206 case DW_OP_breg2:
2207 case DW_OP_breg3:
2208 case DW_OP_breg4:
2209 case DW_OP_breg5:
2210 case DW_OP_breg6:
2211 case DW_OP_breg7:
2212 case DW_OP_breg8:
2213 case DW_OP_breg9:
2214 case DW_OP_breg10:
2215 case DW_OP_breg11:
2216 case DW_OP_breg12:
2217 case DW_OP_breg13:
2218 case DW_OP_breg14:
2219 case DW_OP_breg15:
2220 case DW_OP_breg16:
2221 case DW_OP_breg17:
2222 case DW_OP_breg18:
2223 case DW_OP_breg19:
2224 case DW_OP_breg20:
2225 case DW_OP_breg21:
2226 case DW_OP_breg22:
2227 case DW_OP_breg23:
2228 case DW_OP_breg24:
2229 case DW_OP_breg25:
2230 case DW_OP_breg26:
2231 case DW_OP_breg27:
2232 case DW_OP_breg28:
2233 case DW_OP_breg29:
2234 case DW_OP_breg30:
2235 case DW_OP_breg31:
2236 case DW_OP_fbreg:
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2239 break;
2240
2241 case DW_OP_bregx:
2242 {
2243 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2244 gcc_assert (size_of_uleb128 (r)
2245 == size_of_uleb128 (val1->v.val_unsigned));
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_uleb128_raw (r);
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2250 }
2251 break;
2252
2253 case DW_OP_GNU_implicit_pointer:
2254 case DW_OP_GNU_entry_value:
2255 case DW_OP_GNU_const_type:
2256 case DW_OP_GNU_regval_type:
2257 case DW_OP_GNU_deref_type:
2258 case DW_OP_GNU_convert:
2259 case DW_OP_GNU_reinterpret:
2260 case DW_OP_GNU_parameter_ref:
2261 gcc_unreachable ();
2262 break;
2263
2264 default:
2265 /* Other codes have no operands. */
2266 break;
2267 }
2268 }
2269
2270 void
2271 output_loc_sequence_raw (dw_loc_descr_ref loc)
2272 {
2273 while (1)
2274 {
2275 enum dwarf_location_atom opc = loc->dw_loc_opc;
2276 /* Output the opcode. */
2277 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2278 {
2279 unsigned r = (opc - DW_OP_breg0);
2280 r = DWARF2_FRAME_REG_OUT (r, 1);
2281 gcc_assert (r <= 31);
2282 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2283 }
2284 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2285 {
2286 unsigned r = (opc - DW_OP_reg0);
2287 r = DWARF2_FRAME_REG_OUT (r, 1);
2288 gcc_assert (r <= 31);
2289 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2290 }
2291 /* Output the opcode. */
2292 fprintf (asm_out_file, "%#x", opc);
2293 output_loc_operands_raw (loc);
2294
2295 if (!loc->dw_loc_next)
2296 break;
2297 loc = loc->dw_loc_next;
2298
2299 fputc (',', asm_out_file);
2300 }
2301 }
2302
2303 /* This function builds a dwarf location descriptor sequence from a
2304 dw_cfa_location, adding the given OFFSET to the result of the
2305 expression. */
2306
2307 struct dw_loc_descr_struct *
2308 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2309 {
2310 struct dw_loc_descr_struct *head, *tmp;
2311
2312 offset += cfa->offset;
2313
2314 if (cfa->indirect)
2315 {
2316 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2317 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2318 head->dw_loc_oprnd1.val_entry = NULL;
2319 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2320 add_loc_descr (&head, tmp);
2321 if (offset != 0)
2322 {
2323 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2324 add_loc_descr (&head, tmp);
2325 }
2326 }
2327 else
2328 head = new_reg_loc_descr (cfa->reg, offset);
2329
2330 return head;
2331 }
2332
2333 /* This function builds a dwarf location descriptor sequence for
2334 the address at OFFSET from the CFA when stack is aligned to
2335 ALIGNMENT byte. */
2336
2337 struct dw_loc_descr_struct *
2338 build_cfa_aligned_loc (dw_cfa_location *cfa,
2339 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2340 {
2341 struct dw_loc_descr_struct *head;
2342 unsigned int dwarf_fp
2343 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2344
2345 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2346 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2347 {
2348 head = new_reg_loc_descr (dwarf_fp, 0);
2349 add_loc_descr (&head, int_loc_descriptor (alignment));
2350 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2351 loc_descr_plus_const (&head, offset);
2352 }
2353 else
2354 head = new_reg_loc_descr (dwarf_fp, offset);
2355 return head;
2356 }
2357 \f
2358 /* And now, the support for symbolic debugging information. */
2359
2360 /* .debug_str support. */
2361 static int output_indirect_string (void **, void *);
2362
2363 static void dwarf2out_init (const char *);
2364 static void dwarf2out_finish (const char *);
2365 static void dwarf2out_assembly_start (void);
2366 static void dwarf2out_define (unsigned int, const char *);
2367 static void dwarf2out_undef (unsigned int, const char *);
2368 static void dwarf2out_start_source_file (unsigned, const char *);
2369 static void dwarf2out_end_source_file (unsigned);
2370 static void dwarf2out_function_decl (tree);
2371 static void dwarf2out_begin_block (unsigned, unsigned);
2372 static void dwarf2out_end_block (unsigned, unsigned);
2373 static bool dwarf2out_ignore_block (const_tree);
2374 static void dwarf2out_global_decl (tree);
2375 static void dwarf2out_type_decl (tree, int);
2376 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2377 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2378 dw_die_ref);
2379 static void dwarf2out_abstract_function (tree);
2380 static void dwarf2out_var_location (rtx);
2381 static void dwarf2out_begin_function (tree);
2382 static void dwarf2out_end_function (unsigned int);
2383 static void dwarf2out_set_name (tree, tree);
2384
2385 /* The debug hooks structure. */
2386
2387 const struct gcc_debug_hooks dwarf2_debug_hooks =
2388 {
2389 dwarf2out_init,
2390 dwarf2out_finish,
2391 dwarf2out_assembly_start,
2392 dwarf2out_define,
2393 dwarf2out_undef,
2394 dwarf2out_start_source_file,
2395 dwarf2out_end_source_file,
2396 dwarf2out_begin_block,
2397 dwarf2out_end_block,
2398 dwarf2out_ignore_block,
2399 dwarf2out_source_line,
2400 dwarf2out_begin_prologue,
2401 #if VMS_DEBUGGING_INFO
2402 dwarf2out_vms_end_prologue,
2403 dwarf2out_vms_begin_epilogue,
2404 #else
2405 debug_nothing_int_charstar,
2406 debug_nothing_int_charstar,
2407 #endif
2408 dwarf2out_end_epilogue,
2409 dwarf2out_begin_function,
2410 dwarf2out_end_function, /* end_function */
2411 dwarf2out_function_decl, /* function_decl */
2412 dwarf2out_global_decl,
2413 dwarf2out_type_decl, /* type_decl */
2414 dwarf2out_imported_module_or_decl,
2415 debug_nothing_tree, /* deferred_inline_function */
2416 /* The DWARF 2 backend tries to reduce debugging bloat by not
2417 emitting the abstract description of inline functions until
2418 something tries to reference them. */
2419 dwarf2out_abstract_function, /* outlining_inline_function */
2420 debug_nothing_rtx, /* label */
2421 debug_nothing_int, /* handle_pch */
2422 dwarf2out_var_location,
2423 dwarf2out_switch_text_section,
2424 dwarf2out_set_name,
2425 1, /* start_end_main_source_file */
2426 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2427 };
2428 \f
2429 /* NOTE: In the comments in this file, many references are made to
2430 "Debugging Information Entries". This term is abbreviated as `DIE'
2431 throughout the remainder of this file. */
2432
2433 /* An internal representation of the DWARF output is built, and then
2434 walked to generate the DWARF debugging info. The walk of the internal
2435 representation is done after the entire program has been compiled.
2436 The types below are used to describe the internal representation. */
2437
2438 /* Whether to put type DIEs into their own section .debug_types instead
2439 of making them part of the .debug_info section. Only supported for
2440 Dwarf V4 or higher and the user didn't disable them through
2441 -fno-debug-types-section. It is more efficient to put them in a
2442 separate comdat sections since the linker will then be able to
2443 remove duplicates. But not all tools support .debug_types sections
2444 yet. */
2445
2446 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2447
2448 /* Various DIE's use offsets relative to the beginning of the
2449 .debug_info section to refer to each other. */
2450
2451 typedef long int dw_offset;
2452
2453 /* Define typedefs here to avoid circular dependencies. */
2454
2455 typedef struct dw_attr_struct *dw_attr_ref;
2456 typedef struct dw_line_info_struct *dw_line_info_ref;
2457 typedef struct pubname_struct *pubname_ref;
2458 typedef struct dw_ranges_struct *dw_ranges_ref;
2459 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2460 typedef struct comdat_type_struct *comdat_type_node_ref;
2461
2462 /* The entries in the line_info table more-or-less mirror the opcodes
2463 that are used in the real dwarf line table. Arrays of these entries
2464 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2465 supported. */
2466
2467 enum dw_line_info_opcode {
2468 /* Emit DW_LNE_set_address; the operand is the label index. */
2469 LI_set_address,
2470
2471 /* Emit a row to the matrix with the given line. This may be done
2472 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2473 special opcodes. */
2474 LI_set_line,
2475
2476 /* Emit a DW_LNS_set_file. */
2477 LI_set_file,
2478
2479 /* Emit a DW_LNS_set_column. */
2480 LI_set_column,
2481
2482 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2483 LI_negate_stmt,
2484
2485 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2486 LI_set_prologue_end,
2487 LI_set_epilogue_begin,
2488
2489 /* Emit a DW_LNE_set_discriminator. */
2490 LI_set_discriminator
2491 };
2492
2493 typedef struct GTY(()) dw_line_info_struct {
2494 enum dw_line_info_opcode opcode;
2495 unsigned int val;
2496 } dw_line_info_entry;
2497
2498
2499 typedef struct GTY(()) dw_line_info_table_struct {
2500 /* The label that marks the end of this section. */
2501 const char *end_label;
2502
2503 /* The values for the last row of the matrix, as collected in the table.
2504 These are used to minimize the changes to the next row. */
2505 unsigned int file_num;
2506 unsigned int line_num;
2507 unsigned int column_num;
2508 int discrim_num;
2509 bool is_stmt;
2510 bool in_use;
2511
2512 vec<dw_line_info_entry, va_gc> *entries;
2513 } dw_line_info_table;
2514
2515 typedef dw_line_info_table *dw_line_info_table_p;
2516
2517
2518 /* Each DIE attribute has a field specifying the attribute kind,
2519 a link to the next attribute in the chain, and an attribute value.
2520 Attributes are typically linked below the DIE they modify. */
2521
2522 typedef struct GTY(()) dw_attr_struct {
2523 enum dwarf_attribute dw_attr;
2524 dw_val_node dw_attr_val;
2525 }
2526 dw_attr_node;
2527
2528
2529 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2530 The children of each node form a circular list linked by
2531 die_sib. die_child points to the node *before* the "first" child node. */
2532
2533 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2534 union die_symbol_or_type_node
2535 {
2536 const char * GTY ((tag ("0"))) die_symbol;
2537 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2538 }
2539 GTY ((desc ("%0.comdat_type_p"))) die_id;
2540 vec<dw_attr_node, va_gc> *die_attr;
2541 dw_die_ref die_parent;
2542 dw_die_ref die_child;
2543 dw_die_ref die_sib;
2544 dw_die_ref die_definition; /* ref from a specification to its definition */
2545 dw_offset die_offset;
2546 unsigned long die_abbrev;
2547 int die_mark;
2548 unsigned int decl_id;
2549 enum dwarf_tag die_tag;
2550 /* Die is used and must not be pruned as unused. */
2551 BOOL_BITFIELD die_perennial_p : 1;
2552 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2553 /* Lots of spare bits. */
2554 }
2555 die_node;
2556
2557 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2558 #define FOR_EACH_CHILD(die, c, expr) do { \
2559 c = die->die_child; \
2560 if (c) do { \
2561 c = c->die_sib; \
2562 expr; \
2563 } while (c != die->die_child); \
2564 } while (0)
2565
2566 /* The pubname structure */
2567
2568 typedef struct GTY(()) pubname_struct {
2569 dw_die_ref die;
2570 const char *name;
2571 }
2572 pubname_entry;
2573
2574
2575 struct GTY(()) dw_ranges_struct {
2576 /* If this is positive, it's a block number, otherwise it's a
2577 bitwise-negated index into dw_ranges_by_label. */
2578 int num;
2579 };
2580
2581 /* A structure to hold a macinfo entry. */
2582
2583 typedef struct GTY(()) macinfo_struct {
2584 unsigned char code;
2585 unsigned HOST_WIDE_INT lineno;
2586 const char *info;
2587 }
2588 macinfo_entry;
2589
2590
2591 struct GTY(()) dw_ranges_by_label_struct {
2592 const char *begin;
2593 const char *end;
2594 };
2595
2596 /* The comdat type node structure. */
2597 typedef struct GTY(()) comdat_type_struct
2598 {
2599 dw_die_ref root_die;
2600 dw_die_ref type_die;
2601 dw_die_ref skeleton_die;
2602 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2603 struct comdat_type_struct *next;
2604 }
2605 comdat_type_node;
2606
2607 /* The limbo die list structure. */
2608 typedef struct GTY(()) limbo_die_struct {
2609 dw_die_ref die;
2610 tree created_for;
2611 struct limbo_die_struct *next;
2612 }
2613 limbo_die_node;
2614
2615 typedef struct skeleton_chain_struct
2616 {
2617 dw_die_ref old_die;
2618 dw_die_ref new_die;
2619 struct skeleton_chain_struct *parent;
2620 }
2621 skeleton_chain_node;
2622
2623 /* Define a macro which returns nonzero for a TYPE_DECL which was
2624 implicitly generated for a type.
2625
2626 Note that, unlike the C front-end (which generates a NULL named
2627 TYPE_DECL node for each complete tagged type, each array type,
2628 and each function type node created) the C++ front-end generates
2629 a _named_ TYPE_DECL node for each tagged type node created.
2630 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2631 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2632 front-end, but for each type, tagged or not. */
2633
2634 #define TYPE_DECL_IS_STUB(decl) \
2635 (DECL_NAME (decl) == NULL_TREE \
2636 || (DECL_ARTIFICIAL (decl) \
2637 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2638 /* This is necessary for stub decls that \
2639 appear in nested inline functions. */ \
2640 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2641 && (decl_ultimate_origin (decl) \
2642 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2643
2644 /* Information concerning the compilation unit's programming
2645 language, and compiler version. */
2646
2647 /* Fixed size portion of the DWARF compilation unit header. */
2648 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2649 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2650
2651 /* Fixed size portion of the DWARF comdat type unit header. */
2652 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2653 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2654 + DWARF_OFFSET_SIZE)
2655
2656 /* Fixed size portion of public names info. */
2657 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2658
2659 /* Fixed size portion of the address range info. */
2660 #define DWARF_ARANGES_HEADER_SIZE \
2661 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2662 DWARF2_ADDR_SIZE * 2) \
2663 - DWARF_INITIAL_LENGTH_SIZE)
2664
2665 /* Size of padding portion in the address range info. It must be
2666 aligned to twice the pointer size. */
2667 #define DWARF_ARANGES_PAD_SIZE \
2668 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2669 DWARF2_ADDR_SIZE * 2) \
2670 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2671
2672 /* Use assembler line directives if available. */
2673 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2674 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2675 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2676 #else
2677 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2678 #endif
2679 #endif
2680
2681 /* Minimum line offset in a special line info. opcode.
2682 This value was chosen to give a reasonable range of values. */
2683 #define DWARF_LINE_BASE -10
2684
2685 /* First special line opcode - leave room for the standard opcodes. */
2686 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2687
2688 /* Range of line offsets in a special line info. opcode. */
2689 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2690
2691 /* Flag that indicates the initial value of the is_stmt_start flag.
2692 In the present implementation, we do not mark any lines as
2693 the beginning of a source statement, because that information
2694 is not made available by the GCC front-end. */
2695 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2696
2697 /* Maximum number of operations per instruction bundle. */
2698 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2699 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2700 #endif
2701
2702 /* This location is used by calc_die_sizes() to keep track
2703 the offset of each DIE within the .debug_info section. */
2704 static unsigned long next_die_offset;
2705
2706 /* Record the root of the DIE's built for the current compilation unit. */
2707 static GTY(()) dw_die_ref single_comp_unit_die;
2708
2709 /* A list of type DIEs that have been separated into comdat sections. */
2710 static GTY(()) comdat_type_node *comdat_type_list;
2711
2712 /* A list of DIEs with a NULL parent waiting to be relocated. */
2713 static GTY(()) limbo_die_node *limbo_die_list;
2714
2715 /* A list of DIEs for which we may have to generate
2716 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2717 static GTY(()) limbo_die_node *deferred_asm_name;
2718
2719 /* Filenames referenced by this compilation unit. */
2720 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2721
2722 /* A hash table of references to DIE's that describe declarations.
2723 The key is a DECL_UID() which is a unique number identifying each decl. */
2724 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2725
2726 /* A hash table of references to DIE's that describe COMMON blocks.
2727 The key is DECL_UID() ^ die_parent. */
2728 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2729
2730 typedef struct GTY(()) die_arg_entry_struct {
2731 dw_die_ref die;
2732 tree arg;
2733 } die_arg_entry;
2734
2735
2736 /* Node of the variable location list. */
2737 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2738 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2739 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2740 in mode of the EXPR_LIST node and first EXPR_LIST operand
2741 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2742 location or NULL for padding. For larger bitsizes,
2743 mode is 0 and first operand is a CONCAT with bitsize
2744 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2745 NULL as second operand. */
2746 rtx GTY (()) loc;
2747 const char * GTY (()) label;
2748 struct var_loc_node * GTY (()) next;
2749 };
2750
2751 /* Variable location list. */
2752 struct GTY (()) var_loc_list_def {
2753 struct var_loc_node * GTY (()) first;
2754
2755 /* Pointer to the last but one or last element of the
2756 chained list. If the list is empty, both first and
2757 last are NULL, if the list contains just one node
2758 or the last node certainly is not redundant, it points
2759 to the last node, otherwise points to the last but one.
2760 Do not mark it for GC because it is marked through the chain. */
2761 struct var_loc_node * GTY ((skip ("%h"))) last;
2762
2763 /* Pointer to the last element before section switch,
2764 if NULL, either sections weren't switched or first
2765 is after section switch. */
2766 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2767
2768 /* DECL_UID of the variable decl. */
2769 unsigned int decl_id;
2770 };
2771 typedef struct var_loc_list_def var_loc_list;
2772
2773 /* Call argument location list. */
2774 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2775 rtx GTY (()) call_arg_loc_note;
2776 const char * GTY (()) label;
2777 tree GTY (()) block;
2778 bool tail_call_p;
2779 rtx GTY (()) symbol_ref;
2780 struct call_arg_loc_node * GTY (()) next;
2781 };
2782
2783
2784 /* Table of decl location linked lists. */
2785 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2786
2787 /* Head and tail of call_arg_loc chain. */
2788 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2789 static struct call_arg_loc_node *call_arg_loc_last;
2790
2791 /* Number of call sites in the current function. */
2792 static int call_site_count = -1;
2793 /* Number of tail call sites in the current function. */
2794 static int tail_call_site_count = -1;
2795
2796 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2797 DIEs. */
2798 static vec<dw_die_ref> block_map;
2799
2800 /* A cached location list. */
2801 struct GTY (()) cached_dw_loc_list_def {
2802 /* The DECL_UID of the decl that this entry describes. */
2803 unsigned int decl_id;
2804
2805 /* The cached location list. */
2806 dw_loc_list_ref loc_list;
2807 };
2808 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2809
2810 /* Table of cached location lists. */
2811 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2812
2813 /* A pointer to the base of a list of references to DIE's that
2814 are uniquely identified by their tag, presence/absence of
2815 children DIE's, and list of attribute/value pairs. */
2816 static GTY((length ("abbrev_die_table_allocated")))
2817 dw_die_ref *abbrev_die_table;
2818
2819 /* Number of elements currently allocated for abbrev_die_table. */
2820 static GTY(()) unsigned abbrev_die_table_allocated;
2821
2822 /* Number of elements in type_die_table currently in use. */
2823 static GTY(()) unsigned abbrev_die_table_in_use;
2824
2825 /* Size (in elements) of increments by which we may expand the
2826 abbrev_die_table. */
2827 #define ABBREV_DIE_TABLE_INCREMENT 256
2828
2829 /* A global counter for generating labels for line number data. */
2830 static unsigned int line_info_label_num;
2831
2832 /* The current table to which we should emit line number information
2833 for the current function. This will be set up at the beginning of
2834 assembly for the function. */
2835 static dw_line_info_table *cur_line_info_table;
2836
2837 /* The two default tables of line number info. */
2838 static GTY(()) dw_line_info_table *text_section_line_info;
2839 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2840
2841 /* The set of all non-default tables of line number info. */
2842 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2843
2844 /* A flag to tell pubnames/types export if there is an info section to
2845 refer to. */
2846 static bool info_section_emitted;
2847
2848 /* A pointer to the base of a table that contains a list of publicly
2849 accessible names. */
2850 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2851
2852 /* A pointer to the base of a table that contains a list of publicly
2853 accessible types. */
2854 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2855
2856 /* A pointer to the base of a table that contains a list of macro
2857 defines/undefines (and file start/end markers). */
2858 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2859
2860 /* True if .debug_macinfo or .debug_macros section is going to be
2861 emitted. */
2862 #define have_macinfo \
2863 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2864 && !macinfo_table->is_empty ())
2865
2866 /* Array of dies for which we should generate .debug_ranges info. */
2867 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2868
2869 /* Number of elements currently allocated for ranges_table. */
2870 static GTY(()) unsigned ranges_table_allocated;
2871
2872 /* Number of elements in ranges_table currently in use. */
2873 static GTY(()) unsigned ranges_table_in_use;
2874
2875 /* Array of pairs of labels referenced in ranges_table. */
2876 static GTY ((length ("ranges_by_label_allocated")))
2877 dw_ranges_by_label_ref ranges_by_label;
2878
2879 /* Number of elements currently allocated for ranges_by_label. */
2880 static GTY(()) unsigned ranges_by_label_allocated;
2881
2882 /* Number of elements in ranges_by_label currently in use. */
2883 static GTY(()) unsigned ranges_by_label_in_use;
2884
2885 /* Size (in elements) of increments by which we may expand the
2886 ranges_table. */
2887 #define RANGES_TABLE_INCREMENT 64
2888
2889 /* Whether we have location lists that need outputting */
2890 static GTY(()) bool have_location_lists;
2891
2892 /* Unique label counter. */
2893 static GTY(()) unsigned int loclabel_num;
2894
2895 /* Unique label counter for point-of-call tables. */
2896 static GTY(()) unsigned int poc_label_num;
2897
2898 /* Record whether the function being analyzed contains inlined functions. */
2899 static int current_function_has_inlines;
2900
2901 /* The last file entry emitted by maybe_emit_file(). */
2902 static GTY(()) struct dwarf_file_data * last_emitted_file;
2903
2904 /* Number of internal labels generated by gen_internal_sym(). */
2905 static GTY(()) int label_num;
2906
2907 /* Cached result of previous call to lookup_filename. */
2908 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2909
2910 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2911
2912 /* Instances of generic types for which we need to generate debug
2913 info that describe their generic parameters and arguments. That
2914 generation needs to happen once all types are properly laid out so
2915 we do it at the end of compilation. */
2916 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2917
2918 /* Offset from the "steady-state frame pointer" to the frame base,
2919 within the current function. */
2920 static HOST_WIDE_INT frame_pointer_fb_offset;
2921 static bool frame_pointer_fb_offset_valid;
2922
2923 static vec<dw_die_ref> base_types;
2924
2925 /* Forward declarations for functions defined in this file. */
2926
2927 static int is_pseudo_reg (const_rtx);
2928 static tree type_main_variant (tree);
2929 static int is_tagged_type (const_tree);
2930 static const char *dwarf_tag_name (unsigned);
2931 static const char *dwarf_attr_name (unsigned);
2932 static const char *dwarf_form_name (unsigned);
2933 static tree decl_ultimate_origin (const_tree);
2934 static tree decl_class_context (tree);
2935 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2936 static inline enum dw_val_class AT_class (dw_attr_ref);
2937 static inline unsigned int AT_index (dw_attr_ref);
2938 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2939 static inline unsigned AT_flag (dw_attr_ref);
2940 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2941 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2942 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2943 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2944 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2945 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2946 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2947 unsigned int, unsigned char *);
2948 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2949 static hashval_t debug_str_do_hash (const void *);
2950 static int debug_str_eq (const void *, const void *);
2951 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2952 static inline const char *AT_string (dw_attr_ref);
2953 static enum dwarf_form AT_string_form (dw_attr_ref);
2954 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2955 static void add_AT_specification (dw_die_ref, dw_die_ref);
2956 static inline dw_die_ref AT_ref (dw_attr_ref);
2957 static inline int AT_ref_external (dw_attr_ref);
2958 static inline void set_AT_ref_external (dw_attr_ref, int);
2959 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2960 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2961 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2962 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2963 dw_loc_list_ref);
2964 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2965 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2966 static void remove_addr_table_entry (addr_table_entry *);
2967 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2968 static inline rtx AT_addr (dw_attr_ref);
2969 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2970 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2971 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2972 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2973 unsigned HOST_WIDE_INT);
2974 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2975 unsigned long, bool);
2976 static inline const char *AT_lbl (dw_attr_ref);
2977 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2978 static const char *get_AT_low_pc (dw_die_ref);
2979 static const char *get_AT_hi_pc (dw_die_ref);
2980 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2981 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2982 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2983 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2984 static bool is_cxx (void);
2985 static bool is_fortran (void);
2986 static bool is_ada (void);
2987 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2988 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2989 static void add_child_die (dw_die_ref, dw_die_ref);
2990 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2991 static dw_die_ref lookup_type_die (tree);
2992 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2993 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2994 static void equate_type_number_to_die (tree, dw_die_ref);
2995 static hashval_t decl_die_table_hash (const void *);
2996 static int decl_die_table_eq (const void *, const void *);
2997 static dw_die_ref lookup_decl_die (tree);
2998 static hashval_t common_block_die_table_hash (const void *);
2999 static int common_block_die_table_eq (const void *, const void *);
3000 static hashval_t decl_loc_table_hash (const void *);
3001 static int decl_loc_table_eq (const void *, const void *);
3002 static var_loc_list *lookup_decl_loc (const_tree);
3003 static void equate_decl_number_to_die (tree, dw_die_ref);
3004 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3005 static void print_spaces (FILE *);
3006 static void print_die (dw_die_ref, FILE *);
3007 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3008 static dw_die_ref pop_compile_unit (dw_die_ref);
3009 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3010 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3011 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3012 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3013 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3014 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3015 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3016 struct md5_ctx *, int *);
3017 struct checksum_attributes;
3018 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3019 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3020 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3021 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3022 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3023 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3024 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3025 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3026 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3027 static void compute_section_prefix (dw_die_ref);
3028 static int is_type_die (dw_die_ref);
3029 static int is_comdat_die (dw_die_ref);
3030 static int is_symbol_die (dw_die_ref);
3031 static inline bool is_template_instantiation (dw_die_ref);
3032 static void assign_symbol_names (dw_die_ref);
3033 static void break_out_includes (dw_die_ref);
3034 static int is_declaration_die (dw_die_ref);
3035 static int should_move_die_to_comdat (dw_die_ref);
3036 static dw_die_ref clone_as_declaration (dw_die_ref);
3037 static dw_die_ref clone_die (dw_die_ref);
3038 static dw_die_ref clone_tree (dw_die_ref);
3039 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3040 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3041 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3042 static dw_die_ref generate_skeleton (dw_die_ref);
3043 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3044 dw_die_ref,
3045 dw_die_ref);
3046 static void break_out_comdat_types (dw_die_ref);
3047 static void copy_decls_for_unworthy_types (dw_die_ref);
3048
3049 static void add_sibling_attributes (dw_die_ref);
3050 static void output_location_lists (dw_die_ref);
3051 static int constant_size (unsigned HOST_WIDE_INT);
3052 static unsigned long size_of_die (dw_die_ref);
3053 static void calc_die_sizes (dw_die_ref);
3054 static void calc_base_type_die_sizes (void);
3055 static void mark_dies (dw_die_ref);
3056 static void unmark_dies (dw_die_ref);
3057 static void unmark_all_dies (dw_die_ref);
3058 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3059 static unsigned long size_of_aranges (void);
3060 static enum dwarf_form value_format (dw_attr_ref);
3061 static void output_value_format (dw_attr_ref);
3062 static void output_abbrev_section (void);
3063 static void output_die_abbrevs (unsigned long, dw_die_ref);
3064 static void output_die_symbol (dw_die_ref);
3065 static void output_die (dw_die_ref);
3066 static void output_compilation_unit_header (void);
3067 static void output_comp_unit (dw_die_ref, int);
3068 static void output_comdat_type_unit (comdat_type_node *);
3069 static const char *dwarf2_name (tree, int);
3070 static void add_pubname (tree, dw_die_ref);
3071 static void add_enumerator_pubname (const char *, dw_die_ref);
3072 static void add_pubname_string (const char *, dw_die_ref);
3073 static void add_pubtype (tree, dw_die_ref);
3074 static void output_pubnames (vec<pubname_entry, va_gc> *);
3075 static void output_aranges (unsigned long);
3076 static unsigned int add_ranges_num (int);
3077 static unsigned int add_ranges (const_tree);
3078 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3079 bool *, bool);
3080 static void output_ranges (void);
3081 static dw_line_info_table *new_line_info_table (void);
3082 static void output_line_info (bool);
3083 static void output_file_names (void);
3084 static dw_die_ref base_type_die (tree);
3085 static int is_base_type (tree);
3086 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3087 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3088 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3089 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3090 static int type_is_enum (const_tree);
3091 static unsigned int dbx_reg_number (const_rtx);
3092 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3093 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3094 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3095 enum var_init_status);
3096 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3097 enum var_init_status);
3098 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3099 enum var_init_status);
3100 static int is_based_loc (const_rtx);
3101 static int resolve_one_addr (rtx *, void *);
3102 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3103 enum var_init_status);
3104 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3105 enum var_init_status);
3106 static dw_loc_list_ref loc_list_from_tree (tree, int);
3107 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3108 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3109 static tree field_type (const_tree);
3110 static unsigned int simple_type_align_in_bits (const_tree);
3111 static unsigned int simple_decl_align_in_bits (const_tree);
3112 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3113 static HOST_WIDE_INT field_byte_offset (const_tree);
3114 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3115 dw_loc_list_ref);
3116 static void add_data_member_location_attribute (dw_die_ref, tree);
3117 static bool add_const_value_attribute (dw_die_ref, rtx);
3118 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3119 static void insert_double (double_int, unsigned char *);
3120 static void insert_float (const_rtx, unsigned char *);
3121 static rtx rtl_for_decl_location (tree);
3122 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3123 enum dwarf_attribute);
3124 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3125 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3126 static void add_name_attribute (dw_die_ref, const char *);
3127 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3128 static void add_comp_dir_attribute (dw_die_ref);
3129 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3130 static void add_subscript_info (dw_die_ref, tree, bool);
3131 static void add_byte_size_attribute (dw_die_ref, tree);
3132 static void add_bit_offset_attribute (dw_die_ref, tree);
3133 static void add_bit_size_attribute (dw_die_ref, tree);
3134 static void add_prototyped_attribute (dw_die_ref, tree);
3135 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3136 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3137 static void add_src_coords_attributes (dw_die_ref, tree);
3138 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3139 static void push_decl_scope (tree);
3140 static void pop_decl_scope (void);
3141 static dw_die_ref scope_die_for (tree, dw_die_ref);
3142 static inline int local_scope_p (dw_die_ref);
3143 static inline int class_scope_p (dw_die_ref);
3144 static inline int class_or_namespace_scope_p (dw_die_ref);
3145 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3146 static void add_calling_convention_attribute (dw_die_ref, tree);
3147 static const char *type_tag (const_tree);
3148 static tree member_declared_type (const_tree);
3149 #if 0
3150 static const char *decl_start_label (tree);
3151 #endif
3152 static void gen_array_type_die (tree, dw_die_ref);
3153 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3154 #if 0
3155 static void gen_entry_point_die (tree, dw_die_ref);
3156 #endif
3157 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3158 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3159 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3160 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3161 static void gen_formal_types_die (tree, dw_die_ref);
3162 static void gen_subprogram_die (tree, dw_die_ref);
3163 static void gen_variable_die (tree, tree, dw_die_ref);
3164 static void gen_const_die (tree, dw_die_ref);
3165 static void gen_label_die (tree, dw_die_ref);
3166 static void gen_lexical_block_die (tree, dw_die_ref, int);
3167 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3168 static void gen_field_die (tree, dw_die_ref);
3169 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3170 static dw_die_ref gen_compile_unit_die (const char *);
3171 static void gen_inheritance_die (tree, tree, dw_die_ref);
3172 static void gen_member_die (tree, dw_die_ref);
3173 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3174 enum debug_info_usage);
3175 static void gen_subroutine_type_die (tree, dw_die_ref);
3176 static void gen_typedef_die (tree, dw_die_ref);
3177 static void gen_type_die (tree, dw_die_ref);
3178 static void gen_block_die (tree, dw_die_ref, int);
3179 static void decls_for_scope (tree, dw_die_ref, int);
3180 static inline int is_redundant_typedef (const_tree);
3181 static bool is_naming_typedef_decl (const_tree);
3182 static inline dw_die_ref get_context_die (tree);
3183 static void gen_namespace_die (tree, dw_die_ref);
3184 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3185 static dw_die_ref force_decl_die (tree);
3186 static dw_die_ref force_type_die (tree);
3187 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3188 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3189 static struct dwarf_file_data * lookup_filename (const char *);
3190 static void retry_incomplete_types (void);
3191 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3192 static void gen_generic_params_dies (tree);
3193 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3194 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3195 static void splice_child_die (dw_die_ref, dw_die_ref);
3196 static int file_info_cmp (const void *, const void *);
3197 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3198 const char *, const char *);
3199 static void output_loc_list (dw_loc_list_ref);
3200 static char *gen_internal_sym (const char *);
3201 static bool want_pubnames (void);
3202
3203 static void prune_unmark_dies (dw_die_ref);
3204 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3205 static void prune_unused_types_mark (dw_die_ref, int);
3206 static void prune_unused_types_walk (dw_die_ref);
3207 static void prune_unused_types_walk_attribs (dw_die_ref);
3208 static void prune_unused_types_prune (dw_die_ref);
3209 static void prune_unused_types (void);
3210 static int maybe_emit_file (struct dwarf_file_data *fd);
3211 static inline const char *AT_vms_delta1 (dw_attr_ref);
3212 static inline const char *AT_vms_delta2 (dw_attr_ref);
3213 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3214 const char *, const char *);
3215 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3216 static void gen_remaining_tmpl_value_param_die_attribute (void);
3217 static bool generic_type_p (tree);
3218 static void schedule_generic_params_dies_gen (tree t);
3219 static void gen_scheduled_generic_parms_dies (void);
3220
3221 static const char *comp_dir_string (void);
3222
3223 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3224
3225 /* enum for tracking thread-local variables whose address is really an offset
3226 relative to the TLS pointer, which will need link-time relocation, but will
3227 not need relocation by the DWARF consumer. */
3228
3229 enum dtprel_bool
3230 {
3231 dtprel_false = 0,
3232 dtprel_true = 1
3233 };
3234
3235 /* Return the operator to use for an address of a variable. For dtprel_true, we
3236 use DW_OP_const*. For regular variables, which need both link-time
3237 relocation and consumer-level relocation (e.g., to account for shared objects
3238 loaded at a random address), we use DW_OP_addr*. */
3239
3240 static inline enum dwarf_location_atom
3241 dw_addr_op (enum dtprel_bool dtprel)
3242 {
3243 if (dtprel == dtprel_true)
3244 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3245 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3246 else
3247 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3248 }
3249
3250 /* Return a pointer to a newly allocated address location description. If
3251 dwarf_split_debug_info is true, then record the address with the appropriate
3252 relocation. */
3253 static inline dw_loc_descr_ref
3254 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3255 {
3256 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3257
3258 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3259 ref->dw_loc_oprnd1.v.val_addr = addr;
3260 ref->dtprel = dtprel;
3261 if (dwarf_split_debug_info)
3262 ref->dw_loc_oprnd1.val_entry
3263 = add_addr_table_entry (addr,
3264 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3265 else
3266 ref->dw_loc_oprnd1.val_entry = NULL;
3267
3268 return ref;
3269 }
3270
3271 /* Section names used to hold DWARF debugging information. */
3272
3273 #ifndef DEBUG_INFO_SECTION
3274 #define DEBUG_INFO_SECTION ".debug_info"
3275 #endif
3276 #ifndef DEBUG_DWO_INFO_SECTION
3277 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3278 #endif
3279 #ifndef DEBUG_ABBREV_SECTION
3280 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3281 #endif
3282 #ifndef DEBUG_DWO_ABBREV_SECTION
3283 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3284 #endif
3285 #ifndef DEBUG_ARANGES_SECTION
3286 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3287 #endif
3288 #ifndef DEBUG_ADDR_SECTION
3289 #define DEBUG_ADDR_SECTION ".debug_addr"
3290 #endif
3291 #ifndef DEBUG_NORM_MACINFO_SECTION
3292 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3293 #endif
3294 #ifndef DEBUG_DWO_MACINFO_SECTION
3295 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3296 #endif
3297 #ifndef DEBUG_MACINFO_SECTION
3298 #define DEBUG_MACINFO_SECTION \
3299 (!dwarf_split_debug_info \
3300 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3301 #endif
3302 #ifndef DEBUG_NORM_MACRO_SECTION
3303 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3304 #endif
3305 #ifndef DEBUG_DWO_MACRO_SECTION
3306 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3307 #endif
3308 #ifndef DEBUG_MACRO_SECTION
3309 #define DEBUG_MACRO_SECTION \
3310 (!dwarf_split_debug_info \
3311 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3312 #endif
3313 #ifndef DEBUG_LINE_SECTION
3314 #define DEBUG_LINE_SECTION ".debug_line"
3315 #endif
3316 #ifndef DEBUG_DWO_LINE_SECTION
3317 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3318 #endif
3319 #ifndef DEBUG_LOC_SECTION
3320 #define DEBUG_LOC_SECTION ".debug_loc"
3321 #endif
3322 #ifndef DEBUG_DWO_LOC_SECTION
3323 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3324 #endif
3325 #ifndef DEBUG_PUBNAMES_SECTION
3326 #define DEBUG_PUBNAMES_SECTION \
3327 ((debug_generate_pub_sections == 2) \
3328 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3329 #endif
3330 #ifndef DEBUG_PUBTYPES_SECTION
3331 #define DEBUG_PUBTYPES_SECTION \
3332 ((debug_generate_pub_sections == 2) \
3333 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3334 #endif
3335 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3336 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3337 #ifndef DEBUG_STR_OFFSETS_SECTION
3338 #define DEBUG_STR_OFFSETS_SECTION \
3339 (!dwarf_split_debug_info \
3340 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3341 #endif
3342 #ifndef DEBUG_STR_DWO_SECTION
3343 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3344 #endif
3345 #ifndef DEBUG_STR_SECTION
3346 #define DEBUG_STR_SECTION ".debug_str"
3347 #endif
3348 #ifndef DEBUG_RANGES_SECTION
3349 #define DEBUG_RANGES_SECTION ".debug_ranges"
3350 #endif
3351
3352 /* Standard ELF section names for compiled code and data. */
3353 #ifndef TEXT_SECTION_NAME
3354 #define TEXT_SECTION_NAME ".text"
3355 #endif
3356
3357 /* Section flags for .debug_macinfo/.debug_macro section. */
3358 #define DEBUG_MACRO_SECTION_FLAGS \
3359 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3360
3361 /* Section flags for .debug_str section. */
3362 #define DEBUG_STR_SECTION_FLAGS \
3363 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3364 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3365 : SECTION_DEBUG)
3366
3367 /* Section flags for .debug_str.dwo section. */
3368 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3369
3370 /* Labels we insert at beginning sections we can reference instead of
3371 the section names themselves. */
3372
3373 #ifndef TEXT_SECTION_LABEL
3374 #define TEXT_SECTION_LABEL "Ltext"
3375 #endif
3376 #ifndef COLD_TEXT_SECTION_LABEL
3377 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3378 #endif
3379 #ifndef DEBUG_LINE_SECTION_LABEL
3380 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3381 #endif
3382 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3383 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3384 #endif
3385 #ifndef DEBUG_INFO_SECTION_LABEL
3386 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3387 #endif
3388 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3389 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3390 #endif
3391 #ifndef DEBUG_ABBREV_SECTION_LABEL
3392 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3393 #endif
3394 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3395 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3396 #endif
3397 #ifndef DEBUG_ADDR_SECTION_LABEL
3398 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3399 #endif
3400 #ifndef DEBUG_LOC_SECTION_LABEL
3401 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3402 #endif
3403 #ifndef DEBUG_RANGES_SECTION_LABEL
3404 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3405 #endif
3406 #ifndef DEBUG_MACINFO_SECTION_LABEL
3407 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3408 #endif
3409 #ifndef DEBUG_MACRO_SECTION_LABEL
3410 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3411 #endif
3412 #define SKELETON_COMP_DIE_ABBREV 1
3413 #define SKELETON_TYPE_DIE_ABBREV 2
3414
3415 /* Definitions of defaults for formats and names of various special
3416 (artificial) labels which may be generated within this file (when the -g
3417 options is used and DWARF2_DEBUGGING_INFO is in effect.
3418 If necessary, these may be overridden from within the tm.h file, but
3419 typically, overriding these defaults is unnecessary. */
3420
3421 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3422 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3423 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3424 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3425 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3426 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3427 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3428 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3429 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3430 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3431 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3432 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3433 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3434 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3435
3436 #ifndef TEXT_END_LABEL
3437 #define TEXT_END_LABEL "Letext"
3438 #endif
3439 #ifndef COLD_END_LABEL
3440 #define COLD_END_LABEL "Letext_cold"
3441 #endif
3442 #ifndef BLOCK_BEGIN_LABEL
3443 #define BLOCK_BEGIN_LABEL "LBB"
3444 #endif
3445 #ifndef BLOCK_END_LABEL
3446 #define BLOCK_END_LABEL "LBE"
3447 #endif
3448 #ifndef LINE_CODE_LABEL
3449 #define LINE_CODE_LABEL "LM"
3450 #endif
3451
3452 \f
3453 /* Return the root of the DIE's built for the current compilation unit. */
3454 static dw_die_ref
3455 comp_unit_die (void)
3456 {
3457 if (!single_comp_unit_die)
3458 single_comp_unit_die = gen_compile_unit_die (NULL);
3459 return single_comp_unit_die;
3460 }
3461
3462 /* We allow a language front-end to designate a function that is to be
3463 called to "demangle" any name before it is put into a DIE. */
3464
3465 static const char *(*demangle_name_func) (const char *);
3466
3467 void
3468 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3469 {
3470 demangle_name_func = func;
3471 }
3472
3473 /* Test if rtl node points to a pseudo register. */
3474
3475 static inline int
3476 is_pseudo_reg (const_rtx rtl)
3477 {
3478 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3479 || (GET_CODE (rtl) == SUBREG
3480 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3481 }
3482
3483 /* Return a reference to a type, with its const and volatile qualifiers
3484 removed. */
3485
3486 static inline tree
3487 type_main_variant (tree type)
3488 {
3489 type = TYPE_MAIN_VARIANT (type);
3490
3491 /* ??? There really should be only one main variant among any group of
3492 variants of a given type (and all of the MAIN_VARIANT values for all
3493 members of the group should point to that one type) but sometimes the C
3494 front-end messes this up for array types, so we work around that bug
3495 here. */
3496 if (TREE_CODE (type) == ARRAY_TYPE)
3497 while (type != TYPE_MAIN_VARIANT (type))
3498 type = TYPE_MAIN_VARIANT (type);
3499
3500 return type;
3501 }
3502
3503 /* Return nonzero if the given type node represents a tagged type. */
3504
3505 static inline int
3506 is_tagged_type (const_tree type)
3507 {
3508 enum tree_code code = TREE_CODE (type);
3509
3510 return (code == RECORD_TYPE || code == UNION_TYPE
3511 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3512 }
3513
3514 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3515
3516 static void
3517 get_ref_die_offset_label (char *label, dw_die_ref ref)
3518 {
3519 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3520 }
3521
3522 /* Return die_offset of a DIE reference to a base type. */
3523
3524 static unsigned long int
3525 get_base_type_offset (dw_die_ref ref)
3526 {
3527 if (ref->die_offset)
3528 return ref->die_offset;
3529 if (comp_unit_die ()->die_abbrev)
3530 {
3531 calc_base_type_die_sizes ();
3532 gcc_assert (ref->die_offset);
3533 }
3534 return ref->die_offset;
3535 }
3536
3537 /* Return die_offset of a DIE reference other than base type. */
3538
3539 static unsigned long int
3540 get_ref_die_offset (dw_die_ref ref)
3541 {
3542 gcc_assert (ref->die_offset);
3543 return ref->die_offset;
3544 }
3545
3546 /* Convert a DIE tag into its string name. */
3547
3548 static const char *
3549 dwarf_tag_name (unsigned int tag)
3550 {
3551 const char *name = get_DW_TAG_name (tag);
3552
3553 if (name != NULL)
3554 return name;
3555
3556 return "DW_TAG_<unknown>";
3557 }
3558
3559 /* Convert a DWARF attribute code into its string name. */
3560
3561 static const char *
3562 dwarf_attr_name (unsigned int attr)
3563 {
3564 const char *name;
3565
3566 switch (attr)
3567 {
3568 #if VMS_DEBUGGING_INFO
3569 case DW_AT_HP_prologue:
3570 return "DW_AT_HP_prologue";
3571 #else
3572 case DW_AT_MIPS_loop_unroll_factor:
3573 return "DW_AT_MIPS_loop_unroll_factor";
3574 #endif
3575
3576 #if VMS_DEBUGGING_INFO
3577 case DW_AT_HP_epilogue:
3578 return "DW_AT_HP_epilogue";
3579 #else
3580 case DW_AT_MIPS_stride:
3581 return "DW_AT_MIPS_stride";
3582 #endif
3583 }
3584
3585 name = get_DW_AT_name (attr);
3586
3587 if (name != NULL)
3588 return name;
3589
3590 return "DW_AT_<unknown>";
3591 }
3592
3593 /* Convert a DWARF value form code into its string name. */
3594
3595 static const char *
3596 dwarf_form_name (unsigned int form)
3597 {
3598 const char *name = get_DW_FORM_name (form);
3599
3600 if (name != NULL)
3601 return name;
3602
3603 return "DW_FORM_<unknown>";
3604 }
3605 \f
3606 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3607 instance of an inlined instance of a decl which is local to an inline
3608 function, so we have to trace all of the way back through the origin chain
3609 to find out what sort of node actually served as the original seed for the
3610 given block. */
3611
3612 static tree
3613 decl_ultimate_origin (const_tree decl)
3614 {
3615 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3616 return NULL_TREE;
3617
3618 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3619 nodes in the function to point to themselves; ignore that if
3620 we're trying to output the abstract instance of this function. */
3621 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3622 return NULL_TREE;
3623
3624 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3625 most distant ancestor, this should never happen. */
3626 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3627
3628 return DECL_ABSTRACT_ORIGIN (decl);
3629 }
3630
3631 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3632 of a virtual function may refer to a base class, so we check the 'this'
3633 parameter. */
3634
3635 static tree
3636 decl_class_context (tree decl)
3637 {
3638 tree context = NULL_TREE;
3639
3640 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3641 context = DECL_CONTEXT (decl);
3642 else
3643 context = TYPE_MAIN_VARIANT
3644 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3645
3646 if (context && !TYPE_P (context))
3647 context = NULL_TREE;
3648
3649 return context;
3650 }
3651 \f
3652 /* Add an attribute/value pair to a DIE. */
3653
3654 static inline void
3655 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3656 {
3657 /* Maybe this should be an assert? */
3658 if (die == NULL)
3659 return;
3660
3661 vec_safe_reserve (die->die_attr, 1);
3662 vec_safe_push (die->die_attr, *attr);
3663 }
3664
3665 static inline enum dw_val_class
3666 AT_class (dw_attr_ref a)
3667 {
3668 return a->dw_attr_val.val_class;
3669 }
3670
3671 /* Return the index for any attribute that will be referenced with a
3672 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3673 are stored in dw_attr_val.v.val_str for reference counting
3674 pruning. */
3675
3676 static inline unsigned int
3677 AT_index (dw_attr_ref a)
3678 {
3679 if (AT_class (a) == dw_val_class_str)
3680 return a->dw_attr_val.v.val_str->index;
3681 else if (a->dw_attr_val.val_entry != NULL)
3682 return a->dw_attr_val.val_entry->index;
3683 return NOT_INDEXED;
3684 }
3685
3686 /* Add a flag value attribute to a DIE. */
3687
3688 static inline void
3689 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3690 {
3691 dw_attr_node attr;
3692
3693 attr.dw_attr = attr_kind;
3694 attr.dw_attr_val.val_class = dw_val_class_flag;
3695 attr.dw_attr_val.val_entry = NULL;
3696 attr.dw_attr_val.v.val_flag = flag;
3697 add_dwarf_attr (die, &attr);
3698 }
3699
3700 static inline unsigned
3701 AT_flag (dw_attr_ref a)
3702 {
3703 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3704 return a->dw_attr_val.v.val_flag;
3705 }
3706
3707 /* Add a signed integer attribute value to a DIE. */
3708
3709 static inline void
3710 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3711 {
3712 dw_attr_node attr;
3713
3714 attr.dw_attr = attr_kind;
3715 attr.dw_attr_val.val_class = dw_val_class_const;
3716 attr.dw_attr_val.val_entry = NULL;
3717 attr.dw_attr_val.v.val_int = int_val;
3718 add_dwarf_attr (die, &attr);
3719 }
3720
3721 static inline HOST_WIDE_INT
3722 AT_int (dw_attr_ref a)
3723 {
3724 gcc_assert (a && AT_class (a) == dw_val_class_const);
3725 return a->dw_attr_val.v.val_int;
3726 }
3727
3728 /* Add an unsigned integer attribute value to a DIE. */
3729
3730 static inline void
3731 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3732 unsigned HOST_WIDE_INT unsigned_val)
3733 {
3734 dw_attr_node attr;
3735
3736 attr.dw_attr = attr_kind;
3737 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3738 attr.dw_attr_val.val_entry = NULL;
3739 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3740 add_dwarf_attr (die, &attr);
3741 }
3742
3743 static inline unsigned HOST_WIDE_INT
3744 AT_unsigned (dw_attr_ref a)
3745 {
3746 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3747 return a->dw_attr_val.v.val_unsigned;
3748 }
3749
3750 /* Add an unsigned double integer attribute value to a DIE. */
3751
3752 static inline void
3753 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3754 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3755 {
3756 dw_attr_node attr;
3757
3758 attr.dw_attr = attr_kind;
3759 attr.dw_attr_val.val_class = dw_val_class_const_double;
3760 attr.dw_attr_val.val_entry = NULL;
3761 attr.dw_attr_val.v.val_double.high = high;
3762 attr.dw_attr_val.v.val_double.low = low;
3763 add_dwarf_attr (die, &attr);
3764 }
3765
3766 /* Add a floating point attribute value to a DIE and return it. */
3767
3768 static inline void
3769 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3770 unsigned int length, unsigned int elt_size, unsigned char *array)
3771 {
3772 dw_attr_node attr;
3773
3774 attr.dw_attr = attr_kind;
3775 attr.dw_attr_val.val_class = dw_val_class_vec;
3776 attr.dw_attr_val.val_entry = NULL;
3777 attr.dw_attr_val.v.val_vec.length = length;
3778 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3779 attr.dw_attr_val.v.val_vec.array = array;
3780 add_dwarf_attr (die, &attr);
3781 }
3782
3783 /* Add an 8-byte data attribute value to a DIE. */
3784
3785 static inline void
3786 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3787 unsigned char data8[8])
3788 {
3789 dw_attr_node attr;
3790
3791 attr.dw_attr = attr_kind;
3792 attr.dw_attr_val.val_class = dw_val_class_data8;
3793 attr.dw_attr_val.val_entry = NULL;
3794 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3795 add_dwarf_attr (die, &attr);
3796 }
3797
3798 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3799 dwarf_split_debug_info, address attributes in dies destined for the
3800 final executable have force_direct set to avoid using indexed
3801 references. */
3802
3803 static inline void
3804 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3805 bool force_direct)
3806 {
3807 dw_attr_node attr;
3808 char * lbl_id;
3809
3810 lbl_id = xstrdup (lbl_low);
3811 attr.dw_attr = DW_AT_low_pc;
3812 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3813 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3814 if (dwarf_split_debug_info && !force_direct)
3815 attr.dw_attr_val.val_entry
3816 = add_addr_table_entry (lbl_id, ate_kind_label);
3817 else
3818 attr.dw_attr_val.val_entry = NULL;
3819 add_dwarf_attr (die, &attr);
3820
3821 attr.dw_attr = DW_AT_high_pc;
3822 if (dwarf_version < 4)
3823 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3824 else
3825 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3826 lbl_id = xstrdup (lbl_high);
3827 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3828 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3829 && dwarf_split_debug_info && !force_direct)
3830 attr.dw_attr_val.val_entry
3831 = add_addr_table_entry (lbl_id, ate_kind_label);
3832 else
3833 attr.dw_attr_val.val_entry = NULL;
3834 add_dwarf_attr (die, &attr);
3835 }
3836
3837 /* Hash and equality functions for debug_str_hash. */
3838
3839 static hashval_t
3840 debug_str_do_hash (const void *x)
3841 {
3842 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3843 }
3844
3845 static int
3846 debug_str_eq (const void *x1, const void *x2)
3847 {
3848 return strcmp ((((const struct indirect_string_node *)x1)->str),
3849 (const char *)x2) == 0;
3850 }
3851
3852 /* Add STR to the given string hash table. */
3853
3854 static struct indirect_string_node *
3855 find_AT_string_in_table (const char *str, htab_t table)
3856 {
3857 struct indirect_string_node *node;
3858 void **slot;
3859
3860 slot = htab_find_slot_with_hash (table, str,
3861 htab_hash_string (str), INSERT);
3862 if (*slot == NULL)
3863 {
3864 node = ggc_alloc_cleared_indirect_string_node ();
3865 node->str = ggc_strdup (str);
3866 *slot = node;
3867 }
3868 else
3869 node = (struct indirect_string_node *) *slot;
3870
3871 node->refcount++;
3872 return node;
3873 }
3874
3875 /* Add STR to the indirect string hash table. */
3876
3877 static struct indirect_string_node *
3878 find_AT_string (const char *str)
3879 {
3880 if (! debug_str_hash)
3881 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3882 debug_str_eq, NULL);
3883
3884 return find_AT_string_in_table (str, debug_str_hash);
3885 }
3886
3887 /* Add a string attribute value to a DIE. */
3888
3889 static inline void
3890 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3891 {
3892 dw_attr_node attr;
3893 struct indirect_string_node *node;
3894
3895 node = find_AT_string (str);
3896
3897 attr.dw_attr = attr_kind;
3898 attr.dw_attr_val.val_class = dw_val_class_str;
3899 attr.dw_attr_val.val_entry = NULL;
3900 attr.dw_attr_val.v.val_str = node;
3901 add_dwarf_attr (die, &attr);
3902 }
3903
3904 static inline const char *
3905 AT_string (dw_attr_ref a)
3906 {
3907 gcc_assert (a && AT_class (a) == dw_val_class_str);
3908 return a->dw_attr_val.v.val_str->str;
3909 }
3910
3911 /* Call this function directly to bypass AT_string_form's logic to put
3912 the string inline in the die. */
3913
3914 static void
3915 set_indirect_string (struct indirect_string_node *node)
3916 {
3917 char label[32];
3918 /* Already indirect is a no op. */
3919 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3920 {
3921 gcc_assert (node->label);
3922 return;
3923 }
3924 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3925 ++dw2_string_counter;
3926 node->label = xstrdup (label);
3927
3928 if (!dwarf_split_debug_info)
3929 {
3930 node->form = DW_FORM_strp;
3931 node->index = NOT_INDEXED;
3932 }
3933 else
3934 {
3935 node->form = DW_FORM_GNU_str_index;
3936 node->index = NO_INDEX_ASSIGNED;
3937 }
3938 }
3939
3940 /* Find out whether a string should be output inline in DIE
3941 or out-of-line in .debug_str section. */
3942
3943 static enum dwarf_form
3944 find_string_form (struct indirect_string_node *node)
3945 {
3946 unsigned int len;
3947
3948 if (node->form)
3949 return node->form;
3950
3951 len = strlen (node->str) + 1;
3952
3953 /* If the string is shorter or equal to the size of the reference, it is
3954 always better to put it inline. */
3955 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3956 return node->form = DW_FORM_string;
3957
3958 /* If we cannot expect the linker to merge strings in .debug_str
3959 section, only put it into .debug_str if it is worth even in this
3960 single module. */
3961 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3962 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3963 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3964 return node->form = DW_FORM_string;
3965
3966 set_indirect_string (node);
3967
3968 return node->form;
3969 }
3970
3971 /* Find out whether the string referenced from the attribute should be
3972 output inline in DIE or out-of-line in .debug_str section. */
3973
3974 static enum dwarf_form
3975 AT_string_form (dw_attr_ref a)
3976 {
3977 gcc_assert (a && AT_class (a) == dw_val_class_str);
3978 return find_string_form (a->dw_attr_val.v.val_str);
3979 }
3980
3981 /* Add a DIE reference attribute value to a DIE. */
3982
3983 static inline void
3984 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3985 {
3986 dw_attr_node attr;
3987
3988 #ifdef ENABLE_CHECKING
3989 gcc_assert (targ_die != NULL);
3990 #else
3991 /* With LTO we can end up trying to reference something we didn't create
3992 a DIE for. Avoid crashing later on a NULL referenced DIE. */
3993 if (targ_die == NULL)
3994 return;
3995 #endif
3996
3997 attr.dw_attr = attr_kind;
3998 attr.dw_attr_val.val_class = dw_val_class_die_ref;
3999 attr.dw_attr_val.val_entry = NULL;
4000 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4001 attr.dw_attr_val.v.val_die_ref.external = 0;
4002 add_dwarf_attr (die, &attr);
4003 }
4004
4005 /* Change DIE reference REF to point to NEW_DIE instead. */
4006
4007 static inline void
4008 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4009 {
4010 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4011 ref->dw_attr_val.v.val_die_ref.die = new_die;
4012 ref->dw_attr_val.v.val_die_ref.external = 0;
4013 }
4014
4015 /* Add an AT_specification attribute to a DIE, and also make the back
4016 pointer from the specification to the definition. */
4017
4018 static inline void
4019 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4020 {
4021 add_AT_die_ref (die, DW_AT_specification, targ_die);
4022 gcc_assert (!targ_die->die_definition);
4023 targ_die->die_definition = die;
4024 }
4025
4026 static inline dw_die_ref
4027 AT_ref (dw_attr_ref a)
4028 {
4029 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4030 return a->dw_attr_val.v.val_die_ref.die;
4031 }
4032
4033 static inline int
4034 AT_ref_external (dw_attr_ref a)
4035 {
4036 if (a && AT_class (a) == dw_val_class_die_ref)
4037 return a->dw_attr_val.v.val_die_ref.external;
4038
4039 return 0;
4040 }
4041
4042 static inline void
4043 set_AT_ref_external (dw_attr_ref a, int i)
4044 {
4045 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4046 a->dw_attr_val.v.val_die_ref.external = i;
4047 }
4048
4049 /* Add an FDE reference attribute value to a DIE. */
4050
4051 static inline void
4052 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4053 {
4054 dw_attr_node attr;
4055
4056 attr.dw_attr = attr_kind;
4057 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4058 attr.dw_attr_val.val_entry = NULL;
4059 attr.dw_attr_val.v.val_fde_index = targ_fde;
4060 add_dwarf_attr (die, &attr);
4061 }
4062
4063 /* Add a location description attribute value to a DIE. */
4064
4065 static inline void
4066 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4067 {
4068 dw_attr_node attr;
4069
4070 attr.dw_attr = attr_kind;
4071 attr.dw_attr_val.val_class = dw_val_class_loc;
4072 attr.dw_attr_val.val_entry = NULL;
4073 attr.dw_attr_val.v.val_loc = loc;
4074 add_dwarf_attr (die, &attr);
4075 }
4076
4077 static inline dw_loc_descr_ref
4078 AT_loc (dw_attr_ref a)
4079 {
4080 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4081 return a->dw_attr_val.v.val_loc;
4082 }
4083
4084 static inline void
4085 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4086 {
4087 dw_attr_node attr;
4088
4089 attr.dw_attr = attr_kind;
4090 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4091 attr.dw_attr_val.val_entry = NULL;
4092 attr.dw_attr_val.v.val_loc_list = loc_list;
4093 add_dwarf_attr (die, &attr);
4094 have_location_lists = true;
4095 }
4096
4097 static inline dw_loc_list_ref
4098 AT_loc_list (dw_attr_ref a)
4099 {
4100 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4101 return a->dw_attr_val.v.val_loc_list;
4102 }
4103
4104 static inline dw_loc_list_ref *
4105 AT_loc_list_ptr (dw_attr_ref a)
4106 {
4107 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4108 return &a->dw_attr_val.v.val_loc_list;
4109 }
4110
4111 /* Table of entries into the .debug_addr section. */
4112
4113 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4114
4115 /* Hash an address_table_entry. */
4116
4117 static hashval_t
4118 addr_table_entry_do_hash (const void *x)
4119 {
4120 const addr_table_entry *a = (const addr_table_entry *) x;
4121 switch (a->kind)
4122 {
4123 case ate_kind_rtx:
4124 return iterative_hash_rtx (a->addr.rtl, 0);
4125 case ate_kind_rtx_dtprel:
4126 return iterative_hash_rtx (a->addr.rtl, 1);
4127 case ate_kind_label:
4128 return htab_hash_string (a->addr.label);
4129 default:
4130 gcc_unreachable ();
4131 }
4132 }
4133
4134 /* Determine equality for two address_table_entries. */
4135
4136 static int
4137 addr_table_entry_eq (const void *x1, const void *x2)
4138 {
4139 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4140 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4141
4142 if (a1->kind != a2->kind)
4143 return 0;
4144 switch (a1->kind)
4145 {
4146 case ate_kind_rtx:
4147 case ate_kind_rtx_dtprel:
4148 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4149 case ate_kind_label:
4150 return strcmp (a1->addr.label, a2->addr.label) == 0;
4151 default:
4152 gcc_unreachable ();
4153 }
4154 }
4155
4156 /* Initialize an addr_table_entry. */
4157
4158 void
4159 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4160 {
4161 e->kind = kind;
4162 switch (kind)
4163 {
4164 case ate_kind_rtx:
4165 case ate_kind_rtx_dtprel:
4166 e->addr.rtl = (rtx) addr;
4167 break;
4168 case ate_kind_label:
4169 e->addr.label = (char *) addr;
4170 break;
4171 }
4172 e->refcount = 0;
4173 e->index = NO_INDEX_ASSIGNED;
4174 }
4175
4176 /* Add attr to the address table entry to the table. Defer setting an
4177 index until output time. */
4178
4179 static addr_table_entry *
4180 add_addr_table_entry (void *addr, enum ate_kind kind)
4181 {
4182 addr_table_entry *node;
4183 addr_table_entry finder;
4184 void **slot;
4185
4186 gcc_assert (dwarf_split_debug_info);
4187 if (! addr_index_table)
4188 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4189 addr_table_entry_eq, NULL);
4190 init_addr_table_entry (&finder, kind, addr);
4191 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4192
4193 if (*slot == HTAB_EMPTY_ENTRY)
4194 {
4195 node = ggc_alloc_cleared_addr_table_entry ();
4196 init_addr_table_entry (node, kind, addr);
4197 *slot = node;
4198 }
4199 else
4200 node = (addr_table_entry *) *slot;
4201
4202 node->refcount++;
4203 return node;
4204 }
4205
4206 /* Remove an entry from the addr table by decrementing its refcount.
4207 Strictly, decrementing the refcount would be enough, but the
4208 assertion that the entry is actually in the table has found
4209 bugs. */
4210
4211 static void
4212 remove_addr_table_entry (addr_table_entry *entry)
4213 {
4214 addr_table_entry *node;
4215
4216 gcc_assert (dwarf_split_debug_info && addr_index_table);
4217 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4218 /* After an index is assigned, the table is frozen. */
4219 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4220 node->refcount--;
4221 }
4222
4223 /* Given a location list, remove all addresses it refers to from the
4224 address_table. */
4225
4226 static void
4227 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4228 {
4229 for (; descr; descr = descr->dw_loc_next)
4230 if (descr->dw_loc_oprnd1.val_entry != NULL)
4231 {
4232 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4233 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4234 }
4235 }
4236
4237 /* A helper function for dwarf2out_finish called through
4238 htab_traverse. Assign an addr_table_entry its index. All entries
4239 must be collected into the table when this function is called,
4240 because the indexing code relies on htab_traverse to traverse nodes
4241 in the same order for each run. */
4242
4243 static int
4244 index_addr_table_entry (void **h, void *v)
4245 {
4246 addr_table_entry *node = (addr_table_entry *) *h;
4247 unsigned int *index = (unsigned int *) v;
4248
4249 /* Don't index unreferenced nodes. */
4250 if (node->refcount == 0)
4251 return 1;
4252
4253 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4254 node->index = *index;
4255 *index += 1;
4256
4257 return 1;
4258 }
4259
4260 /* Add an address constant attribute value to a DIE. When using
4261 dwarf_split_debug_info, address attributes in dies destined for the
4262 final executable should be direct references--setting the parameter
4263 force_direct ensures this behavior. */
4264
4265 static inline void
4266 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4267 bool force_direct)
4268 {
4269 dw_attr_node attr;
4270
4271 attr.dw_attr = attr_kind;
4272 attr.dw_attr_val.val_class = dw_val_class_addr;
4273 attr.dw_attr_val.v.val_addr = addr;
4274 if (dwarf_split_debug_info && !force_direct)
4275 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4276 else
4277 attr.dw_attr_val.val_entry = NULL;
4278 add_dwarf_attr (die, &attr);
4279 }
4280
4281 /* Get the RTX from to an address DIE attribute. */
4282
4283 static inline rtx
4284 AT_addr (dw_attr_ref a)
4285 {
4286 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4287 return a->dw_attr_val.v.val_addr;
4288 }
4289
4290 /* Add a file attribute value to a DIE. */
4291
4292 static inline void
4293 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4294 struct dwarf_file_data *fd)
4295 {
4296 dw_attr_node attr;
4297
4298 attr.dw_attr = attr_kind;
4299 attr.dw_attr_val.val_class = dw_val_class_file;
4300 attr.dw_attr_val.val_entry = NULL;
4301 attr.dw_attr_val.v.val_file = fd;
4302 add_dwarf_attr (die, &attr);
4303 }
4304
4305 /* Get the dwarf_file_data from a file DIE attribute. */
4306
4307 static inline struct dwarf_file_data *
4308 AT_file (dw_attr_ref a)
4309 {
4310 gcc_assert (a && AT_class (a) == dw_val_class_file);
4311 return a->dw_attr_val.v.val_file;
4312 }
4313
4314 /* Add a vms delta attribute value to a DIE. */
4315
4316 static inline void
4317 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4318 const char *lbl1, const char *lbl2)
4319 {
4320 dw_attr_node attr;
4321
4322 attr.dw_attr = attr_kind;
4323 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4324 attr.dw_attr_val.val_entry = NULL;
4325 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4326 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4327 add_dwarf_attr (die, &attr);
4328 }
4329
4330 /* Add a label identifier attribute value to a DIE. */
4331
4332 static inline void
4333 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4334 const char *lbl_id)
4335 {
4336 dw_attr_node attr;
4337
4338 attr.dw_attr = attr_kind;
4339 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4340 attr.dw_attr_val.val_entry = NULL;
4341 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4342 if (dwarf_split_debug_info)
4343 attr.dw_attr_val.val_entry
4344 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4345 ate_kind_label);
4346 add_dwarf_attr (die, &attr);
4347 }
4348
4349 /* Add a section offset attribute value to a DIE, an offset into the
4350 debug_line section. */
4351
4352 static inline void
4353 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4354 const char *label)
4355 {
4356 dw_attr_node attr;
4357
4358 attr.dw_attr = attr_kind;
4359 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4360 attr.dw_attr_val.val_entry = NULL;
4361 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4362 add_dwarf_attr (die, &attr);
4363 }
4364
4365 /* Add a section offset attribute value to a DIE, an offset into the
4366 debug_macinfo section. */
4367
4368 static inline void
4369 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4370 const char *label)
4371 {
4372 dw_attr_node attr;
4373
4374 attr.dw_attr = attr_kind;
4375 attr.dw_attr_val.val_class = dw_val_class_macptr;
4376 attr.dw_attr_val.val_entry = NULL;
4377 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4378 add_dwarf_attr (die, &attr);
4379 }
4380
4381 /* Add an offset attribute value to a DIE. */
4382
4383 static inline void
4384 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4385 unsigned HOST_WIDE_INT offset)
4386 {
4387 dw_attr_node attr;
4388
4389 attr.dw_attr = attr_kind;
4390 attr.dw_attr_val.val_class = dw_val_class_offset;
4391 attr.dw_attr_val.val_entry = NULL;
4392 attr.dw_attr_val.v.val_offset = offset;
4393 add_dwarf_attr (die, &attr);
4394 }
4395
4396 /* Add a range_list attribute value to a DIE. When using
4397 dwarf_split_debug_info, address attributes in dies destined for the
4398 final executable should be direct references--setting the parameter
4399 force_direct ensures this behavior. */
4400
4401 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4402 #define RELOCATED_OFFSET (NULL)
4403
4404 static void
4405 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4406 long unsigned int offset, bool force_direct)
4407 {
4408 dw_attr_node attr;
4409
4410 attr.dw_attr = attr_kind;
4411 attr.dw_attr_val.val_class = dw_val_class_range_list;
4412 /* For the range_list attribute, use val_entry to store whether the
4413 offset should follow split-debug-info or normal semantics. This
4414 value is read in output_range_list_offset. */
4415 if (dwarf_split_debug_info && !force_direct)
4416 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4417 else
4418 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4419 attr.dw_attr_val.v.val_offset = offset;
4420 add_dwarf_attr (die, &attr);
4421 }
4422
4423 /* Return the start label of a delta attribute. */
4424
4425 static inline const char *
4426 AT_vms_delta1 (dw_attr_ref a)
4427 {
4428 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4429 return a->dw_attr_val.v.val_vms_delta.lbl1;
4430 }
4431
4432 /* Return the end label of a delta attribute. */
4433
4434 static inline const char *
4435 AT_vms_delta2 (dw_attr_ref a)
4436 {
4437 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4438 return a->dw_attr_val.v.val_vms_delta.lbl2;
4439 }
4440
4441 static inline const char *
4442 AT_lbl (dw_attr_ref a)
4443 {
4444 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4445 || AT_class (a) == dw_val_class_lineptr
4446 || AT_class (a) == dw_val_class_macptr
4447 || AT_class (a) == dw_val_class_high_pc));
4448 return a->dw_attr_val.v.val_lbl_id;
4449 }
4450
4451 /* Get the attribute of type attr_kind. */
4452
4453 static dw_attr_ref
4454 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4455 {
4456 dw_attr_ref a;
4457 unsigned ix;
4458 dw_die_ref spec = NULL;
4459
4460 if (! die)
4461 return NULL;
4462
4463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4464 if (a->dw_attr == attr_kind)
4465 return a;
4466 else if (a->dw_attr == DW_AT_specification
4467 || a->dw_attr == DW_AT_abstract_origin)
4468 spec = AT_ref (a);
4469
4470 if (spec)
4471 return get_AT (spec, attr_kind);
4472
4473 return NULL;
4474 }
4475
4476 /* Returns the parent of the declaration of DIE. */
4477
4478 static dw_die_ref
4479 get_die_parent (dw_die_ref die)
4480 {
4481 dw_die_ref t;
4482
4483 if (!die)
4484 return NULL;
4485
4486 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4487 || (t = get_AT_ref (die, DW_AT_specification)))
4488 die = t;
4489
4490 return die->die_parent;
4491 }
4492
4493 /* Return the "low pc" attribute value, typically associated with a subprogram
4494 DIE. Return null if the "low pc" attribute is either not present, or if it
4495 cannot be represented as an assembler label identifier. */
4496
4497 static inline const char *
4498 get_AT_low_pc (dw_die_ref die)
4499 {
4500 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4501
4502 return a ? AT_lbl (a) : NULL;
4503 }
4504
4505 /* Return the "high pc" attribute value, typically associated with a subprogram
4506 DIE. Return null if the "high pc" attribute is either not present, or if it
4507 cannot be represented as an assembler label identifier. */
4508
4509 static inline const char *
4510 get_AT_hi_pc (dw_die_ref die)
4511 {
4512 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4513
4514 return a ? AT_lbl (a) : NULL;
4515 }
4516
4517 /* Return the value of the string attribute designated by ATTR_KIND, or
4518 NULL if it is not present. */
4519
4520 static inline const char *
4521 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 {
4523 dw_attr_ref a = get_AT (die, attr_kind);
4524
4525 return a ? AT_string (a) : NULL;
4526 }
4527
4528 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4529 if it is not present. */
4530
4531 static inline int
4532 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4533 {
4534 dw_attr_ref a = get_AT (die, attr_kind);
4535
4536 return a ? AT_flag (a) : 0;
4537 }
4538
4539 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4540 if it is not present. */
4541
4542 static inline unsigned
4543 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4544 {
4545 dw_attr_ref a = get_AT (die, attr_kind);
4546
4547 return a ? AT_unsigned (a) : 0;
4548 }
4549
4550 static inline dw_die_ref
4551 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4552 {
4553 dw_attr_ref a = get_AT (die, attr_kind);
4554
4555 return a ? AT_ref (a) : NULL;
4556 }
4557
4558 static inline struct dwarf_file_data *
4559 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4560 {
4561 dw_attr_ref a = get_AT (die, attr_kind);
4562
4563 return a ? AT_file (a) : NULL;
4564 }
4565
4566 /* Return TRUE if the language is C++. */
4567
4568 static inline bool
4569 is_cxx (void)
4570 {
4571 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4572
4573 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4574 }
4575
4576 /* Return TRUE if the language is Java. */
4577
4578 static inline bool
4579 is_java (void)
4580 {
4581 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4582
4583 return lang == DW_LANG_Java;
4584 }
4585
4586 /* Return TRUE if the language is Fortran. */
4587
4588 static inline bool
4589 is_fortran (void)
4590 {
4591 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4592
4593 return (lang == DW_LANG_Fortran77
4594 || lang == DW_LANG_Fortran90
4595 || lang == DW_LANG_Fortran95);
4596 }
4597
4598 /* Return TRUE if the language is Ada. */
4599
4600 static inline bool
4601 is_ada (void)
4602 {
4603 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4604
4605 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4606 }
4607
4608 /* Remove the specified attribute if present. */
4609
4610 static void
4611 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4612 {
4613 dw_attr_ref a;
4614 unsigned ix;
4615
4616 if (! die)
4617 return;
4618
4619 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4620 if (a->dw_attr == attr_kind)
4621 {
4622 if (AT_class (a) == dw_val_class_str)
4623 if (a->dw_attr_val.v.val_str->refcount)
4624 a->dw_attr_val.v.val_str->refcount--;
4625
4626 /* vec::ordered_remove should help reduce the number of abbrevs
4627 that are needed. */
4628 die->die_attr->ordered_remove (ix);
4629 return;
4630 }
4631 }
4632
4633 /* Remove CHILD from its parent. PREV must have the property that
4634 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4635
4636 static void
4637 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4638 {
4639 gcc_assert (child->die_parent == prev->die_parent);
4640 gcc_assert (prev->die_sib == child);
4641 if (prev == child)
4642 {
4643 gcc_assert (child->die_parent->die_child == child);
4644 prev = NULL;
4645 }
4646 else
4647 prev->die_sib = child->die_sib;
4648 if (child->die_parent->die_child == child)
4649 child->die_parent->die_child = prev;
4650 }
4651
4652 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4653 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4654
4655 static void
4656 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4657 {
4658 dw_die_ref parent = old_child->die_parent;
4659
4660 gcc_assert (parent == prev->die_parent);
4661 gcc_assert (prev->die_sib == old_child);
4662
4663 new_child->die_parent = parent;
4664 if (prev == old_child)
4665 {
4666 gcc_assert (parent->die_child == old_child);
4667 new_child->die_sib = new_child;
4668 }
4669 else
4670 {
4671 prev->die_sib = new_child;
4672 new_child->die_sib = old_child->die_sib;
4673 }
4674 if (old_child->die_parent->die_child == old_child)
4675 old_child->die_parent->die_child = new_child;
4676 }
4677
4678 /* Move all children from OLD_PARENT to NEW_PARENT. */
4679
4680 static void
4681 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4682 {
4683 dw_die_ref c;
4684 new_parent->die_child = old_parent->die_child;
4685 old_parent->die_child = NULL;
4686 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4687 }
4688
4689 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4690 matches TAG. */
4691
4692 static void
4693 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4694 {
4695 dw_die_ref c;
4696
4697 c = die->die_child;
4698 if (c) do {
4699 dw_die_ref prev = c;
4700 c = c->die_sib;
4701 while (c->die_tag == tag)
4702 {
4703 remove_child_with_prev (c, prev);
4704 /* Might have removed every child. */
4705 if (c == c->die_sib)
4706 return;
4707 c = c->die_sib;
4708 }
4709 } while (c != die->die_child);
4710 }
4711
4712 /* Add a CHILD_DIE as the last child of DIE. */
4713
4714 static void
4715 add_child_die (dw_die_ref die, dw_die_ref child_die)
4716 {
4717 /* FIXME this should probably be an assert. */
4718 if (! die || ! child_die)
4719 return;
4720 gcc_assert (die != child_die);
4721
4722 child_die->die_parent = die;
4723 if (die->die_child)
4724 {
4725 child_die->die_sib = die->die_child->die_sib;
4726 die->die_child->die_sib = child_die;
4727 }
4728 else
4729 child_die->die_sib = child_die;
4730 die->die_child = child_die;
4731 }
4732
4733 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4734 is the specification, to the end of PARENT's list of children.
4735 This is done by removing and re-adding it. */
4736
4737 static void
4738 splice_child_die (dw_die_ref parent, dw_die_ref child)
4739 {
4740 dw_die_ref p;
4741
4742 /* We want the declaration DIE from inside the class, not the
4743 specification DIE at toplevel. */
4744 if (child->die_parent != parent)
4745 {
4746 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4747
4748 if (tmp)
4749 child = tmp;
4750 }
4751
4752 gcc_assert (child->die_parent == parent
4753 || (child->die_parent
4754 == get_AT_ref (parent, DW_AT_specification)));
4755
4756 for (p = child->die_parent->die_child; ; p = p->die_sib)
4757 if (p->die_sib == child)
4758 {
4759 remove_child_with_prev (child, p);
4760 break;
4761 }
4762
4763 add_child_die (parent, child);
4764 }
4765
4766 /* Return a pointer to a newly created DIE node. */
4767
4768 static inline dw_die_ref
4769 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4770 {
4771 dw_die_ref die = ggc_alloc_cleared_die_node ();
4772
4773 die->die_tag = tag_value;
4774
4775 if (parent_die != NULL)
4776 add_child_die (parent_die, die);
4777 else
4778 {
4779 limbo_die_node *limbo_node;
4780
4781 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4782 limbo_node->die = die;
4783 limbo_node->created_for = t;
4784 limbo_node->next = limbo_die_list;
4785 limbo_die_list = limbo_node;
4786 }
4787
4788 return die;
4789 }
4790
4791 /* Return the DIE associated with the given type specifier. */
4792
4793 static inline dw_die_ref
4794 lookup_type_die (tree type)
4795 {
4796 return TYPE_SYMTAB_DIE (type);
4797 }
4798
4799 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4800 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4801 anonymous type instead the one of the naming typedef. */
4802
4803 static inline dw_die_ref
4804 strip_naming_typedef (tree type, dw_die_ref type_die)
4805 {
4806 if (type
4807 && TREE_CODE (type) == RECORD_TYPE
4808 && type_die
4809 && type_die->die_tag == DW_TAG_typedef
4810 && is_naming_typedef_decl (TYPE_NAME (type)))
4811 type_die = get_AT_ref (type_die, DW_AT_type);
4812 return type_die;
4813 }
4814
4815 /* Like lookup_type_die, but if type is an anonymous type named by a
4816 typedef[1], return the DIE of the anonymous type instead the one of
4817 the naming typedef. This is because in gen_typedef_die, we did
4818 equate the anonymous struct named by the typedef with the DIE of
4819 the naming typedef. So by default, lookup_type_die on an anonymous
4820 struct yields the DIE of the naming typedef.
4821
4822 [1]: Read the comment of is_naming_typedef_decl to learn about what
4823 a naming typedef is. */
4824
4825 static inline dw_die_ref
4826 lookup_type_die_strip_naming_typedef (tree type)
4827 {
4828 dw_die_ref die = lookup_type_die (type);
4829 return strip_naming_typedef (type, die);
4830 }
4831
4832 /* Equate a DIE to a given type specifier. */
4833
4834 static inline void
4835 equate_type_number_to_die (tree type, dw_die_ref type_die)
4836 {
4837 TYPE_SYMTAB_DIE (type) = type_die;
4838 }
4839
4840 /* Returns a hash value for X (which really is a die_struct). */
4841
4842 static hashval_t
4843 decl_die_table_hash (const void *x)
4844 {
4845 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4846 }
4847
4848 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4849
4850 static int
4851 decl_die_table_eq (const void *x, const void *y)
4852 {
4853 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4854 }
4855
4856 /* Return the DIE associated with a given declaration. */
4857
4858 static inline dw_die_ref
4859 lookup_decl_die (tree decl)
4860 {
4861 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4862 }
4863
4864 /* Returns a hash value for X (which really is a var_loc_list). */
4865
4866 static hashval_t
4867 decl_loc_table_hash (const void *x)
4868 {
4869 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4870 }
4871
4872 /* Return nonzero if decl_id of var_loc_list X is the same as
4873 UID of decl *Y. */
4874
4875 static int
4876 decl_loc_table_eq (const void *x, const void *y)
4877 {
4878 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4879 }
4880
4881 /* Return the var_loc list associated with a given declaration. */
4882
4883 static inline var_loc_list *
4884 lookup_decl_loc (const_tree decl)
4885 {
4886 if (!decl_loc_table)
4887 return NULL;
4888 return (var_loc_list *)
4889 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4890 }
4891
4892 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4893
4894 static hashval_t
4895 cached_dw_loc_list_table_hash (const void *x)
4896 {
4897 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4898 }
4899
4900 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4901 UID of decl *Y. */
4902
4903 static int
4904 cached_dw_loc_list_table_eq (const void *x, const void *y)
4905 {
4906 return (((const cached_dw_loc_list *) x)->decl_id
4907 == DECL_UID ((const_tree) y));
4908 }
4909
4910 /* Equate a DIE to a particular declaration. */
4911
4912 static void
4913 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4914 {
4915 unsigned int decl_id = DECL_UID (decl);
4916 void **slot;
4917
4918 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4919 *slot = decl_die;
4920 decl_die->decl_id = decl_id;
4921 }
4922
4923 /* Return how many bits covers PIECE EXPR_LIST. */
4924
4925 static int
4926 decl_piece_bitsize (rtx piece)
4927 {
4928 int ret = (int) GET_MODE (piece);
4929 if (ret)
4930 return ret;
4931 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4932 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4933 return INTVAL (XEXP (XEXP (piece, 0), 0));
4934 }
4935
4936 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
4937
4938 static rtx *
4939 decl_piece_varloc_ptr (rtx piece)
4940 {
4941 if ((int) GET_MODE (piece))
4942 return &XEXP (piece, 0);
4943 else
4944 return &XEXP (XEXP (piece, 0), 1);
4945 }
4946
4947 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4948 Next is the chain of following piece nodes. */
4949
4950 static rtx
4951 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4952 {
4953 if (bitsize <= (int) MAX_MACHINE_MODE)
4954 return alloc_EXPR_LIST (bitsize, loc_note, next);
4955 else
4956 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4957 GEN_INT (bitsize),
4958 loc_note), next);
4959 }
4960
4961 /* Return rtx that should be stored into loc field for
4962 LOC_NOTE and BITPOS/BITSIZE. */
4963
4964 static rtx
4965 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4966 HOST_WIDE_INT bitsize)
4967 {
4968 if (bitsize != -1)
4969 {
4970 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4971 if (bitpos != 0)
4972 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4973 }
4974 return loc_note;
4975 }
4976
4977 /* This function either modifies location piece list *DEST in
4978 place (if SRC and INNER is NULL), or copies location piece list
4979 *SRC to *DEST while modifying it. Location BITPOS is modified
4980 to contain LOC_NOTE, any pieces overlapping it are removed resp.
4981 not copied and if needed some padding around it is added.
4982 When modifying in place, DEST should point to EXPR_LIST where
4983 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4984 to the start of the whole list and INNER points to the EXPR_LIST
4985 where earlier pieces cover PIECE_BITPOS bits. */
4986
4987 static void
4988 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4989 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4990 HOST_WIDE_INT bitsize, rtx loc_note)
4991 {
4992 int diff;
4993 bool copy = inner != NULL;
4994
4995 if (copy)
4996 {
4997 /* First copy all nodes preceding the current bitpos. */
4998 while (src != inner)
4999 {
5000 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5001 decl_piece_bitsize (*src), NULL_RTX);
5002 dest = &XEXP (*dest, 1);
5003 src = &XEXP (*src, 1);
5004 }
5005 }
5006 /* Add padding if needed. */
5007 if (bitpos != piece_bitpos)
5008 {
5009 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5010 copy ? NULL_RTX : *dest);
5011 dest = &XEXP (*dest, 1);
5012 }
5013 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5014 {
5015 gcc_assert (!copy);
5016 /* A piece with correct bitpos and bitsize already exist,
5017 just update the location for it and return. */
5018 *decl_piece_varloc_ptr (*dest) = loc_note;
5019 return;
5020 }
5021 /* Add the piece that changed. */
5022 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5023 dest = &XEXP (*dest, 1);
5024 /* Skip over pieces that overlap it. */
5025 diff = bitpos - piece_bitpos + bitsize;
5026 if (!copy)
5027 src = dest;
5028 while (diff > 0 && *src)
5029 {
5030 rtx piece = *src;
5031 diff -= decl_piece_bitsize (piece);
5032 if (copy)
5033 src = &XEXP (piece, 1);
5034 else
5035 {
5036 *src = XEXP (piece, 1);
5037 free_EXPR_LIST_node (piece);
5038 }
5039 }
5040 /* Add padding if needed. */
5041 if (diff < 0 && *src)
5042 {
5043 if (!copy)
5044 dest = src;
5045 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5046 dest = &XEXP (*dest, 1);
5047 }
5048 if (!copy)
5049 return;
5050 /* Finally copy all nodes following it. */
5051 while (*src)
5052 {
5053 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5054 decl_piece_bitsize (*src), NULL_RTX);
5055 dest = &XEXP (*dest, 1);
5056 src = &XEXP (*src, 1);
5057 }
5058 }
5059
5060 /* Add a variable location node to the linked list for DECL. */
5061
5062 static struct var_loc_node *
5063 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5064 {
5065 unsigned int decl_id;
5066 var_loc_list *temp;
5067 void **slot;
5068 struct var_loc_node *loc = NULL;
5069 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5070
5071 if (TREE_CODE (decl) == VAR_DECL
5072 && DECL_HAS_DEBUG_EXPR_P (decl))
5073 {
5074 tree realdecl = DECL_DEBUG_EXPR (decl);
5075 if (handled_component_p (realdecl)
5076 || (TREE_CODE (realdecl) == MEM_REF
5077 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5078 {
5079 HOST_WIDE_INT maxsize;
5080 tree innerdecl;
5081 innerdecl
5082 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5083 if (!DECL_P (innerdecl)
5084 || DECL_IGNORED_P (innerdecl)
5085 || TREE_STATIC (innerdecl)
5086 || bitsize <= 0
5087 || bitpos + bitsize > 256
5088 || bitsize != maxsize)
5089 return NULL;
5090 decl = innerdecl;
5091 }
5092 }
5093
5094 decl_id = DECL_UID (decl);
5095 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5096 if (*slot == NULL)
5097 {
5098 temp = ggc_alloc_cleared_var_loc_list ();
5099 temp->decl_id = decl_id;
5100 *slot = temp;
5101 }
5102 else
5103 temp = (var_loc_list *) *slot;
5104
5105 /* For PARM_DECLs try to keep around the original incoming value,
5106 even if that means we'll emit a zero-range .debug_loc entry. */
5107 if (temp->last
5108 && temp->first == temp->last
5109 && TREE_CODE (decl) == PARM_DECL
5110 && NOTE_P (temp->first->loc)
5111 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5112 && DECL_INCOMING_RTL (decl)
5113 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5114 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5115 == GET_CODE (DECL_INCOMING_RTL (decl))
5116 && prev_real_insn (temp->first->loc) == NULL_RTX
5117 && (bitsize != -1
5118 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5119 NOTE_VAR_LOCATION_LOC (loc_note))
5120 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5121 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5122 {
5123 loc = ggc_alloc_cleared_var_loc_node ();
5124 temp->first->next = loc;
5125 temp->last = loc;
5126 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5127 }
5128 else if (temp->last)
5129 {
5130 struct var_loc_node *last = temp->last, *unused = NULL;
5131 rtx *piece_loc = NULL, last_loc_note;
5132 int piece_bitpos = 0;
5133 if (last->next)
5134 {
5135 last = last->next;
5136 gcc_assert (last->next == NULL);
5137 }
5138 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5139 {
5140 piece_loc = &last->loc;
5141 do
5142 {
5143 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5144 if (piece_bitpos + cur_bitsize > bitpos)
5145 break;
5146 piece_bitpos += cur_bitsize;
5147 piece_loc = &XEXP (*piece_loc, 1);
5148 }
5149 while (*piece_loc);
5150 }
5151 /* TEMP->LAST here is either pointer to the last but one or
5152 last element in the chained list, LAST is pointer to the
5153 last element. */
5154 if (label && strcmp (last->label, label) == 0)
5155 {
5156 /* For SRA optimized variables if there weren't any real
5157 insns since last note, just modify the last node. */
5158 if (piece_loc != NULL)
5159 {
5160 adjust_piece_list (piece_loc, NULL, NULL,
5161 bitpos, piece_bitpos, bitsize, loc_note);
5162 return NULL;
5163 }
5164 /* If the last note doesn't cover any instructions, remove it. */
5165 if (temp->last != last)
5166 {
5167 temp->last->next = NULL;
5168 unused = last;
5169 last = temp->last;
5170 gcc_assert (strcmp (last->label, label) != 0);
5171 }
5172 else
5173 {
5174 gcc_assert (temp->first == temp->last
5175 || (temp->first->next == temp->last
5176 && TREE_CODE (decl) == PARM_DECL));
5177 memset (temp->last, '\0', sizeof (*temp->last));
5178 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5179 return temp->last;
5180 }
5181 }
5182 if (bitsize == -1 && NOTE_P (last->loc))
5183 last_loc_note = last->loc;
5184 else if (piece_loc != NULL
5185 && *piece_loc != NULL_RTX
5186 && piece_bitpos == bitpos
5187 && decl_piece_bitsize (*piece_loc) == bitsize)
5188 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5189 else
5190 last_loc_note = NULL_RTX;
5191 /* If the current location is the same as the end of the list,
5192 and either both or neither of the locations is uninitialized,
5193 we have nothing to do. */
5194 if (last_loc_note == NULL_RTX
5195 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5196 NOTE_VAR_LOCATION_LOC (loc_note)))
5197 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5198 != NOTE_VAR_LOCATION_STATUS (loc_note))
5199 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5200 == VAR_INIT_STATUS_UNINITIALIZED)
5201 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5202 == VAR_INIT_STATUS_UNINITIALIZED))))
5203 {
5204 /* Add LOC to the end of list and update LAST. If the last
5205 element of the list has been removed above, reuse its
5206 memory for the new node, otherwise allocate a new one. */
5207 if (unused)
5208 {
5209 loc = unused;
5210 memset (loc, '\0', sizeof (*loc));
5211 }
5212 else
5213 loc = ggc_alloc_cleared_var_loc_node ();
5214 if (bitsize == -1 || piece_loc == NULL)
5215 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5216 else
5217 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5218 bitpos, piece_bitpos, bitsize, loc_note);
5219 last->next = loc;
5220 /* Ensure TEMP->LAST will point either to the new last but one
5221 element of the chain, or to the last element in it. */
5222 if (last != temp->last)
5223 temp->last = last;
5224 }
5225 else if (unused)
5226 ggc_free (unused);
5227 }
5228 else
5229 {
5230 loc = ggc_alloc_cleared_var_loc_node ();
5231 temp->first = loc;
5232 temp->last = loc;
5233 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5234 }
5235 return loc;
5236 }
5237 \f
5238 /* Keep track of the number of spaces used to indent the
5239 output of the debugging routines that print the structure of
5240 the DIE internal representation. */
5241 static int print_indent;
5242
5243 /* Indent the line the number of spaces given by print_indent. */
5244
5245 static inline void
5246 print_spaces (FILE *outfile)
5247 {
5248 fprintf (outfile, "%*s", print_indent, "");
5249 }
5250
5251 /* Print a type signature in hex. */
5252
5253 static inline void
5254 print_signature (FILE *outfile, char *sig)
5255 {
5256 int i;
5257
5258 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5259 fprintf (outfile, "%02x", sig[i] & 0xff);
5260 }
5261
5262 /* Print the information associated with a given DIE, and its children.
5263 This routine is a debugging aid only. */
5264
5265 static void
5266 print_die (dw_die_ref die, FILE *outfile)
5267 {
5268 dw_attr_ref a;
5269 dw_die_ref c;
5270 unsigned ix;
5271
5272 print_spaces (outfile);
5273 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5274 die->die_offset, dwarf_tag_name (die->die_tag),
5275 (void*) die);
5276 print_spaces (outfile);
5277 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5278 fprintf (outfile, " offset: %ld", die->die_offset);
5279 fprintf (outfile, " mark: %d\n", die->die_mark);
5280
5281 if (die->comdat_type_p)
5282 {
5283 print_spaces (outfile);
5284 fprintf (outfile, " signature: ");
5285 print_signature (outfile, die->die_id.die_type_node->signature);
5286 fprintf (outfile, "\n");
5287 }
5288
5289 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5290 {
5291 print_spaces (outfile);
5292 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5293
5294 switch (AT_class (a))
5295 {
5296 case dw_val_class_addr:
5297 fprintf (outfile, "address");
5298 break;
5299 case dw_val_class_offset:
5300 fprintf (outfile, "offset");
5301 break;
5302 case dw_val_class_loc:
5303 fprintf (outfile, "location descriptor");
5304 break;
5305 case dw_val_class_loc_list:
5306 fprintf (outfile, "location list -> label:%s",
5307 AT_loc_list (a)->ll_symbol);
5308 break;
5309 case dw_val_class_range_list:
5310 fprintf (outfile, "range list");
5311 break;
5312 case dw_val_class_const:
5313 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5314 break;
5315 case dw_val_class_unsigned_const:
5316 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5317 break;
5318 case dw_val_class_const_double:
5319 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5320 HOST_WIDE_INT_PRINT_UNSIGNED")",
5321 a->dw_attr_val.v.val_double.high,
5322 a->dw_attr_val.v.val_double.low);
5323 break;
5324 case dw_val_class_vec:
5325 fprintf (outfile, "floating-point or vector constant");
5326 break;
5327 case dw_val_class_flag:
5328 fprintf (outfile, "%u", AT_flag (a));
5329 break;
5330 case dw_val_class_die_ref:
5331 if (AT_ref (a) != NULL)
5332 {
5333 if (AT_ref (a)->comdat_type_p)
5334 {
5335 fprintf (outfile, "die -> signature: ");
5336 print_signature (outfile,
5337 AT_ref (a)->die_id.die_type_node->signature);
5338 }
5339 else if (AT_ref (a)->die_id.die_symbol)
5340 fprintf (outfile, "die -> label: %s",
5341 AT_ref (a)->die_id.die_symbol);
5342 else
5343 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5344 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5345 }
5346 else
5347 fprintf (outfile, "die -> <null>");
5348 break;
5349 case dw_val_class_vms_delta:
5350 fprintf (outfile, "delta: @slotcount(%s-%s)",
5351 AT_vms_delta2 (a), AT_vms_delta1 (a));
5352 break;
5353 case dw_val_class_lbl_id:
5354 case dw_val_class_lineptr:
5355 case dw_val_class_macptr:
5356 case dw_val_class_high_pc:
5357 fprintf (outfile, "label: %s", AT_lbl (a));
5358 break;
5359 case dw_val_class_str:
5360 if (AT_string (a) != NULL)
5361 fprintf (outfile, "\"%s\"", AT_string (a));
5362 else
5363 fprintf (outfile, "<null>");
5364 break;
5365 case dw_val_class_file:
5366 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5367 AT_file (a)->emitted_number);
5368 break;
5369 case dw_val_class_data8:
5370 {
5371 int i;
5372
5373 for (i = 0; i < 8; i++)
5374 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5375 break;
5376 }
5377 default:
5378 break;
5379 }
5380
5381 fprintf (outfile, "\n");
5382 }
5383
5384 if (die->die_child != NULL)
5385 {
5386 print_indent += 4;
5387 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5388 print_indent -= 4;
5389 }
5390 if (print_indent == 0)
5391 fprintf (outfile, "\n");
5392 }
5393
5394 /* Print the information collected for a given DIE. */
5395
5396 DEBUG_FUNCTION void
5397 debug_dwarf_die (dw_die_ref die)
5398 {
5399 print_die (die, stderr);
5400 }
5401
5402 DEBUG_FUNCTION void
5403 debug (die_struct &ref)
5404 {
5405 print_die (&ref, stderr);
5406 }
5407
5408 DEBUG_FUNCTION void
5409 debug (die_struct *ptr)
5410 {
5411 if (ptr)
5412 debug (*ptr);
5413 else
5414 fprintf (stderr, "<nil>\n");
5415 }
5416
5417
5418 /* Print all DWARF information collected for the compilation unit.
5419 This routine is a debugging aid only. */
5420
5421 DEBUG_FUNCTION void
5422 debug_dwarf (void)
5423 {
5424 print_indent = 0;
5425 print_die (comp_unit_die (), stderr);
5426 }
5427 \f
5428 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5429 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5430 DIE that marks the start of the DIEs for this include file. */
5431
5432 static dw_die_ref
5433 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5434 {
5435 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5436 dw_die_ref new_unit = gen_compile_unit_die (filename);
5437
5438 new_unit->die_sib = old_unit;
5439 return new_unit;
5440 }
5441
5442 /* Close an include-file CU and reopen the enclosing one. */
5443
5444 static dw_die_ref
5445 pop_compile_unit (dw_die_ref old_unit)
5446 {
5447 dw_die_ref new_unit = old_unit->die_sib;
5448
5449 old_unit->die_sib = NULL;
5450 return new_unit;
5451 }
5452
5453 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5454 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5455 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5456
5457 /* Calculate the checksum of a location expression. */
5458
5459 static inline void
5460 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5461 {
5462 int tem;
5463 hashval_t hash = 0;
5464
5465 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5466 CHECKSUM (tem);
5467 hash = hash_loc_operands (loc, hash);
5468 CHECKSUM (hash);
5469 }
5470
5471 /* Calculate the checksum of an attribute. */
5472
5473 static void
5474 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5475 {
5476 dw_loc_descr_ref loc;
5477 rtx r;
5478
5479 CHECKSUM (at->dw_attr);
5480
5481 /* We don't care that this was compiled with a different compiler
5482 snapshot; if the output is the same, that's what matters. */
5483 if (at->dw_attr == DW_AT_producer)
5484 return;
5485
5486 switch (AT_class (at))
5487 {
5488 case dw_val_class_const:
5489 CHECKSUM (at->dw_attr_val.v.val_int);
5490 break;
5491 case dw_val_class_unsigned_const:
5492 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5493 break;
5494 case dw_val_class_const_double:
5495 CHECKSUM (at->dw_attr_val.v.val_double);
5496 break;
5497 case dw_val_class_vec:
5498 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5499 (at->dw_attr_val.v.val_vec.length
5500 * at->dw_attr_val.v.val_vec.elt_size));
5501 break;
5502 case dw_val_class_flag:
5503 CHECKSUM (at->dw_attr_val.v.val_flag);
5504 break;
5505 case dw_val_class_str:
5506 CHECKSUM_STRING (AT_string (at));
5507 break;
5508
5509 case dw_val_class_addr:
5510 r = AT_addr (at);
5511 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5512 CHECKSUM_STRING (XSTR (r, 0));
5513 break;
5514
5515 case dw_val_class_offset:
5516 CHECKSUM (at->dw_attr_val.v.val_offset);
5517 break;
5518
5519 case dw_val_class_loc:
5520 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5521 loc_checksum (loc, ctx);
5522 break;
5523
5524 case dw_val_class_die_ref:
5525 die_checksum (AT_ref (at), ctx, mark);
5526 break;
5527
5528 case dw_val_class_fde_ref:
5529 case dw_val_class_vms_delta:
5530 case dw_val_class_lbl_id:
5531 case dw_val_class_lineptr:
5532 case dw_val_class_macptr:
5533 case dw_val_class_high_pc:
5534 break;
5535
5536 case dw_val_class_file:
5537 CHECKSUM_STRING (AT_file (at)->filename);
5538 break;
5539
5540 case dw_val_class_data8:
5541 CHECKSUM (at->dw_attr_val.v.val_data8);
5542 break;
5543
5544 default:
5545 break;
5546 }
5547 }
5548
5549 /* Calculate the checksum of a DIE. */
5550
5551 static void
5552 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5553 {
5554 dw_die_ref c;
5555 dw_attr_ref a;
5556 unsigned ix;
5557
5558 /* To avoid infinite recursion. */
5559 if (die->die_mark)
5560 {
5561 CHECKSUM (die->die_mark);
5562 return;
5563 }
5564 die->die_mark = ++(*mark);
5565
5566 CHECKSUM (die->die_tag);
5567
5568 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5569 attr_checksum (a, ctx, mark);
5570
5571 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5572 }
5573
5574 #undef CHECKSUM
5575 #undef CHECKSUM_BLOCK
5576 #undef CHECKSUM_STRING
5577
5578 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5579 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5580 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5581 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5582 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5583 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5584 #define CHECKSUM_ATTR(FOO) \
5585 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5586
5587 /* Calculate the checksum of a number in signed LEB128 format. */
5588
5589 static void
5590 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5591 {
5592 unsigned char byte;
5593 bool more;
5594
5595 while (1)
5596 {
5597 byte = (value & 0x7f);
5598 value >>= 7;
5599 more = !((value == 0 && (byte & 0x40) == 0)
5600 || (value == -1 && (byte & 0x40) != 0));
5601 if (more)
5602 byte |= 0x80;
5603 CHECKSUM (byte);
5604 if (!more)
5605 break;
5606 }
5607 }
5608
5609 /* Calculate the checksum of a number in unsigned LEB128 format. */
5610
5611 static void
5612 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5613 {
5614 while (1)
5615 {
5616 unsigned char byte = (value & 0x7f);
5617 value >>= 7;
5618 if (value != 0)
5619 /* More bytes to follow. */
5620 byte |= 0x80;
5621 CHECKSUM (byte);
5622 if (value == 0)
5623 break;
5624 }
5625 }
5626
5627 /* Checksum the context of the DIE. This adds the names of any
5628 surrounding namespaces or structures to the checksum. */
5629
5630 static void
5631 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5632 {
5633 const char *name;
5634 dw_die_ref spec;
5635 int tag = die->die_tag;
5636
5637 if (tag != DW_TAG_namespace
5638 && tag != DW_TAG_structure_type
5639 && tag != DW_TAG_class_type)
5640 return;
5641
5642 name = get_AT_string (die, DW_AT_name);
5643
5644 spec = get_AT_ref (die, DW_AT_specification);
5645 if (spec != NULL)
5646 die = spec;
5647
5648 if (die->die_parent != NULL)
5649 checksum_die_context (die->die_parent, ctx);
5650
5651 CHECKSUM_ULEB128 ('C');
5652 CHECKSUM_ULEB128 (tag);
5653 if (name != NULL)
5654 CHECKSUM_STRING (name);
5655 }
5656
5657 /* Calculate the checksum of a location expression. */
5658
5659 static inline void
5660 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5661 {
5662 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5663 were emitted as a DW_FORM_sdata instead of a location expression. */
5664 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5665 {
5666 CHECKSUM_ULEB128 (DW_FORM_sdata);
5667 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5668 return;
5669 }
5670
5671 /* Otherwise, just checksum the raw location expression. */
5672 while (loc != NULL)
5673 {
5674 hashval_t hash = 0;
5675
5676 CHECKSUM_ULEB128 (loc->dtprel);
5677 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5678 hash = hash_loc_operands (loc, hash);
5679 CHECKSUM (hash);
5680 loc = loc->dw_loc_next;
5681 }
5682 }
5683
5684 /* Calculate the checksum of an attribute. */
5685
5686 static void
5687 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5688 struct md5_ctx *ctx, int *mark)
5689 {
5690 dw_loc_descr_ref loc;
5691 rtx r;
5692
5693 if (AT_class (at) == dw_val_class_die_ref)
5694 {
5695 dw_die_ref target_die = AT_ref (at);
5696
5697 /* For pointer and reference types, we checksum only the (qualified)
5698 name of the target type (if there is a name). For friend entries,
5699 we checksum only the (qualified) name of the target type or function.
5700 This allows the checksum to remain the same whether the target type
5701 is complete or not. */
5702 if ((at->dw_attr == DW_AT_type
5703 && (tag == DW_TAG_pointer_type
5704 || tag == DW_TAG_reference_type
5705 || tag == DW_TAG_rvalue_reference_type
5706 || tag == DW_TAG_ptr_to_member_type))
5707 || (at->dw_attr == DW_AT_friend
5708 && tag == DW_TAG_friend))
5709 {
5710 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5711
5712 if (name_attr != NULL)
5713 {
5714 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5715
5716 if (decl == NULL)
5717 decl = target_die;
5718 CHECKSUM_ULEB128 ('N');
5719 CHECKSUM_ULEB128 (at->dw_attr);
5720 if (decl->die_parent != NULL)
5721 checksum_die_context (decl->die_parent, ctx);
5722 CHECKSUM_ULEB128 ('E');
5723 CHECKSUM_STRING (AT_string (name_attr));
5724 return;
5725 }
5726 }
5727
5728 /* For all other references to another DIE, we check to see if the
5729 target DIE has already been visited. If it has, we emit a
5730 backward reference; if not, we descend recursively. */
5731 if (target_die->die_mark > 0)
5732 {
5733 CHECKSUM_ULEB128 ('R');
5734 CHECKSUM_ULEB128 (at->dw_attr);
5735 CHECKSUM_ULEB128 (target_die->die_mark);
5736 }
5737 else
5738 {
5739 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5740
5741 if (decl == NULL)
5742 decl = target_die;
5743 target_die->die_mark = ++(*mark);
5744 CHECKSUM_ULEB128 ('T');
5745 CHECKSUM_ULEB128 (at->dw_attr);
5746 if (decl->die_parent != NULL)
5747 checksum_die_context (decl->die_parent, ctx);
5748 die_checksum_ordered (target_die, ctx, mark);
5749 }
5750 return;
5751 }
5752
5753 CHECKSUM_ULEB128 ('A');
5754 CHECKSUM_ULEB128 (at->dw_attr);
5755
5756 switch (AT_class (at))
5757 {
5758 case dw_val_class_const:
5759 CHECKSUM_ULEB128 (DW_FORM_sdata);
5760 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5761 break;
5762
5763 case dw_val_class_unsigned_const:
5764 CHECKSUM_ULEB128 (DW_FORM_sdata);
5765 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5766 break;
5767
5768 case dw_val_class_const_double:
5769 CHECKSUM_ULEB128 (DW_FORM_block);
5770 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5771 CHECKSUM (at->dw_attr_val.v.val_double);
5772 break;
5773
5774 case dw_val_class_vec:
5775 CHECKSUM_ULEB128 (DW_FORM_block);
5776 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5777 * at->dw_attr_val.v.val_vec.elt_size);
5778 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5779 (at->dw_attr_val.v.val_vec.length
5780 * at->dw_attr_val.v.val_vec.elt_size));
5781 break;
5782
5783 case dw_val_class_flag:
5784 CHECKSUM_ULEB128 (DW_FORM_flag);
5785 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5786 break;
5787
5788 case dw_val_class_str:
5789 CHECKSUM_ULEB128 (DW_FORM_string);
5790 CHECKSUM_STRING (AT_string (at));
5791 break;
5792
5793 case dw_val_class_addr:
5794 r = AT_addr (at);
5795 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5796 CHECKSUM_ULEB128 (DW_FORM_string);
5797 CHECKSUM_STRING (XSTR (r, 0));
5798 break;
5799
5800 case dw_val_class_offset:
5801 CHECKSUM_ULEB128 (DW_FORM_sdata);
5802 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5803 break;
5804
5805 case dw_val_class_loc:
5806 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5807 loc_checksum_ordered (loc, ctx);
5808 break;
5809
5810 case dw_val_class_fde_ref:
5811 case dw_val_class_lbl_id:
5812 case dw_val_class_lineptr:
5813 case dw_val_class_macptr:
5814 case dw_val_class_high_pc:
5815 break;
5816
5817 case dw_val_class_file:
5818 CHECKSUM_ULEB128 (DW_FORM_string);
5819 CHECKSUM_STRING (AT_file (at)->filename);
5820 break;
5821
5822 case dw_val_class_data8:
5823 CHECKSUM (at->dw_attr_val.v.val_data8);
5824 break;
5825
5826 default:
5827 break;
5828 }
5829 }
5830
5831 struct checksum_attributes
5832 {
5833 dw_attr_ref at_name;
5834 dw_attr_ref at_type;
5835 dw_attr_ref at_friend;
5836 dw_attr_ref at_accessibility;
5837 dw_attr_ref at_address_class;
5838 dw_attr_ref at_allocated;
5839 dw_attr_ref at_artificial;
5840 dw_attr_ref at_associated;
5841 dw_attr_ref at_binary_scale;
5842 dw_attr_ref at_bit_offset;
5843 dw_attr_ref at_bit_size;
5844 dw_attr_ref at_bit_stride;
5845 dw_attr_ref at_byte_size;
5846 dw_attr_ref at_byte_stride;
5847 dw_attr_ref at_const_value;
5848 dw_attr_ref at_containing_type;
5849 dw_attr_ref at_count;
5850 dw_attr_ref at_data_location;
5851 dw_attr_ref at_data_member_location;
5852 dw_attr_ref at_decimal_scale;
5853 dw_attr_ref at_decimal_sign;
5854 dw_attr_ref at_default_value;
5855 dw_attr_ref at_digit_count;
5856 dw_attr_ref at_discr;
5857 dw_attr_ref at_discr_list;
5858 dw_attr_ref at_discr_value;
5859 dw_attr_ref at_encoding;
5860 dw_attr_ref at_endianity;
5861 dw_attr_ref at_explicit;
5862 dw_attr_ref at_is_optional;
5863 dw_attr_ref at_location;
5864 dw_attr_ref at_lower_bound;
5865 dw_attr_ref at_mutable;
5866 dw_attr_ref at_ordering;
5867 dw_attr_ref at_picture_string;
5868 dw_attr_ref at_prototyped;
5869 dw_attr_ref at_small;
5870 dw_attr_ref at_segment;
5871 dw_attr_ref at_string_length;
5872 dw_attr_ref at_threads_scaled;
5873 dw_attr_ref at_upper_bound;
5874 dw_attr_ref at_use_location;
5875 dw_attr_ref at_use_UTF8;
5876 dw_attr_ref at_variable_parameter;
5877 dw_attr_ref at_virtuality;
5878 dw_attr_ref at_visibility;
5879 dw_attr_ref at_vtable_elem_location;
5880 };
5881
5882 /* Collect the attributes that we will want to use for the checksum. */
5883
5884 static void
5885 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5886 {
5887 dw_attr_ref a;
5888 unsigned ix;
5889
5890 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5891 {
5892 switch (a->dw_attr)
5893 {
5894 case DW_AT_name:
5895 attrs->at_name = a;
5896 break;
5897 case DW_AT_type:
5898 attrs->at_type = a;
5899 break;
5900 case DW_AT_friend:
5901 attrs->at_friend = a;
5902 break;
5903 case DW_AT_accessibility:
5904 attrs->at_accessibility = a;
5905 break;
5906 case DW_AT_address_class:
5907 attrs->at_address_class = a;
5908 break;
5909 case DW_AT_allocated:
5910 attrs->at_allocated = a;
5911 break;
5912 case DW_AT_artificial:
5913 attrs->at_artificial = a;
5914 break;
5915 case DW_AT_associated:
5916 attrs->at_associated = a;
5917 break;
5918 case DW_AT_binary_scale:
5919 attrs->at_binary_scale = a;
5920 break;
5921 case DW_AT_bit_offset:
5922 attrs->at_bit_offset = a;
5923 break;
5924 case DW_AT_bit_size:
5925 attrs->at_bit_size = a;
5926 break;
5927 case DW_AT_bit_stride:
5928 attrs->at_bit_stride = a;
5929 break;
5930 case DW_AT_byte_size:
5931 attrs->at_byte_size = a;
5932 break;
5933 case DW_AT_byte_stride:
5934 attrs->at_byte_stride = a;
5935 break;
5936 case DW_AT_const_value:
5937 attrs->at_const_value = a;
5938 break;
5939 case DW_AT_containing_type:
5940 attrs->at_containing_type = a;
5941 break;
5942 case DW_AT_count:
5943 attrs->at_count = a;
5944 break;
5945 case DW_AT_data_location:
5946 attrs->at_data_location = a;
5947 break;
5948 case DW_AT_data_member_location:
5949 attrs->at_data_member_location = a;
5950 break;
5951 case DW_AT_decimal_scale:
5952 attrs->at_decimal_scale = a;
5953 break;
5954 case DW_AT_decimal_sign:
5955 attrs->at_decimal_sign = a;
5956 break;
5957 case DW_AT_default_value:
5958 attrs->at_default_value = a;
5959 break;
5960 case DW_AT_digit_count:
5961 attrs->at_digit_count = a;
5962 break;
5963 case DW_AT_discr:
5964 attrs->at_discr = a;
5965 break;
5966 case DW_AT_discr_list:
5967 attrs->at_discr_list = a;
5968 break;
5969 case DW_AT_discr_value:
5970 attrs->at_discr_value = a;
5971 break;
5972 case DW_AT_encoding:
5973 attrs->at_encoding = a;
5974 break;
5975 case DW_AT_endianity:
5976 attrs->at_endianity = a;
5977 break;
5978 case DW_AT_explicit:
5979 attrs->at_explicit = a;
5980 break;
5981 case DW_AT_is_optional:
5982 attrs->at_is_optional = a;
5983 break;
5984 case DW_AT_location:
5985 attrs->at_location = a;
5986 break;
5987 case DW_AT_lower_bound:
5988 attrs->at_lower_bound = a;
5989 break;
5990 case DW_AT_mutable:
5991 attrs->at_mutable = a;
5992 break;
5993 case DW_AT_ordering:
5994 attrs->at_ordering = a;
5995 break;
5996 case DW_AT_picture_string:
5997 attrs->at_picture_string = a;
5998 break;
5999 case DW_AT_prototyped:
6000 attrs->at_prototyped = a;
6001 break;
6002 case DW_AT_small:
6003 attrs->at_small = a;
6004 break;
6005 case DW_AT_segment:
6006 attrs->at_segment = a;
6007 break;
6008 case DW_AT_string_length:
6009 attrs->at_string_length = a;
6010 break;
6011 case DW_AT_threads_scaled:
6012 attrs->at_threads_scaled = a;
6013 break;
6014 case DW_AT_upper_bound:
6015 attrs->at_upper_bound = a;
6016 break;
6017 case DW_AT_use_location:
6018 attrs->at_use_location = a;
6019 break;
6020 case DW_AT_use_UTF8:
6021 attrs->at_use_UTF8 = a;
6022 break;
6023 case DW_AT_variable_parameter:
6024 attrs->at_variable_parameter = a;
6025 break;
6026 case DW_AT_virtuality:
6027 attrs->at_virtuality = a;
6028 break;
6029 case DW_AT_visibility:
6030 attrs->at_visibility = a;
6031 break;
6032 case DW_AT_vtable_elem_location:
6033 attrs->at_vtable_elem_location = a;
6034 break;
6035 default:
6036 break;
6037 }
6038 }
6039 }
6040
6041 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6042
6043 static void
6044 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6045 {
6046 dw_die_ref c;
6047 dw_die_ref decl;
6048 struct checksum_attributes attrs;
6049
6050 CHECKSUM_ULEB128 ('D');
6051 CHECKSUM_ULEB128 (die->die_tag);
6052
6053 memset (&attrs, 0, sizeof (attrs));
6054
6055 decl = get_AT_ref (die, DW_AT_specification);
6056 if (decl != NULL)
6057 collect_checksum_attributes (&attrs, decl);
6058 collect_checksum_attributes (&attrs, die);
6059
6060 CHECKSUM_ATTR (attrs.at_name);
6061 CHECKSUM_ATTR (attrs.at_accessibility);
6062 CHECKSUM_ATTR (attrs.at_address_class);
6063 CHECKSUM_ATTR (attrs.at_allocated);
6064 CHECKSUM_ATTR (attrs.at_artificial);
6065 CHECKSUM_ATTR (attrs.at_associated);
6066 CHECKSUM_ATTR (attrs.at_binary_scale);
6067 CHECKSUM_ATTR (attrs.at_bit_offset);
6068 CHECKSUM_ATTR (attrs.at_bit_size);
6069 CHECKSUM_ATTR (attrs.at_bit_stride);
6070 CHECKSUM_ATTR (attrs.at_byte_size);
6071 CHECKSUM_ATTR (attrs.at_byte_stride);
6072 CHECKSUM_ATTR (attrs.at_const_value);
6073 CHECKSUM_ATTR (attrs.at_containing_type);
6074 CHECKSUM_ATTR (attrs.at_count);
6075 CHECKSUM_ATTR (attrs.at_data_location);
6076 CHECKSUM_ATTR (attrs.at_data_member_location);
6077 CHECKSUM_ATTR (attrs.at_decimal_scale);
6078 CHECKSUM_ATTR (attrs.at_decimal_sign);
6079 CHECKSUM_ATTR (attrs.at_default_value);
6080 CHECKSUM_ATTR (attrs.at_digit_count);
6081 CHECKSUM_ATTR (attrs.at_discr);
6082 CHECKSUM_ATTR (attrs.at_discr_list);
6083 CHECKSUM_ATTR (attrs.at_discr_value);
6084 CHECKSUM_ATTR (attrs.at_encoding);
6085 CHECKSUM_ATTR (attrs.at_endianity);
6086 CHECKSUM_ATTR (attrs.at_explicit);
6087 CHECKSUM_ATTR (attrs.at_is_optional);
6088 CHECKSUM_ATTR (attrs.at_location);
6089 CHECKSUM_ATTR (attrs.at_lower_bound);
6090 CHECKSUM_ATTR (attrs.at_mutable);
6091 CHECKSUM_ATTR (attrs.at_ordering);
6092 CHECKSUM_ATTR (attrs.at_picture_string);
6093 CHECKSUM_ATTR (attrs.at_prototyped);
6094 CHECKSUM_ATTR (attrs.at_small);
6095 CHECKSUM_ATTR (attrs.at_segment);
6096 CHECKSUM_ATTR (attrs.at_string_length);
6097 CHECKSUM_ATTR (attrs.at_threads_scaled);
6098 CHECKSUM_ATTR (attrs.at_upper_bound);
6099 CHECKSUM_ATTR (attrs.at_use_location);
6100 CHECKSUM_ATTR (attrs.at_use_UTF8);
6101 CHECKSUM_ATTR (attrs.at_variable_parameter);
6102 CHECKSUM_ATTR (attrs.at_virtuality);
6103 CHECKSUM_ATTR (attrs.at_visibility);
6104 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6105 CHECKSUM_ATTR (attrs.at_type);
6106 CHECKSUM_ATTR (attrs.at_friend);
6107
6108 /* Checksum the child DIEs. */
6109 c = die->die_child;
6110 if (c) do {
6111 dw_attr_ref name_attr;
6112
6113 c = c->die_sib;
6114 name_attr = get_AT (c, DW_AT_name);
6115 if (is_template_instantiation (c))
6116 {
6117 /* Ignore instantiations of member type and function templates. */
6118 }
6119 else if (name_attr != NULL
6120 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6121 {
6122 /* Use a shallow checksum for named nested types and member
6123 functions. */
6124 CHECKSUM_ULEB128 ('S');
6125 CHECKSUM_ULEB128 (c->die_tag);
6126 CHECKSUM_STRING (AT_string (name_attr));
6127 }
6128 else
6129 {
6130 /* Use a deep checksum for other children. */
6131 /* Mark this DIE so it gets processed when unmarking. */
6132 if (c->die_mark == 0)
6133 c->die_mark = -1;
6134 die_checksum_ordered (c, ctx, mark);
6135 }
6136 } while (c != die->die_child);
6137
6138 CHECKSUM_ULEB128 (0);
6139 }
6140
6141 /* Add a type name and tag to a hash. */
6142 static void
6143 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6144 {
6145 CHECKSUM_ULEB128 (tag);
6146 CHECKSUM_STRING (name);
6147 }
6148
6149 #undef CHECKSUM
6150 #undef CHECKSUM_STRING
6151 #undef CHECKSUM_ATTR
6152 #undef CHECKSUM_LEB128
6153 #undef CHECKSUM_ULEB128
6154
6155 /* Generate the type signature for DIE. This is computed by generating an
6156 MD5 checksum over the DIE's tag, its relevant attributes, and its
6157 children. Attributes that are references to other DIEs are processed
6158 by recursion, using the MARK field to prevent infinite recursion.
6159 If the DIE is nested inside a namespace or another type, we also
6160 need to include that context in the signature. The lower 64 bits
6161 of the resulting MD5 checksum comprise the signature. */
6162
6163 static void
6164 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6165 {
6166 int mark;
6167 const char *name;
6168 unsigned char checksum[16];
6169 struct md5_ctx ctx;
6170 dw_die_ref decl;
6171 dw_die_ref parent;
6172
6173 name = get_AT_string (die, DW_AT_name);
6174 decl = get_AT_ref (die, DW_AT_specification);
6175 parent = get_die_parent (die);
6176
6177 /* First, compute a signature for just the type name (and its surrounding
6178 context, if any. This is stored in the type unit DIE for link-time
6179 ODR (one-definition rule) checking. */
6180
6181 if (is_cxx () && name != NULL)
6182 {
6183 md5_init_ctx (&ctx);
6184
6185 /* Checksum the names of surrounding namespaces and structures. */
6186 if (parent != NULL)
6187 checksum_die_context (parent, &ctx);
6188
6189 /* Checksum the current DIE. */
6190 die_odr_checksum (die->die_tag, name, &ctx);
6191 md5_finish_ctx (&ctx, checksum);
6192
6193 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6194 }
6195
6196 /* Next, compute the complete type signature. */
6197
6198 md5_init_ctx (&ctx);
6199 mark = 1;
6200 die->die_mark = mark;
6201
6202 /* Checksum the names of surrounding namespaces and structures. */
6203 if (parent != NULL)
6204 checksum_die_context (parent, &ctx);
6205
6206 /* Checksum the DIE and its children. */
6207 die_checksum_ordered (die, &ctx, &mark);
6208 unmark_all_dies (die);
6209 md5_finish_ctx (&ctx, checksum);
6210
6211 /* Store the signature in the type node and link the type DIE and the
6212 type node together. */
6213 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6214 DWARF_TYPE_SIGNATURE_SIZE);
6215 die->comdat_type_p = true;
6216 die->die_id.die_type_node = type_node;
6217 type_node->type_die = die;
6218
6219 /* If the DIE is a specification, link its declaration to the type node
6220 as well. */
6221 if (decl != NULL)
6222 {
6223 decl->comdat_type_p = true;
6224 decl->die_id.die_type_node = type_node;
6225 }
6226 }
6227
6228 /* Do the location expressions look same? */
6229 static inline int
6230 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6231 {
6232 return loc1->dw_loc_opc == loc2->dw_loc_opc
6233 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6234 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6235 }
6236
6237 /* Do the values look the same? */
6238 static int
6239 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6240 {
6241 dw_loc_descr_ref loc1, loc2;
6242 rtx r1, r2;
6243
6244 if (v1->val_class != v2->val_class)
6245 return 0;
6246
6247 switch (v1->val_class)
6248 {
6249 case dw_val_class_const:
6250 return v1->v.val_int == v2->v.val_int;
6251 case dw_val_class_unsigned_const:
6252 return v1->v.val_unsigned == v2->v.val_unsigned;
6253 case dw_val_class_const_double:
6254 return v1->v.val_double.high == v2->v.val_double.high
6255 && v1->v.val_double.low == v2->v.val_double.low;
6256 case dw_val_class_vec:
6257 if (v1->v.val_vec.length != v2->v.val_vec.length
6258 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6259 return 0;
6260 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6261 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6262 return 0;
6263 return 1;
6264 case dw_val_class_flag:
6265 return v1->v.val_flag == v2->v.val_flag;
6266 case dw_val_class_str:
6267 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6268
6269 case dw_val_class_addr:
6270 r1 = v1->v.val_addr;
6271 r2 = v2->v.val_addr;
6272 if (GET_CODE (r1) != GET_CODE (r2))
6273 return 0;
6274 return !rtx_equal_p (r1, r2);
6275
6276 case dw_val_class_offset:
6277 return v1->v.val_offset == v2->v.val_offset;
6278
6279 case dw_val_class_loc:
6280 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6281 loc1 && loc2;
6282 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6283 if (!same_loc_p (loc1, loc2, mark))
6284 return 0;
6285 return !loc1 && !loc2;
6286
6287 case dw_val_class_die_ref:
6288 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6289
6290 case dw_val_class_fde_ref:
6291 case dw_val_class_vms_delta:
6292 case dw_val_class_lbl_id:
6293 case dw_val_class_lineptr:
6294 case dw_val_class_macptr:
6295 case dw_val_class_high_pc:
6296 return 1;
6297
6298 case dw_val_class_file:
6299 return v1->v.val_file == v2->v.val_file;
6300
6301 case dw_val_class_data8:
6302 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6303
6304 default:
6305 return 1;
6306 }
6307 }
6308
6309 /* Do the attributes look the same? */
6310
6311 static int
6312 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6313 {
6314 if (at1->dw_attr != at2->dw_attr)
6315 return 0;
6316
6317 /* We don't care that this was compiled with a different compiler
6318 snapshot; if the output is the same, that's what matters. */
6319 if (at1->dw_attr == DW_AT_producer)
6320 return 1;
6321
6322 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6323 }
6324
6325 /* Do the dies look the same? */
6326
6327 static int
6328 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6329 {
6330 dw_die_ref c1, c2;
6331 dw_attr_ref a1;
6332 unsigned ix;
6333
6334 /* To avoid infinite recursion. */
6335 if (die1->die_mark)
6336 return die1->die_mark == die2->die_mark;
6337 die1->die_mark = die2->die_mark = ++(*mark);
6338
6339 if (die1->die_tag != die2->die_tag)
6340 return 0;
6341
6342 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6343 return 0;
6344
6345 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6346 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6347 return 0;
6348
6349 c1 = die1->die_child;
6350 c2 = die2->die_child;
6351 if (! c1)
6352 {
6353 if (c2)
6354 return 0;
6355 }
6356 else
6357 for (;;)
6358 {
6359 if (!same_die_p (c1, c2, mark))
6360 return 0;
6361 c1 = c1->die_sib;
6362 c2 = c2->die_sib;
6363 if (c1 == die1->die_child)
6364 {
6365 if (c2 == die2->die_child)
6366 break;
6367 else
6368 return 0;
6369 }
6370 }
6371
6372 return 1;
6373 }
6374
6375 /* Do the dies look the same? Wrapper around same_die_p. */
6376
6377 static int
6378 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6379 {
6380 int mark = 0;
6381 int ret = same_die_p (die1, die2, &mark);
6382
6383 unmark_all_dies (die1);
6384 unmark_all_dies (die2);
6385
6386 return ret;
6387 }
6388
6389 /* The prefix to attach to symbols on DIEs in the current comdat debug
6390 info section. */
6391 static const char *comdat_symbol_id;
6392
6393 /* The index of the current symbol within the current comdat CU. */
6394 static unsigned int comdat_symbol_number;
6395
6396 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6397 children, and set comdat_symbol_id accordingly. */
6398
6399 static void
6400 compute_section_prefix (dw_die_ref unit_die)
6401 {
6402 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6403 const char *base = die_name ? lbasename (die_name) : "anonymous";
6404 char *name = XALLOCAVEC (char, strlen (base) + 64);
6405 char *p;
6406 int i, mark;
6407 unsigned char checksum[16];
6408 struct md5_ctx ctx;
6409
6410 /* Compute the checksum of the DIE, then append part of it as hex digits to
6411 the name filename of the unit. */
6412
6413 md5_init_ctx (&ctx);
6414 mark = 0;
6415 die_checksum (unit_die, &ctx, &mark);
6416 unmark_all_dies (unit_die);
6417 md5_finish_ctx (&ctx, checksum);
6418
6419 sprintf (name, "%s.", base);
6420 clean_symbol_name (name);
6421
6422 p = name + strlen (name);
6423 for (i = 0; i < 4; i++)
6424 {
6425 sprintf (p, "%.2x", checksum[i]);
6426 p += 2;
6427 }
6428
6429 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6430 comdat_symbol_number = 0;
6431 }
6432
6433 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6434
6435 static int
6436 is_type_die (dw_die_ref die)
6437 {
6438 switch (die->die_tag)
6439 {
6440 case DW_TAG_array_type:
6441 case DW_TAG_class_type:
6442 case DW_TAG_interface_type:
6443 case DW_TAG_enumeration_type:
6444 case DW_TAG_pointer_type:
6445 case DW_TAG_reference_type:
6446 case DW_TAG_rvalue_reference_type:
6447 case DW_TAG_string_type:
6448 case DW_TAG_structure_type:
6449 case DW_TAG_subroutine_type:
6450 case DW_TAG_union_type:
6451 case DW_TAG_ptr_to_member_type:
6452 case DW_TAG_set_type:
6453 case DW_TAG_subrange_type:
6454 case DW_TAG_base_type:
6455 case DW_TAG_const_type:
6456 case DW_TAG_file_type:
6457 case DW_TAG_packed_type:
6458 case DW_TAG_volatile_type:
6459 case DW_TAG_typedef:
6460 return 1;
6461 default:
6462 return 0;
6463 }
6464 }
6465
6466 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6467 Basically, we want to choose the bits that are likely to be shared between
6468 compilations (types) and leave out the bits that are specific to individual
6469 compilations (functions). */
6470
6471 static int
6472 is_comdat_die (dw_die_ref c)
6473 {
6474 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6475 we do for stabs. The advantage is a greater likelihood of sharing between
6476 objects that don't include headers in the same order (and therefore would
6477 put the base types in a different comdat). jason 8/28/00 */
6478
6479 if (c->die_tag == DW_TAG_base_type)
6480 return 0;
6481
6482 if (c->die_tag == DW_TAG_pointer_type
6483 || c->die_tag == DW_TAG_reference_type
6484 || c->die_tag == DW_TAG_rvalue_reference_type
6485 || c->die_tag == DW_TAG_const_type
6486 || c->die_tag == DW_TAG_volatile_type)
6487 {
6488 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6489
6490 return t ? is_comdat_die (t) : 0;
6491 }
6492
6493 return is_type_die (c);
6494 }
6495
6496 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6497 compilation unit. */
6498
6499 static int
6500 is_symbol_die (dw_die_ref c)
6501 {
6502 return (is_type_die (c)
6503 || is_declaration_die (c)
6504 || c->die_tag == DW_TAG_namespace
6505 || c->die_tag == DW_TAG_module);
6506 }
6507
6508 /* Returns true iff C is a compile-unit DIE. */
6509
6510 static inline bool
6511 is_cu_die (dw_die_ref c)
6512 {
6513 return c && c->die_tag == DW_TAG_compile_unit;
6514 }
6515
6516 /* Returns true iff C is a unit DIE of some sort. */
6517
6518 static inline bool
6519 is_unit_die (dw_die_ref c)
6520 {
6521 return c && (c->die_tag == DW_TAG_compile_unit
6522 || c->die_tag == DW_TAG_partial_unit
6523 || c->die_tag == DW_TAG_type_unit);
6524 }
6525
6526 /* Returns true iff C is a namespace DIE. */
6527
6528 static inline bool
6529 is_namespace_die (dw_die_ref c)
6530 {
6531 return c && c->die_tag == DW_TAG_namespace;
6532 }
6533
6534 /* Returns true iff C is a class or structure DIE. */
6535
6536 static inline bool
6537 is_class_die (dw_die_ref c)
6538 {
6539 return c && (c->die_tag == DW_TAG_class_type
6540 || c->die_tag == DW_TAG_structure_type);
6541 }
6542
6543 /* Return non-zero if this DIE is a template parameter. */
6544
6545 static inline bool
6546 is_template_parameter (dw_die_ref die)
6547 {
6548 switch (die->die_tag)
6549 {
6550 case DW_TAG_template_type_param:
6551 case DW_TAG_template_value_param:
6552 case DW_TAG_GNU_template_template_param:
6553 case DW_TAG_GNU_template_parameter_pack:
6554 return true;
6555 default:
6556 return false;
6557 }
6558 }
6559
6560 /* Return non-zero if this DIE represents a template instantiation. */
6561
6562 static inline bool
6563 is_template_instantiation (dw_die_ref die)
6564 {
6565 dw_die_ref c;
6566
6567 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6568 return false;
6569 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6570 return false;
6571 }
6572
6573 static char *
6574 gen_internal_sym (const char *prefix)
6575 {
6576 char buf[256];
6577
6578 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6579 return xstrdup (buf);
6580 }
6581
6582 /* Assign symbols to all worthy DIEs under DIE. */
6583
6584 static void
6585 assign_symbol_names (dw_die_ref die)
6586 {
6587 dw_die_ref c;
6588
6589 if (is_symbol_die (die) && !die->comdat_type_p)
6590 {
6591 if (comdat_symbol_id)
6592 {
6593 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6594
6595 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6596 comdat_symbol_id, comdat_symbol_number++);
6597 die->die_id.die_symbol = xstrdup (p);
6598 }
6599 else
6600 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6601 }
6602
6603 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6604 }
6605
6606 struct cu_hash_table_entry
6607 {
6608 dw_die_ref cu;
6609 unsigned min_comdat_num, max_comdat_num;
6610 struct cu_hash_table_entry *next;
6611 };
6612
6613 /* Helpers to manipulate hash table of CUs. */
6614
6615 struct cu_hash_table_entry_hasher
6616 {
6617 typedef cu_hash_table_entry value_type;
6618 typedef die_struct compare_type;
6619 static inline hashval_t hash (const value_type *);
6620 static inline bool equal (const value_type *, const compare_type *);
6621 static inline void remove (value_type *);
6622 };
6623
6624 inline hashval_t
6625 cu_hash_table_entry_hasher::hash (const value_type *entry)
6626 {
6627 return htab_hash_string (entry->cu->die_id.die_symbol);
6628 }
6629
6630 inline bool
6631 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6632 const compare_type *entry2)
6633 {
6634 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6635 }
6636
6637 inline void
6638 cu_hash_table_entry_hasher::remove (value_type *entry)
6639 {
6640 struct cu_hash_table_entry *next;
6641
6642 while (entry)
6643 {
6644 next = entry->next;
6645 free (entry);
6646 entry = next;
6647 }
6648 }
6649
6650 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6651
6652 /* Check whether we have already seen this CU and set up SYM_NUM
6653 accordingly. */
6654 static int
6655 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6656 {
6657 struct cu_hash_table_entry dummy;
6658 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6659
6660 dummy.max_comdat_num = 0;
6661
6662 slot = htable.find_slot_with_hash (cu,
6663 htab_hash_string (cu->die_id.die_symbol),
6664 INSERT);
6665 entry = *slot;
6666
6667 for (; entry; last = entry, entry = entry->next)
6668 {
6669 if (same_die_p_wrap (cu, entry->cu))
6670 break;
6671 }
6672
6673 if (entry)
6674 {
6675 *sym_num = entry->min_comdat_num;
6676 return 1;
6677 }
6678
6679 entry = XCNEW (struct cu_hash_table_entry);
6680 entry->cu = cu;
6681 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6682 entry->next = *slot;
6683 *slot = entry;
6684
6685 return 0;
6686 }
6687
6688 /* Record SYM_NUM to record of CU in HTABLE. */
6689 static void
6690 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6691 unsigned int sym_num)
6692 {
6693 struct cu_hash_table_entry **slot, *entry;
6694
6695 slot = htable.find_slot_with_hash (cu,
6696 htab_hash_string (cu->die_id.die_symbol),
6697 NO_INSERT);
6698 entry = *slot;
6699
6700 entry->max_comdat_num = sym_num;
6701 }
6702
6703 /* Traverse the DIE (which is always comp_unit_die), and set up
6704 additional compilation units for each of the include files we see
6705 bracketed by BINCL/EINCL. */
6706
6707 static void
6708 break_out_includes (dw_die_ref die)
6709 {
6710 dw_die_ref c;
6711 dw_die_ref unit = NULL;
6712 limbo_die_node *node, **pnode;
6713 cu_hash_type cu_hash_table;
6714
6715 c = die->die_child;
6716 if (c) do {
6717 dw_die_ref prev = c;
6718 c = c->die_sib;
6719 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6720 || (unit && is_comdat_die (c)))
6721 {
6722 dw_die_ref next = c->die_sib;
6723
6724 /* This DIE is for a secondary CU; remove it from the main one. */
6725 remove_child_with_prev (c, prev);
6726
6727 if (c->die_tag == DW_TAG_GNU_BINCL)
6728 unit = push_new_compile_unit (unit, c);
6729 else if (c->die_tag == DW_TAG_GNU_EINCL)
6730 unit = pop_compile_unit (unit);
6731 else
6732 add_child_die (unit, c);
6733 c = next;
6734 if (c == die->die_child)
6735 break;
6736 }
6737 } while (c != die->die_child);
6738
6739 #if 0
6740 /* We can only use this in debugging, since the frontend doesn't check
6741 to make sure that we leave every include file we enter. */
6742 gcc_assert (!unit);
6743 #endif
6744
6745 assign_symbol_names (die);
6746 cu_hash_table.create (10);
6747 for (node = limbo_die_list, pnode = &limbo_die_list;
6748 node;
6749 node = node->next)
6750 {
6751 int is_dupl;
6752
6753 compute_section_prefix (node->die);
6754 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6755 &comdat_symbol_number);
6756 assign_symbol_names (node->die);
6757 if (is_dupl)
6758 *pnode = node->next;
6759 else
6760 {
6761 pnode = &node->next;
6762 record_comdat_symbol_number (node->die, cu_hash_table,
6763 comdat_symbol_number);
6764 }
6765 }
6766 cu_hash_table.dispose ();
6767 }
6768
6769 /* Return non-zero if this DIE is a declaration. */
6770
6771 static int
6772 is_declaration_die (dw_die_ref die)
6773 {
6774 dw_attr_ref a;
6775 unsigned ix;
6776
6777 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6778 if (a->dw_attr == DW_AT_declaration)
6779 return 1;
6780
6781 return 0;
6782 }
6783
6784 /* Return non-zero if this DIE is nested inside a subprogram. */
6785
6786 static int
6787 is_nested_in_subprogram (dw_die_ref die)
6788 {
6789 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6790
6791 if (decl == NULL)
6792 decl = die;
6793 return local_scope_p (decl);
6794 }
6795
6796 /* Return non-zero if this DIE contains a defining declaration of a
6797 subprogram. */
6798
6799 static int
6800 contains_subprogram_definition (dw_die_ref die)
6801 {
6802 dw_die_ref c;
6803
6804 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6805 return 1;
6806 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6807 return 0;
6808 }
6809
6810 /* Return non-zero if this is a type DIE that should be moved to a
6811 COMDAT .debug_types section. */
6812
6813 static int
6814 should_move_die_to_comdat (dw_die_ref die)
6815 {
6816 switch (die->die_tag)
6817 {
6818 case DW_TAG_class_type:
6819 case DW_TAG_structure_type:
6820 case DW_TAG_enumeration_type:
6821 case DW_TAG_union_type:
6822 /* Don't move declarations, inlined instances, or types nested in a
6823 subprogram. */
6824 if (is_declaration_die (die)
6825 || get_AT (die, DW_AT_abstract_origin)
6826 || is_nested_in_subprogram (die))
6827 return 0;
6828 /* A type definition should never contain a subprogram definition. */
6829 gcc_assert (!contains_subprogram_definition (die));
6830 return 1;
6831 case DW_TAG_array_type:
6832 case DW_TAG_interface_type:
6833 case DW_TAG_pointer_type:
6834 case DW_TAG_reference_type:
6835 case DW_TAG_rvalue_reference_type:
6836 case DW_TAG_string_type:
6837 case DW_TAG_subroutine_type:
6838 case DW_TAG_ptr_to_member_type:
6839 case DW_TAG_set_type:
6840 case DW_TAG_subrange_type:
6841 case DW_TAG_base_type:
6842 case DW_TAG_const_type:
6843 case DW_TAG_file_type:
6844 case DW_TAG_packed_type:
6845 case DW_TAG_volatile_type:
6846 case DW_TAG_typedef:
6847 default:
6848 return 0;
6849 }
6850 }
6851
6852 /* Make a clone of DIE. */
6853
6854 static dw_die_ref
6855 clone_die (dw_die_ref die)
6856 {
6857 dw_die_ref clone;
6858 dw_attr_ref a;
6859 unsigned ix;
6860
6861 clone = ggc_alloc_cleared_die_node ();
6862 clone->die_tag = die->die_tag;
6863
6864 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6865 add_dwarf_attr (clone, a);
6866
6867 return clone;
6868 }
6869
6870 /* Make a clone of the tree rooted at DIE. */
6871
6872 static dw_die_ref
6873 clone_tree (dw_die_ref die)
6874 {
6875 dw_die_ref c;
6876 dw_die_ref clone = clone_die (die);
6877
6878 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6879
6880 return clone;
6881 }
6882
6883 /* Make a clone of DIE as a declaration. */
6884
6885 static dw_die_ref
6886 clone_as_declaration (dw_die_ref die)
6887 {
6888 dw_die_ref clone;
6889 dw_die_ref decl;
6890 dw_attr_ref a;
6891 unsigned ix;
6892
6893 /* If the DIE is already a declaration, just clone it. */
6894 if (is_declaration_die (die))
6895 return clone_die (die);
6896
6897 /* If the DIE is a specification, just clone its declaration DIE. */
6898 decl = get_AT_ref (die, DW_AT_specification);
6899 if (decl != NULL)
6900 {
6901 clone = clone_die (decl);
6902 if (die->comdat_type_p)
6903 add_AT_die_ref (clone, DW_AT_signature, die);
6904 return clone;
6905 }
6906
6907 clone = ggc_alloc_cleared_die_node ();
6908 clone->die_tag = die->die_tag;
6909
6910 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6911 {
6912 /* We don't want to copy over all attributes.
6913 For example we don't want DW_AT_byte_size because otherwise we will no
6914 longer have a declaration and GDB will treat it as a definition. */
6915
6916 switch (a->dw_attr)
6917 {
6918 case DW_AT_artificial:
6919 case DW_AT_containing_type:
6920 case DW_AT_external:
6921 case DW_AT_name:
6922 case DW_AT_type:
6923 case DW_AT_virtuality:
6924 case DW_AT_linkage_name:
6925 case DW_AT_MIPS_linkage_name:
6926 add_dwarf_attr (clone, a);
6927 break;
6928 case DW_AT_byte_size:
6929 default:
6930 break;
6931 }
6932 }
6933
6934 if (die->comdat_type_p)
6935 add_AT_die_ref (clone, DW_AT_signature, die);
6936
6937 add_AT_flag (clone, DW_AT_declaration, 1);
6938 return clone;
6939 }
6940
6941
6942 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
6943
6944 struct decl_table_entry
6945 {
6946 dw_die_ref orig;
6947 dw_die_ref copy;
6948 };
6949
6950 /* Helpers to manipulate hash table of copied declarations. */
6951
6952 /* Hashtable helpers. */
6953
6954 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
6955 {
6956 typedef decl_table_entry value_type;
6957 typedef die_struct compare_type;
6958 static inline hashval_t hash (const value_type *);
6959 static inline bool equal (const value_type *, const compare_type *);
6960 };
6961
6962 inline hashval_t
6963 decl_table_entry_hasher::hash (const value_type *entry)
6964 {
6965 return htab_hash_pointer (entry->orig);
6966 }
6967
6968 inline bool
6969 decl_table_entry_hasher::equal (const value_type *entry1,
6970 const compare_type *entry2)
6971 {
6972 return entry1->orig == entry2;
6973 }
6974
6975 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
6976
6977 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6978 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
6979 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
6980 to check if the ancestor has already been copied into UNIT. */
6981
6982 static dw_die_ref
6983 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
6984 {
6985 dw_die_ref parent = die->die_parent;
6986 dw_die_ref new_parent = unit;
6987 dw_die_ref copy;
6988 decl_table_entry **slot = NULL;
6989 struct decl_table_entry *entry = NULL;
6990
6991 if (decl_table.is_created ())
6992 {
6993 /* Check if the entry has already been copied to UNIT. */
6994 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
6995 INSERT);
6996 if (*slot != HTAB_EMPTY_ENTRY)
6997 {
6998 entry = *slot;
6999 return entry->copy;
7000 }
7001
7002 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7003 entry = XCNEW (struct decl_table_entry);
7004 entry->orig = die;
7005 entry->copy = NULL;
7006 *slot = entry;
7007 }
7008
7009 if (parent != NULL)
7010 {
7011 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7012 if (spec != NULL)
7013 parent = spec;
7014 if (!is_unit_die (parent))
7015 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7016 }
7017
7018 copy = clone_as_declaration (die);
7019 add_child_die (new_parent, copy);
7020
7021 if (decl_table.is_created ())
7022 {
7023 /* Record the pointer to the copy. */
7024 entry->copy = copy;
7025 }
7026
7027 return copy;
7028 }
7029 /* Copy the declaration context to the new type unit DIE. This includes
7030 any surrounding namespace or type declarations. If the DIE has an
7031 AT_specification attribute, it also includes attributes and children
7032 attached to the specification, and returns a pointer to the original
7033 parent of the declaration DIE. Returns NULL otherwise. */
7034
7035 static dw_die_ref
7036 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7037 {
7038 dw_die_ref decl;
7039 dw_die_ref new_decl;
7040 dw_die_ref orig_parent = NULL;
7041
7042 decl = get_AT_ref (die, DW_AT_specification);
7043 if (decl == NULL)
7044 decl = die;
7045 else
7046 {
7047 unsigned ix;
7048 dw_die_ref c;
7049 dw_attr_ref a;
7050
7051 /* The original DIE will be changed to a declaration, and must
7052 be moved to be a child of the original declaration DIE. */
7053 orig_parent = decl->die_parent;
7054
7055 /* Copy the type node pointer from the new DIE to the original
7056 declaration DIE so we can forward references later. */
7057 decl->comdat_type_p = true;
7058 decl->die_id.die_type_node = die->die_id.die_type_node;
7059
7060 remove_AT (die, DW_AT_specification);
7061
7062 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7063 {
7064 if (a->dw_attr != DW_AT_name
7065 && a->dw_attr != DW_AT_declaration
7066 && a->dw_attr != DW_AT_external)
7067 add_dwarf_attr (die, a);
7068 }
7069
7070 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7071 }
7072
7073 if (decl->die_parent != NULL
7074 && !is_unit_die (decl->die_parent))
7075 {
7076 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7077 if (new_decl != NULL)
7078 {
7079 remove_AT (new_decl, DW_AT_signature);
7080 add_AT_specification (die, new_decl);
7081 }
7082 }
7083
7084 return orig_parent;
7085 }
7086
7087 /* Generate the skeleton ancestor tree for the given NODE, then clone
7088 the DIE and add the clone into the tree. */
7089
7090 static void
7091 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7092 {
7093 if (node->new_die != NULL)
7094 return;
7095
7096 node->new_die = clone_as_declaration (node->old_die);
7097
7098 if (node->parent != NULL)
7099 {
7100 generate_skeleton_ancestor_tree (node->parent);
7101 add_child_die (node->parent->new_die, node->new_die);
7102 }
7103 }
7104
7105 /* Generate a skeleton tree of DIEs containing any declarations that are
7106 found in the original tree. We traverse the tree looking for declaration
7107 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7108
7109 static void
7110 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7111 {
7112 skeleton_chain_node node;
7113 dw_die_ref c;
7114 dw_die_ref first;
7115 dw_die_ref prev = NULL;
7116 dw_die_ref next = NULL;
7117
7118 node.parent = parent;
7119
7120 first = c = parent->old_die->die_child;
7121 if (c)
7122 next = c->die_sib;
7123 if (c) do {
7124 if (prev == NULL || prev->die_sib == c)
7125 prev = c;
7126 c = next;
7127 next = (c == first ? NULL : c->die_sib);
7128 node.old_die = c;
7129 node.new_die = NULL;
7130 if (is_declaration_die (c))
7131 {
7132 if (is_template_instantiation (c))
7133 {
7134 /* Instantiated templates do not need to be cloned into the
7135 type unit. Just move the DIE and its children back to
7136 the skeleton tree (in the main CU). */
7137 remove_child_with_prev (c, prev);
7138 add_child_die (parent->new_die, c);
7139 c = prev;
7140 }
7141 else
7142 {
7143 /* Clone the existing DIE, move the original to the skeleton
7144 tree (which is in the main CU), and put the clone, with
7145 all the original's children, where the original came from
7146 (which is about to be moved to the type unit). */
7147 dw_die_ref clone = clone_die (c);
7148 move_all_children (c, clone);
7149
7150 replace_child (c, clone, prev);
7151 generate_skeleton_ancestor_tree (parent);
7152 add_child_die (parent->new_die, c);
7153 node.new_die = c;
7154 c = clone;
7155 }
7156 }
7157 generate_skeleton_bottom_up (&node);
7158 } while (next != NULL);
7159 }
7160
7161 /* Wrapper function for generate_skeleton_bottom_up. */
7162
7163 static dw_die_ref
7164 generate_skeleton (dw_die_ref die)
7165 {
7166 skeleton_chain_node node;
7167
7168 node.old_die = die;
7169 node.new_die = NULL;
7170 node.parent = NULL;
7171
7172 /* If this type definition is nested inside another type,
7173 and is not an instantiation of a template, always leave
7174 at least a declaration in its place. */
7175 if (die->die_parent != NULL
7176 && is_type_die (die->die_parent)
7177 && !is_template_instantiation (die))
7178 node.new_die = clone_as_declaration (die);
7179
7180 generate_skeleton_bottom_up (&node);
7181 return node.new_die;
7182 }
7183
7184 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7185 declaration. The original DIE is moved to a new compile unit so that
7186 existing references to it follow it to the new location. If any of the
7187 original DIE's descendants is a declaration, we need to replace the
7188 original DIE with a skeleton tree and move the declarations back into the
7189 skeleton tree. */
7190
7191 static dw_die_ref
7192 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7193 dw_die_ref prev)
7194 {
7195 dw_die_ref skeleton, orig_parent;
7196
7197 /* Copy the declaration context to the type unit DIE. If the returned
7198 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7199 that DIE. */
7200 orig_parent = copy_declaration_context (unit, child);
7201
7202 skeleton = generate_skeleton (child);
7203 if (skeleton == NULL)
7204 remove_child_with_prev (child, prev);
7205 else
7206 {
7207 skeleton->comdat_type_p = true;
7208 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7209
7210 /* If the original DIE was a specification, we need to put
7211 the skeleton under the parent DIE of the declaration.
7212 This leaves the original declaration in the tree, but
7213 it will be pruned later since there are no longer any
7214 references to it. */
7215 if (orig_parent != NULL)
7216 {
7217 remove_child_with_prev (child, prev);
7218 add_child_die (orig_parent, skeleton);
7219 }
7220 else
7221 replace_child (child, skeleton, prev);
7222 }
7223
7224 return skeleton;
7225 }
7226
7227 /* Traverse the DIE and set up additional .debug_types sections for each
7228 type worthy of being placed in a COMDAT section. */
7229
7230 static void
7231 break_out_comdat_types (dw_die_ref die)
7232 {
7233 dw_die_ref c;
7234 dw_die_ref first;
7235 dw_die_ref prev = NULL;
7236 dw_die_ref next = NULL;
7237 dw_die_ref unit = NULL;
7238
7239 first = c = die->die_child;
7240 if (c)
7241 next = c->die_sib;
7242 if (c) do {
7243 if (prev == NULL || prev->die_sib == c)
7244 prev = c;
7245 c = next;
7246 next = (c == first ? NULL : c->die_sib);
7247 if (should_move_die_to_comdat (c))
7248 {
7249 dw_die_ref replacement;
7250 comdat_type_node_ref type_node;
7251
7252 /* Break out nested types into their own type units. */
7253 break_out_comdat_types (c);
7254
7255 /* Create a new type unit DIE as the root for the new tree, and
7256 add it to the list of comdat types. */
7257 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7258 add_AT_unsigned (unit, DW_AT_language,
7259 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7260 type_node = ggc_alloc_cleared_comdat_type_node ();
7261 type_node->root_die = unit;
7262 type_node->next = comdat_type_list;
7263 comdat_type_list = type_node;
7264
7265 /* Generate the type signature. */
7266 generate_type_signature (c, type_node);
7267
7268 /* Copy the declaration context, attributes, and children of the
7269 declaration into the new type unit DIE, then remove this DIE
7270 from the main CU (or replace it with a skeleton if necessary). */
7271 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7272 type_node->skeleton_die = replacement;
7273
7274 /* Add the DIE to the new compunit. */
7275 add_child_die (unit, c);
7276
7277 if (replacement != NULL)
7278 c = replacement;
7279 }
7280 else if (c->die_tag == DW_TAG_namespace
7281 || c->die_tag == DW_TAG_class_type
7282 || c->die_tag == DW_TAG_structure_type
7283 || c->die_tag == DW_TAG_union_type)
7284 {
7285 /* Look for nested types that can be broken out. */
7286 break_out_comdat_types (c);
7287 }
7288 } while (next != NULL);
7289 }
7290
7291 /* Like clone_tree, but additionally enter all the children into
7292 the hash table decl_table. */
7293
7294 static dw_die_ref
7295 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7296 {
7297 dw_die_ref c;
7298 dw_die_ref clone = clone_die (die);
7299 struct decl_table_entry *entry;
7300 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7301 htab_hash_pointer (die), INSERT);
7302 /* Assert that DIE isn't in the hash table yet. If it would be there
7303 before, the ancestors would be necessarily there as well, therefore
7304 clone_tree_hash wouldn't be called. */
7305 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7306 entry = XCNEW (struct decl_table_entry);
7307 entry->orig = die;
7308 entry->copy = clone;
7309 *slot = entry;
7310
7311 FOR_EACH_CHILD (die, c,
7312 add_child_die (clone, clone_tree_hash (c, decl_table)));
7313
7314 return clone;
7315 }
7316
7317 /* Walk the DIE and its children, looking for references to incomplete
7318 or trivial types that are unmarked (i.e., that are not in the current
7319 type_unit). */
7320
7321 static void
7322 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7323 {
7324 dw_die_ref c;
7325 dw_attr_ref a;
7326 unsigned ix;
7327
7328 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7329 {
7330 if (AT_class (a) == dw_val_class_die_ref)
7331 {
7332 dw_die_ref targ = AT_ref (a);
7333 decl_table_entry **slot;
7334 struct decl_table_entry *entry;
7335
7336 if (targ->die_mark != 0 || targ->comdat_type_p)
7337 continue;
7338
7339 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7340 INSERT);
7341
7342 if (*slot != HTAB_EMPTY_ENTRY)
7343 {
7344 /* TARG has already been copied, so we just need to
7345 modify the reference to point to the copy. */
7346 entry = *slot;
7347 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7348 }
7349 else
7350 {
7351 dw_die_ref parent = unit;
7352 dw_die_ref copy = clone_die (targ);
7353
7354 /* Record in DECL_TABLE that TARG has been copied.
7355 Need to do this now, before the recursive call,
7356 because DECL_TABLE may be expanded and SLOT
7357 would no longer be a valid pointer. */
7358 entry = XCNEW (struct decl_table_entry);
7359 entry->orig = targ;
7360 entry->copy = copy;
7361 *slot = entry;
7362
7363 FOR_EACH_CHILD (targ, c,
7364 add_child_die (copy,
7365 clone_tree_hash (c, decl_table)));
7366
7367 /* Make sure the cloned tree is marked as part of the
7368 type unit. */
7369 mark_dies (copy);
7370
7371 /* If TARG has surrounding context, copy its ancestor tree
7372 into the new type unit. */
7373 if (targ->die_parent != NULL
7374 && !is_unit_die (targ->die_parent))
7375 parent = copy_ancestor_tree (unit, targ->die_parent,
7376 decl_table);
7377
7378 add_child_die (parent, copy);
7379 a->dw_attr_val.v.val_die_ref.die = copy;
7380
7381 /* Make sure the newly-copied DIE is walked. If it was
7382 installed in a previously-added context, it won't
7383 get visited otherwise. */
7384 if (parent != unit)
7385 {
7386 /* Find the highest point of the newly-added tree,
7387 mark each node along the way, and walk from there. */
7388 parent->die_mark = 1;
7389 while (parent->die_parent
7390 && parent->die_parent->die_mark == 0)
7391 {
7392 parent = parent->die_parent;
7393 parent->die_mark = 1;
7394 }
7395 copy_decls_walk (unit, parent, decl_table);
7396 }
7397 }
7398 }
7399 }
7400
7401 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7402 }
7403
7404 /* Copy declarations for "unworthy" types into the new comdat section.
7405 Incomplete types, modified types, and certain other types aren't broken
7406 out into comdat sections of their own, so they don't have a signature,
7407 and we need to copy the declaration into the same section so that we
7408 don't have an external reference. */
7409
7410 static void
7411 copy_decls_for_unworthy_types (dw_die_ref unit)
7412 {
7413 decl_hash_type decl_table;
7414
7415 mark_dies (unit);
7416 decl_table.create (10);
7417 copy_decls_walk (unit, unit, decl_table);
7418 decl_table.dispose ();
7419 unmark_dies (unit);
7420 }
7421
7422 /* Traverse the DIE and add a sibling attribute if it may have the
7423 effect of speeding up access to siblings. To save some space,
7424 avoid generating sibling attributes for DIE's without children. */
7425
7426 static void
7427 add_sibling_attributes (dw_die_ref die)
7428 {
7429 dw_die_ref c;
7430
7431 if (! die->die_child)
7432 return;
7433
7434 if (die->die_parent && die != die->die_parent->die_child)
7435 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7436
7437 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7438 }
7439
7440 /* Output all location lists for the DIE and its children. */
7441
7442 static void
7443 output_location_lists (dw_die_ref die)
7444 {
7445 dw_die_ref c;
7446 dw_attr_ref a;
7447 unsigned ix;
7448
7449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7450 if (AT_class (a) == dw_val_class_loc_list)
7451 output_loc_list (AT_loc_list (a));
7452
7453 FOR_EACH_CHILD (die, c, output_location_lists (c));
7454 }
7455
7456 /* We want to limit the number of external references, because they are
7457 larger than local references: a relocation takes multiple words, and
7458 even a sig8 reference is always eight bytes, whereas a local reference
7459 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7460 So if we encounter multiple external references to the same type DIE, we
7461 make a local typedef stub for it and redirect all references there.
7462
7463 This is the element of the hash table for keeping track of these
7464 references. */
7465
7466 struct external_ref
7467 {
7468 dw_die_ref type;
7469 dw_die_ref stub;
7470 unsigned n_refs;
7471 };
7472
7473 /* Hashtable helpers. */
7474
7475 struct external_ref_hasher : typed_free_remove <external_ref>
7476 {
7477 typedef external_ref value_type;
7478 typedef external_ref compare_type;
7479 static inline hashval_t hash (const value_type *);
7480 static inline bool equal (const value_type *, const compare_type *);
7481 };
7482
7483 inline hashval_t
7484 external_ref_hasher::hash (const value_type *r)
7485 {
7486 dw_die_ref die = r->type;
7487 hashval_t h = 0;
7488
7489 /* We can't use the address of the DIE for hashing, because
7490 that will make the order of the stub DIEs non-deterministic. */
7491 if (! die->comdat_type_p)
7492 /* We have a symbol; use it to compute a hash. */
7493 h = htab_hash_string (die->die_id.die_symbol);
7494 else
7495 {
7496 /* We have a type signature; use a subset of the bits as the hash.
7497 The 8-byte signature is at least as large as hashval_t. */
7498 comdat_type_node_ref type_node = die->die_id.die_type_node;
7499 memcpy (&h, type_node->signature, sizeof (h));
7500 }
7501 return h;
7502 }
7503
7504 inline bool
7505 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7506 {
7507 return r1->type == r2->type;
7508 }
7509
7510 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7511
7512 /* Return a pointer to the external_ref for references to DIE. */
7513
7514 static struct external_ref *
7515 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7516 {
7517 struct external_ref ref, *ref_p;
7518 external_ref **slot;
7519
7520 ref.type = die;
7521 slot = map.find_slot (&ref, INSERT);
7522 if (*slot != HTAB_EMPTY_ENTRY)
7523 return *slot;
7524
7525 ref_p = XCNEW (struct external_ref);
7526 ref_p->type = die;
7527 *slot = ref_p;
7528 return ref_p;
7529 }
7530
7531 /* Subroutine of optimize_external_refs, below.
7532
7533 If we see a type skeleton, record it as our stub. If we see external
7534 references, remember how many we've seen. */
7535
7536 static void
7537 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7538 {
7539 dw_die_ref c;
7540 dw_attr_ref a;
7541 unsigned ix;
7542 struct external_ref *ref_p;
7543
7544 if (is_type_die (die)
7545 && (c = get_AT_ref (die, DW_AT_signature)))
7546 {
7547 /* This is a local skeleton; use it for local references. */
7548 ref_p = lookup_external_ref (map, c);
7549 ref_p->stub = die;
7550 }
7551
7552 /* Scan the DIE references, and remember any that refer to DIEs from
7553 other CUs (i.e. those which are not marked). */
7554 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7555 if (AT_class (a) == dw_val_class_die_ref
7556 && (c = AT_ref (a))->die_mark == 0
7557 && is_type_die (c))
7558 {
7559 ref_p = lookup_external_ref (map, c);
7560 ref_p->n_refs++;
7561 }
7562
7563 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7564 }
7565
7566 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7567 points to an external_ref, DATA is the CU we're processing. If we don't
7568 already have a local stub, and we have multiple refs, build a stub. */
7569
7570 int
7571 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7572 {
7573 struct external_ref *ref_p = *slot;
7574
7575 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7576 {
7577 /* We have multiple references to this type, so build a small stub.
7578 Both of these forms are a bit dodgy from the perspective of the
7579 DWARF standard, since technically they should have names. */
7580 dw_die_ref cu = data;
7581 dw_die_ref type = ref_p->type;
7582 dw_die_ref stub = NULL;
7583
7584 if (type->comdat_type_p)
7585 {
7586 /* If we refer to this type via sig8, use AT_signature. */
7587 stub = new_die (type->die_tag, cu, NULL_TREE);
7588 add_AT_die_ref (stub, DW_AT_signature, type);
7589 }
7590 else
7591 {
7592 /* Otherwise, use a typedef with no name. */
7593 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7594 add_AT_die_ref (stub, DW_AT_type, type);
7595 }
7596
7597 stub->die_mark++;
7598 ref_p->stub = stub;
7599 }
7600 return 1;
7601 }
7602
7603 /* DIE is a unit; look through all the DIE references to see if there are
7604 any external references to types, and if so, create local stubs for
7605 them which will be applied in build_abbrev_table. This is useful because
7606 references to local DIEs are smaller. */
7607
7608 static external_ref_hash_type
7609 optimize_external_refs (dw_die_ref die)
7610 {
7611 external_ref_hash_type map;
7612 map.create (10);
7613 optimize_external_refs_1 (die, map);
7614 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7615 return map;
7616 }
7617
7618 /* The format of each DIE (and its attribute value pairs) is encoded in an
7619 abbreviation table. This routine builds the abbreviation table and assigns
7620 a unique abbreviation id for each abbreviation entry. The children of each
7621 die are visited recursively. */
7622
7623 static void
7624 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7625 {
7626 unsigned long abbrev_id;
7627 unsigned int n_alloc;
7628 dw_die_ref c;
7629 dw_attr_ref a;
7630 unsigned ix;
7631
7632 /* Scan the DIE references, and replace any that refer to
7633 DIEs from other CUs (i.e. those which are not marked) with
7634 the local stubs we built in optimize_external_refs. */
7635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7636 if (AT_class (a) == dw_val_class_die_ref
7637 && (c = AT_ref (a))->die_mark == 0)
7638 {
7639 struct external_ref *ref_p;
7640 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7641
7642 ref_p = lookup_external_ref (extern_map, c);
7643 if (ref_p->stub && ref_p->stub != die)
7644 change_AT_die_ref (a, ref_p->stub);
7645 else
7646 /* We aren't changing this reference, so mark it external. */
7647 set_AT_ref_external (a, 1);
7648 }
7649
7650 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7651 {
7652 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7653 dw_attr_ref die_a, abbrev_a;
7654 unsigned ix;
7655 bool ok = true;
7656
7657 if (abbrev->die_tag != die->die_tag)
7658 continue;
7659 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7660 continue;
7661
7662 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7663 continue;
7664
7665 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7666 {
7667 abbrev_a = &(*abbrev->die_attr)[ix];
7668 if ((abbrev_a->dw_attr != die_a->dw_attr)
7669 || (value_format (abbrev_a) != value_format (die_a)))
7670 {
7671 ok = false;
7672 break;
7673 }
7674 }
7675 if (ok)
7676 break;
7677 }
7678
7679 if (abbrev_id >= abbrev_die_table_in_use)
7680 {
7681 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7682 {
7683 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7684 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7685 n_alloc);
7686
7687 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7688 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7689 abbrev_die_table_allocated = n_alloc;
7690 }
7691
7692 ++abbrev_die_table_in_use;
7693 abbrev_die_table[abbrev_id] = die;
7694 }
7695
7696 die->die_abbrev = abbrev_id;
7697 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7698 }
7699 \f
7700 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7701
7702 static int
7703 constant_size (unsigned HOST_WIDE_INT value)
7704 {
7705 int log;
7706
7707 if (value == 0)
7708 log = 0;
7709 else
7710 log = floor_log2 (value);
7711
7712 log = log / 8;
7713 log = 1 << (floor_log2 (log) + 1);
7714
7715 return log;
7716 }
7717
7718 /* Return the size of a DIE as it is represented in the
7719 .debug_info section. */
7720
7721 static unsigned long
7722 size_of_die (dw_die_ref die)
7723 {
7724 unsigned long size = 0;
7725 dw_attr_ref a;
7726 unsigned ix;
7727 enum dwarf_form form;
7728
7729 size += size_of_uleb128 (die->die_abbrev);
7730 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7731 {
7732 switch (AT_class (a))
7733 {
7734 case dw_val_class_addr:
7735 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7736 {
7737 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7738 size += size_of_uleb128 (AT_index (a));
7739 }
7740 else
7741 size += DWARF2_ADDR_SIZE;
7742 break;
7743 case dw_val_class_offset:
7744 size += DWARF_OFFSET_SIZE;
7745 break;
7746 case dw_val_class_loc:
7747 {
7748 unsigned long lsize = size_of_locs (AT_loc (a));
7749
7750 /* Block length. */
7751 if (dwarf_version >= 4)
7752 size += size_of_uleb128 (lsize);
7753 else
7754 size += constant_size (lsize);
7755 size += lsize;
7756 }
7757 break;
7758 case dw_val_class_loc_list:
7759 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7760 {
7761 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7762 size += size_of_uleb128 (AT_index (a));
7763 }
7764 else
7765 size += DWARF_OFFSET_SIZE;
7766 break;
7767 case dw_val_class_range_list:
7768 size += DWARF_OFFSET_SIZE;
7769 break;
7770 case dw_val_class_const:
7771 size += size_of_sleb128 (AT_int (a));
7772 break;
7773 case dw_val_class_unsigned_const:
7774 {
7775 int csize = constant_size (AT_unsigned (a));
7776 if (dwarf_version == 3
7777 && a->dw_attr == DW_AT_data_member_location
7778 && csize >= 4)
7779 size += size_of_uleb128 (AT_unsigned (a));
7780 else
7781 size += csize;
7782 }
7783 break;
7784 case dw_val_class_const_double:
7785 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7786 if (HOST_BITS_PER_WIDE_INT >= 64)
7787 size++; /* block */
7788 break;
7789 case dw_val_class_vec:
7790 size += constant_size (a->dw_attr_val.v.val_vec.length
7791 * a->dw_attr_val.v.val_vec.elt_size)
7792 + a->dw_attr_val.v.val_vec.length
7793 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7794 break;
7795 case dw_val_class_flag:
7796 if (dwarf_version >= 4)
7797 /* Currently all add_AT_flag calls pass in 1 as last argument,
7798 so DW_FORM_flag_present can be used. If that ever changes,
7799 we'll need to use DW_FORM_flag and have some optimization
7800 in build_abbrev_table that will change those to
7801 DW_FORM_flag_present if it is set to 1 in all DIEs using
7802 the same abbrev entry. */
7803 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7804 else
7805 size += 1;
7806 break;
7807 case dw_val_class_die_ref:
7808 if (AT_ref_external (a))
7809 {
7810 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7811 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7812 is sized by target address length, whereas in DWARF3
7813 it's always sized as an offset. */
7814 if (use_debug_types)
7815 size += DWARF_TYPE_SIGNATURE_SIZE;
7816 else if (dwarf_version == 2)
7817 size += DWARF2_ADDR_SIZE;
7818 else
7819 size += DWARF_OFFSET_SIZE;
7820 }
7821 else
7822 size += DWARF_OFFSET_SIZE;
7823 break;
7824 case dw_val_class_fde_ref:
7825 size += DWARF_OFFSET_SIZE;
7826 break;
7827 case dw_val_class_lbl_id:
7828 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7829 {
7830 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7831 size += size_of_uleb128 (AT_index (a));
7832 }
7833 else
7834 size += DWARF2_ADDR_SIZE;
7835 break;
7836 case dw_val_class_lineptr:
7837 case dw_val_class_macptr:
7838 size += DWARF_OFFSET_SIZE;
7839 break;
7840 case dw_val_class_str:
7841 form = AT_string_form (a);
7842 if (form == DW_FORM_strp)
7843 size += DWARF_OFFSET_SIZE;
7844 else if (form == DW_FORM_GNU_str_index)
7845 size += size_of_uleb128 (AT_index (a));
7846 else
7847 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7848 break;
7849 case dw_val_class_file:
7850 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7851 break;
7852 case dw_val_class_data8:
7853 size += 8;
7854 break;
7855 case dw_val_class_vms_delta:
7856 size += DWARF_OFFSET_SIZE;
7857 break;
7858 case dw_val_class_high_pc:
7859 size += DWARF2_ADDR_SIZE;
7860 break;
7861 default:
7862 gcc_unreachable ();
7863 }
7864 }
7865
7866 return size;
7867 }
7868
7869 /* Size the debugging information associated with a given DIE. Visits the
7870 DIE's children recursively. Updates the global variable next_die_offset, on
7871 each time through. Uses the current value of next_die_offset to update the
7872 die_offset field in each DIE. */
7873
7874 static void
7875 calc_die_sizes (dw_die_ref die)
7876 {
7877 dw_die_ref c;
7878
7879 gcc_assert (die->die_offset == 0
7880 || (unsigned long int) die->die_offset == next_die_offset);
7881 die->die_offset = next_die_offset;
7882 next_die_offset += size_of_die (die);
7883
7884 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7885
7886 if (die->die_child != NULL)
7887 /* Count the null byte used to terminate sibling lists. */
7888 next_die_offset += 1;
7889 }
7890
7891 /* Size just the base type children at the start of the CU.
7892 This is needed because build_abbrev needs to size locs
7893 and sizing of type based stack ops needs to know die_offset
7894 values for the base types. */
7895
7896 static void
7897 calc_base_type_die_sizes (void)
7898 {
7899 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7900 unsigned int i;
7901 dw_die_ref base_type;
7902 #if ENABLE_ASSERT_CHECKING
7903 dw_die_ref prev = comp_unit_die ()->die_child;
7904 #endif
7905
7906 die_offset += size_of_die (comp_unit_die ());
7907 for (i = 0; base_types.iterate (i, &base_type); i++)
7908 {
7909 #if ENABLE_ASSERT_CHECKING
7910 gcc_assert (base_type->die_offset == 0
7911 && prev->die_sib == base_type
7912 && base_type->die_child == NULL
7913 && base_type->die_abbrev);
7914 prev = base_type;
7915 #endif
7916 base_type->die_offset = die_offset;
7917 die_offset += size_of_die (base_type);
7918 }
7919 }
7920
7921 /* Set the marks for a die and its children. We do this so
7922 that we know whether or not a reference needs to use FORM_ref_addr; only
7923 DIEs in the same CU will be marked. We used to clear out the offset
7924 and use that as the flag, but ran into ordering problems. */
7925
7926 static void
7927 mark_dies (dw_die_ref die)
7928 {
7929 dw_die_ref c;
7930
7931 gcc_assert (!die->die_mark);
7932
7933 die->die_mark = 1;
7934 FOR_EACH_CHILD (die, c, mark_dies (c));
7935 }
7936
7937 /* Clear the marks for a die and its children. */
7938
7939 static void
7940 unmark_dies (dw_die_ref die)
7941 {
7942 dw_die_ref c;
7943
7944 if (! use_debug_types)
7945 gcc_assert (die->die_mark);
7946
7947 die->die_mark = 0;
7948 FOR_EACH_CHILD (die, c, unmark_dies (c));
7949 }
7950
7951 /* Clear the marks for a die, its children and referred dies. */
7952
7953 static void
7954 unmark_all_dies (dw_die_ref die)
7955 {
7956 dw_die_ref c;
7957 dw_attr_ref a;
7958 unsigned ix;
7959
7960 if (!die->die_mark)
7961 return;
7962 die->die_mark = 0;
7963
7964 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7965
7966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7967 if (AT_class (a) == dw_val_class_die_ref)
7968 unmark_all_dies (AT_ref (a));
7969 }
7970
7971 /* Calculate if the entry should appear in the final output file. It may be
7972 from a pruned a type. */
7973
7974 static bool
7975 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
7976 {
7977 /* By limiting gnu pubnames to definitions only, gold can generate a
7978 gdb index without entries for declarations, which don't include
7979 enough information to be useful. */
7980 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
7981 return false;
7982
7983 if (table == pubname_table)
7984 {
7985 /* Enumerator names are part of the pubname table, but the
7986 parent DW_TAG_enumeration_type die may have been pruned.
7987 Don't output them if that is the case. */
7988 if (p->die->die_tag == DW_TAG_enumerator &&
7989 (p->die->die_parent == NULL
7990 || !p->die->die_parent->die_perennial_p))
7991 return false;
7992
7993 /* Everything else in the pubname table is included. */
7994 return true;
7995 }
7996
7997 /* The pubtypes table shouldn't include types that have been
7998 pruned. */
7999 return (p->die->die_offset != 0
8000 || !flag_eliminate_unused_debug_types);
8001 }
8002
8003 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8004 generated for the compilation unit. */
8005
8006 static unsigned long
8007 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8008 {
8009 unsigned long size;
8010 unsigned i;
8011 pubname_ref p;
8012 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8013
8014 size = DWARF_PUBNAMES_HEADER_SIZE;
8015 FOR_EACH_VEC_ELT (*names, i, p)
8016 if (include_pubname_in_output (names, p))
8017 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8018
8019 size += DWARF_OFFSET_SIZE;
8020 return size;
8021 }
8022
8023 /* Return the size of the information in the .debug_aranges section. */
8024
8025 static unsigned long
8026 size_of_aranges (void)
8027 {
8028 unsigned long size;
8029
8030 size = DWARF_ARANGES_HEADER_SIZE;
8031
8032 /* Count the address/length pair for this compilation unit. */
8033 if (text_section_used)
8034 size += 2 * DWARF2_ADDR_SIZE;
8035 if (cold_text_section_used)
8036 size += 2 * DWARF2_ADDR_SIZE;
8037 if (have_multiple_function_sections)
8038 {
8039 unsigned fde_idx;
8040 dw_fde_ref fde;
8041
8042 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8043 {
8044 if (DECL_IGNORED_P (fde->decl))
8045 continue;
8046 if (!fde->in_std_section)
8047 size += 2 * DWARF2_ADDR_SIZE;
8048 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8049 size += 2 * DWARF2_ADDR_SIZE;
8050 }
8051 }
8052
8053 /* Count the two zero words used to terminated the address range table. */
8054 size += 2 * DWARF2_ADDR_SIZE;
8055 return size;
8056 }
8057 \f
8058 /* Select the encoding of an attribute value. */
8059
8060 static enum dwarf_form
8061 value_format (dw_attr_ref a)
8062 {
8063 switch (AT_class (a))
8064 {
8065 case dw_val_class_addr:
8066 /* Only very few attributes allow DW_FORM_addr. */
8067 switch (a->dw_attr)
8068 {
8069 case DW_AT_low_pc:
8070 case DW_AT_high_pc:
8071 case DW_AT_entry_pc:
8072 case DW_AT_trampoline:
8073 return (AT_index (a) == NOT_INDEXED
8074 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8075 default:
8076 break;
8077 }
8078 switch (DWARF2_ADDR_SIZE)
8079 {
8080 case 1:
8081 return DW_FORM_data1;
8082 case 2:
8083 return DW_FORM_data2;
8084 case 4:
8085 return DW_FORM_data4;
8086 case 8:
8087 return DW_FORM_data8;
8088 default:
8089 gcc_unreachable ();
8090 }
8091 case dw_val_class_range_list:
8092 case dw_val_class_loc_list:
8093 if (dwarf_version >= 4)
8094 return DW_FORM_sec_offset;
8095 /* FALLTHRU */
8096 case dw_val_class_vms_delta:
8097 case dw_val_class_offset:
8098 switch (DWARF_OFFSET_SIZE)
8099 {
8100 case 4:
8101 return DW_FORM_data4;
8102 case 8:
8103 return DW_FORM_data8;
8104 default:
8105 gcc_unreachable ();
8106 }
8107 case dw_val_class_loc:
8108 if (dwarf_version >= 4)
8109 return DW_FORM_exprloc;
8110 switch (constant_size (size_of_locs (AT_loc (a))))
8111 {
8112 case 1:
8113 return DW_FORM_block1;
8114 case 2:
8115 return DW_FORM_block2;
8116 case 4:
8117 return DW_FORM_block4;
8118 default:
8119 gcc_unreachable ();
8120 }
8121 case dw_val_class_const:
8122 return DW_FORM_sdata;
8123 case dw_val_class_unsigned_const:
8124 switch (constant_size (AT_unsigned (a)))
8125 {
8126 case 1:
8127 return DW_FORM_data1;
8128 case 2:
8129 return DW_FORM_data2;
8130 case 4:
8131 /* In DWARF3 DW_AT_data_member_location with
8132 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8133 constant, so we need to use DW_FORM_udata if we need
8134 a large constant. */
8135 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8136 return DW_FORM_udata;
8137 return DW_FORM_data4;
8138 case 8:
8139 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8140 return DW_FORM_udata;
8141 return DW_FORM_data8;
8142 default:
8143 gcc_unreachable ();
8144 }
8145 case dw_val_class_const_double:
8146 switch (HOST_BITS_PER_WIDE_INT)
8147 {
8148 case 8:
8149 return DW_FORM_data2;
8150 case 16:
8151 return DW_FORM_data4;
8152 case 32:
8153 return DW_FORM_data8;
8154 case 64:
8155 default:
8156 return DW_FORM_block1;
8157 }
8158 case dw_val_class_vec:
8159 switch (constant_size (a->dw_attr_val.v.val_vec.length
8160 * a->dw_attr_val.v.val_vec.elt_size))
8161 {
8162 case 1:
8163 return DW_FORM_block1;
8164 case 2:
8165 return DW_FORM_block2;
8166 case 4:
8167 return DW_FORM_block4;
8168 default:
8169 gcc_unreachable ();
8170 }
8171 case dw_val_class_flag:
8172 if (dwarf_version >= 4)
8173 {
8174 /* Currently all add_AT_flag calls pass in 1 as last argument,
8175 so DW_FORM_flag_present can be used. If that ever changes,
8176 we'll need to use DW_FORM_flag and have some optimization
8177 in build_abbrev_table that will change those to
8178 DW_FORM_flag_present if it is set to 1 in all DIEs using
8179 the same abbrev entry. */
8180 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8181 return DW_FORM_flag_present;
8182 }
8183 return DW_FORM_flag;
8184 case dw_val_class_die_ref:
8185 if (AT_ref_external (a))
8186 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8187 else
8188 return DW_FORM_ref;
8189 case dw_val_class_fde_ref:
8190 return DW_FORM_data;
8191 case dw_val_class_lbl_id:
8192 return (AT_index (a) == NOT_INDEXED
8193 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8194 case dw_val_class_lineptr:
8195 case dw_val_class_macptr:
8196 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8197 case dw_val_class_str:
8198 return AT_string_form (a);
8199 case dw_val_class_file:
8200 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8201 {
8202 case 1:
8203 return DW_FORM_data1;
8204 case 2:
8205 return DW_FORM_data2;
8206 case 4:
8207 return DW_FORM_data4;
8208 default:
8209 gcc_unreachable ();
8210 }
8211
8212 case dw_val_class_data8:
8213 return DW_FORM_data8;
8214
8215 case dw_val_class_high_pc:
8216 switch (DWARF2_ADDR_SIZE)
8217 {
8218 case 1:
8219 return DW_FORM_data1;
8220 case 2:
8221 return DW_FORM_data2;
8222 case 4:
8223 return DW_FORM_data4;
8224 case 8:
8225 return DW_FORM_data8;
8226 default:
8227 gcc_unreachable ();
8228 }
8229
8230 default:
8231 gcc_unreachable ();
8232 }
8233 }
8234
8235 /* Output the encoding of an attribute value. */
8236
8237 static void
8238 output_value_format (dw_attr_ref a)
8239 {
8240 enum dwarf_form form = value_format (a);
8241
8242 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8243 }
8244
8245 /* Given a die and id, produce the appropriate abbreviations. */
8246
8247 static void
8248 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8249 {
8250 unsigned ix;
8251 dw_attr_ref a_attr;
8252
8253 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8254 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8255 dwarf_tag_name (abbrev->die_tag));
8256
8257 if (abbrev->die_child != NULL)
8258 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8259 else
8260 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8261
8262 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8263 {
8264 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8265 dwarf_attr_name (a_attr->dw_attr));
8266 output_value_format (a_attr);
8267 }
8268
8269 dw2_asm_output_data (1, 0, NULL);
8270 dw2_asm_output_data (1, 0, NULL);
8271 }
8272
8273
8274 /* Output the .debug_abbrev section which defines the DIE abbreviation
8275 table. */
8276
8277 static void
8278 output_abbrev_section (void)
8279 {
8280 unsigned long abbrev_id;
8281
8282 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8283 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8284
8285 /* Terminate the table. */
8286 dw2_asm_output_data (1, 0, NULL);
8287 }
8288
8289 /* Output a symbol we can use to refer to this DIE from another CU. */
8290
8291 static inline void
8292 output_die_symbol (dw_die_ref die)
8293 {
8294 const char *sym = die->die_id.die_symbol;
8295
8296 gcc_assert (!die->comdat_type_p);
8297
8298 if (sym == 0)
8299 return;
8300
8301 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8302 /* We make these global, not weak; if the target doesn't support
8303 .linkonce, it doesn't support combining the sections, so debugging
8304 will break. */
8305 targetm.asm_out.globalize_label (asm_out_file, sym);
8306
8307 ASM_OUTPUT_LABEL (asm_out_file, sym);
8308 }
8309
8310 /* Return a new location list, given the begin and end range, and the
8311 expression. */
8312
8313 static inline dw_loc_list_ref
8314 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8315 const char *section)
8316 {
8317 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8318
8319 retlist->begin = begin;
8320 retlist->begin_entry = NULL;
8321 retlist->end = end;
8322 retlist->expr = expr;
8323 retlist->section = section;
8324
8325 return retlist;
8326 }
8327
8328 /* Generate a new internal symbol for this location list node, if it
8329 hasn't got one yet. */
8330
8331 static inline void
8332 gen_llsym (dw_loc_list_ref list)
8333 {
8334 gcc_assert (!list->ll_symbol);
8335 list->ll_symbol = gen_internal_sym ("LLST");
8336 }
8337
8338 /* Output the location list given to us. */
8339
8340 static void
8341 output_loc_list (dw_loc_list_ref list_head)
8342 {
8343 dw_loc_list_ref curr = list_head;
8344
8345 if (list_head->emitted)
8346 return;
8347 list_head->emitted = true;
8348
8349 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8350
8351 /* Walk the location list, and output each range + expression. */
8352 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8353 {
8354 unsigned long size;
8355 /* Don't output an entry that starts and ends at the same address. */
8356 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8357 continue;
8358 size = size_of_locs (curr->expr);
8359 /* If the expression is too large, drop it on the floor. We could
8360 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8361 in the expression, but >= 64KB expressions for a single value
8362 in a single range are unlikely very useful. */
8363 if (size > 0xffff)
8364 continue;
8365 if (dwarf_split_debug_info)
8366 {
8367 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8368 "Location list start/length entry (%s)",
8369 list_head->ll_symbol);
8370 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8371 "Location list range start index (%s)",
8372 curr->begin);
8373 /* The length field is 4 bytes. If we ever need to support
8374 an 8-byte length, we can add a new DW_LLE code or fall back
8375 to DW_LLE_GNU_start_end_entry. */
8376 dw2_asm_output_delta (4, curr->end, curr->begin,
8377 "Location list range length (%s)",
8378 list_head->ll_symbol);
8379 }
8380 else if (!have_multiple_function_sections)
8381 {
8382 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8383 "Location list begin address (%s)",
8384 list_head->ll_symbol);
8385 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8386 "Location list end address (%s)",
8387 list_head->ll_symbol);
8388 }
8389 else
8390 {
8391 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8392 "Location list begin address (%s)",
8393 list_head->ll_symbol);
8394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8395 "Location list end address (%s)",
8396 list_head->ll_symbol);
8397 }
8398
8399 /* Output the block length for this list of location operations. */
8400 gcc_assert (size <= 0xffff);
8401 dw2_asm_output_data (2, size, "%s", "Location expression size");
8402
8403 output_loc_sequence (curr->expr, -1);
8404 }
8405
8406 if (dwarf_split_debug_info)
8407 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8408 "Location list terminator (%s)",
8409 list_head->ll_symbol);
8410 else
8411 {
8412 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8413 "Location list terminator begin (%s)",
8414 list_head->ll_symbol);
8415 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8416 "Location list terminator end (%s)",
8417 list_head->ll_symbol);
8418 }
8419 }
8420
8421 /* Output a range_list offset into the debug_range section. Emit a
8422 relocated reference if val_entry is NULL, otherwise, emit an
8423 indirect reference. */
8424
8425 static void
8426 output_range_list_offset (dw_attr_ref a)
8427 {
8428 const char *name = dwarf_attr_name (a->dw_attr);
8429
8430 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8431 {
8432 char *p = strchr (ranges_section_label, '\0');
8433 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8435 debug_ranges_section, "%s", name);
8436 *p = '\0';
8437 }
8438 else
8439 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8440 "%s (offset from %s)", name, ranges_section_label);
8441 }
8442
8443 /* Output the offset into the debug_loc section. */
8444
8445 static void
8446 output_loc_list_offset (dw_attr_ref a)
8447 {
8448 char *sym = AT_loc_list (a)->ll_symbol;
8449
8450 gcc_assert (sym);
8451 if (dwarf_split_debug_info)
8452 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8453 "%s", dwarf_attr_name (a->dw_attr));
8454 else
8455 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8456 "%s", dwarf_attr_name (a->dw_attr));
8457 }
8458
8459 /* Output an attribute's index or value appropriately. */
8460
8461 static void
8462 output_attr_index_or_value (dw_attr_ref a)
8463 {
8464 const char *name = dwarf_attr_name (a->dw_attr);
8465
8466 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8467 {
8468 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8469 return;
8470 }
8471 switch (AT_class (a))
8472 {
8473 case dw_val_class_addr:
8474 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8475 break;
8476 case dw_val_class_high_pc:
8477 case dw_val_class_lbl_id:
8478 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8479 break;
8480 case dw_val_class_loc_list:
8481 output_loc_list_offset (a);
8482 break;
8483 default:
8484 gcc_unreachable ();
8485 }
8486 }
8487
8488 /* Output a type signature. */
8489
8490 static inline void
8491 output_signature (const char *sig, const char *name)
8492 {
8493 int i;
8494
8495 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8496 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8497 }
8498
8499 /* Output the DIE and its attributes. Called recursively to generate
8500 the definitions of each child DIE. */
8501
8502 static void
8503 output_die (dw_die_ref die)
8504 {
8505 dw_attr_ref a;
8506 dw_die_ref c;
8507 unsigned long size;
8508 unsigned ix;
8509
8510 /* If someone in another CU might refer to us, set up a symbol for
8511 them to point to. */
8512 if (! die->comdat_type_p && die->die_id.die_symbol)
8513 output_die_symbol (die);
8514
8515 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8516 (unsigned long)die->die_offset,
8517 dwarf_tag_name (die->die_tag));
8518
8519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8520 {
8521 const char *name = dwarf_attr_name (a->dw_attr);
8522
8523 switch (AT_class (a))
8524 {
8525 case dw_val_class_addr:
8526 output_attr_index_or_value (a);
8527 break;
8528
8529 case dw_val_class_offset:
8530 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8531 "%s", name);
8532 break;
8533
8534 case dw_val_class_range_list:
8535 output_range_list_offset (a);
8536 break;
8537
8538 case dw_val_class_loc:
8539 size = size_of_locs (AT_loc (a));
8540
8541 /* Output the block length for this list of location operations. */
8542 if (dwarf_version >= 4)
8543 dw2_asm_output_data_uleb128 (size, "%s", name);
8544 else
8545 dw2_asm_output_data (constant_size (size), size, "%s", name);
8546
8547 output_loc_sequence (AT_loc (a), -1);
8548 break;
8549
8550 case dw_val_class_const:
8551 /* ??? It would be slightly more efficient to use a scheme like is
8552 used for unsigned constants below, but gdb 4.x does not sign
8553 extend. Gdb 5.x does sign extend. */
8554 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8555 break;
8556
8557 case dw_val_class_unsigned_const:
8558 {
8559 int csize = constant_size (AT_unsigned (a));
8560 if (dwarf_version == 3
8561 && a->dw_attr == DW_AT_data_member_location
8562 && csize >= 4)
8563 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8564 else
8565 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8566 }
8567 break;
8568
8569 case dw_val_class_const_double:
8570 {
8571 unsigned HOST_WIDE_INT first, second;
8572
8573 if (HOST_BITS_PER_WIDE_INT >= 64)
8574 dw2_asm_output_data (1,
8575 HOST_BITS_PER_DOUBLE_INT
8576 / HOST_BITS_PER_CHAR,
8577 NULL);
8578
8579 if (WORDS_BIG_ENDIAN)
8580 {
8581 first = a->dw_attr_val.v.val_double.high;
8582 second = a->dw_attr_val.v.val_double.low;
8583 }
8584 else
8585 {
8586 first = a->dw_attr_val.v.val_double.low;
8587 second = a->dw_attr_val.v.val_double.high;
8588 }
8589
8590 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8591 first, "%s", name);
8592 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8593 second, NULL);
8594 }
8595 break;
8596
8597 case dw_val_class_vec:
8598 {
8599 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8600 unsigned int len = a->dw_attr_val.v.val_vec.length;
8601 unsigned int i;
8602 unsigned char *p;
8603
8604 dw2_asm_output_data (constant_size (len * elt_size),
8605 len * elt_size, "%s", name);
8606 if (elt_size > sizeof (HOST_WIDE_INT))
8607 {
8608 elt_size /= 2;
8609 len *= 2;
8610 }
8611 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8612 i < len;
8613 i++, p += elt_size)
8614 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8615 "fp or vector constant word %u", i);
8616 break;
8617 }
8618
8619 case dw_val_class_flag:
8620 if (dwarf_version >= 4)
8621 {
8622 /* Currently all add_AT_flag calls pass in 1 as last argument,
8623 so DW_FORM_flag_present can be used. If that ever changes,
8624 we'll need to use DW_FORM_flag and have some optimization
8625 in build_abbrev_table that will change those to
8626 DW_FORM_flag_present if it is set to 1 in all DIEs using
8627 the same abbrev entry. */
8628 gcc_assert (AT_flag (a) == 1);
8629 if (flag_debug_asm)
8630 fprintf (asm_out_file, "\t\t\t%s %s\n",
8631 ASM_COMMENT_START, name);
8632 break;
8633 }
8634 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8635 break;
8636
8637 case dw_val_class_loc_list:
8638 output_attr_index_or_value (a);
8639 break;
8640
8641 case dw_val_class_die_ref:
8642 if (AT_ref_external (a))
8643 {
8644 if (AT_ref (a)->comdat_type_p)
8645 {
8646 comdat_type_node_ref type_node =
8647 AT_ref (a)->die_id.die_type_node;
8648
8649 gcc_assert (type_node);
8650 output_signature (type_node->signature, name);
8651 }
8652 else
8653 {
8654 const char *sym = AT_ref (a)->die_id.die_symbol;
8655 int size;
8656
8657 gcc_assert (sym);
8658 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8659 length, whereas in DWARF3 it's always sized as an
8660 offset. */
8661 if (dwarf_version == 2)
8662 size = DWARF2_ADDR_SIZE;
8663 else
8664 size = DWARF_OFFSET_SIZE;
8665 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8666 name);
8667 }
8668 }
8669 else
8670 {
8671 gcc_assert (AT_ref (a)->die_offset);
8672 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8673 "%s", name);
8674 }
8675 break;
8676
8677 case dw_val_class_fde_ref:
8678 {
8679 char l1[20];
8680
8681 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8682 a->dw_attr_val.v.val_fde_index * 2);
8683 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8684 "%s", name);
8685 }
8686 break;
8687
8688 case dw_val_class_vms_delta:
8689 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8690 AT_vms_delta2 (a), AT_vms_delta1 (a),
8691 "%s", name);
8692 break;
8693
8694 case dw_val_class_lbl_id:
8695 output_attr_index_or_value (a);
8696 break;
8697
8698 case dw_val_class_lineptr:
8699 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8700 debug_line_section, "%s", name);
8701 break;
8702
8703 case dw_val_class_macptr:
8704 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8705 debug_macinfo_section, "%s", name);
8706 break;
8707
8708 case dw_val_class_str:
8709 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8710 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8711 a->dw_attr_val.v.val_str->label,
8712 debug_str_section,
8713 "%s: \"%s\"", name, AT_string (a));
8714 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8715 dw2_asm_output_data_uleb128 (AT_index (a),
8716 "%s: \"%s\"", name, AT_string (a));
8717 else
8718 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8719 break;
8720
8721 case dw_val_class_file:
8722 {
8723 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8724
8725 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8726 a->dw_attr_val.v.val_file->filename);
8727 break;
8728 }
8729
8730 case dw_val_class_data8:
8731 {
8732 int i;
8733
8734 for (i = 0; i < 8; i++)
8735 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8736 i == 0 ? "%s" : NULL, name);
8737 break;
8738 }
8739
8740 case dw_val_class_high_pc:
8741 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8742 get_AT_low_pc (die), "DW_AT_high_pc");
8743 break;
8744
8745 default:
8746 gcc_unreachable ();
8747 }
8748 }
8749
8750 FOR_EACH_CHILD (die, c, output_die (c));
8751
8752 /* Add null byte to terminate sibling list. */
8753 if (die->die_child != NULL)
8754 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8755 (unsigned long) die->die_offset);
8756 }
8757
8758 /* Output the compilation unit that appears at the beginning of the
8759 .debug_info section, and precedes the DIE descriptions. */
8760
8761 static void
8762 output_compilation_unit_header (void)
8763 {
8764 int ver = dwarf_version;
8765
8766 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8767 dw2_asm_output_data (4, 0xffffffff,
8768 "Initial length escape value indicating 64-bit DWARF extension");
8769 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8770 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8771 "Length of Compilation Unit Info");
8772 dw2_asm_output_data (2, ver, "DWARF version number");
8773 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8774 debug_abbrev_section,
8775 "Offset Into Abbrev. Section");
8776 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8777 }
8778
8779 /* Output the compilation unit DIE and its children. */
8780
8781 static void
8782 output_comp_unit (dw_die_ref die, int output_if_empty)
8783 {
8784 const char *secname, *oldsym;
8785 char *tmp;
8786 external_ref_hash_type extern_map;
8787
8788 /* Unless we are outputting main CU, we may throw away empty ones. */
8789 if (!output_if_empty && die->die_child == NULL)
8790 return;
8791
8792 /* Even if there are no children of this DIE, we must output the information
8793 about the compilation unit. Otherwise, on an empty translation unit, we
8794 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8795 will then complain when examining the file. First mark all the DIEs in
8796 this CU so we know which get local refs. */
8797 mark_dies (die);
8798
8799 extern_map = optimize_external_refs (die);
8800
8801 build_abbrev_table (die, extern_map);
8802
8803 extern_map.dispose ();
8804
8805 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8806 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8807 calc_die_sizes (die);
8808
8809 oldsym = die->die_id.die_symbol;
8810 if (oldsym)
8811 {
8812 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8813
8814 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8815 secname = tmp;
8816 die->die_id.die_symbol = NULL;
8817 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8818 }
8819 else
8820 {
8821 switch_to_section (debug_info_section);
8822 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8823 info_section_emitted = true;
8824 }
8825
8826 /* Output debugging information. */
8827 output_compilation_unit_header ();
8828 output_die (die);
8829
8830 /* Leave the marks on the main CU, so we can check them in
8831 output_pubnames. */
8832 if (oldsym)
8833 {
8834 unmark_dies (die);
8835 die->die_id.die_symbol = oldsym;
8836 }
8837 }
8838
8839 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8840 and .debug_pubtypes. This is configured per-target, but can be
8841 overridden by the -gpubnames or -gno-pubnames options. */
8842
8843 static inline bool
8844 want_pubnames (void)
8845 {
8846 if (debug_generate_pub_sections != -1)
8847 return debug_generate_pub_sections;
8848 return targetm.want_debug_pub_sections;
8849 }
8850
8851 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8852
8853 static void
8854 add_AT_pubnames (dw_die_ref die)
8855 {
8856 if (want_pubnames ())
8857 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8858 }
8859
8860 /* Add a string attribute value to a skeleton DIE. */
8861
8862 static inline void
8863 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
8864 const char *str)
8865 {
8866 dw_attr_node attr;
8867 struct indirect_string_node *node;
8868
8869 if (! skeleton_debug_str_hash)
8870 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
8871 debug_str_eq, NULL);
8872
8873 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
8874 find_string_form (node);
8875 if (node->form == DW_FORM_GNU_str_index)
8876 node->form = DW_FORM_strp;
8877
8878 attr.dw_attr = attr_kind;
8879 attr.dw_attr_val.val_class = dw_val_class_str;
8880 attr.dw_attr_val.val_entry = NULL;
8881 attr.dw_attr_val.v.val_str = node;
8882 add_dwarf_attr (die, &attr);
8883 }
8884
8885 /* Helper function to generate top-level dies for skeleton debug_info and
8886 debug_types. */
8887
8888 static void
8889 add_top_level_skeleton_die_attrs (dw_die_ref die)
8890 {
8891 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8892 const char *comp_dir = comp_dir_string ();
8893
8894 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8895 if (comp_dir != NULL)
8896 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
8897 add_AT_pubnames (die);
8898 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8899 }
8900
8901 /* Return the single type-unit die for skeleton type units. */
8902
8903 static dw_die_ref
8904 get_skeleton_type_unit (void)
8905 {
8906 /* For dwarf_split_debug_sections with use_type info, all type units in the
8907 skeleton sections have identical dies (but different headers). This
8908 single die will be output many times. */
8909
8910 static dw_die_ref skeleton_type_unit = NULL;
8911
8912 if (skeleton_type_unit == NULL)
8913 {
8914 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8915 add_top_level_skeleton_die_attrs (skeleton_type_unit);
8916 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8917 }
8918 return skeleton_type_unit;
8919 }
8920
8921 /* Output skeleton debug sections that point to the dwo file. */
8922
8923 static void
8924 output_skeleton_debug_sections (dw_die_ref comp_unit)
8925 {
8926 /* These attributes will be found in the full debug_info section. */
8927 remove_AT (comp_unit, DW_AT_producer);
8928 remove_AT (comp_unit, DW_AT_language);
8929
8930 switch_to_section (debug_skeleton_info_section);
8931 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8932
8933 /* Produce the skeleton compilation-unit header. This one differs enough from
8934 a normal CU header that it's better not to call output_compilation_unit
8935 header. */
8936 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8937 dw2_asm_output_data (4, 0xffffffff,
8938 "Initial length escape value indicating 64-bit DWARF extension");
8939
8940 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8941 DWARF_COMPILE_UNIT_HEADER_SIZE
8942 - DWARF_INITIAL_LENGTH_SIZE
8943 + size_of_die (comp_unit),
8944 "Length of Compilation Unit Info");
8945 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8947 debug_abbrev_section,
8948 "Offset Into Abbrev. Section");
8949 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8950
8951 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8952 output_die (comp_unit);
8953
8954 /* Build the skeleton debug_abbrev section. */
8955 switch_to_section (debug_skeleton_abbrev_section);
8956 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8957
8958 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8959 if (use_debug_types)
8960 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8961
8962 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8963 }
8964
8965 /* Output a comdat type unit DIE and its children. */
8966
8967 static void
8968 output_comdat_type_unit (comdat_type_node *node)
8969 {
8970 const char *secname;
8971 char *tmp;
8972 int i;
8973 #if defined (OBJECT_FORMAT_ELF)
8974 tree comdat_key;
8975 #endif
8976 external_ref_hash_type extern_map;
8977
8978 /* First mark all the DIEs in this CU so we know which get local refs. */
8979 mark_dies (node->root_die);
8980
8981 extern_map = optimize_external_refs (node->root_die);
8982
8983 build_abbrev_table (node->root_die, extern_map);
8984
8985 extern_map.dispose ();
8986
8987 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8988 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8989 calc_die_sizes (node->root_die);
8990
8991 #if defined (OBJECT_FORMAT_ELF)
8992 if (!dwarf_split_debug_info)
8993 secname = ".debug_types";
8994 else
8995 secname = ".debug_types.dwo";
8996
8997 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8998 sprintf (tmp, "wt.");
8999 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9000 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9001 comdat_key = get_identifier (tmp);
9002 targetm.asm_out.named_section (secname,
9003 SECTION_DEBUG | SECTION_LINKONCE,
9004 comdat_key);
9005 #else
9006 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9007 sprintf (tmp, ".gnu.linkonce.wt.");
9008 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9009 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9010 secname = tmp;
9011 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9012 #endif
9013
9014 /* Output debugging information. */
9015 output_compilation_unit_header ();
9016 output_signature (node->signature, "Type Signature");
9017 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9018 "Offset to Type DIE");
9019 output_die (node->root_die);
9020
9021 unmark_dies (node->root_die);
9022
9023 #if defined (OBJECT_FORMAT_ELF)
9024 if (dwarf_split_debug_info)
9025 {
9026 /* Produce the skeleton type-unit header. */
9027 const char *secname = ".debug_types";
9028
9029 targetm.asm_out.named_section (secname,
9030 SECTION_DEBUG | SECTION_LINKONCE,
9031 comdat_key);
9032 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9033 dw2_asm_output_data (4, 0xffffffff,
9034 "Initial length escape value indicating 64-bit DWARF extension");
9035
9036 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9037 DWARF_COMPILE_UNIT_HEADER_SIZE
9038 - DWARF_INITIAL_LENGTH_SIZE
9039 + size_of_die (get_skeleton_type_unit ())
9040 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9041 "Length of Type Unit Info");
9042 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9043 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9044 debug_skeleton_abbrev_section_label,
9045 debug_abbrev_section,
9046 "Offset Into Abbrev. Section");
9047 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9048 output_signature (node->signature, "Type Signature");
9049 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9050
9051 output_die (get_skeleton_type_unit ());
9052 }
9053 #endif
9054 }
9055
9056 /* Return the DWARF2/3 pubname associated with a decl. */
9057
9058 static const char *
9059 dwarf2_name (tree decl, int scope)
9060 {
9061 if (DECL_NAMELESS (decl))
9062 return NULL;
9063 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9064 }
9065
9066 /* Add a new entry to .debug_pubnames if appropriate. */
9067
9068 static void
9069 add_pubname_string (const char *str, dw_die_ref die)
9070 {
9071 pubname_entry e;
9072
9073 e.die = die;
9074 e.name = xstrdup (str);
9075 vec_safe_push (pubname_table, e);
9076 }
9077
9078 static void
9079 add_pubname (tree decl, dw_die_ref die)
9080 {
9081 if (!want_pubnames ())
9082 return;
9083
9084 /* Don't add items to the table when we expect that the consumer will have
9085 just read the enclosing die. For example, if the consumer is looking at a
9086 class_member, it will either be inside the class already, or will have just
9087 looked up the class to find the member. Either way, searching the class is
9088 faster than searching the index. */
9089 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9090 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9091 {
9092 const char *name = dwarf2_name (decl, 1);
9093
9094 if (name)
9095 add_pubname_string (name, die);
9096 }
9097 }
9098
9099 /* Add an enumerator to the pubnames section. */
9100
9101 static void
9102 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9103 {
9104 pubname_entry e;
9105
9106 gcc_assert (scope_name);
9107 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9108 e.die = die;
9109 vec_safe_push (pubname_table, e);
9110 }
9111
9112 /* Add a new entry to .debug_pubtypes if appropriate. */
9113
9114 static void
9115 add_pubtype (tree decl, dw_die_ref die)
9116 {
9117 pubname_entry e;
9118
9119 if (!want_pubnames ())
9120 return;
9121
9122 if ((TREE_PUBLIC (decl)
9123 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9124 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9125 {
9126 tree scope = NULL;
9127 const char *scope_name = "";
9128 const char *sep = is_cxx () ? "::" : ".";
9129 const char *name;
9130
9131 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9132 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9133 {
9134 scope_name = lang_hooks.dwarf_name (scope, 1);
9135 if (scope_name != NULL && scope_name[0] != '\0')
9136 scope_name = concat (scope_name, sep, NULL);
9137 else
9138 scope_name = "";
9139 }
9140
9141 if (TYPE_P (decl))
9142 name = type_tag (decl);
9143 else
9144 name = lang_hooks.dwarf_name (decl, 1);
9145
9146 /* If we don't have a name for the type, there's no point in adding
9147 it to the table. */
9148 if (name != NULL && name[0] != '\0')
9149 {
9150 e.die = die;
9151 e.name = concat (scope_name, name, NULL);
9152 vec_safe_push (pubtype_table, e);
9153 }
9154
9155 /* Although it might be more consistent to add the pubinfo for the
9156 enumerators as their dies are created, they should only be added if the
9157 enum type meets the criteria above. So rather than re-check the parent
9158 enum type whenever an enumerator die is created, just output them all
9159 here. This isn't protected by the name conditional because anonymous
9160 enums don't have names. */
9161 if (die->die_tag == DW_TAG_enumeration_type)
9162 {
9163 dw_die_ref c;
9164
9165 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9166 }
9167 }
9168 }
9169
9170 /* Output a single entry in the pubnames table. */
9171
9172 static void
9173 output_pubname (dw_offset die_offset, pubname_entry *entry)
9174 {
9175 dw_die_ref die = entry->die;
9176 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9177
9178 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9179
9180 if (debug_generate_pub_sections == 2)
9181 {
9182 /* This logic follows gdb's method for determining the value of the flag
9183 byte. */
9184 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9185 switch (die->die_tag)
9186 {
9187 case DW_TAG_typedef:
9188 case DW_TAG_base_type:
9189 case DW_TAG_subrange_type:
9190 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9191 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9192 break;
9193 case DW_TAG_enumerator:
9194 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9195 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9196 if (!is_cxx () && !is_java ())
9197 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9198 break;
9199 case DW_TAG_subprogram:
9200 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9201 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9202 if (!is_ada ())
9203 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9204 break;
9205 case DW_TAG_constant:
9206 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9207 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9208 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9209 break;
9210 case DW_TAG_variable:
9211 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9212 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9213 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9214 break;
9215 case DW_TAG_namespace:
9216 case DW_TAG_imported_declaration:
9217 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9218 break;
9219 case DW_TAG_class_type:
9220 case DW_TAG_interface_type:
9221 case DW_TAG_structure_type:
9222 case DW_TAG_union_type:
9223 case DW_TAG_enumeration_type:
9224 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9225 if (!is_cxx () && !is_java ())
9226 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9227 break;
9228 default:
9229 /* An unusual tag. Leave the flag-byte empty. */
9230 break;
9231 }
9232 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9233 "GDB-index flags");
9234 }
9235
9236 dw2_asm_output_nstring (entry->name, -1, "external name");
9237 }
9238
9239
9240 /* Output the public names table used to speed up access to externally
9241 visible names; or the public types table used to find type definitions. */
9242
9243 static void
9244 output_pubnames (vec<pubname_entry, va_gc> *names)
9245 {
9246 unsigned i;
9247 unsigned long pubnames_length = size_of_pubnames (names);
9248 pubname_ref pub;
9249
9250 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9251 dw2_asm_output_data (4, 0xffffffff,
9252 "Initial length escape value indicating 64-bit DWARF extension");
9253 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9254
9255 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9256 dw2_asm_output_data (2, 2, "DWARF Version");
9257
9258 if (dwarf_split_debug_info)
9259 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9260 debug_skeleton_info_section,
9261 "Offset of Compilation Unit Info");
9262 else
9263 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9264 debug_info_section,
9265 "Offset of Compilation Unit Info");
9266 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9267 "Compilation Unit Length");
9268
9269 FOR_EACH_VEC_ELT (*names, i, pub)
9270 {
9271 if (include_pubname_in_output (names, pub))
9272 {
9273 dw_offset die_offset = pub->die->die_offset;
9274
9275 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9276 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9277 gcc_assert (pub->die->die_mark);
9278
9279 /* If we're putting types in their own .debug_types sections,
9280 the .debug_pubtypes table will still point to the compile
9281 unit (not the type unit), so we want to use the offset of
9282 the skeleton DIE (if there is one). */
9283 if (pub->die->comdat_type_p && names == pubtype_table)
9284 {
9285 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9286
9287 if (type_node != NULL)
9288 die_offset = (type_node->skeleton_die != NULL
9289 ? type_node->skeleton_die->die_offset
9290 : 0);
9291 }
9292
9293 output_pubname (die_offset, pub);
9294 }
9295 }
9296
9297 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9298 }
9299
9300 /* Output public names and types tables if necessary. */
9301
9302 static void
9303 output_pubtables (void)
9304 {
9305 if (!want_pubnames () || !info_section_emitted)
9306 return;
9307
9308 switch_to_section (debug_pubnames_section);
9309 output_pubnames (pubname_table);
9310 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9311 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9312 simply won't look for the section. */
9313 switch_to_section (debug_pubtypes_section);
9314 output_pubnames (pubtype_table);
9315 }
9316
9317
9318 /* Output the information that goes into the .debug_aranges table.
9319 Namely, define the beginning and ending address range of the
9320 text section generated for this compilation unit. */
9321
9322 static void
9323 output_aranges (unsigned long aranges_length)
9324 {
9325 unsigned i;
9326
9327 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9328 dw2_asm_output_data (4, 0xffffffff,
9329 "Initial length escape value indicating 64-bit DWARF extension");
9330 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9331 "Length of Address Ranges Info");
9332 /* Version number for aranges is still 2, even in DWARF3. */
9333 dw2_asm_output_data (2, 2, "DWARF Version");
9334 if (dwarf_split_debug_info)
9335 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9336 debug_skeleton_info_section,
9337 "Offset of Compilation Unit Info");
9338 else
9339 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9340 debug_info_section,
9341 "Offset of Compilation Unit Info");
9342 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9343 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9344
9345 /* We need to align to twice the pointer size here. */
9346 if (DWARF_ARANGES_PAD_SIZE)
9347 {
9348 /* Pad using a 2 byte words so that padding is correct for any
9349 pointer size. */
9350 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9351 2 * DWARF2_ADDR_SIZE);
9352 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9353 dw2_asm_output_data (2, 0, NULL);
9354 }
9355
9356 /* It is necessary not to output these entries if the sections were
9357 not used; if the sections were not used, the length will be 0 and
9358 the address may end up as 0 if the section is discarded by ld
9359 --gc-sections, leaving an invalid (0, 0) entry that can be
9360 confused with the terminator. */
9361 if (text_section_used)
9362 {
9363 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9364 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9365 text_section_label, "Length");
9366 }
9367 if (cold_text_section_used)
9368 {
9369 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9370 "Address");
9371 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9372 cold_text_section_label, "Length");
9373 }
9374
9375 if (have_multiple_function_sections)
9376 {
9377 unsigned fde_idx;
9378 dw_fde_ref fde;
9379
9380 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9381 {
9382 if (DECL_IGNORED_P (fde->decl))
9383 continue;
9384 if (!fde->in_std_section)
9385 {
9386 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9387 "Address");
9388 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9389 fde->dw_fde_begin, "Length");
9390 }
9391 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9392 {
9393 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9394 "Address");
9395 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9396 fde->dw_fde_second_begin, "Length");
9397 }
9398 }
9399 }
9400
9401 /* Output the terminator words. */
9402 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9403 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9404 }
9405
9406 /* Add a new entry to .debug_ranges. Return the offset at which it
9407 was placed. */
9408
9409 static unsigned int
9410 add_ranges_num (int num)
9411 {
9412 unsigned int in_use = ranges_table_in_use;
9413
9414 if (in_use == ranges_table_allocated)
9415 {
9416 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9417 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9418 ranges_table_allocated);
9419 memset (ranges_table + ranges_table_in_use, 0,
9420 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9421 }
9422
9423 ranges_table[in_use].num = num;
9424 ranges_table_in_use = in_use + 1;
9425
9426 return in_use * 2 * DWARF2_ADDR_SIZE;
9427 }
9428
9429 /* Add a new entry to .debug_ranges corresponding to a block, or a
9430 range terminator if BLOCK is NULL. */
9431
9432 static unsigned int
9433 add_ranges (const_tree block)
9434 {
9435 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9436 }
9437
9438 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9439 When using dwarf_split_debug_info, address attributes in dies destined
9440 for the final executable should be direct references--setting the
9441 parameter force_direct ensures this behavior. */
9442
9443 static void
9444 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9445 bool *added, bool force_direct)
9446 {
9447 unsigned int in_use = ranges_by_label_in_use;
9448 unsigned int offset;
9449
9450 if (in_use == ranges_by_label_allocated)
9451 {
9452 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9453 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9454 ranges_by_label,
9455 ranges_by_label_allocated);
9456 memset (ranges_by_label + ranges_by_label_in_use, 0,
9457 RANGES_TABLE_INCREMENT
9458 * sizeof (struct dw_ranges_by_label_struct));
9459 }
9460
9461 ranges_by_label[in_use].begin = begin;
9462 ranges_by_label[in_use].end = end;
9463 ranges_by_label_in_use = in_use + 1;
9464
9465 offset = add_ranges_num (-(int)in_use - 1);
9466 if (!*added)
9467 {
9468 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9469 *added = true;
9470 }
9471 }
9472
9473 static void
9474 output_ranges (void)
9475 {
9476 unsigned i;
9477 static const char *const start_fmt = "Offset %#x";
9478 const char *fmt = start_fmt;
9479
9480 for (i = 0; i < ranges_table_in_use; i++)
9481 {
9482 int block_num = ranges_table[i].num;
9483
9484 if (block_num > 0)
9485 {
9486 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9487 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9488
9489 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9490 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9491
9492 /* If all code is in the text section, then the compilation
9493 unit base address defaults to DW_AT_low_pc, which is the
9494 base of the text section. */
9495 if (!have_multiple_function_sections)
9496 {
9497 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9498 text_section_label,
9499 fmt, i * 2 * DWARF2_ADDR_SIZE);
9500 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9501 text_section_label, NULL);
9502 }
9503
9504 /* Otherwise, the compilation unit base address is zero,
9505 which allows us to use absolute addresses, and not worry
9506 about whether the target supports cross-section
9507 arithmetic. */
9508 else
9509 {
9510 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9511 fmt, i * 2 * DWARF2_ADDR_SIZE);
9512 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9513 }
9514
9515 fmt = NULL;
9516 }
9517
9518 /* Negative block_num stands for an index into ranges_by_label. */
9519 else if (block_num < 0)
9520 {
9521 int lab_idx = - block_num - 1;
9522
9523 if (!have_multiple_function_sections)
9524 {
9525 gcc_unreachable ();
9526 #if 0
9527 /* If we ever use add_ranges_by_labels () for a single
9528 function section, all we have to do is to take out
9529 the #if 0 above. */
9530 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9531 ranges_by_label[lab_idx].begin,
9532 text_section_label,
9533 fmt, i * 2 * DWARF2_ADDR_SIZE);
9534 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9535 ranges_by_label[lab_idx].end,
9536 text_section_label, NULL);
9537 #endif
9538 }
9539 else
9540 {
9541 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9542 ranges_by_label[lab_idx].begin,
9543 fmt, i * 2 * DWARF2_ADDR_SIZE);
9544 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9545 ranges_by_label[lab_idx].end,
9546 NULL);
9547 }
9548 }
9549 else
9550 {
9551 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9552 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9553 fmt = start_fmt;
9554 }
9555 }
9556 }
9557
9558 /* Data structure containing information about input files. */
9559 struct file_info
9560 {
9561 const char *path; /* Complete file name. */
9562 const char *fname; /* File name part. */
9563 int length; /* Length of entire string. */
9564 struct dwarf_file_data * file_idx; /* Index in input file table. */
9565 int dir_idx; /* Index in directory table. */
9566 };
9567
9568 /* Data structure containing information about directories with source
9569 files. */
9570 struct dir_info
9571 {
9572 const char *path; /* Path including directory name. */
9573 int length; /* Path length. */
9574 int prefix; /* Index of directory entry which is a prefix. */
9575 int count; /* Number of files in this directory. */
9576 int dir_idx; /* Index of directory used as base. */
9577 };
9578
9579 /* Callback function for file_info comparison. We sort by looking at
9580 the directories in the path. */
9581
9582 static int
9583 file_info_cmp (const void *p1, const void *p2)
9584 {
9585 const struct file_info *const s1 = (const struct file_info *) p1;
9586 const struct file_info *const s2 = (const struct file_info *) p2;
9587 const unsigned char *cp1;
9588 const unsigned char *cp2;
9589
9590 /* Take care of file names without directories. We need to make sure that
9591 we return consistent values to qsort since some will get confused if
9592 we return the same value when identical operands are passed in opposite
9593 orders. So if neither has a directory, return 0 and otherwise return
9594 1 or -1 depending on which one has the directory. */
9595 if ((s1->path == s1->fname || s2->path == s2->fname))
9596 return (s2->path == s2->fname) - (s1->path == s1->fname);
9597
9598 cp1 = (const unsigned char *) s1->path;
9599 cp2 = (const unsigned char *) s2->path;
9600
9601 while (1)
9602 {
9603 ++cp1;
9604 ++cp2;
9605 /* Reached the end of the first path? If so, handle like above. */
9606 if ((cp1 == (const unsigned char *) s1->fname)
9607 || (cp2 == (const unsigned char *) s2->fname))
9608 return ((cp2 == (const unsigned char *) s2->fname)
9609 - (cp1 == (const unsigned char *) s1->fname));
9610
9611 /* Character of current path component the same? */
9612 else if (*cp1 != *cp2)
9613 return *cp1 - *cp2;
9614 }
9615 }
9616
9617 struct file_name_acquire_data
9618 {
9619 struct file_info *files;
9620 int used_files;
9621 int max_files;
9622 };
9623
9624 /* Traversal function for the hash table. */
9625
9626 static int
9627 file_name_acquire (void ** slot, void *data)
9628 {
9629 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9630 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9631 struct file_info *fi;
9632 const char *f;
9633
9634 gcc_assert (fnad->max_files >= d->emitted_number);
9635
9636 if (! d->emitted_number)
9637 return 1;
9638
9639 gcc_assert (fnad->max_files != fnad->used_files);
9640
9641 fi = fnad->files + fnad->used_files++;
9642
9643 /* Skip all leading "./". */
9644 f = d->filename;
9645 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9646 f += 2;
9647
9648 /* Create a new array entry. */
9649 fi->path = f;
9650 fi->length = strlen (f);
9651 fi->file_idx = d;
9652
9653 /* Search for the file name part. */
9654 f = strrchr (f, DIR_SEPARATOR);
9655 #if defined (DIR_SEPARATOR_2)
9656 {
9657 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9658
9659 if (g != NULL)
9660 {
9661 if (f == NULL || f < g)
9662 f = g;
9663 }
9664 }
9665 #endif
9666
9667 fi->fname = f == NULL ? fi->path : f + 1;
9668 return 1;
9669 }
9670
9671 /* Output the directory table and the file name table. We try to minimize
9672 the total amount of memory needed. A heuristic is used to avoid large
9673 slowdowns with many input files. */
9674
9675 static void
9676 output_file_names (void)
9677 {
9678 struct file_name_acquire_data fnad;
9679 int numfiles;
9680 struct file_info *files;
9681 struct dir_info *dirs;
9682 int *saved;
9683 int *savehere;
9684 int *backmap;
9685 int ndirs;
9686 int idx_offset;
9687 int i;
9688
9689 if (!last_emitted_file)
9690 {
9691 dw2_asm_output_data (1, 0, "End directory table");
9692 dw2_asm_output_data (1, 0, "End file name table");
9693 return;
9694 }
9695
9696 numfiles = last_emitted_file->emitted_number;
9697
9698 /* Allocate the various arrays we need. */
9699 files = XALLOCAVEC (struct file_info, numfiles);
9700 dirs = XALLOCAVEC (struct dir_info, numfiles);
9701
9702 fnad.files = files;
9703 fnad.used_files = 0;
9704 fnad.max_files = numfiles;
9705 htab_traverse (file_table, file_name_acquire, &fnad);
9706 gcc_assert (fnad.used_files == fnad.max_files);
9707
9708 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9709
9710 /* Find all the different directories used. */
9711 dirs[0].path = files[0].path;
9712 dirs[0].length = files[0].fname - files[0].path;
9713 dirs[0].prefix = -1;
9714 dirs[0].count = 1;
9715 dirs[0].dir_idx = 0;
9716 files[0].dir_idx = 0;
9717 ndirs = 1;
9718
9719 for (i = 1; i < numfiles; i++)
9720 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9721 && memcmp (dirs[ndirs - 1].path, files[i].path,
9722 dirs[ndirs - 1].length) == 0)
9723 {
9724 /* Same directory as last entry. */
9725 files[i].dir_idx = ndirs - 1;
9726 ++dirs[ndirs - 1].count;
9727 }
9728 else
9729 {
9730 int j;
9731
9732 /* This is a new directory. */
9733 dirs[ndirs].path = files[i].path;
9734 dirs[ndirs].length = files[i].fname - files[i].path;
9735 dirs[ndirs].count = 1;
9736 dirs[ndirs].dir_idx = ndirs;
9737 files[i].dir_idx = ndirs;
9738
9739 /* Search for a prefix. */
9740 dirs[ndirs].prefix = -1;
9741 for (j = 0; j < ndirs; j++)
9742 if (dirs[j].length < dirs[ndirs].length
9743 && dirs[j].length > 1
9744 && (dirs[ndirs].prefix == -1
9745 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9746 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9747 dirs[ndirs].prefix = j;
9748
9749 ++ndirs;
9750 }
9751
9752 /* Now to the actual work. We have to find a subset of the directories which
9753 allow expressing the file name using references to the directory table
9754 with the least amount of characters. We do not do an exhaustive search
9755 where we would have to check out every combination of every single
9756 possible prefix. Instead we use a heuristic which provides nearly optimal
9757 results in most cases and never is much off. */
9758 saved = XALLOCAVEC (int, ndirs);
9759 savehere = XALLOCAVEC (int, ndirs);
9760
9761 memset (saved, '\0', ndirs * sizeof (saved[0]));
9762 for (i = 0; i < ndirs; i++)
9763 {
9764 int j;
9765 int total;
9766
9767 /* We can always save some space for the current directory. But this
9768 does not mean it will be enough to justify adding the directory. */
9769 savehere[i] = dirs[i].length;
9770 total = (savehere[i] - saved[i]) * dirs[i].count;
9771
9772 for (j = i + 1; j < ndirs; j++)
9773 {
9774 savehere[j] = 0;
9775 if (saved[j] < dirs[i].length)
9776 {
9777 /* Determine whether the dirs[i] path is a prefix of the
9778 dirs[j] path. */
9779 int k;
9780
9781 k = dirs[j].prefix;
9782 while (k != -1 && k != (int) i)
9783 k = dirs[k].prefix;
9784
9785 if (k == (int) i)
9786 {
9787 /* Yes it is. We can possibly save some memory by
9788 writing the filenames in dirs[j] relative to
9789 dirs[i]. */
9790 savehere[j] = dirs[i].length;
9791 total += (savehere[j] - saved[j]) * dirs[j].count;
9792 }
9793 }
9794 }
9795
9796 /* Check whether we can save enough to justify adding the dirs[i]
9797 directory. */
9798 if (total > dirs[i].length + 1)
9799 {
9800 /* It's worthwhile adding. */
9801 for (j = i; j < ndirs; j++)
9802 if (savehere[j] > 0)
9803 {
9804 /* Remember how much we saved for this directory so far. */
9805 saved[j] = savehere[j];
9806
9807 /* Remember the prefix directory. */
9808 dirs[j].dir_idx = i;
9809 }
9810 }
9811 }
9812
9813 /* Emit the directory name table. */
9814 idx_offset = dirs[0].length > 0 ? 1 : 0;
9815 for (i = 1 - idx_offset; i < ndirs; i++)
9816 dw2_asm_output_nstring (dirs[i].path,
9817 dirs[i].length
9818 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9819 "Directory Entry: %#x", i + idx_offset);
9820
9821 dw2_asm_output_data (1, 0, "End directory table");
9822
9823 /* We have to emit them in the order of emitted_number since that's
9824 used in the debug info generation. To do this efficiently we
9825 generate a back-mapping of the indices first. */
9826 backmap = XALLOCAVEC (int, numfiles);
9827 for (i = 0; i < numfiles; i++)
9828 backmap[files[i].file_idx->emitted_number - 1] = i;
9829
9830 /* Now write all the file names. */
9831 for (i = 0; i < numfiles; i++)
9832 {
9833 int file_idx = backmap[i];
9834 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9835
9836 #ifdef VMS_DEBUGGING_INFO
9837 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9838
9839 /* Setting these fields can lead to debugger miscomparisons,
9840 but VMS Debug requires them to be set correctly. */
9841
9842 int ver;
9843 long long cdt;
9844 long siz;
9845 int maxfilelen = strlen (files[file_idx].path)
9846 + dirs[dir_idx].length
9847 + MAX_VMS_VERSION_LEN + 1;
9848 char *filebuf = XALLOCAVEC (char, maxfilelen);
9849
9850 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9851 snprintf (filebuf, maxfilelen, "%s;%d",
9852 files[file_idx].path + dirs[dir_idx].length, ver);
9853
9854 dw2_asm_output_nstring
9855 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9856
9857 /* Include directory index. */
9858 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9859
9860 /* Modification time. */
9861 dw2_asm_output_data_uleb128
9862 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9863 ? cdt : 0,
9864 NULL);
9865
9866 /* File length in bytes. */
9867 dw2_asm_output_data_uleb128
9868 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9869 ? siz : 0,
9870 NULL);
9871 #else
9872 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9873 "File Entry: %#x", (unsigned) i + 1);
9874
9875 /* Include directory index. */
9876 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9877
9878 /* Modification time. */
9879 dw2_asm_output_data_uleb128 (0, NULL);
9880
9881 /* File length in bytes. */
9882 dw2_asm_output_data_uleb128 (0, NULL);
9883 #endif /* VMS_DEBUGGING_INFO */
9884 }
9885
9886 dw2_asm_output_data (1, 0, "End file name table");
9887 }
9888
9889
9890 /* Output one line number table into the .debug_line section. */
9891
9892 static void
9893 output_one_line_info_table (dw_line_info_table *table)
9894 {
9895 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9896 unsigned int current_line = 1;
9897 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9898 dw_line_info_entry *ent;
9899 size_t i;
9900
9901 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9902 {
9903 switch (ent->opcode)
9904 {
9905 case LI_set_address:
9906 /* ??? Unfortunately, we have little choice here currently, and
9907 must always use the most general form. GCC does not know the
9908 address delta itself, so we can't use DW_LNS_advance_pc. Many
9909 ports do have length attributes which will give an upper bound
9910 on the address range. We could perhaps use length attributes
9911 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
9912 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9913
9914 /* This can handle any delta. This takes
9915 4+DWARF2_ADDR_SIZE bytes. */
9916 dw2_asm_output_data (1, 0, "set address %s", line_label);
9917 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9918 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9919 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9920 break;
9921
9922 case LI_set_line:
9923 if (ent->val == current_line)
9924 {
9925 /* We still need to start a new row, so output a copy insn. */
9926 dw2_asm_output_data (1, DW_LNS_copy,
9927 "copy line %u", current_line);
9928 }
9929 else
9930 {
9931 int line_offset = ent->val - current_line;
9932 int line_delta = line_offset - DWARF_LINE_BASE;
9933
9934 current_line = ent->val;
9935 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9936 {
9937 /* This can handle deltas from -10 to 234, using the current
9938 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9939 This takes 1 byte. */
9940 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9941 "line %u", current_line);
9942 }
9943 else
9944 {
9945 /* This can handle any delta. This takes at least 4 bytes,
9946 depending on the value being encoded. */
9947 dw2_asm_output_data (1, DW_LNS_advance_line,
9948 "advance to line %u", current_line);
9949 dw2_asm_output_data_sleb128 (line_offset, NULL);
9950 dw2_asm_output_data (1, DW_LNS_copy, NULL);
9951 }
9952 }
9953 break;
9954
9955 case LI_set_file:
9956 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9957 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9958 break;
9959
9960 case LI_set_column:
9961 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9962 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9963 break;
9964
9965 case LI_negate_stmt:
9966 current_is_stmt = !current_is_stmt;
9967 dw2_asm_output_data (1, DW_LNS_negate_stmt,
9968 "is_stmt %d", current_is_stmt);
9969 break;
9970
9971 case LI_set_prologue_end:
9972 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9973 "set prologue end");
9974 break;
9975
9976 case LI_set_epilogue_begin:
9977 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9978 "set epilogue begin");
9979 break;
9980
9981 case LI_set_discriminator:
9982 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9983 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9984 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9985 dw2_asm_output_data_uleb128 (ent->val, NULL);
9986 break;
9987 }
9988 }
9989
9990 /* Emit debug info for the address of the end of the table. */
9991 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9992 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9993 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9995
9996 dw2_asm_output_data (1, 0, "end sequence");
9997 dw2_asm_output_data_uleb128 (1, NULL);
9998 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9999 }
10000
10001 /* Output the source line number correspondence information. This
10002 information goes into the .debug_line section. */
10003
10004 static void
10005 output_line_info (bool prologue_only)
10006 {
10007 char l1[20], l2[20], p1[20], p2[20];
10008 int ver = dwarf_version;
10009 bool saw_one = false;
10010 int opc;
10011
10012 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10013 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10014 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10015 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10016
10017 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10018 dw2_asm_output_data (4, 0xffffffff,
10019 "Initial length escape value indicating 64-bit DWARF extension");
10020 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10021 "Length of Source Line Info");
10022 ASM_OUTPUT_LABEL (asm_out_file, l1);
10023
10024 dw2_asm_output_data (2, ver, "DWARF Version");
10025 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10026 ASM_OUTPUT_LABEL (asm_out_file, p1);
10027
10028 /* Define the architecture-dependent minimum instruction length (in bytes).
10029 In this implementation of DWARF, this field is used for information
10030 purposes only. Since GCC generates assembly language, we have no
10031 a priori knowledge of how many instruction bytes are generated for each
10032 source line, and therefore can use only the DW_LNE_set_address and
10033 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10034 this as '1', which is "correct enough" for all architectures,
10035 and don't let the target override. */
10036 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10037
10038 if (ver >= 4)
10039 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10040 "Maximum Operations Per Instruction");
10041 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10042 "Default is_stmt_start flag");
10043 dw2_asm_output_data (1, DWARF_LINE_BASE,
10044 "Line Base Value (Special Opcodes)");
10045 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10046 "Line Range Value (Special Opcodes)");
10047 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10048 "Special Opcode Base");
10049
10050 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10051 {
10052 int n_op_args;
10053 switch (opc)
10054 {
10055 case DW_LNS_advance_pc:
10056 case DW_LNS_advance_line:
10057 case DW_LNS_set_file:
10058 case DW_LNS_set_column:
10059 case DW_LNS_fixed_advance_pc:
10060 case DW_LNS_set_isa:
10061 n_op_args = 1;
10062 break;
10063 default:
10064 n_op_args = 0;
10065 break;
10066 }
10067
10068 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10069 opc, n_op_args);
10070 }
10071
10072 /* Write out the information about the files we use. */
10073 output_file_names ();
10074 ASM_OUTPUT_LABEL (asm_out_file, p2);
10075 if (prologue_only)
10076 {
10077 /* Output the marker for the end of the line number info. */
10078 ASM_OUTPUT_LABEL (asm_out_file, l2);
10079 return;
10080 }
10081
10082 if (separate_line_info)
10083 {
10084 dw_line_info_table *table;
10085 size_t i;
10086
10087 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10088 if (table->in_use)
10089 {
10090 output_one_line_info_table (table);
10091 saw_one = true;
10092 }
10093 }
10094 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10095 {
10096 output_one_line_info_table (cold_text_section_line_info);
10097 saw_one = true;
10098 }
10099
10100 /* ??? Some Darwin linkers crash on a .debug_line section with no
10101 sequences. Further, merely a DW_LNE_end_sequence entry is not
10102 sufficient -- the address column must also be initialized.
10103 Make sure to output at least one set_address/end_sequence pair,
10104 choosing .text since that section is always present. */
10105 if (text_section_line_info->in_use || !saw_one)
10106 output_one_line_info_table (text_section_line_info);
10107
10108 /* Output the marker for the end of the line number info. */
10109 ASM_OUTPUT_LABEL (asm_out_file, l2);
10110 }
10111 \f
10112 /* Given a pointer to a tree node for some base type, return a pointer to
10113 a DIE that describes the given type.
10114
10115 This routine must only be called for GCC type nodes that correspond to
10116 Dwarf base (fundamental) types. */
10117
10118 static dw_die_ref
10119 base_type_die (tree type)
10120 {
10121 dw_die_ref base_type_result;
10122 enum dwarf_type encoding;
10123
10124 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10125 return 0;
10126
10127 /* If this is a subtype that should not be emitted as a subrange type,
10128 use the base type. See subrange_type_for_debug_p. */
10129 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10130 type = TREE_TYPE (type);
10131
10132 switch (TREE_CODE (type))
10133 {
10134 case INTEGER_TYPE:
10135 if ((dwarf_version >= 4 || !dwarf_strict)
10136 && TYPE_NAME (type)
10137 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10138 && DECL_IS_BUILTIN (TYPE_NAME (type))
10139 && DECL_NAME (TYPE_NAME (type)))
10140 {
10141 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10142 if (strcmp (name, "char16_t") == 0
10143 || strcmp (name, "char32_t") == 0)
10144 {
10145 encoding = DW_ATE_UTF;
10146 break;
10147 }
10148 }
10149 if (TYPE_STRING_FLAG (type))
10150 {
10151 if (TYPE_UNSIGNED (type))
10152 encoding = DW_ATE_unsigned_char;
10153 else
10154 encoding = DW_ATE_signed_char;
10155 }
10156 else if (TYPE_UNSIGNED (type))
10157 encoding = DW_ATE_unsigned;
10158 else
10159 encoding = DW_ATE_signed;
10160 break;
10161
10162 case REAL_TYPE:
10163 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10164 {
10165 if (dwarf_version >= 3 || !dwarf_strict)
10166 encoding = DW_ATE_decimal_float;
10167 else
10168 encoding = DW_ATE_lo_user;
10169 }
10170 else
10171 encoding = DW_ATE_float;
10172 break;
10173
10174 case FIXED_POINT_TYPE:
10175 if (!(dwarf_version >= 3 || !dwarf_strict))
10176 encoding = DW_ATE_lo_user;
10177 else if (TYPE_UNSIGNED (type))
10178 encoding = DW_ATE_unsigned_fixed;
10179 else
10180 encoding = DW_ATE_signed_fixed;
10181 break;
10182
10183 /* Dwarf2 doesn't know anything about complex ints, so use
10184 a user defined type for it. */
10185 case COMPLEX_TYPE:
10186 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10187 encoding = DW_ATE_complex_float;
10188 else
10189 encoding = DW_ATE_lo_user;
10190 break;
10191
10192 case BOOLEAN_TYPE:
10193 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10194 encoding = DW_ATE_boolean;
10195 break;
10196
10197 default:
10198 /* No other TREE_CODEs are Dwarf fundamental types. */
10199 gcc_unreachable ();
10200 }
10201
10202 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10203
10204 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10205 int_size_in_bytes (type));
10206 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10207 add_pubtype (type, base_type_result);
10208
10209 return base_type_result;
10210 }
10211
10212 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10213 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10214
10215 static inline int
10216 is_base_type (tree type)
10217 {
10218 switch (TREE_CODE (type))
10219 {
10220 case ERROR_MARK:
10221 case VOID_TYPE:
10222 case INTEGER_TYPE:
10223 case REAL_TYPE:
10224 case FIXED_POINT_TYPE:
10225 case COMPLEX_TYPE:
10226 case BOOLEAN_TYPE:
10227 return 1;
10228
10229 case ARRAY_TYPE:
10230 case RECORD_TYPE:
10231 case UNION_TYPE:
10232 case QUAL_UNION_TYPE:
10233 case ENUMERAL_TYPE:
10234 case FUNCTION_TYPE:
10235 case METHOD_TYPE:
10236 case POINTER_TYPE:
10237 case REFERENCE_TYPE:
10238 case NULLPTR_TYPE:
10239 case OFFSET_TYPE:
10240 case LANG_TYPE:
10241 case VECTOR_TYPE:
10242 return 0;
10243
10244 default:
10245 gcc_unreachable ();
10246 }
10247
10248 return 0;
10249 }
10250
10251 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10252 node, return the size in bits for the type if it is a constant, or else
10253 return the alignment for the type if the type's size is not constant, or
10254 else return BITS_PER_WORD if the type actually turns out to be an
10255 ERROR_MARK node. */
10256
10257 static inline unsigned HOST_WIDE_INT
10258 simple_type_size_in_bits (const_tree type)
10259 {
10260 if (TREE_CODE (type) == ERROR_MARK)
10261 return BITS_PER_WORD;
10262 else if (TYPE_SIZE (type) == NULL_TREE)
10263 return 0;
10264 else if (host_integerp (TYPE_SIZE (type), 1))
10265 return tree_low_cst (TYPE_SIZE (type), 1);
10266 else
10267 return TYPE_ALIGN (type);
10268 }
10269
10270 /* Similarly, but return a double_int instead of UHWI. */
10271
10272 static inline double_int
10273 double_int_type_size_in_bits (const_tree type)
10274 {
10275 if (TREE_CODE (type) == ERROR_MARK)
10276 return double_int::from_uhwi (BITS_PER_WORD);
10277 else if (TYPE_SIZE (type) == NULL_TREE)
10278 return double_int_zero;
10279 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10280 return tree_to_double_int (TYPE_SIZE (type));
10281 else
10282 return double_int::from_uhwi (TYPE_ALIGN (type));
10283 }
10284
10285 /* Given a pointer to a tree node for a subrange type, return a pointer
10286 to a DIE that describes the given type. */
10287
10288 static dw_die_ref
10289 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10290 {
10291 dw_die_ref subrange_die;
10292 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10293
10294 if (context_die == NULL)
10295 context_die = comp_unit_die ();
10296
10297 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10298
10299 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10300 {
10301 /* The size of the subrange type and its base type do not match,
10302 so we need to generate a size attribute for the subrange type. */
10303 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10304 }
10305
10306 if (low)
10307 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10308 if (high)
10309 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10310
10311 return subrange_die;
10312 }
10313
10314 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10315 entry that chains various modifiers in front of the given type. */
10316
10317 static dw_die_ref
10318 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10319 dw_die_ref context_die)
10320 {
10321 enum tree_code code = TREE_CODE (type);
10322 dw_die_ref mod_type_die;
10323 dw_die_ref sub_die = NULL;
10324 tree item_type = NULL;
10325 tree qualified_type;
10326 tree name, low, high;
10327 dw_die_ref mod_scope;
10328
10329 if (code == ERROR_MARK)
10330 return NULL;
10331
10332 /* See if we already have the appropriately qualified variant of
10333 this type. */
10334 qualified_type
10335 = get_qualified_type (type,
10336 ((is_const_type ? TYPE_QUAL_CONST : 0)
10337 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10338
10339 if (qualified_type == sizetype
10340 && TYPE_NAME (qualified_type)
10341 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10342 {
10343 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10344
10345 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10346 && TYPE_PRECISION (t)
10347 == TYPE_PRECISION (qualified_type)
10348 && TYPE_UNSIGNED (t)
10349 == TYPE_UNSIGNED (qualified_type));
10350 qualified_type = t;
10351 }
10352
10353 /* If we do, then we can just use its DIE, if it exists. */
10354 if (qualified_type)
10355 {
10356 mod_type_die = lookup_type_die (qualified_type);
10357 if (mod_type_die)
10358 return mod_type_die;
10359 }
10360
10361 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10362
10363 /* Handle C typedef types. */
10364 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10365 && !DECL_ARTIFICIAL (name))
10366 {
10367 tree dtype = TREE_TYPE (name);
10368
10369 if (qualified_type == dtype)
10370 {
10371 /* For a named type, use the typedef. */
10372 gen_type_die (qualified_type, context_die);
10373 return lookup_type_die (qualified_type);
10374 }
10375 else if (is_const_type < TYPE_READONLY (dtype)
10376 || is_volatile_type < TYPE_VOLATILE (dtype)
10377 || (is_const_type <= TYPE_READONLY (dtype)
10378 && is_volatile_type <= TYPE_VOLATILE (dtype)
10379 && DECL_ORIGINAL_TYPE (name) != type))
10380 /* cv-unqualified version of named type. Just use the unnamed
10381 type to which it refers. */
10382 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10383 is_const_type, is_volatile_type,
10384 context_die);
10385 /* Else cv-qualified version of named type; fall through. */
10386 }
10387
10388 mod_scope = scope_die_for (type, context_die);
10389
10390 if (is_const_type
10391 /* If both is_const_type and is_volatile_type, prefer the path
10392 which leads to a qualified type. */
10393 && (!is_volatile_type
10394 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10395 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10396 {
10397 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10398 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10399 }
10400 else if (is_volatile_type)
10401 {
10402 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10403 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10404 }
10405 else if (code == POINTER_TYPE)
10406 {
10407 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10408 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10409 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10410 item_type = TREE_TYPE (type);
10411 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10412 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10413 TYPE_ADDR_SPACE (item_type));
10414 }
10415 else if (code == REFERENCE_TYPE)
10416 {
10417 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10418 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10419 type);
10420 else
10421 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10422 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10423 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10424 item_type = TREE_TYPE (type);
10425 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10426 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10427 TYPE_ADDR_SPACE (item_type));
10428 }
10429 else if (code == INTEGER_TYPE
10430 && TREE_TYPE (type) != NULL_TREE
10431 && subrange_type_for_debug_p (type, &low, &high))
10432 {
10433 mod_type_die = subrange_type_die (type, low, high, context_die);
10434 item_type = TREE_TYPE (type);
10435 }
10436 else if (is_base_type (type))
10437 mod_type_die = base_type_die (type);
10438 else
10439 {
10440 gen_type_die (type, context_die);
10441
10442 /* We have to get the type_main_variant here (and pass that to the
10443 `lookup_type_die' routine) because the ..._TYPE node we have
10444 might simply be a *copy* of some original type node (where the
10445 copy was created to help us keep track of typedef names) and
10446 that copy might have a different TYPE_UID from the original
10447 ..._TYPE node. */
10448 if (TREE_CODE (type) != VECTOR_TYPE)
10449 return lookup_type_die (type_main_variant (type));
10450 else
10451 /* Vectors have the debugging information in the type,
10452 not the main variant. */
10453 return lookup_type_die (type);
10454 }
10455
10456 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10457 don't output a DW_TAG_typedef, since there isn't one in the
10458 user's program; just attach a DW_AT_name to the type.
10459 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10460 if the base type already has the same name. */
10461 if (name
10462 && ((TREE_CODE (name) != TYPE_DECL
10463 && (qualified_type == TYPE_MAIN_VARIANT (type)
10464 || (!is_const_type && !is_volatile_type)))
10465 || (TREE_CODE (name) == TYPE_DECL
10466 && TREE_TYPE (name) == qualified_type
10467 && DECL_NAME (name))))
10468 {
10469 if (TREE_CODE (name) == TYPE_DECL)
10470 /* Could just call add_name_and_src_coords_attributes here,
10471 but since this is a builtin type it doesn't have any
10472 useful source coordinates anyway. */
10473 name = DECL_NAME (name);
10474 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10475 }
10476 /* This probably indicates a bug. */
10477 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10478 {
10479 name = TYPE_NAME (type);
10480 if (name
10481 && TREE_CODE (name) == TYPE_DECL)
10482 name = DECL_NAME (name);
10483 add_name_attribute (mod_type_die,
10484 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10485 }
10486
10487 if (qualified_type)
10488 equate_type_number_to_die (qualified_type, mod_type_die);
10489
10490 if (item_type)
10491 /* We must do this after the equate_type_number_to_die call, in case
10492 this is a recursive type. This ensures that the modified_type_die
10493 recursion will terminate even if the type is recursive. Recursive
10494 types are possible in Ada. */
10495 sub_die = modified_type_die (item_type,
10496 TYPE_READONLY (item_type),
10497 TYPE_VOLATILE (item_type),
10498 context_die);
10499
10500 if (sub_die != NULL)
10501 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10502
10503 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10504 if (TYPE_ARTIFICIAL (type))
10505 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10506
10507 return mod_type_die;
10508 }
10509
10510 /* Generate DIEs for the generic parameters of T.
10511 T must be either a generic type or a generic function.
10512 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10513
10514 static void
10515 gen_generic_params_dies (tree t)
10516 {
10517 tree parms, args;
10518 int parms_num, i;
10519 dw_die_ref die = NULL;
10520 int non_default;
10521
10522 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10523 return;
10524
10525 if (TYPE_P (t))
10526 die = lookup_type_die (t);
10527 else if (DECL_P (t))
10528 die = lookup_decl_die (t);
10529
10530 gcc_assert (die);
10531
10532 parms = lang_hooks.get_innermost_generic_parms (t);
10533 if (!parms)
10534 /* T has no generic parameter. It means T is neither a generic type
10535 or function. End of story. */
10536 return;
10537
10538 parms_num = TREE_VEC_LENGTH (parms);
10539 args = lang_hooks.get_innermost_generic_args (t);
10540 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10541 non_default = int_cst_value (TREE_CHAIN (args));
10542 else
10543 non_default = TREE_VEC_LENGTH (args);
10544 for (i = 0; i < parms_num; i++)
10545 {
10546 tree parm, arg, arg_pack_elems;
10547 dw_die_ref parm_die;
10548
10549 parm = TREE_VEC_ELT (parms, i);
10550 arg = TREE_VEC_ELT (args, i);
10551 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10552 gcc_assert (parm && TREE_VALUE (parm) && arg);
10553
10554 if (parm && TREE_VALUE (parm) && arg)
10555 {
10556 /* If PARM represents a template parameter pack,
10557 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10558 by DW_TAG_template_*_parameter DIEs for the argument
10559 pack elements of ARG. Note that ARG would then be
10560 an argument pack. */
10561 if (arg_pack_elems)
10562 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10563 arg_pack_elems,
10564 die);
10565 else
10566 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10567 true /* emit name */, die);
10568 if (i >= non_default)
10569 add_AT_flag (parm_die, DW_AT_default_value, 1);
10570 }
10571 }
10572 }
10573
10574 /* Create and return a DIE for PARM which should be
10575 the representation of a generic type parameter.
10576 For instance, in the C++ front end, PARM would be a template parameter.
10577 ARG is the argument to PARM.
10578 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10579 name of the PARM.
10580 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10581 as a child node. */
10582
10583 static dw_die_ref
10584 generic_parameter_die (tree parm, tree arg,
10585 bool emit_name_p,
10586 dw_die_ref parent_die)
10587 {
10588 dw_die_ref tmpl_die = NULL;
10589 const char *name = NULL;
10590
10591 if (!parm || !DECL_NAME (parm) || !arg)
10592 return NULL;
10593
10594 /* We support non-type generic parameters and arguments,
10595 type generic parameters and arguments, as well as
10596 generic generic parameters (a.k.a. template template parameters in C++)
10597 and arguments. */
10598 if (TREE_CODE (parm) == PARM_DECL)
10599 /* PARM is a nontype generic parameter */
10600 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10601 else if (TREE_CODE (parm) == TYPE_DECL)
10602 /* PARM is a type generic parameter. */
10603 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10604 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10605 /* PARM is a generic generic parameter.
10606 Its DIE is a GNU extension. It shall have a
10607 DW_AT_name attribute to represent the name of the template template
10608 parameter, and a DW_AT_GNU_template_name attribute to represent the
10609 name of the template template argument. */
10610 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10611 parent_die, parm);
10612 else
10613 gcc_unreachable ();
10614
10615 if (tmpl_die)
10616 {
10617 tree tmpl_type;
10618
10619 /* If PARM is a generic parameter pack, it means we are
10620 emitting debug info for a template argument pack element.
10621 In other terms, ARG is a template argument pack element.
10622 In that case, we don't emit any DW_AT_name attribute for
10623 the die. */
10624 if (emit_name_p)
10625 {
10626 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10627 gcc_assert (name);
10628 add_AT_string (tmpl_die, DW_AT_name, name);
10629 }
10630
10631 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10632 {
10633 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10634 TMPL_DIE should have a child DW_AT_type attribute that is set
10635 to the type of the argument to PARM, which is ARG.
10636 If PARM is a type generic parameter, TMPL_DIE should have a
10637 child DW_AT_type that is set to ARG. */
10638 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10639 add_type_attribute (tmpl_die, tmpl_type, 0,
10640 TREE_THIS_VOLATILE (tmpl_type),
10641 parent_die);
10642 }
10643 else
10644 {
10645 /* So TMPL_DIE is a DIE representing a
10646 a generic generic template parameter, a.k.a template template
10647 parameter in C++ and arg is a template. */
10648
10649 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10650 to the name of the argument. */
10651 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10652 if (name)
10653 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10654 }
10655
10656 if (TREE_CODE (parm) == PARM_DECL)
10657 /* So PARM is a non-type generic parameter.
10658 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10659 attribute of TMPL_DIE which value represents the value
10660 of ARG.
10661 We must be careful here:
10662 The value of ARG might reference some function decls.
10663 We might currently be emitting debug info for a generic
10664 type and types are emitted before function decls, we don't
10665 know if the function decls referenced by ARG will actually be
10666 emitted after cgraph computations.
10667 So must defer the generation of the DW_AT_const_value to
10668 after cgraph is ready. */
10669 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10670 }
10671
10672 return tmpl_die;
10673 }
10674
10675 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10676 PARM_PACK must be a template parameter pack. The returned DIE
10677 will be child DIE of PARENT_DIE. */
10678
10679 static dw_die_ref
10680 template_parameter_pack_die (tree parm_pack,
10681 tree parm_pack_args,
10682 dw_die_ref parent_die)
10683 {
10684 dw_die_ref die;
10685 int j;
10686
10687 gcc_assert (parent_die && parm_pack);
10688
10689 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10690 add_name_and_src_coords_attributes (die, parm_pack);
10691 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10692 generic_parameter_die (parm_pack,
10693 TREE_VEC_ELT (parm_pack_args, j),
10694 false /* Don't emit DW_AT_name */,
10695 die);
10696 return die;
10697 }
10698
10699 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10700 an enumerated type. */
10701
10702 static inline int
10703 type_is_enum (const_tree type)
10704 {
10705 return TREE_CODE (type) == ENUMERAL_TYPE;
10706 }
10707
10708 /* Return the DBX register number described by a given RTL node. */
10709
10710 static unsigned int
10711 dbx_reg_number (const_rtx rtl)
10712 {
10713 unsigned regno = REGNO (rtl);
10714
10715 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10716
10717 #ifdef LEAF_REG_REMAP
10718 if (crtl->uses_only_leaf_regs)
10719 {
10720 int leaf_reg = LEAF_REG_REMAP (regno);
10721 if (leaf_reg != -1)
10722 regno = (unsigned) leaf_reg;
10723 }
10724 #endif
10725
10726 regno = DBX_REGISTER_NUMBER (regno);
10727 gcc_assert (regno != INVALID_REGNUM);
10728 return regno;
10729 }
10730
10731 /* Optionally add a DW_OP_piece term to a location description expression.
10732 DW_OP_piece is only added if the location description expression already
10733 doesn't end with DW_OP_piece. */
10734
10735 static void
10736 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10737 {
10738 dw_loc_descr_ref loc;
10739
10740 if (*list_head != NULL)
10741 {
10742 /* Find the end of the chain. */
10743 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10744 ;
10745
10746 if (loc->dw_loc_opc != DW_OP_piece)
10747 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10748 }
10749 }
10750
10751 /* Return a location descriptor that designates a machine register or
10752 zero if there is none. */
10753
10754 static dw_loc_descr_ref
10755 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10756 {
10757 rtx regs;
10758
10759 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10760 return 0;
10761
10762 /* We only use "frame base" when we're sure we're talking about the
10763 post-prologue local stack frame. We do this by *not* running
10764 register elimination until this point, and recognizing the special
10765 argument pointer and soft frame pointer rtx's.
10766 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10767 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10768 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10769 {
10770 dw_loc_descr_ref result = NULL;
10771
10772 if (dwarf_version >= 4 || !dwarf_strict)
10773 {
10774 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10775 initialized);
10776 if (result)
10777 add_loc_descr (&result,
10778 new_loc_descr (DW_OP_stack_value, 0, 0));
10779 }
10780 return result;
10781 }
10782
10783 regs = targetm.dwarf_register_span (rtl);
10784
10785 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10786 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10787 else
10788 {
10789 unsigned int dbx_regnum = dbx_reg_number (rtl);
10790 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10791 return 0;
10792 return one_reg_loc_descriptor (dbx_regnum, initialized);
10793 }
10794 }
10795
10796 /* Return a location descriptor that designates a machine register for
10797 a given hard register number. */
10798
10799 static dw_loc_descr_ref
10800 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10801 {
10802 dw_loc_descr_ref reg_loc_descr;
10803
10804 if (regno <= 31)
10805 reg_loc_descr
10806 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10807 else
10808 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10809
10810 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10811 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10812
10813 return reg_loc_descr;
10814 }
10815
10816 /* Given an RTL of a register, return a location descriptor that
10817 designates a value that spans more than one register. */
10818
10819 static dw_loc_descr_ref
10820 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10821 enum var_init_status initialized)
10822 {
10823 int size, i;
10824 dw_loc_descr_ref loc_result = NULL;
10825
10826 /* Simple, contiguous registers. */
10827 if (regs == NULL_RTX)
10828 {
10829 unsigned reg = REGNO (rtl);
10830 int nregs;
10831
10832 #ifdef LEAF_REG_REMAP
10833 if (crtl->uses_only_leaf_regs)
10834 {
10835 int leaf_reg = LEAF_REG_REMAP (reg);
10836 if (leaf_reg != -1)
10837 reg = (unsigned) leaf_reg;
10838 }
10839 #endif
10840
10841 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10842 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10843
10844 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10845
10846 loc_result = NULL;
10847 while (nregs--)
10848 {
10849 dw_loc_descr_ref t;
10850
10851 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10852 VAR_INIT_STATUS_INITIALIZED);
10853 add_loc_descr (&loc_result, t);
10854 add_loc_descr_op_piece (&loc_result, size);
10855 ++reg;
10856 }
10857 return loc_result;
10858 }
10859
10860 /* Now onto stupid register sets in non contiguous locations. */
10861
10862 gcc_assert (GET_CODE (regs) == PARALLEL);
10863
10864 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10865 loc_result = NULL;
10866
10867 for (i = 0; i < XVECLEN (regs, 0); ++i)
10868 {
10869 dw_loc_descr_ref t;
10870
10871 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
10872 VAR_INIT_STATUS_INITIALIZED);
10873 add_loc_descr (&loc_result, t);
10874 add_loc_descr_op_piece (&loc_result, size);
10875 }
10876
10877 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10878 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10879 return loc_result;
10880 }
10881
10882 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10883
10884 /* Return a location descriptor that designates a constant i,
10885 as a compound operation from constant (i >> shift), constant shift
10886 and DW_OP_shl. */
10887
10888 static dw_loc_descr_ref
10889 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10890 {
10891 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10892 add_loc_descr (&ret, int_loc_descriptor (shift));
10893 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10894 return ret;
10895 }
10896
10897 /* Return a location descriptor that designates a constant. */
10898
10899 static dw_loc_descr_ref
10900 int_loc_descriptor (HOST_WIDE_INT i)
10901 {
10902 enum dwarf_location_atom op;
10903
10904 /* Pick the smallest representation of a constant, rather than just
10905 defaulting to the LEB encoding. */
10906 if (i >= 0)
10907 {
10908 int clz = clz_hwi (i);
10909 int ctz = ctz_hwi (i);
10910 if (i <= 31)
10911 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10912 else if (i <= 0xff)
10913 op = DW_OP_const1u;
10914 else if (i <= 0xffff)
10915 op = DW_OP_const2u;
10916 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10917 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10918 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10919 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10920 while DW_OP_const4u is 5 bytes. */
10921 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10922 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10923 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10924 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10925 while DW_OP_const4u is 5 bytes. */
10926 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10927 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10928 op = DW_OP_const4u;
10929 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10930 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10931 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10932 while DW_OP_constu of constant >= 0x100000000 takes at least
10933 6 bytes. */
10934 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10935 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10936 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10937 >= HOST_BITS_PER_WIDE_INT)
10938 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10939 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10940 while DW_OP_constu takes in this case at least 6 bytes. */
10941 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10942 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10943 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10944 && size_of_uleb128 (i) > 6)
10945 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
10946 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10947 else
10948 op = DW_OP_constu;
10949 }
10950 else
10951 {
10952 if (i >= -0x80)
10953 op = DW_OP_const1s;
10954 else if (i >= -0x8000)
10955 op = DW_OP_const2s;
10956 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10957 {
10958 if (size_of_int_loc_descriptor (i) < 5)
10959 {
10960 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10961 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10962 return ret;
10963 }
10964 op = DW_OP_const4s;
10965 }
10966 else
10967 {
10968 if (size_of_int_loc_descriptor (i)
10969 < (unsigned long) 1 + size_of_sleb128 (i))
10970 {
10971 dw_loc_descr_ref ret = int_loc_descriptor (-i);
10972 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10973 return ret;
10974 }
10975 op = DW_OP_consts;
10976 }
10977 }
10978
10979 return new_loc_descr (op, i, 0);
10980 }
10981
10982 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10983 without actually allocating it. */
10984
10985 static unsigned long
10986 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10987 {
10988 return size_of_int_loc_descriptor (i >> shift)
10989 + size_of_int_loc_descriptor (shift)
10990 + 1;
10991 }
10992
10993 /* Return size_of_locs (int_loc_descriptor (i)) without
10994 actually allocating it. */
10995
10996 static unsigned long
10997 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10998 {
10999 unsigned long s;
11000
11001 if (i >= 0)
11002 {
11003 int clz, ctz;
11004 if (i <= 31)
11005 return 1;
11006 else if (i <= 0xff)
11007 return 2;
11008 else if (i <= 0xffff)
11009 return 3;
11010 clz = clz_hwi (i);
11011 ctz = ctz_hwi (i);
11012 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11013 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11014 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11015 - clz - 5);
11016 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11017 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11018 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11019 - clz - 8);
11020 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11021 return 5;
11022 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11023 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11024 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11025 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11026 - clz - 8);
11027 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11028 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11029 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11030 - clz - 16);
11031 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11032 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11033 && s > 6)
11034 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11035 - clz - 32);
11036 else
11037 return 1 + s;
11038 }
11039 else
11040 {
11041 if (i >= -0x80)
11042 return 2;
11043 else if (i >= -0x8000)
11044 return 3;
11045 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11046 {
11047 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11048 {
11049 s = size_of_int_loc_descriptor (-i) + 1;
11050 if (s < 5)
11051 return s;
11052 }
11053 return 5;
11054 }
11055 else
11056 {
11057 unsigned long r = 1 + size_of_sleb128 (i);
11058 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11059 {
11060 s = size_of_int_loc_descriptor (-i) + 1;
11061 if (s < r)
11062 return s;
11063 }
11064 return r;
11065 }
11066 }
11067 }
11068
11069 /* Return loc description representing "address" of integer value.
11070 This can appear only as toplevel expression. */
11071
11072 static dw_loc_descr_ref
11073 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11074 {
11075 int litsize;
11076 dw_loc_descr_ref loc_result = NULL;
11077
11078 if (!(dwarf_version >= 4 || !dwarf_strict))
11079 return NULL;
11080
11081 litsize = size_of_int_loc_descriptor (i);
11082 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11083 is more compact. For DW_OP_stack_value we need:
11084 litsize + 1 (DW_OP_stack_value)
11085 and for DW_OP_implicit_value:
11086 1 (DW_OP_implicit_value) + 1 (length) + size. */
11087 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11088 {
11089 loc_result = int_loc_descriptor (i);
11090 add_loc_descr (&loc_result,
11091 new_loc_descr (DW_OP_stack_value, 0, 0));
11092 return loc_result;
11093 }
11094
11095 loc_result = new_loc_descr (DW_OP_implicit_value,
11096 size, 0);
11097 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11098 loc_result->dw_loc_oprnd2.v.val_int = i;
11099 return loc_result;
11100 }
11101
11102 /* Return a location descriptor that designates a base+offset location. */
11103
11104 static dw_loc_descr_ref
11105 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11106 enum var_init_status initialized)
11107 {
11108 unsigned int regno;
11109 dw_loc_descr_ref result;
11110 dw_fde_ref fde = cfun->fde;
11111
11112 /* We only use "frame base" when we're sure we're talking about the
11113 post-prologue local stack frame. We do this by *not* running
11114 register elimination until this point, and recognizing the special
11115 argument pointer and soft frame pointer rtx's. */
11116 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11117 {
11118 rtx elim = (ira_use_lra_p
11119 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11120 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11121
11122 if (elim != reg)
11123 {
11124 if (GET_CODE (elim) == PLUS)
11125 {
11126 offset += INTVAL (XEXP (elim, 1));
11127 elim = XEXP (elim, 0);
11128 }
11129 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11130 && (elim == hard_frame_pointer_rtx
11131 || elim == stack_pointer_rtx))
11132 || elim == (frame_pointer_needed
11133 ? hard_frame_pointer_rtx
11134 : stack_pointer_rtx));
11135
11136 /* If drap register is used to align stack, use frame
11137 pointer + offset to access stack variables. If stack
11138 is aligned without drap, use stack pointer + offset to
11139 access stack variables. */
11140 if (crtl->stack_realign_tried
11141 && reg == frame_pointer_rtx)
11142 {
11143 int base_reg
11144 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11145 ? HARD_FRAME_POINTER_REGNUM
11146 : REGNO (elim));
11147 return new_reg_loc_descr (base_reg, offset);
11148 }
11149
11150 gcc_assert (frame_pointer_fb_offset_valid);
11151 offset += frame_pointer_fb_offset;
11152 return new_loc_descr (DW_OP_fbreg, offset, 0);
11153 }
11154 }
11155
11156 regno = REGNO (reg);
11157 #ifdef LEAF_REG_REMAP
11158 if (crtl->uses_only_leaf_regs)
11159 {
11160 int leaf_reg = LEAF_REG_REMAP (regno);
11161 if (leaf_reg != -1)
11162 regno = (unsigned) leaf_reg;
11163 }
11164 #endif
11165 regno = DWARF_FRAME_REGNUM (regno);
11166
11167 if (!optimize && fde
11168 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11169 {
11170 /* Use cfa+offset to represent the location of arguments passed
11171 on the stack when drap is used to align stack.
11172 Only do this when not optimizing, for optimized code var-tracking
11173 is supposed to track where the arguments live and the register
11174 used as vdrap or drap in some spot might be used for something
11175 else in other part of the routine. */
11176 return new_loc_descr (DW_OP_fbreg, offset, 0);
11177 }
11178
11179 if (regno <= 31)
11180 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11181 offset, 0);
11182 else
11183 result = new_loc_descr (DW_OP_bregx, regno, offset);
11184
11185 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11186 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11187
11188 return result;
11189 }
11190
11191 /* Return true if this RTL expression describes a base+offset calculation. */
11192
11193 static inline int
11194 is_based_loc (const_rtx rtl)
11195 {
11196 return (GET_CODE (rtl) == PLUS
11197 && ((REG_P (XEXP (rtl, 0))
11198 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11199 && CONST_INT_P (XEXP (rtl, 1)))));
11200 }
11201
11202 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11203 failed. */
11204
11205 static dw_loc_descr_ref
11206 tls_mem_loc_descriptor (rtx mem)
11207 {
11208 tree base;
11209 dw_loc_descr_ref loc_result;
11210
11211 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11212 return NULL;
11213
11214 base = get_base_address (MEM_EXPR (mem));
11215 if (base == NULL
11216 || TREE_CODE (base) != VAR_DECL
11217 || !DECL_THREAD_LOCAL_P (base))
11218 return NULL;
11219
11220 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11221 if (loc_result == NULL)
11222 return NULL;
11223
11224 if (MEM_OFFSET (mem))
11225 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11226
11227 return loc_result;
11228 }
11229
11230 /* Output debug info about reason why we failed to expand expression as dwarf
11231 expression. */
11232
11233 static void
11234 expansion_failed (tree expr, rtx rtl, char const *reason)
11235 {
11236 if (dump_file && (dump_flags & TDF_DETAILS))
11237 {
11238 fprintf (dump_file, "Failed to expand as dwarf: ");
11239 if (expr)
11240 print_generic_expr (dump_file, expr, dump_flags);
11241 if (rtl)
11242 {
11243 fprintf (dump_file, "\n");
11244 print_rtl (dump_file, rtl);
11245 }
11246 fprintf (dump_file, "\nReason: %s\n", reason);
11247 }
11248 }
11249
11250 /* Helper function for const_ok_for_output, called either directly
11251 or via for_each_rtx. */
11252
11253 static int
11254 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11255 {
11256 rtx rtl = *rtlp;
11257
11258 if (GET_CODE (rtl) == UNSPEC)
11259 {
11260 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11261 we can't express it in the debug info. */
11262 #ifdef ENABLE_CHECKING
11263 /* Don't complain about TLS UNSPECs, those are just too hard to
11264 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11265 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11266 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11267 if (XVECLEN (rtl, 0) == 0
11268 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11269 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11270 inform (current_function_decl
11271 ? DECL_SOURCE_LOCATION (current_function_decl)
11272 : UNKNOWN_LOCATION,
11273 #if NUM_UNSPEC_VALUES > 0
11274 "non-delegitimized UNSPEC %s (%d) found in variable location",
11275 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11276 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11277 XINT (rtl, 1));
11278 #else
11279 "non-delegitimized UNSPEC %d found in variable location",
11280 XINT (rtl, 1));
11281 #endif
11282 #endif
11283 expansion_failed (NULL_TREE, rtl,
11284 "UNSPEC hasn't been delegitimized.\n");
11285 return 1;
11286 }
11287
11288 if (targetm.const_not_ok_for_debug_p (rtl))
11289 {
11290 expansion_failed (NULL_TREE, rtl,
11291 "Expression rejected for debug by the backend.\n");
11292 return 1;
11293 }
11294
11295 if (GET_CODE (rtl) != SYMBOL_REF)
11296 return 0;
11297
11298 if (CONSTANT_POOL_ADDRESS_P (rtl))
11299 {
11300 bool marked;
11301 get_pool_constant_mark (rtl, &marked);
11302 /* If all references to this pool constant were optimized away,
11303 it was not output and thus we can't represent it. */
11304 if (!marked)
11305 {
11306 expansion_failed (NULL_TREE, rtl,
11307 "Constant was removed from constant pool.\n");
11308 return 1;
11309 }
11310 }
11311
11312 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11313 return 1;
11314
11315 /* Avoid references to external symbols in debug info, on several targets
11316 the linker might even refuse to link when linking a shared library,
11317 and in many other cases the relocations for .debug_info/.debug_loc are
11318 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11319 to be defined within the same shared library or executable are fine. */
11320 if (SYMBOL_REF_EXTERNAL_P (rtl))
11321 {
11322 tree decl = SYMBOL_REF_DECL (rtl);
11323
11324 if (decl == NULL || !targetm.binds_local_p (decl))
11325 {
11326 expansion_failed (NULL_TREE, rtl,
11327 "Symbol not defined in current TU.\n");
11328 return 1;
11329 }
11330 }
11331
11332 return 0;
11333 }
11334
11335 /* Return true if constant RTL can be emitted in DW_OP_addr or
11336 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11337 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11338
11339 static bool
11340 const_ok_for_output (rtx rtl)
11341 {
11342 if (GET_CODE (rtl) == SYMBOL_REF)
11343 return const_ok_for_output_1 (&rtl, NULL) == 0;
11344
11345 if (GET_CODE (rtl) == CONST)
11346 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11347
11348 return true;
11349 }
11350
11351 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11352 if possible, NULL otherwise. */
11353
11354 static dw_die_ref
11355 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11356 {
11357 dw_die_ref type_die;
11358 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11359
11360 if (type == NULL)
11361 return NULL;
11362 switch (TREE_CODE (type))
11363 {
11364 case INTEGER_TYPE:
11365 case REAL_TYPE:
11366 break;
11367 default:
11368 return NULL;
11369 }
11370 type_die = lookup_type_die (type);
11371 if (!type_die)
11372 type_die = modified_type_die (type, false, false, comp_unit_die ());
11373 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11374 return NULL;
11375 return type_die;
11376 }
11377
11378 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11379 type matching MODE, or, if MODE is narrower than or as wide as
11380 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11381 possible. */
11382
11383 static dw_loc_descr_ref
11384 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11385 {
11386 enum machine_mode outer_mode = mode;
11387 dw_die_ref type_die;
11388 dw_loc_descr_ref cvt;
11389
11390 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11391 {
11392 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11393 return op;
11394 }
11395 type_die = base_type_for_mode (outer_mode, 1);
11396 if (type_die == NULL)
11397 return NULL;
11398 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11399 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11400 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11401 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11402 add_loc_descr (&op, cvt);
11403 return op;
11404 }
11405
11406 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11407
11408 static dw_loc_descr_ref
11409 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11410 dw_loc_descr_ref op1)
11411 {
11412 dw_loc_descr_ref ret = op0;
11413 add_loc_descr (&ret, op1);
11414 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11415 if (STORE_FLAG_VALUE != 1)
11416 {
11417 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11418 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11419 }
11420 return ret;
11421 }
11422
11423 /* Return location descriptor for signed comparison OP RTL. */
11424
11425 static dw_loc_descr_ref
11426 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11427 enum machine_mode mem_mode)
11428 {
11429 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11430 dw_loc_descr_ref op0, op1;
11431 int shift;
11432
11433 if (op_mode == VOIDmode)
11434 op_mode = GET_MODE (XEXP (rtl, 1));
11435 if (op_mode == VOIDmode)
11436 return NULL;
11437
11438 if (dwarf_strict
11439 && (GET_MODE_CLASS (op_mode) != MODE_INT
11440 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11441 return NULL;
11442
11443 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11444 VAR_INIT_STATUS_INITIALIZED);
11445 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11446 VAR_INIT_STATUS_INITIALIZED);
11447
11448 if (op0 == NULL || op1 == NULL)
11449 return NULL;
11450
11451 if (GET_MODE_CLASS (op_mode) != MODE_INT
11452 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11453 return compare_loc_descriptor (op, op0, op1);
11454
11455 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11456 {
11457 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11458 dw_loc_descr_ref cvt;
11459
11460 if (type_die == NULL)
11461 return NULL;
11462 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11463 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11464 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11465 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11466 add_loc_descr (&op0, cvt);
11467 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11468 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11469 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11470 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11471 add_loc_descr (&op1, cvt);
11472 return compare_loc_descriptor (op, op0, op1);
11473 }
11474
11475 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11476 /* For eq/ne, if the operands are known to be zero-extended,
11477 there is no need to do the fancy shifting up. */
11478 if (op == DW_OP_eq || op == DW_OP_ne)
11479 {
11480 dw_loc_descr_ref last0, last1;
11481 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11482 ;
11483 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11484 ;
11485 /* deref_size zero extends, and for constants we can check
11486 whether they are zero extended or not. */
11487 if (((last0->dw_loc_opc == DW_OP_deref_size
11488 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11489 || (CONST_INT_P (XEXP (rtl, 0))
11490 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11491 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11492 && ((last1->dw_loc_opc == DW_OP_deref_size
11493 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11494 || (CONST_INT_P (XEXP (rtl, 1))
11495 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11496 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11497 return compare_loc_descriptor (op, op0, op1);
11498
11499 /* EQ/NE comparison against constant in narrower type than
11500 DWARF2_ADDR_SIZE can be performed either as
11501 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11502 DW_OP_{eq,ne}
11503 or
11504 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11505 DW_OP_{eq,ne}. Pick whatever is shorter. */
11506 if (CONST_INT_P (XEXP (rtl, 1))
11507 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11508 && (size_of_int_loc_descriptor (shift) + 1
11509 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11510 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11511 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11512 & GET_MODE_MASK (op_mode))))
11513 {
11514 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11515 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11516 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11517 & GET_MODE_MASK (op_mode));
11518 return compare_loc_descriptor (op, op0, op1);
11519 }
11520 }
11521 add_loc_descr (&op0, int_loc_descriptor (shift));
11522 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11523 if (CONST_INT_P (XEXP (rtl, 1)))
11524 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11525 else
11526 {
11527 add_loc_descr (&op1, int_loc_descriptor (shift));
11528 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11529 }
11530 return compare_loc_descriptor (op, op0, op1);
11531 }
11532
11533 /* Return location descriptor for unsigned comparison OP RTL. */
11534
11535 static dw_loc_descr_ref
11536 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11537 enum machine_mode mem_mode)
11538 {
11539 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11540 dw_loc_descr_ref op0, op1;
11541
11542 if (op_mode == VOIDmode)
11543 op_mode = GET_MODE (XEXP (rtl, 1));
11544 if (op_mode == VOIDmode)
11545 return NULL;
11546 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11547 return NULL;
11548
11549 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11550 return NULL;
11551
11552 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11553 VAR_INIT_STATUS_INITIALIZED);
11554 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11555 VAR_INIT_STATUS_INITIALIZED);
11556
11557 if (op0 == NULL || op1 == NULL)
11558 return NULL;
11559
11560 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11561 {
11562 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11563 dw_loc_descr_ref last0, last1;
11564 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11565 ;
11566 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11567 ;
11568 if (CONST_INT_P (XEXP (rtl, 0)))
11569 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11570 /* deref_size zero extends, so no need to mask it again. */
11571 else if (last0->dw_loc_opc != DW_OP_deref_size
11572 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11573 {
11574 add_loc_descr (&op0, int_loc_descriptor (mask));
11575 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11576 }
11577 if (CONST_INT_P (XEXP (rtl, 1)))
11578 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11579 /* deref_size zero extends, so no need to mask it again. */
11580 else if (last1->dw_loc_opc != DW_OP_deref_size
11581 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11582 {
11583 add_loc_descr (&op1, int_loc_descriptor (mask));
11584 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11585 }
11586 }
11587 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11588 {
11589 HOST_WIDE_INT bias = 1;
11590 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11591 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11592 if (CONST_INT_P (XEXP (rtl, 1)))
11593 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11594 + INTVAL (XEXP (rtl, 1)));
11595 else
11596 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11597 bias, 0));
11598 }
11599 return compare_loc_descriptor (op, op0, op1);
11600 }
11601
11602 /* Return location descriptor for {U,S}{MIN,MAX}. */
11603
11604 static dw_loc_descr_ref
11605 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11606 enum machine_mode mem_mode)
11607 {
11608 enum dwarf_location_atom op;
11609 dw_loc_descr_ref op0, op1, ret;
11610 dw_loc_descr_ref bra_node, drop_node;
11611
11612 if (dwarf_strict
11613 && (GET_MODE_CLASS (mode) != MODE_INT
11614 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11615 return NULL;
11616
11617 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11618 VAR_INIT_STATUS_INITIALIZED);
11619 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11620 VAR_INIT_STATUS_INITIALIZED);
11621
11622 if (op0 == NULL || op1 == NULL)
11623 return NULL;
11624
11625 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11626 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11627 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11628 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11629 {
11630 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11631 {
11632 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11633 add_loc_descr (&op0, int_loc_descriptor (mask));
11634 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11635 add_loc_descr (&op1, int_loc_descriptor (mask));
11636 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11637 }
11638 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11639 {
11640 HOST_WIDE_INT bias = 1;
11641 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11642 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11643 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11644 }
11645 }
11646 else if (GET_MODE_CLASS (mode) == MODE_INT
11647 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11648 {
11649 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11650 add_loc_descr (&op0, int_loc_descriptor (shift));
11651 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11652 add_loc_descr (&op1, int_loc_descriptor (shift));
11653 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11654 }
11655 else if (GET_MODE_CLASS (mode) == MODE_INT
11656 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11657 {
11658 dw_die_ref type_die = base_type_for_mode (mode, 0);
11659 dw_loc_descr_ref cvt;
11660 if (type_die == NULL)
11661 return NULL;
11662 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11663 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11664 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11665 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11666 add_loc_descr (&op0, cvt);
11667 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11668 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11669 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11670 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11671 add_loc_descr (&op1, cvt);
11672 }
11673
11674 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11675 op = DW_OP_lt;
11676 else
11677 op = DW_OP_gt;
11678 ret = op0;
11679 add_loc_descr (&ret, op1);
11680 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11681 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11682 add_loc_descr (&ret, bra_node);
11683 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11684 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11685 add_loc_descr (&ret, drop_node);
11686 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11687 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11688 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11689 && GET_MODE_CLASS (mode) == MODE_INT
11690 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11691 ret = convert_descriptor_to_mode (mode, ret);
11692 return ret;
11693 }
11694
11695 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11696 but after converting arguments to type_die, afterwards
11697 convert back to unsigned. */
11698
11699 static dw_loc_descr_ref
11700 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11701 enum machine_mode mode, enum machine_mode mem_mode)
11702 {
11703 dw_loc_descr_ref cvt, op0, op1;
11704
11705 if (type_die == NULL)
11706 return NULL;
11707 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11708 VAR_INIT_STATUS_INITIALIZED);
11709 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11710 VAR_INIT_STATUS_INITIALIZED);
11711 if (op0 == NULL || op1 == NULL)
11712 return NULL;
11713 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11714 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11715 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11716 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11717 add_loc_descr (&op0, cvt);
11718 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11719 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11720 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11721 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11722 add_loc_descr (&op1, cvt);
11723 add_loc_descr (&op0, op1);
11724 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11725 return convert_descriptor_to_mode (mode, op0);
11726 }
11727
11728 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11729 const0 is DW_OP_lit0 or corresponding typed constant,
11730 const1 is DW_OP_lit1 or corresponding typed constant
11731 and constMSB is constant with just the MSB bit set
11732 for the mode):
11733 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11734 L1: const0 DW_OP_swap
11735 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11736 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11737 L3: DW_OP_drop
11738 L4: DW_OP_nop
11739
11740 CTZ is similar:
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 const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
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 FFS is similar:
11749 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11750 L1: const1 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 static dw_loc_descr_ref
11757 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11758 enum machine_mode mem_mode)
11759 {
11760 dw_loc_descr_ref op0, ret, tmp;
11761 HOST_WIDE_INT valv;
11762 dw_loc_descr_ref l1jump, l1label;
11763 dw_loc_descr_ref l2jump, l2label;
11764 dw_loc_descr_ref l3jump, l3label;
11765 dw_loc_descr_ref l4jump, l4label;
11766 rtx msb;
11767
11768 if (GET_MODE_CLASS (mode) != MODE_INT
11769 || GET_MODE (XEXP (rtl, 0)) != mode
11770 || (GET_CODE (rtl) == CLZ
11771 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11772 return NULL;
11773
11774 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11775 VAR_INIT_STATUS_INITIALIZED);
11776 if (op0 == NULL)
11777 return NULL;
11778 ret = op0;
11779 if (GET_CODE (rtl) == CLZ)
11780 {
11781 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11782 valv = GET_MODE_BITSIZE (mode);
11783 }
11784 else if (GET_CODE (rtl) == FFS)
11785 valv = 0;
11786 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11787 valv = GET_MODE_BITSIZE (mode);
11788 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11789 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11790 add_loc_descr (&ret, l1jump);
11791 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11792 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11793 VAR_INIT_STATUS_INITIALIZED);
11794 if (tmp == NULL)
11795 return NULL;
11796 add_loc_descr (&ret, tmp);
11797 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11798 add_loc_descr (&ret, l4jump);
11799 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11800 ? const1_rtx : const0_rtx,
11801 mode, mem_mode,
11802 VAR_INIT_STATUS_INITIALIZED);
11803 if (l1label == NULL)
11804 return NULL;
11805 add_loc_descr (&ret, l1label);
11806 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11807 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11808 add_loc_descr (&ret, l2label);
11809 if (GET_CODE (rtl) != CLZ)
11810 msb = const1_rtx;
11811 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11812 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11813 << (GET_MODE_BITSIZE (mode) - 1));
11814 else
11815 msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11816 << (GET_MODE_BITSIZE (mode)
11817 - HOST_BITS_PER_WIDE_INT - 1), mode);
11818 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11819 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11820 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11821 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11822 else
11823 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11824 VAR_INIT_STATUS_INITIALIZED);
11825 if (tmp == NULL)
11826 return NULL;
11827 add_loc_descr (&ret, tmp);
11828 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11829 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11830 add_loc_descr (&ret, l3jump);
11831 tmp = mem_loc_descriptor (const1_rtx, 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 (GET_CODE (rtl) == CLZ
11837 ? DW_OP_shl : DW_OP_shr, 0, 0));
11838 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11839 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11840 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11841 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11842 add_loc_descr (&ret, l2jump);
11843 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11844 add_loc_descr (&ret, l3label);
11845 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11846 add_loc_descr (&ret, l4label);
11847 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11848 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11849 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11850 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11851 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11852 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11853 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11854 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11855 return ret;
11856 }
11857
11858 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11859 const1 is DW_OP_lit1 or corresponding typed constant):
11860 const0 DW_OP_swap
11861 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11862 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11863 L2: DW_OP_drop
11864
11865 PARITY is similar:
11866 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11867 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11868 L2: DW_OP_drop */
11869
11870 static dw_loc_descr_ref
11871 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11872 enum machine_mode mem_mode)
11873 {
11874 dw_loc_descr_ref op0, ret, tmp;
11875 dw_loc_descr_ref l1jump, l1label;
11876 dw_loc_descr_ref l2jump, l2label;
11877
11878 if (GET_MODE_CLASS (mode) != MODE_INT
11879 || GET_MODE (XEXP (rtl, 0)) != mode)
11880 return NULL;
11881
11882 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11883 VAR_INIT_STATUS_INITIALIZED);
11884 if (op0 == NULL)
11885 return NULL;
11886 ret = op0;
11887 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11888 VAR_INIT_STATUS_INITIALIZED);
11889 if (tmp == NULL)
11890 return NULL;
11891 add_loc_descr (&ret, tmp);
11892 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11893 l1label = new_loc_descr (DW_OP_dup, 0, 0);
11894 add_loc_descr (&ret, l1label);
11895 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11896 add_loc_descr (&ret, l2jump);
11897 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11898 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11899 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11900 VAR_INIT_STATUS_INITIALIZED);
11901 if (tmp == NULL)
11902 return NULL;
11903 add_loc_descr (&ret, tmp);
11904 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11905 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11906 ? DW_OP_plus : DW_OP_xor, 0, 0));
11907 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11908 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11909 VAR_INIT_STATUS_INITIALIZED);
11910 add_loc_descr (&ret, tmp);
11911 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11912 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11913 add_loc_descr (&ret, l1jump);
11914 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11915 add_loc_descr (&ret, l2label);
11916 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11917 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11918 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11919 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11920 return ret;
11921 }
11922
11923 /* BSWAP (constS is initial shift count, either 56 or 24):
11924 constS const0
11925 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11926 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11927 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11928 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11929 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
11930
11931 static dw_loc_descr_ref
11932 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11933 enum machine_mode mem_mode)
11934 {
11935 dw_loc_descr_ref op0, ret, tmp;
11936 dw_loc_descr_ref l1jump, l1label;
11937 dw_loc_descr_ref l2jump, l2label;
11938
11939 if (GET_MODE_CLASS (mode) != MODE_INT
11940 || BITS_PER_UNIT != 8
11941 || (GET_MODE_BITSIZE (mode) != 32
11942 && GET_MODE_BITSIZE (mode) != 64))
11943 return NULL;
11944
11945 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11946 VAR_INIT_STATUS_INITIALIZED);
11947 if (op0 == NULL)
11948 return NULL;
11949
11950 ret = op0;
11951 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11952 mode, mem_mode,
11953 VAR_INIT_STATUS_INITIALIZED);
11954 if (tmp == NULL)
11955 return NULL;
11956 add_loc_descr (&ret, tmp);
11957 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11958 VAR_INIT_STATUS_INITIALIZED);
11959 if (tmp == NULL)
11960 return NULL;
11961 add_loc_descr (&ret, tmp);
11962 l1label = new_loc_descr (DW_OP_pick, 2, 0);
11963 add_loc_descr (&ret, l1label);
11964 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11965 mode, mem_mode,
11966 VAR_INIT_STATUS_INITIALIZED);
11967 add_loc_descr (&ret, tmp);
11968 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11969 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11970 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11971 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11972 VAR_INIT_STATUS_INITIALIZED);
11973 if (tmp == NULL)
11974 return NULL;
11975 add_loc_descr (&ret, tmp);
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11977 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11979 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11980 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11981 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11982 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11983 VAR_INIT_STATUS_INITIALIZED);
11984 add_loc_descr (&ret, tmp);
11985 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11986 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11987 add_loc_descr (&ret, l2jump);
11988 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11989 VAR_INIT_STATUS_INITIALIZED);
11990 add_loc_descr (&ret, tmp);
11991 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11992 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11993 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11994 add_loc_descr (&ret, l1jump);
11995 l2label = new_loc_descr (DW_OP_drop, 0, 0);
11996 add_loc_descr (&ret, l2label);
11997 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11999 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12000 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12001 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12002 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12003 return ret;
12004 }
12005
12006 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12007 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12008 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12009 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12010
12011 ROTATERT is similar:
12012 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12013 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12014 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12015
12016 static dw_loc_descr_ref
12017 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12018 enum machine_mode mem_mode)
12019 {
12020 rtx rtlop1 = XEXP (rtl, 1);
12021 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12022 int i;
12023
12024 if (GET_MODE_CLASS (mode) != MODE_INT)
12025 return NULL;
12026
12027 if (GET_MODE (rtlop1) != VOIDmode
12028 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12029 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12030 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12031 VAR_INIT_STATUS_INITIALIZED);
12032 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12033 VAR_INIT_STATUS_INITIALIZED);
12034 if (op0 == NULL || op1 == NULL)
12035 return NULL;
12036 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12037 for (i = 0; i < 2; i++)
12038 {
12039 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12040 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12041 mode, mem_mode,
12042 VAR_INIT_STATUS_INITIALIZED);
12043 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12044 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12045 ? DW_OP_const4u
12046 : HOST_BITS_PER_WIDE_INT == 64
12047 ? DW_OP_const8u : DW_OP_constu,
12048 GET_MODE_MASK (mode), 0);
12049 else
12050 mask[i] = NULL;
12051 if (mask[i] == NULL)
12052 return NULL;
12053 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12054 }
12055 ret = op0;
12056 add_loc_descr (&ret, op1);
12057 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12058 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12059 if (GET_CODE (rtl) == ROTATERT)
12060 {
12061 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12062 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12063 GET_MODE_BITSIZE (mode), 0));
12064 }
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12066 if (mask[0] != NULL)
12067 add_loc_descr (&ret, mask[0]);
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12069 if (mask[1] != NULL)
12070 {
12071 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12072 add_loc_descr (&ret, mask[1]);
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12074 }
12075 if (GET_CODE (rtl) == ROTATE)
12076 {
12077 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12078 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12079 GET_MODE_BITSIZE (mode), 0));
12080 }
12081 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12083 return ret;
12084 }
12085
12086 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12087 for DEBUG_PARAMETER_REF RTL. */
12088
12089 static dw_loc_descr_ref
12090 parameter_ref_descriptor (rtx rtl)
12091 {
12092 dw_loc_descr_ref ret;
12093 dw_die_ref ref;
12094
12095 if (dwarf_strict)
12096 return NULL;
12097 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12098 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12099 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12100 if (ref)
12101 {
12102 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12103 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12104 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12105 }
12106 else
12107 {
12108 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12109 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12110 }
12111 return ret;
12112 }
12113
12114 /* The following routine converts the RTL for a variable or parameter
12115 (resident in memory) into an equivalent Dwarf representation of a
12116 mechanism for getting the address of that same variable onto the top of a
12117 hypothetical "address evaluation" stack.
12118
12119 When creating memory location descriptors, we are effectively transforming
12120 the RTL for a memory-resident object into its Dwarf postfix expression
12121 equivalent. This routine recursively descends an RTL tree, turning
12122 it into Dwarf postfix code as it goes.
12123
12124 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12125
12126 MEM_MODE is the mode of the memory reference, needed to handle some
12127 autoincrement addressing modes.
12128
12129 Return 0 if we can't represent the location. */
12130
12131 dw_loc_descr_ref
12132 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12133 enum machine_mode mem_mode,
12134 enum var_init_status initialized)
12135 {
12136 dw_loc_descr_ref mem_loc_result = NULL;
12137 enum dwarf_location_atom op;
12138 dw_loc_descr_ref op0, op1;
12139 rtx inner = NULL_RTX;
12140
12141 if (mode == VOIDmode)
12142 mode = GET_MODE (rtl);
12143
12144 /* Note that for a dynamically sized array, the location we will generate a
12145 description of here will be the lowest numbered location which is
12146 actually within the array. That's *not* necessarily the same as the
12147 zeroth element of the array. */
12148
12149 rtl = targetm.delegitimize_address (rtl);
12150
12151 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12152 return NULL;
12153
12154 switch (GET_CODE (rtl))
12155 {
12156 case POST_INC:
12157 case POST_DEC:
12158 case POST_MODIFY:
12159 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12160
12161 case SUBREG:
12162 /* The case of a subreg may arise when we have a local (register)
12163 variable or a formal (register) parameter which doesn't quite fill
12164 up an entire register. For now, just assume that it is
12165 legitimate to make the Dwarf info refer to the whole register which
12166 contains the given subreg. */
12167 if (!subreg_lowpart_p (rtl))
12168 break;
12169 inner = SUBREG_REG (rtl);
12170 case TRUNCATE:
12171 if (inner == NULL_RTX)
12172 inner = XEXP (rtl, 0);
12173 if (GET_MODE_CLASS (mode) == MODE_INT
12174 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12175 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12176 #ifdef POINTERS_EXTEND_UNSIGNED
12177 || (mode == Pmode && mem_mode != VOIDmode)
12178 #endif
12179 )
12180 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12181 {
12182 mem_loc_result = mem_loc_descriptor (inner,
12183 GET_MODE (inner),
12184 mem_mode, initialized);
12185 break;
12186 }
12187 if (dwarf_strict)
12188 break;
12189 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12190 break;
12191 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12192 && (GET_MODE_CLASS (mode) != MODE_INT
12193 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12194 break;
12195 else
12196 {
12197 dw_die_ref type_die;
12198 dw_loc_descr_ref cvt;
12199
12200 mem_loc_result = mem_loc_descriptor (inner,
12201 GET_MODE (inner),
12202 mem_mode, initialized);
12203 if (mem_loc_result == NULL)
12204 break;
12205 type_die = base_type_for_mode (mode,
12206 GET_MODE_CLASS (mode) == MODE_INT);
12207 if (type_die == NULL)
12208 {
12209 mem_loc_result = NULL;
12210 break;
12211 }
12212 if (GET_MODE_SIZE (mode)
12213 != GET_MODE_SIZE (GET_MODE (inner)))
12214 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12215 else
12216 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12217 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12218 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12219 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12220 add_loc_descr (&mem_loc_result, cvt);
12221 }
12222 break;
12223
12224 case REG:
12225 if (GET_MODE_CLASS (mode) != MODE_INT
12226 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12227 && rtl != arg_pointer_rtx
12228 && rtl != frame_pointer_rtx
12229 #ifdef POINTERS_EXTEND_UNSIGNED
12230 && (mode != Pmode || mem_mode == VOIDmode)
12231 #endif
12232 ))
12233 {
12234 dw_die_ref type_die;
12235 unsigned int dbx_regnum;
12236
12237 if (dwarf_strict)
12238 break;
12239 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12240 break;
12241 type_die = base_type_for_mode (mode,
12242 GET_MODE_CLASS (mode) == MODE_INT);
12243 if (type_die == NULL)
12244 break;
12245
12246 dbx_regnum = dbx_reg_number (rtl);
12247 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12248 break;
12249 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12250 dbx_regnum, 0);
12251 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12252 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12253 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12254 break;
12255 }
12256 /* Whenever a register number forms a part of the description of the
12257 method for calculating the (dynamic) address of a memory resident
12258 object, DWARF rules require the register number be referred to as
12259 a "base register". This distinction is not based in any way upon
12260 what category of register the hardware believes the given register
12261 belongs to. This is strictly DWARF terminology we're dealing with
12262 here. Note that in cases where the location of a memory-resident
12263 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12264 OP_CONST (0)) the actual DWARF location descriptor that we generate
12265 may just be OP_BASEREG (basereg). This may look deceptively like
12266 the object in question was allocated to a register (rather than in
12267 memory) so DWARF consumers need to be aware of the subtle
12268 distinction between OP_REG and OP_BASEREG. */
12269 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12270 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12271 else if (stack_realign_drap
12272 && crtl->drap_reg
12273 && crtl->args.internal_arg_pointer == rtl
12274 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12275 {
12276 /* If RTL is internal_arg_pointer, which has been optimized
12277 out, use DRAP instead. */
12278 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12279 VAR_INIT_STATUS_INITIALIZED);
12280 }
12281 break;
12282
12283 case SIGN_EXTEND:
12284 case ZERO_EXTEND:
12285 if (GET_MODE_CLASS (mode) != MODE_INT)
12286 break;
12287 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12288 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12289 if (op0 == 0)
12290 break;
12291 else if (GET_CODE (rtl) == ZERO_EXTEND
12292 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12293 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12294 < HOST_BITS_PER_WIDE_INT
12295 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12296 to expand zero extend as two shifts instead of
12297 masking. */
12298 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12299 {
12300 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12301 mem_loc_result = op0;
12302 add_loc_descr (&mem_loc_result,
12303 int_loc_descriptor (GET_MODE_MASK (imode)));
12304 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12305 }
12306 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12307 {
12308 int shift = DWARF2_ADDR_SIZE
12309 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12310 shift *= BITS_PER_UNIT;
12311 if (GET_CODE (rtl) == SIGN_EXTEND)
12312 op = DW_OP_shra;
12313 else
12314 op = DW_OP_shr;
12315 mem_loc_result = op0;
12316 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12317 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12318 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12319 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12320 }
12321 else if (!dwarf_strict)
12322 {
12323 dw_die_ref type_die1, type_die2;
12324 dw_loc_descr_ref cvt;
12325
12326 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12327 GET_CODE (rtl) == ZERO_EXTEND);
12328 if (type_die1 == NULL)
12329 break;
12330 type_die2 = base_type_for_mode (mode, 1);
12331 if (type_die2 == NULL)
12332 break;
12333 mem_loc_result = op0;
12334 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12335 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12336 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12337 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12338 add_loc_descr (&mem_loc_result, cvt);
12339 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12340 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12341 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12342 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12343 add_loc_descr (&mem_loc_result, cvt);
12344 }
12345 break;
12346
12347 case MEM:
12348 {
12349 rtx new_rtl = avoid_constant_pool_reference (rtl);
12350 if (new_rtl != rtl)
12351 {
12352 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12353 initialized);
12354 if (mem_loc_result != NULL)
12355 return mem_loc_result;
12356 }
12357 }
12358 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12359 get_address_mode (rtl), mode,
12360 VAR_INIT_STATUS_INITIALIZED);
12361 if (mem_loc_result == NULL)
12362 mem_loc_result = tls_mem_loc_descriptor (rtl);
12363 if (mem_loc_result != NULL)
12364 {
12365 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12366 || GET_MODE_CLASS (mode) != MODE_INT)
12367 {
12368 dw_die_ref type_die;
12369 dw_loc_descr_ref deref;
12370
12371 if (dwarf_strict)
12372 return NULL;
12373 type_die
12374 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12375 if (type_die == NULL)
12376 return NULL;
12377 deref = new_loc_descr (DW_OP_GNU_deref_type,
12378 GET_MODE_SIZE (mode), 0);
12379 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12380 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12381 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12382 add_loc_descr (&mem_loc_result, deref);
12383 }
12384 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12385 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12386 else
12387 add_loc_descr (&mem_loc_result,
12388 new_loc_descr (DW_OP_deref_size,
12389 GET_MODE_SIZE (mode), 0));
12390 }
12391 break;
12392
12393 case LO_SUM:
12394 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12395
12396 case LABEL_REF:
12397 /* Some ports can transform a symbol ref into a label ref, because
12398 the symbol ref is too far away and has to be dumped into a constant
12399 pool. */
12400 case CONST:
12401 case SYMBOL_REF:
12402 if (GET_MODE_CLASS (mode) != MODE_INT
12403 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12404 #ifdef POINTERS_EXTEND_UNSIGNED
12405 && (mode != Pmode || mem_mode == VOIDmode)
12406 #endif
12407 ))
12408 break;
12409 if (GET_CODE (rtl) == SYMBOL_REF
12410 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12411 {
12412 dw_loc_descr_ref temp;
12413
12414 /* If this is not defined, we have no way to emit the data. */
12415 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12416 break;
12417
12418 temp = new_addr_loc_descr (rtl, dtprel_true);
12419
12420 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12421 add_loc_descr (&mem_loc_result, temp);
12422
12423 break;
12424 }
12425
12426 if (!const_ok_for_output (rtl))
12427 break;
12428
12429 symref:
12430 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12431 vec_safe_push (used_rtx_array, rtl);
12432 break;
12433
12434 case CONCAT:
12435 case CONCATN:
12436 case VAR_LOCATION:
12437 case DEBUG_IMPLICIT_PTR:
12438 expansion_failed (NULL_TREE, rtl,
12439 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12440 return 0;
12441
12442 case ENTRY_VALUE:
12443 if (dwarf_strict)
12444 return NULL;
12445 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12446 {
12447 if (GET_MODE_CLASS (mode) != MODE_INT
12448 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12449 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12450 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12451 else
12452 {
12453 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12454 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12455 return NULL;
12456 op0 = one_reg_loc_descriptor (dbx_regnum,
12457 VAR_INIT_STATUS_INITIALIZED);
12458 }
12459 }
12460 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12461 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12462 {
12463 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12464 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12465 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12466 return NULL;
12467 }
12468 else
12469 gcc_unreachable ();
12470 if (op0 == NULL)
12471 return NULL;
12472 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12473 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12474 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12475 break;
12476
12477 case DEBUG_PARAMETER_REF:
12478 mem_loc_result = parameter_ref_descriptor (rtl);
12479 break;
12480
12481 case PRE_MODIFY:
12482 /* Extract the PLUS expression nested inside and fall into
12483 PLUS code below. */
12484 rtl = XEXP (rtl, 1);
12485 goto plus;
12486
12487 case PRE_INC:
12488 case PRE_DEC:
12489 /* Turn these into a PLUS expression and fall into the PLUS code
12490 below. */
12491 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12492 gen_int_mode (GET_CODE (rtl) == PRE_INC
12493 ? GET_MODE_UNIT_SIZE (mem_mode)
12494 : -GET_MODE_UNIT_SIZE (mem_mode),
12495 mode));
12496
12497 /* ... fall through ... */
12498
12499 case PLUS:
12500 plus:
12501 if (is_based_loc (rtl)
12502 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12503 || XEXP (rtl, 0) == arg_pointer_rtx
12504 || XEXP (rtl, 0) == frame_pointer_rtx)
12505 && GET_MODE_CLASS (mode) == MODE_INT)
12506 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12507 INTVAL (XEXP (rtl, 1)),
12508 VAR_INIT_STATUS_INITIALIZED);
12509 else
12510 {
12511 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12512 VAR_INIT_STATUS_INITIALIZED);
12513 if (mem_loc_result == 0)
12514 break;
12515
12516 if (CONST_INT_P (XEXP (rtl, 1))
12517 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12518 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12519 else
12520 {
12521 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12522 VAR_INIT_STATUS_INITIALIZED);
12523 if (op1 == 0)
12524 break;
12525 add_loc_descr (&mem_loc_result, op1);
12526 add_loc_descr (&mem_loc_result,
12527 new_loc_descr (DW_OP_plus, 0, 0));
12528 }
12529 }
12530 break;
12531
12532 /* If a pseudo-reg is optimized away, it is possible for it to
12533 be replaced with a MEM containing a multiply or shift. */
12534 case MINUS:
12535 op = DW_OP_minus;
12536 goto do_binop;
12537
12538 case MULT:
12539 op = DW_OP_mul;
12540 goto do_binop;
12541
12542 case DIV:
12543 if (!dwarf_strict
12544 && GET_MODE_CLASS (mode) == MODE_INT
12545 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12546 {
12547 mem_loc_result = typed_binop (DW_OP_div, rtl,
12548 base_type_for_mode (mode, 0),
12549 mode, mem_mode);
12550 break;
12551 }
12552 op = DW_OP_div;
12553 goto do_binop;
12554
12555 case UMOD:
12556 op = DW_OP_mod;
12557 goto do_binop;
12558
12559 case ASHIFT:
12560 op = DW_OP_shl;
12561 goto do_shift;
12562
12563 case ASHIFTRT:
12564 op = DW_OP_shra;
12565 goto do_shift;
12566
12567 case LSHIFTRT:
12568 op = DW_OP_shr;
12569 goto do_shift;
12570
12571 do_shift:
12572 if (GET_MODE_CLASS (mode) != MODE_INT)
12573 break;
12574 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12575 VAR_INIT_STATUS_INITIALIZED);
12576 {
12577 rtx rtlop1 = XEXP (rtl, 1);
12578 if (GET_MODE (rtlop1) != VOIDmode
12579 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12580 < GET_MODE_BITSIZE (mode))
12581 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12582 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12583 VAR_INIT_STATUS_INITIALIZED);
12584 }
12585
12586 if (op0 == 0 || op1 == 0)
12587 break;
12588
12589 mem_loc_result = op0;
12590 add_loc_descr (&mem_loc_result, op1);
12591 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12592 break;
12593
12594 case AND:
12595 op = DW_OP_and;
12596 goto do_binop;
12597
12598 case IOR:
12599 op = DW_OP_or;
12600 goto do_binop;
12601
12602 case XOR:
12603 op = DW_OP_xor;
12604 goto do_binop;
12605
12606 do_binop:
12607 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12608 VAR_INIT_STATUS_INITIALIZED);
12609 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12610 VAR_INIT_STATUS_INITIALIZED);
12611
12612 if (op0 == 0 || op1 == 0)
12613 break;
12614
12615 mem_loc_result = op0;
12616 add_loc_descr (&mem_loc_result, op1);
12617 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12618 break;
12619
12620 case MOD:
12621 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12622 {
12623 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12624 base_type_for_mode (mode, 0),
12625 mode, mem_mode);
12626 break;
12627 }
12628
12629 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12630 VAR_INIT_STATUS_INITIALIZED);
12631 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12632 VAR_INIT_STATUS_INITIALIZED);
12633
12634 if (op0 == 0 || op1 == 0)
12635 break;
12636
12637 mem_loc_result = op0;
12638 add_loc_descr (&mem_loc_result, op1);
12639 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12640 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12641 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12642 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12643 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12644 break;
12645
12646 case UDIV:
12647 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12648 {
12649 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12650 {
12651 op = DW_OP_div;
12652 goto do_binop;
12653 }
12654 mem_loc_result = typed_binop (DW_OP_div, rtl,
12655 base_type_for_mode (mode, 1),
12656 mode, mem_mode);
12657 }
12658 break;
12659
12660 case NOT:
12661 op = DW_OP_not;
12662 goto do_unop;
12663
12664 case ABS:
12665 op = DW_OP_abs;
12666 goto do_unop;
12667
12668 case NEG:
12669 op = DW_OP_neg;
12670 goto do_unop;
12671
12672 do_unop:
12673 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12674 VAR_INIT_STATUS_INITIALIZED);
12675
12676 if (op0 == 0)
12677 break;
12678
12679 mem_loc_result = op0;
12680 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12681 break;
12682
12683 case CONST_INT:
12684 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12685 #ifdef POINTERS_EXTEND_UNSIGNED
12686 || (mode == Pmode
12687 && mem_mode != VOIDmode
12688 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12689 #endif
12690 )
12691 {
12692 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12693 break;
12694 }
12695 if (!dwarf_strict
12696 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12697 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12698 {
12699 dw_die_ref type_die = base_type_for_mode (mode, 1);
12700 enum machine_mode amode;
12701 if (type_die == NULL)
12702 return NULL;
12703 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12704 MODE_INT, 0);
12705 if (INTVAL (rtl) >= 0
12706 && amode != BLKmode
12707 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12708 /* const DW_OP_GNU_convert <XXX> vs.
12709 DW_OP_GNU_const_type <XXX, 1, const>. */
12710 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12711 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12712 {
12713 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12714 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12715 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12716 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12717 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12718 add_loc_descr (&mem_loc_result, op0);
12719 return mem_loc_result;
12720 }
12721 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12722 INTVAL (rtl));
12723 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12724 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12725 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12726 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12727 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12728 else
12729 {
12730 mem_loc_result->dw_loc_oprnd2.val_class
12731 = dw_val_class_const_double;
12732 mem_loc_result->dw_loc_oprnd2.v.val_double
12733 = double_int::from_shwi (INTVAL (rtl));
12734 }
12735 }
12736 break;
12737
12738 case CONST_DOUBLE:
12739 if (!dwarf_strict)
12740 {
12741 dw_die_ref type_die;
12742
12743 /* Note that a CONST_DOUBLE rtx could represent either an integer
12744 or a floating-point constant. A CONST_DOUBLE is used whenever
12745 the constant requires more than one word in order to be
12746 adequately represented. We output CONST_DOUBLEs as blocks. */
12747 if (mode == VOIDmode
12748 || (GET_MODE (rtl) == VOIDmode
12749 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12750 break;
12751 type_die = base_type_for_mode (mode,
12752 GET_MODE_CLASS (mode) == MODE_INT);
12753 if (type_die == NULL)
12754 return NULL;
12755 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12756 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12757 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12758 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12759 if (SCALAR_FLOAT_MODE_P (mode))
12760 {
12761 unsigned int length = GET_MODE_SIZE (mode);
12762 unsigned char *array
12763 = (unsigned char*) ggc_alloc_atomic (length);
12764
12765 insert_float (rtl, array);
12766 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12767 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12768 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12769 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12770 }
12771 else
12772 {
12773 mem_loc_result->dw_loc_oprnd2.val_class
12774 = dw_val_class_const_double;
12775 mem_loc_result->dw_loc_oprnd2.v.val_double
12776 = rtx_to_double_int (rtl);
12777 }
12778 }
12779 break;
12780
12781 case EQ:
12782 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12783 break;
12784
12785 case GE:
12786 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12787 break;
12788
12789 case GT:
12790 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12791 break;
12792
12793 case LE:
12794 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12795 break;
12796
12797 case LT:
12798 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12799 break;
12800
12801 case NE:
12802 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12803 break;
12804
12805 case GEU:
12806 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12807 break;
12808
12809 case GTU:
12810 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12811 break;
12812
12813 case LEU:
12814 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12815 break;
12816
12817 case LTU:
12818 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12819 break;
12820
12821 case UMIN:
12822 case UMAX:
12823 if (GET_MODE_CLASS (mode) != MODE_INT)
12824 break;
12825 /* FALLTHRU */
12826 case SMIN:
12827 case SMAX:
12828 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12829 break;
12830
12831 case ZERO_EXTRACT:
12832 case SIGN_EXTRACT:
12833 if (CONST_INT_P (XEXP (rtl, 1))
12834 && CONST_INT_P (XEXP (rtl, 2))
12835 && ((unsigned) INTVAL (XEXP (rtl, 1))
12836 + (unsigned) INTVAL (XEXP (rtl, 2))
12837 <= GET_MODE_BITSIZE (mode))
12838 && GET_MODE_CLASS (mode) == MODE_INT
12839 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12840 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12841 {
12842 int shift, size;
12843 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12844 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12845 if (op0 == 0)
12846 break;
12847 if (GET_CODE (rtl) == SIGN_EXTRACT)
12848 op = DW_OP_shra;
12849 else
12850 op = DW_OP_shr;
12851 mem_loc_result = op0;
12852 size = INTVAL (XEXP (rtl, 1));
12853 shift = INTVAL (XEXP (rtl, 2));
12854 if (BITS_BIG_ENDIAN)
12855 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12856 - shift - size;
12857 if (shift + size != (int) DWARF2_ADDR_SIZE)
12858 {
12859 add_loc_descr (&mem_loc_result,
12860 int_loc_descriptor (DWARF2_ADDR_SIZE
12861 - shift - size));
12862 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12863 }
12864 if (size != (int) DWARF2_ADDR_SIZE)
12865 {
12866 add_loc_descr (&mem_loc_result,
12867 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12868 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12869 }
12870 }
12871 break;
12872
12873 case IF_THEN_ELSE:
12874 {
12875 dw_loc_descr_ref op2, bra_node, drop_node;
12876 op0 = mem_loc_descriptor (XEXP (rtl, 0),
12877 GET_MODE (XEXP (rtl, 0)) == VOIDmode
12878 ? word_mode : GET_MODE (XEXP (rtl, 0)),
12879 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12880 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12881 VAR_INIT_STATUS_INITIALIZED);
12882 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12883 VAR_INIT_STATUS_INITIALIZED);
12884 if (op0 == NULL || op1 == NULL || op2 == NULL)
12885 break;
12886
12887 mem_loc_result = op1;
12888 add_loc_descr (&mem_loc_result, op2);
12889 add_loc_descr (&mem_loc_result, op0);
12890 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12891 add_loc_descr (&mem_loc_result, bra_node);
12892 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12893 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12894 add_loc_descr (&mem_loc_result, drop_node);
12895 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12896 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12897 }
12898 break;
12899
12900 case FLOAT_EXTEND:
12901 case FLOAT_TRUNCATE:
12902 case FLOAT:
12903 case UNSIGNED_FLOAT:
12904 case FIX:
12905 case UNSIGNED_FIX:
12906 if (!dwarf_strict)
12907 {
12908 dw_die_ref type_die;
12909 dw_loc_descr_ref cvt;
12910
12911 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12912 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12913 if (op0 == NULL)
12914 break;
12915 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12916 && (GET_CODE (rtl) == FLOAT
12917 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12918 <= DWARF2_ADDR_SIZE))
12919 {
12920 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12921 GET_CODE (rtl) == UNSIGNED_FLOAT);
12922 if (type_die == NULL)
12923 break;
12924 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12925 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12926 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12927 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12928 add_loc_descr (&op0, cvt);
12929 }
12930 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12931 if (type_die == NULL)
12932 break;
12933 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12934 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12935 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12936 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12937 add_loc_descr (&op0, cvt);
12938 if (GET_MODE_CLASS (mode) == MODE_INT
12939 && (GET_CODE (rtl) == FIX
12940 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12941 {
12942 op0 = convert_descriptor_to_mode (mode, op0);
12943 if (op0 == NULL)
12944 break;
12945 }
12946 mem_loc_result = op0;
12947 }
12948 break;
12949
12950 case CLZ:
12951 case CTZ:
12952 case FFS:
12953 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12954 break;
12955
12956 case POPCOUNT:
12957 case PARITY:
12958 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12959 break;
12960
12961 case BSWAP:
12962 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12963 break;
12964
12965 case ROTATE:
12966 case ROTATERT:
12967 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12968 break;
12969
12970 case COMPARE:
12971 /* In theory, we could implement the above. */
12972 /* DWARF cannot represent the unsigned compare operations
12973 natively. */
12974 case SS_MULT:
12975 case US_MULT:
12976 case SS_DIV:
12977 case US_DIV:
12978 case SS_PLUS:
12979 case US_PLUS:
12980 case SS_MINUS:
12981 case US_MINUS:
12982 case SS_NEG:
12983 case US_NEG:
12984 case SS_ABS:
12985 case SS_ASHIFT:
12986 case US_ASHIFT:
12987 case SS_TRUNCATE:
12988 case US_TRUNCATE:
12989 case UNORDERED:
12990 case ORDERED:
12991 case UNEQ:
12992 case UNGE:
12993 case UNGT:
12994 case UNLE:
12995 case UNLT:
12996 case LTGT:
12997 case FRACT_CONVERT:
12998 case UNSIGNED_FRACT_CONVERT:
12999 case SAT_FRACT:
13000 case UNSIGNED_SAT_FRACT:
13001 case SQRT:
13002 case ASM_OPERANDS:
13003 case VEC_MERGE:
13004 case VEC_SELECT:
13005 case VEC_CONCAT:
13006 case VEC_DUPLICATE:
13007 case UNSPEC:
13008 case HIGH:
13009 case FMA:
13010 case STRICT_LOW_PART:
13011 case CONST_VECTOR:
13012 case CONST_FIXED:
13013 case CLRSB:
13014 case CLOBBER:
13015 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13016 can't express it in the debug info. This can happen e.g. with some
13017 TLS UNSPECs. */
13018 break;
13019
13020 case CONST_STRING:
13021 resolve_one_addr (&rtl, NULL);
13022 goto symref;
13023
13024 default:
13025 #ifdef ENABLE_CHECKING
13026 print_rtl (stderr, rtl);
13027 gcc_unreachable ();
13028 #else
13029 break;
13030 #endif
13031 }
13032
13033 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13034 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13035
13036 return mem_loc_result;
13037 }
13038
13039 /* Return a descriptor that describes the concatenation of two locations.
13040 This is typically a complex variable. */
13041
13042 static dw_loc_descr_ref
13043 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13044 {
13045 dw_loc_descr_ref cc_loc_result = NULL;
13046 dw_loc_descr_ref x0_ref
13047 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13048 dw_loc_descr_ref x1_ref
13049 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13050
13051 if (x0_ref == 0 || x1_ref == 0)
13052 return 0;
13053
13054 cc_loc_result = x0_ref;
13055 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13056
13057 add_loc_descr (&cc_loc_result, x1_ref);
13058 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13059
13060 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13061 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13062
13063 return cc_loc_result;
13064 }
13065
13066 /* Return a descriptor that describes the concatenation of N
13067 locations. */
13068
13069 static dw_loc_descr_ref
13070 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13071 {
13072 unsigned int i;
13073 dw_loc_descr_ref cc_loc_result = NULL;
13074 unsigned int n = XVECLEN (concatn, 0);
13075
13076 for (i = 0; i < n; ++i)
13077 {
13078 dw_loc_descr_ref ref;
13079 rtx x = XVECEXP (concatn, 0, i);
13080
13081 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13082 if (ref == NULL)
13083 return NULL;
13084
13085 add_loc_descr (&cc_loc_result, ref);
13086 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13087 }
13088
13089 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13090 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13091
13092 return cc_loc_result;
13093 }
13094
13095 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13096 for DEBUG_IMPLICIT_PTR RTL. */
13097
13098 static dw_loc_descr_ref
13099 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13100 {
13101 dw_loc_descr_ref ret;
13102 dw_die_ref ref;
13103
13104 if (dwarf_strict)
13105 return NULL;
13106 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13107 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13108 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13109 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13110 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13111 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13112 if (ref)
13113 {
13114 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13115 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13116 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13117 }
13118 else
13119 {
13120 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13121 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13122 }
13123 return ret;
13124 }
13125
13126 /* Output a proper Dwarf location descriptor for a variable or parameter
13127 which is either allocated in a register or in a memory location. For a
13128 register, we just generate an OP_REG and the register number. For a
13129 memory location we provide a Dwarf postfix expression describing how to
13130 generate the (dynamic) address of the object onto the address stack.
13131
13132 MODE is mode of the decl if this loc_descriptor is going to be used in
13133 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13134 allowed, VOIDmode otherwise.
13135
13136 If we don't know how to describe it, return 0. */
13137
13138 static dw_loc_descr_ref
13139 loc_descriptor (rtx rtl, enum machine_mode mode,
13140 enum var_init_status initialized)
13141 {
13142 dw_loc_descr_ref loc_result = NULL;
13143
13144 switch (GET_CODE (rtl))
13145 {
13146 case SUBREG:
13147 /* The case of a subreg may arise when we have a local (register)
13148 variable or a formal (register) parameter which doesn't quite fill
13149 up an entire register. For now, just assume that it is
13150 legitimate to make the Dwarf info refer to the whole register which
13151 contains the given subreg. */
13152 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13153 loc_result = loc_descriptor (SUBREG_REG (rtl),
13154 GET_MODE (SUBREG_REG (rtl)), initialized);
13155 else
13156 goto do_default;
13157 break;
13158
13159 case REG:
13160 loc_result = reg_loc_descriptor (rtl, initialized);
13161 break;
13162
13163 case MEM:
13164 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13165 GET_MODE (rtl), initialized);
13166 if (loc_result == NULL)
13167 loc_result = tls_mem_loc_descriptor (rtl);
13168 if (loc_result == NULL)
13169 {
13170 rtx new_rtl = avoid_constant_pool_reference (rtl);
13171 if (new_rtl != rtl)
13172 loc_result = loc_descriptor (new_rtl, mode, initialized);
13173 }
13174 break;
13175
13176 case CONCAT:
13177 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13178 initialized);
13179 break;
13180
13181 case CONCATN:
13182 loc_result = concatn_loc_descriptor (rtl, initialized);
13183 break;
13184
13185 case VAR_LOCATION:
13186 /* Single part. */
13187 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13188 {
13189 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13190 if (GET_CODE (loc) == EXPR_LIST)
13191 loc = XEXP (loc, 0);
13192 loc_result = loc_descriptor (loc, mode, initialized);
13193 break;
13194 }
13195
13196 rtl = XEXP (rtl, 1);
13197 /* FALLTHRU */
13198
13199 case PARALLEL:
13200 {
13201 rtvec par_elems = XVEC (rtl, 0);
13202 int num_elem = GET_NUM_ELEM (par_elems);
13203 enum machine_mode mode;
13204 int i;
13205
13206 /* Create the first one, so we have something to add to. */
13207 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13208 VOIDmode, initialized);
13209 if (loc_result == NULL)
13210 return NULL;
13211 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13212 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13213 for (i = 1; i < num_elem; i++)
13214 {
13215 dw_loc_descr_ref temp;
13216
13217 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13218 VOIDmode, initialized);
13219 if (temp == NULL)
13220 return NULL;
13221 add_loc_descr (&loc_result, temp);
13222 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13223 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13224 }
13225 }
13226 break;
13227
13228 case CONST_INT:
13229 if (mode != VOIDmode && mode != BLKmode)
13230 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13231 INTVAL (rtl));
13232 break;
13233
13234 case CONST_DOUBLE:
13235 if (mode == VOIDmode)
13236 mode = GET_MODE (rtl);
13237
13238 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13239 {
13240 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13241
13242 /* Note that a CONST_DOUBLE rtx could represent either an integer
13243 or a floating-point constant. A CONST_DOUBLE is used whenever
13244 the constant requires more than one word in order to be
13245 adequately represented. We output CONST_DOUBLEs as blocks. */
13246 loc_result = new_loc_descr (DW_OP_implicit_value,
13247 GET_MODE_SIZE (mode), 0);
13248 if (SCALAR_FLOAT_MODE_P (mode))
13249 {
13250 unsigned int length = GET_MODE_SIZE (mode);
13251 unsigned char *array
13252 = (unsigned char*) ggc_alloc_atomic (length);
13253
13254 insert_float (rtl, array);
13255 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13256 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13257 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13258 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13259 }
13260 else
13261 {
13262 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13263 loc_result->dw_loc_oprnd2.v.val_double
13264 = rtx_to_double_int (rtl);
13265 }
13266 }
13267 break;
13268
13269 case CONST_VECTOR:
13270 if (mode == VOIDmode)
13271 mode = GET_MODE (rtl);
13272
13273 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13274 {
13275 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13276 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13277 unsigned char *array = (unsigned char *)
13278 ggc_alloc_atomic (length * elt_size);
13279 unsigned int i;
13280 unsigned char *p;
13281
13282 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13283 switch (GET_MODE_CLASS (mode))
13284 {
13285 case MODE_VECTOR_INT:
13286 for (i = 0, p = array; i < length; i++, p += elt_size)
13287 {
13288 rtx elt = CONST_VECTOR_ELT (rtl, i);
13289 double_int val = rtx_to_double_int (elt);
13290
13291 if (elt_size <= sizeof (HOST_WIDE_INT))
13292 insert_int (val.to_shwi (), elt_size, p);
13293 else
13294 {
13295 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13296 insert_double (val, p);
13297 }
13298 }
13299 break;
13300
13301 case MODE_VECTOR_FLOAT:
13302 for (i = 0, p = array; i < length; i++, p += elt_size)
13303 {
13304 rtx elt = CONST_VECTOR_ELT (rtl, i);
13305 insert_float (elt, p);
13306 }
13307 break;
13308
13309 default:
13310 gcc_unreachable ();
13311 }
13312
13313 loc_result = new_loc_descr (DW_OP_implicit_value,
13314 length * elt_size, 0);
13315 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13316 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13317 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13318 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13319 }
13320 break;
13321
13322 case CONST:
13323 if (mode == VOIDmode
13324 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13325 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13326 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13327 {
13328 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13329 break;
13330 }
13331 /* FALLTHROUGH */
13332 case SYMBOL_REF:
13333 if (!const_ok_for_output (rtl))
13334 break;
13335 case LABEL_REF:
13336 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13337 && (dwarf_version >= 4 || !dwarf_strict))
13338 {
13339 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13340 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13341 vec_safe_push (used_rtx_array, rtl);
13342 }
13343 break;
13344
13345 case DEBUG_IMPLICIT_PTR:
13346 loc_result = implicit_ptr_descriptor (rtl, 0);
13347 break;
13348
13349 case PLUS:
13350 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13351 && CONST_INT_P (XEXP (rtl, 1)))
13352 {
13353 loc_result
13354 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13355 break;
13356 }
13357 /* FALLTHRU */
13358 do_default:
13359 default:
13360 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13361 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13362 && dwarf_version >= 4)
13363 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13364 {
13365 /* Value expression. */
13366 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13367 if (loc_result)
13368 add_loc_descr (&loc_result,
13369 new_loc_descr (DW_OP_stack_value, 0, 0));
13370 }
13371 break;
13372 }
13373
13374 return loc_result;
13375 }
13376
13377 /* We need to figure out what section we should use as the base for the
13378 address ranges where a given location is valid.
13379 1. If this particular DECL has a section associated with it, use that.
13380 2. If this function has a section associated with it, use that.
13381 3. Otherwise, use the text section.
13382 XXX: If you split a variable across multiple sections, we won't notice. */
13383
13384 static const char *
13385 secname_for_decl (const_tree decl)
13386 {
13387 const char *secname;
13388
13389 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13390 {
13391 tree sectree = DECL_SECTION_NAME (decl);
13392 secname = TREE_STRING_POINTER (sectree);
13393 }
13394 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13395 {
13396 tree sectree = DECL_SECTION_NAME (current_function_decl);
13397 secname = TREE_STRING_POINTER (sectree);
13398 }
13399 else if (cfun && in_cold_section_p)
13400 secname = crtl->subsections.cold_section_label;
13401 else
13402 secname = text_section_label;
13403
13404 return secname;
13405 }
13406
13407 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13408
13409 static bool
13410 decl_by_reference_p (tree decl)
13411 {
13412 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13413 || TREE_CODE (decl) == VAR_DECL)
13414 && DECL_BY_REFERENCE (decl));
13415 }
13416
13417 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13418 for VARLOC. */
13419
13420 static dw_loc_descr_ref
13421 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13422 enum var_init_status initialized)
13423 {
13424 int have_address = 0;
13425 dw_loc_descr_ref descr;
13426 enum machine_mode mode;
13427
13428 if (want_address != 2)
13429 {
13430 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13431 /* Single part. */
13432 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13433 {
13434 varloc = PAT_VAR_LOCATION_LOC (varloc);
13435 if (GET_CODE (varloc) == EXPR_LIST)
13436 varloc = XEXP (varloc, 0);
13437 mode = GET_MODE (varloc);
13438 if (MEM_P (varloc))
13439 {
13440 rtx addr = XEXP (varloc, 0);
13441 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13442 mode, initialized);
13443 if (descr)
13444 have_address = 1;
13445 else
13446 {
13447 rtx x = avoid_constant_pool_reference (varloc);
13448 if (x != varloc)
13449 descr = mem_loc_descriptor (x, mode, VOIDmode,
13450 initialized);
13451 }
13452 }
13453 else
13454 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13455 }
13456 else
13457 return 0;
13458 }
13459 else
13460 {
13461 if (GET_CODE (varloc) == VAR_LOCATION)
13462 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13463 else
13464 mode = DECL_MODE (loc);
13465 descr = loc_descriptor (varloc, mode, initialized);
13466 have_address = 1;
13467 }
13468
13469 if (!descr)
13470 return 0;
13471
13472 if (want_address == 2 && !have_address
13473 && (dwarf_version >= 4 || !dwarf_strict))
13474 {
13475 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13476 {
13477 expansion_failed (loc, NULL_RTX,
13478 "DWARF address size mismatch");
13479 return 0;
13480 }
13481 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13482 have_address = 1;
13483 }
13484 /* Show if we can't fill the request for an address. */
13485 if (want_address && !have_address)
13486 {
13487 expansion_failed (loc, NULL_RTX,
13488 "Want address and only have value");
13489 return 0;
13490 }
13491
13492 /* If we've got an address and don't want one, dereference. */
13493 if (!want_address && have_address)
13494 {
13495 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13496 enum dwarf_location_atom op;
13497
13498 if (size > DWARF2_ADDR_SIZE || size == -1)
13499 {
13500 expansion_failed (loc, NULL_RTX,
13501 "DWARF address size mismatch");
13502 return 0;
13503 }
13504 else if (size == DWARF2_ADDR_SIZE)
13505 op = DW_OP_deref;
13506 else
13507 op = DW_OP_deref_size;
13508
13509 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13510 }
13511
13512 return descr;
13513 }
13514
13515 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13516 if it is not possible. */
13517
13518 static dw_loc_descr_ref
13519 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13520 {
13521 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13522 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13523 else if (dwarf_version >= 3 || !dwarf_strict)
13524 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13525 else
13526 return NULL;
13527 }
13528
13529 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13530 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13531
13532 static dw_loc_descr_ref
13533 dw_sra_loc_expr (tree decl, rtx loc)
13534 {
13535 rtx p;
13536 unsigned int padsize = 0;
13537 dw_loc_descr_ref descr, *descr_tail;
13538 unsigned HOST_WIDE_INT decl_size;
13539 rtx varloc;
13540 enum var_init_status initialized;
13541
13542 if (DECL_SIZE (decl) == NULL
13543 || !host_integerp (DECL_SIZE (decl), 1))
13544 return NULL;
13545
13546 decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13547 descr = NULL;
13548 descr_tail = &descr;
13549
13550 for (p = loc; p; p = XEXP (p, 1))
13551 {
13552 unsigned int bitsize = decl_piece_bitsize (p);
13553 rtx loc_note = *decl_piece_varloc_ptr (p);
13554 dw_loc_descr_ref cur_descr;
13555 dw_loc_descr_ref *tail, last = NULL;
13556 unsigned int opsize = 0;
13557
13558 if (loc_note == NULL_RTX
13559 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13560 {
13561 padsize += bitsize;
13562 continue;
13563 }
13564 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13565 varloc = NOTE_VAR_LOCATION (loc_note);
13566 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13567 if (cur_descr == NULL)
13568 {
13569 padsize += bitsize;
13570 continue;
13571 }
13572
13573 /* Check that cur_descr either doesn't use
13574 DW_OP_*piece operations, or their sum is equal
13575 to bitsize. Otherwise we can't embed it. */
13576 for (tail = &cur_descr; *tail != NULL;
13577 tail = &(*tail)->dw_loc_next)
13578 if ((*tail)->dw_loc_opc == DW_OP_piece)
13579 {
13580 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13581 * BITS_PER_UNIT;
13582 last = *tail;
13583 }
13584 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13585 {
13586 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13587 last = *tail;
13588 }
13589
13590 if (last != NULL && opsize != bitsize)
13591 {
13592 padsize += bitsize;
13593 /* Discard the current piece of the descriptor and release any
13594 addr_table entries it uses. */
13595 remove_loc_list_addr_table_entries (cur_descr);
13596 continue;
13597 }
13598
13599 /* If there is a hole, add DW_OP_*piece after empty DWARF
13600 expression, which means that those bits are optimized out. */
13601 if (padsize)
13602 {
13603 if (padsize > decl_size)
13604 {
13605 remove_loc_list_addr_table_entries (cur_descr);
13606 goto discard_descr;
13607 }
13608 decl_size -= padsize;
13609 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13610 if (*descr_tail == NULL)
13611 {
13612 remove_loc_list_addr_table_entries (cur_descr);
13613 goto discard_descr;
13614 }
13615 descr_tail = &(*descr_tail)->dw_loc_next;
13616 padsize = 0;
13617 }
13618 *descr_tail = cur_descr;
13619 descr_tail = tail;
13620 if (bitsize > decl_size)
13621 goto discard_descr;
13622 decl_size -= bitsize;
13623 if (last == NULL)
13624 {
13625 HOST_WIDE_INT offset = 0;
13626 if (GET_CODE (varloc) == VAR_LOCATION
13627 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13628 {
13629 varloc = PAT_VAR_LOCATION_LOC (varloc);
13630 if (GET_CODE (varloc) == EXPR_LIST)
13631 varloc = XEXP (varloc, 0);
13632 }
13633 do
13634 {
13635 if (GET_CODE (varloc) == CONST
13636 || GET_CODE (varloc) == SIGN_EXTEND
13637 || GET_CODE (varloc) == ZERO_EXTEND)
13638 varloc = XEXP (varloc, 0);
13639 else if (GET_CODE (varloc) == SUBREG)
13640 varloc = SUBREG_REG (varloc);
13641 else
13642 break;
13643 }
13644 while (1);
13645 /* DW_OP_bit_size offset should be zero for register
13646 or implicit location descriptions and empty location
13647 descriptions, but for memory addresses needs big endian
13648 adjustment. */
13649 if (MEM_P (varloc))
13650 {
13651 unsigned HOST_WIDE_INT memsize
13652 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13653 if (memsize != bitsize)
13654 {
13655 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13656 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13657 goto discard_descr;
13658 if (memsize < bitsize)
13659 goto discard_descr;
13660 if (BITS_BIG_ENDIAN)
13661 offset = memsize - bitsize;
13662 }
13663 }
13664
13665 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13666 if (*descr_tail == NULL)
13667 goto discard_descr;
13668 descr_tail = &(*descr_tail)->dw_loc_next;
13669 }
13670 }
13671
13672 /* If there were any non-empty expressions, add padding till the end of
13673 the decl. */
13674 if (descr != NULL && decl_size != 0)
13675 {
13676 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13677 if (*descr_tail == NULL)
13678 goto discard_descr;
13679 }
13680 return descr;
13681
13682 discard_descr:
13683 /* Discard the descriptor and release any addr_table entries it uses. */
13684 remove_loc_list_addr_table_entries (descr);
13685 return NULL;
13686 }
13687
13688 /* Return the dwarf representation of the location list LOC_LIST of
13689 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13690 function. */
13691
13692 static dw_loc_list_ref
13693 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13694 {
13695 const char *endname, *secname;
13696 rtx varloc;
13697 enum var_init_status initialized;
13698 struct var_loc_node *node;
13699 dw_loc_descr_ref descr;
13700 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13701 dw_loc_list_ref list = NULL;
13702 dw_loc_list_ref *listp = &list;
13703
13704 /* Now that we know what section we are using for a base,
13705 actually construct the list of locations.
13706 The first location information is what is passed to the
13707 function that creates the location list, and the remaining
13708 locations just get added on to that list.
13709 Note that we only know the start address for a location
13710 (IE location changes), so to build the range, we use
13711 the range [current location start, next location start].
13712 This means we have to special case the last node, and generate
13713 a range of [last location start, end of function label]. */
13714
13715 secname = secname_for_decl (decl);
13716
13717 for (node = loc_list->first; node; node = node->next)
13718 if (GET_CODE (node->loc) == EXPR_LIST
13719 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13720 {
13721 if (GET_CODE (node->loc) == EXPR_LIST)
13722 {
13723 /* This requires DW_OP_{,bit_}piece, which is not usable
13724 inside DWARF expressions. */
13725 if (want_address != 2)
13726 continue;
13727 descr = dw_sra_loc_expr (decl, node->loc);
13728 if (descr == NULL)
13729 continue;
13730 }
13731 else
13732 {
13733 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13734 varloc = NOTE_VAR_LOCATION (node->loc);
13735 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13736 }
13737 if (descr)
13738 {
13739 bool range_across_switch = false;
13740 /* If section switch happens in between node->label
13741 and node->next->label (or end of function) and
13742 we can't emit it as a single entry list,
13743 emit two ranges, first one ending at the end
13744 of first partition and second one starting at the
13745 beginning of second partition. */
13746 if (node == loc_list->last_before_switch
13747 && (node != loc_list->first || loc_list->first->next)
13748 && current_function_decl)
13749 {
13750 endname = cfun->fde->dw_fde_end;
13751 range_across_switch = true;
13752 }
13753 /* The variable has a location between NODE->LABEL and
13754 NODE->NEXT->LABEL. */
13755 else if (node->next)
13756 endname = node->next->label;
13757 /* If the variable has a location at the last label
13758 it keeps its location until the end of function. */
13759 else if (!current_function_decl)
13760 endname = text_end_label;
13761 else
13762 {
13763 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13764 current_function_funcdef_no);
13765 endname = ggc_strdup (label_id);
13766 }
13767
13768 *listp = new_loc_list (descr, node->label, endname, secname);
13769 if (TREE_CODE (decl) == PARM_DECL
13770 && node == loc_list->first
13771 && NOTE_P (node->loc)
13772 && strcmp (node->label, endname) == 0)
13773 (*listp)->force = true;
13774 listp = &(*listp)->dw_loc_next;
13775
13776 if (range_across_switch)
13777 {
13778 if (GET_CODE (node->loc) == EXPR_LIST)
13779 descr = dw_sra_loc_expr (decl, node->loc);
13780 else
13781 {
13782 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13783 varloc = NOTE_VAR_LOCATION (node->loc);
13784 descr = dw_loc_list_1 (decl, varloc, want_address,
13785 initialized);
13786 }
13787 gcc_assert (descr);
13788 /* The variable has a location between NODE->LABEL and
13789 NODE->NEXT->LABEL. */
13790 if (node->next)
13791 endname = node->next->label;
13792 else
13793 endname = cfun->fde->dw_fde_second_end;
13794 *listp = new_loc_list (descr,
13795 cfun->fde->dw_fde_second_begin,
13796 endname, secname);
13797 listp = &(*listp)->dw_loc_next;
13798 }
13799 }
13800 }
13801
13802 /* Try to avoid the overhead of a location list emitting a location
13803 expression instead, but only if we didn't have more than one
13804 location entry in the first place. If some entries were not
13805 representable, we don't want to pretend a single entry that was
13806 applies to the entire scope in which the variable is
13807 available. */
13808 if (list && loc_list->first->next)
13809 gen_llsym (list);
13810
13811 return list;
13812 }
13813
13814 /* Return if the loc_list has only single element and thus can be represented
13815 as location description. */
13816
13817 static bool
13818 single_element_loc_list_p (dw_loc_list_ref list)
13819 {
13820 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13821 return !list->ll_symbol;
13822 }
13823
13824 /* To each location in list LIST add loc descr REF. */
13825
13826 static void
13827 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13828 {
13829 dw_loc_descr_ref copy;
13830 add_loc_descr (&list->expr, ref);
13831 list = list->dw_loc_next;
13832 while (list)
13833 {
13834 copy = ggc_alloc_dw_loc_descr_node ();
13835 memcpy (copy, ref, sizeof (dw_loc_descr_node));
13836 add_loc_descr (&list->expr, copy);
13837 while (copy->dw_loc_next)
13838 {
13839 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13840 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13841 copy->dw_loc_next = new_copy;
13842 copy = new_copy;
13843 }
13844 list = list->dw_loc_next;
13845 }
13846 }
13847
13848 /* Given two lists RET and LIST
13849 produce location list that is result of adding expression in LIST
13850 to expression in RET on each position in program.
13851 Might be destructive on both RET and LIST.
13852
13853 TODO: We handle only simple cases of RET or LIST having at most one
13854 element. General case would inolve sorting the lists in program order
13855 and merging them that will need some additional work.
13856 Adding that will improve quality of debug info especially for SRA-ed
13857 structures. */
13858
13859 static void
13860 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13861 {
13862 if (!list)
13863 return;
13864 if (!*ret)
13865 {
13866 *ret = list;
13867 return;
13868 }
13869 if (!list->dw_loc_next)
13870 {
13871 add_loc_descr_to_each (*ret, list->expr);
13872 return;
13873 }
13874 if (!(*ret)->dw_loc_next)
13875 {
13876 add_loc_descr_to_each (list, (*ret)->expr);
13877 *ret = list;
13878 return;
13879 }
13880 expansion_failed (NULL_TREE, NULL_RTX,
13881 "Don't know how to merge two non-trivial"
13882 " location lists.\n");
13883 *ret = NULL;
13884 return;
13885 }
13886
13887 /* LOC is constant expression. Try a luck, look it up in constant
13888 pool and return its loc_descr of its address. */
13889
13890 static dw_loc_descr_ref
13891 cst_pool_loc_descr (tree loc)
13892 {
13893 /* Get an RTL for this, if something has been emitted. */
13894 rtx rtl = lookup_constant_def (loc);
13895
13896 if (!rtl || !MEM_P (rtl))
13897 {
13898 gcc_assert (!rtl);
13899 return 0;
13900 }
13901 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13902
13903 /* TODO: We might get more coverage if we was actually delaying expansion
13904 of all expressions till end of compilation when constant pools are fully
13905 populated. */
13906 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13907 {
13908 expansion_failed (loc, NULL_RTX,
13909 "CST value in contant pool but not marked.");
13910 return 0;
13911 }
13912 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13913 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13914 }
13915
13916 /* Return dw_loc_list representing address of addr_expr LOC
13917 by looking for inner INDIRECT_REF expression and turning
13918 it into simple arithmetics. */
13919
13920 static dw_loc_list_ref
13921 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13922 {
13923 tree obj, offset;
13924 HOST_WIDE_INT bitsize, bitpos, bytepos;
13925 enum machine_mode mode;
13926 int unsignedp, volatilep = 0;
13927 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13928
13929 obj = get_inner_reference (TREE_OPERAND (loc, 0),
13930 &bitsize, &bitpos, &offset, &mode,
13931 &unsignedp, &volatilep, false);
13932 STRIP_NOPS (obj);
13933 if (bitpos % BITS_PER_UNIT)
13934 {
13935 expansion_failed (loc, NULL_RTX, "bitfield access");
13936 return 0;
13937 }
13938 if (!INDIRECT_REF_P (obj))
13939 {
13940 expansion_failed (obj,
13941 NULL_RTX, "no indirect ref in inner refrence");
13942 return 0;
13943 }
13944 if (!offset && !bitpos)
13945 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13946 else if (toplev
13947 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13948 && (dwarf_version >= 4 || !dwarf_strict))
13949 {
13950 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13951 if (!list_ret)
13952 return 0;
13953 if (offset)
13954 {
13955 /* Variable offset. */
13956 list_ret1 = loc_list_from_tree (offset, 0);
13957 if (list_ret1 == 0)
13958 return 0;
13959 add_loc_list (&list_ret, list_ret1);
13960 if (!list_ret)
13961 return 0;
13962 add_loc_descr_to_each (list_ret,
13963 new_loc_descr (DW_OP_plus, 0, 0));
13964 }
13965 bytepos = bitpos / BITS_PER_UNIT;
13966 if (bytepos > 0)
13967 add_loc_descr_to_each (list_ret,
13968 new_loc_descr (DW_OP_plus_uconst,
13969 bytepos, 0));
13970 else if (bytepos < 0)
13971 loc_list_plus_const (list_ret, bytepos);
13972 add_loc_descr_to_each (list_ret,
13973 new_loc_descr (DW_OP_stack_value, 0, 0));
13974 }
13975 return list_ret;
13976 }
13977
13978
13979 /* Generate Dwarf location list representing LOC.
13980 If WANT_ADDRESS is false, expression computing LOC will be computed
13981 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13982 if WANT_ADDRESS is 2, expression computing address useable in location
13983 will be returned (i.e. DW_OP_reg can be used
13984 to refer to register values). */
13985
13986 static dw_loc_list_ref
13987 loc_list_from_tree (tree loc, int want_address)
13988 {
13989 dw_loc_descr_ref ret = NULL, ret1 = NULL;
13990 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13991 int have_address = 0;
13992 enum dwarf_location_atom op;
13993
13994 /* ??? Most of the time we do not take proper care for sign/zero
13995 extending the values properly. Hopefully this won't be a real
13996 problem... */
13997
13998 switch (TREE_CODE (loc))
13999 {
14000 case ERROR_MARK:
14001 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14002 return 0;
14003
14004 case PLACEHOLDER_EXPR:
14005 /* This case involves extracting fields from an object to determine the
14006 position of other fields. We don't try to encode this here. The
14007 only user of this is Ada, which encodes the needed information using
14008 the names of types. */
14009 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14010 return 0;
14011
14012 case CALL_EXPR:
14013 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14014 /* There are no opcodes for these operations. */
14015 return 0;
14016
14017 case PREINCREMENT_EXPR:
14018 case PREDECREMENT_EXPR:
14019 case POSTINCREMENT_EXPR:
14020 case POSTDECREMENT_EXPR:
14021 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14022 /* There are no opcodes for these operations. */
14023 return 0;
14024
14025 case ADDR_EXPR:
14026 /* If we already want an address, see if there is INDIRECT_REF inside
14027 e.g. for &this->field. */
14028 if (want_address)
14029 {
14030 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14031 (loc, want_address == 2);
14032 if (list_ret)
14033 have_address = 1;
14034 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14035 && (ret = cst_pool_loc_descr (loc)))
14036 have_address = 1;
14037 }
14038 /* Otherwise, process the argument and look for the address. */
14039 if (!list_ret && !ret)
14040 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14041 else
14042 {
14043 if (want_address)
14044 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14045 return NULL;
14046 }
14047 break;
14048
14049 case VAR_DECL:
14050 if (DECL_THREAD_LOCAL_P (loc))
14051 {
14052 rtx rtl;
14053 enum dwarf_location_atom tls_op;
14054 enum dtprel_bool dtprel = dtprel_false;
14055
14056 if (targetm.have_tls)
14057 {
14058 /* If this is not defined, we have no way to emit the
14059 data. */
14060 if (!targetm.asm_out.output_dwarf_dtprel)
14061 return 0;
14062
14063 /* The way DW_OP_GNU_push_tls_address is specified, we
14064 can only look up addresses of objects in the current
14065 module. We used DW_OP_addr as first op, but that's
14066 wrong, because DW_OP_addr is relocated by the debug
14067 info consumer, while DW_OP_GNU_push_tls_address
14068 operand shouldn't be. */
14069 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14070 return 0;
14071 dtprel = dtprel_true;
14072 tls_op = DW_OP_GNU_push_tls_address;
14073 }
14074 else
14075 {
14076 if (!targetm.emutls.debug_form_tls_address
14077 || !(dwarf_version >= 3 || !dwarf_strict))
14078 return 0;
14079 /* We stuffed the control variable into the DECL_VALUE_EXPR
14080 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14081 no longer appear in gimple code. We used the control
14082 variable in specific so that we could pick it up here. */
14083 loc = DECL_VALUE_EXPR (loc);
14084 tls_op = DW_OP_form_tls_address;
14085 }
14086
14087 rtl = rtl_for_decl_location (loc);
14088 if (rtl == NULL_RTX)
14089 return 0;
14090
14091 if (!MEM_P (rtl))
14092 return 0;
14093 rtl = XEXP (rtl, 0);
14094 if (! CONSTANT_P (rtl))
14095 return 0;
14096
14097 ret = new_addr_loc_descr (rtl, dtprel);
14098 ret1 = new_loc_descr (tls_op, 0, 0);
14099 add_loc_descr (&ret, ret1);
14100
14101 have_address = 1;
14102 break;
14103 }
14104 /* FALLTHRU */
14105
14106 case PARM_DECL:
14107 case RESULT_DECL:
14108 if (DECL_HAS_VALUE_EXPR_P (loc))
14109 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14110 want_address);
14111 /* FALLTHRU */
14112
14113 case FUNCTION_DECL:
14114 {
14115 rtx rtl;
14116 var_loc_list *loc_list = lookup_decl_loc (loc);
14117
14118 if (loc_list && loc_list->first)
14119 {
14120 list_ret = dw_loc_list (loc_list, loc, want_address);
14121 have_address = want_address != 0;
14122 break;
14123 }
14124 rtl = rtl_for_decl_location (loc);
14125 if (rtl == NULL_RTX)
14126 {
14127 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14128 return 0;
14129 }
14130 else if (CONST_INT_P (rtl))
14131 {
14132 HOST_WIDE_INT val = INTVAL (rtl);
14133 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14134 val &= GET_MODE_MASK (DECL_MODE (loc));
14135 ret = int_loc_descriptor (val);
14136 }
14137 else if (GET_CODE (rtl) == CONST_STRING)
14138 {
14139 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14140 return 0;
14141 }
14142 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14143 ret = new_addr_loc_descr (rtl, dtprel_false);
14144 else
14145 {
14146 enum machine_mode mode, mem_mode;
14147
14148 /* Certain constructs can only be represented at top-level. */
14149 if (want_address == 2)
14150 {
14151 ret = loc_descriptor (rtl, VOIDmode,
14152 VAR_INIT_STATUS_INITIALIZED);
14153 have_address = 1;
14154 }
14155 else
14156 {
14157 mode = GET_MODE (rtl);
14158 mem_mode = VOIDmode;
14159 if (MEM_P (rtl))
14160 {
14161 mem_mode = mode;
14162 mode = get_address_mode (rtl);
14163 rtl = XEXP (rtl, 0);
14164 have_address = 1;
14165 }
14166 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14167 VAR_INIT_STATUS_INITIALIZED);
14168 }
14169 if (!ret)
14170 expansion_failed (loc, rtl,
14171 "failed to produce loc descriptor for rtl");
14172 }
14173 }
14174 break;
14175
14176 case MEM_REF:
14177 /* ??? FIXME. */
14178 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14179 return 0;
14180 /* Fallthru. */
14181 case INDIRECT_REF:
14182 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14183 have_address = 1;
14184 break;
14185
14186 case COMPOUND_EXPR:
14187 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14188
14189 CASE_CONVERT:
14190 case VIEW_CONVERT_EXPR:
14191 case SAVE_EXPR:
14192 case MODIFY_EXPR:
14193 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14194
14195 case COMPONENT_REF:
14196 case BIT_FIELD_REF:
14197 case ARRAY_REF:
14198 case ARRAY_RANGE_REF:
14199 case REALPART_EXPR:
14200 case IMAGPART_EXPR:
14201 {
14202 tree obj, offset;
14203 HOST_WIDE_INT bitsize, bitpos, bytepos;
14204 enum machine_mode mode;
14205 int unsignedp, volatilep = 0;
14206
14207 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14208 &unsignedp, &volatilep, false);
14209
14210 gcc_assert (obj != loc);
14211
14212 list_ret = loc_list_from_tree (obj,
14213 want_address == 2
14214 && !bitpos && !offset ? 2 : 1);
14215 /* TODO: We can extract value of the small expression via shifting even
14216 for nonzero bitpos. */
14217 if (list_ret == 0)
14218 return 0;
14219 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14220 {
14221 expansion_failed (loc, NULL_RTX,
14222 "bitfield access");
14223 return 0;
14224 }
14225
14226 if (offset != NULL_TREE)
14227 {
14228 /* Variable offset. */
14229 list_ret1 = loc_list_from_tree (offset, 0);
14230 if (list_ret1 == 0)
14231 return 0;
14232 add_loc_list (&list_ret, list_ret1);
14233 if (!list_ret)
14234 return 0;
14235 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14236 }
14237
14238 bytepos = bitpos / BITS_PER_UNIT;
14239 if (bytepos > 0)
14240 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14241 else if (bytepos < 0)
14242 loc_list_plus_const (list_ret, bytepos);
14243
14244 have_address = 1;
14245 break;
14246 }
14247
14248 case INTEGER_CST:
14249 if ((want_address || !host_integerp (loc, 0))
14250 && (ret = cst_pool_loc_descr (loc)))
14251 have_address = 1;
14252 else if (want_address == 2
14253 && host_integerp (loc, 0)
14254 && (ret = address_of_int_loc_descriptor
14255 (int_size_in_bytes (TREE_TYPE (loc)),
14256 tree_low_cst (loc, 0))))
14257 have_address = 1;
14258 else if (host_integerp (loc, 0))
14259 ret = int_loc_descriptor (tree_low_cst (loc, 0));
14260 else
14261 {
14262 expansion_failed (loc, NULL_RTX,
14263 "Integer operand is not host integer");
14264 return 0;
14265 }
14266 break;
14267
14268 case CONSTRUCTOR:
14269 case REAL_CST:
14270 case STRING_CST:
14271 case COMPLEX_CST:
14272 if ((ret = cst_pool_loc_descr (loc)))
14273 have_address = 1;
14274 else
14275 /* We can construct small constants here using int_loc_descriptor. */
14276 expansion_failed (loc, NULL_RTX,
14277 "constructor or constant not in constant pool");
14278 break;
14279
14280 case TRUTH_AND_EXPR:
14281 case TRUTH_ANDIF_EXPR:
14282 case BIT_AND_EXPR:
14283 op = DW_OP_and;
14284 goto do_binop;
14285
14286 case TRUTH_XOR_EXPR:
14287 case BIT_XOR_EXPR:
14288 op = DW_OP_xor;
14289 goto do_binop;
14290
14291 case TRUTH_OR_EXPR:
14292 case TRUTH_ORIF_EXPR:
14293 case BIT_IOR_EXPR:
14294 op = DW_OP_or;
14295 goto do_binop;
14296
14297 case FLOOR_DIV_EXPR:
14298 case CEIL_DIV_EXPR:
14299 case ROUND_DIV_EXPR:
14300 case TRUNC_DIV_EXPR:
14301 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14302 return 0;
14303 op = DW_OP_div;
14304 goto do_binop;
14305
14306 case MINUS_EXPR:
14307 op = DW_OP_minus;
14308 goto do_binop;
14309
14310 case FLOOR_MOD_EXPR:
14311 case CEIL_MOD_EXPR:
14312 case ROUND_MOD_EXPR:
14313 case TRUNC_MOD_EXPR:
14314 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14315 {
14316 op = DW_OP_mod;
14317 goto do_binop;
14318 }
14319 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14320 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14321 if (list_ret == 0 || list_ret1 == 0)
14322 return 0;
14323
14324 add_loc_list (&list_ret, list_ret1);
14325 if (list_ret == 0)
14326 return 0;
14327 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14328 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14329 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14330 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14331 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14332 break;
14333
14334 case MULT_EXPR:
14335 op = DW_OP_mul;
14336 goto do_binop;
14337
14338 case LSHIFT_EXPR:
14339 op = DW_OP_shl;
14340 goto do_binop;
14341
14342 case RSHIFT_EXPR:
14343 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14344 goto do_binop;
14345
14346 case POINTER_PLUS_EXPR:
14347 case PLUS_EXPR:
14348 if (host_integerp (TREE_OPERAND (loc, 1), 0))
14349 {
14350 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14351 if (list_ret == 0)
14352 return 0;
14353
14354 loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14355 break;
14356 }
14357
14358 op = DW_OP_plus;
14359 goto do_binop;
14360
14361 case LE_EXPR:
14362 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14363 return 0;
14364
14365 op = DW_OP_le;
14366 goto do_binop;
14367
14368 case GE_EXPR:
14369 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14370 return 0;
14371
14372 op = DW_OP_ge;
14373 goto do_binop;
14374
14375 case LT_EXPR:
14376 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14377 return 0;
14378
14379 op = DW_OP_lt;
14380 goto do_binop;
14381
14382 case GT_EXPR:
14383 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14384 return 0;
14385
14386 op = DW_OP_gt;
14387 goto do_binop;
14388
14389 case EQ_EXPR:
14390 op = DW_OP_eq;
14391 goto do_binop;
14392
14393 case NE_EXPR:
14394 op = DW_OP_ne;
14395 goto do_binop;
14396
14397 do_binop:
14398 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14399 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14400 if (list_ret == 0 || list_ret1 == 0)
14401 return 0;
14402
14403 add_loc_list (&list_ret, list_ret1);
14404 if (list_ret == 0)
14405 return 0;
14406 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14407 break;
14408
14409 case TRUTH_NOT_EXPR:
14410 case BIT_NOT_EXPR:
14411 op = DW_OP_not;
14412 goto do_unop;
14413
14414 case ABS_EXPR:
14415 op = DW_OP_abs;
14416 goto do_unop;
14417
14418 case NEGATE_EXPR:
14419 op = DW_OP_neg;
14420 goto do_unop;
14421
14422 do_unop:
14423 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14424 if (list_ret == 0)
14425 return 0;
14426
14427 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14428 break;
14429
14430 case MIN_EXPR:
14431 case MAX_EXPR:
14432 {
14433 const enum tree_code code =
14434 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14435
14436 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14437 build2 (code, integer_type_node,
14438 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14439 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14440 }
14441
14442 /* ... fall through ... */
14443
14444 case COND_EXPR:
14445 {
14446 dw_loc_descr_ref lhs
14447 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14448 dw_loc_list_ref rhs
14449 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14450 dw_loc_descr_ref bra_node, jump_node, tmp;
14451
14452 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14453 if (list_ret == 0 || lhs == 0 || rhs == 0)
14454 return 0;
14455
14456 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14457 add_loc_descr_to_each (list_ret, bra_node);
14458
14459 add_loc_list (&list_ret, rhs);
14460 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14461 add_loc_descr_to_each (list_ret, jump_node);
14462
14463 add_loc_descr_to_each (list_ret, lhs);
14464 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14465 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14466
14467 /* ??? Need a node to point the skip at. Use a nop. */
14468 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14469 add_loc_descr_to_each (list_ret, tmp);
14470 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14471 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14472 }
14473 break;
14474
14475 case FIX_TRUNC_EXPR:
14476 return 0;
14477
14478 default:
14479 /* Leave front-end specific codes as simply unknown. This comes
14480 up, for instance, with the C STMT_EXPR. */
14481 if ((unsigned int) TREE_CODE (loc)
14482 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14483 {
14484 expansion_failed (loc, NULL_RTX,
14485 "language specific tree node");
14486 return 0;
14487 }
14488
14489 #ifdef ENABLE_CHECKING
14490 /* Otherwise this is a generic code; we should just lists all of
14491 these explicitly. We forgot one. */
14492 gcc_unreachable ();
14493 #else
14494 /* In a release build, we want to degrade gracefully: better to
14495 generate incomplete debugging information than to crash. */
14496 return NULL;
14497 #endif
14498 }
14499
14500 if (!ret && !list_ret)
14501 return 0;
14502
14503 if (want_address == 2 && !have_address
14504 && (dwarf_version >= 4 || !dwarf_strict))
14505 {
14506 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14507 {
14508 expansion_failed (loc, NULL_RTX,
14509 "DWARF address size mismatch");
14510 return 0;
14511 }
14512 if (ret)
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14514 else
14515 add_loc_descr_to_each (list_ret,
14516 new_loc_descr (DW_OP_stack_value, 0, 0));
14517 have_address = 1;
14518 }
14519 /* Show if we can't fill the request for an address. */
14520 if (want_address && !have_address)
14521 {
14522 expansion_failed (loc, NULL_RTX,
14523 "Want address and only have value");
14524 return 0;
14525 }
14526
14527 gcc_assert (!ret || !list_ret);
14528
14529 /* If we've got an address and don't want one, dereference. */
14530 if (!want_address && have_address)
14531 {
14532 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14533
14534 if (size > DWARF2_ADDR_SIZE || size == -1)
14535 {
14536 expansion_failed (loc, NULL_RTX,
14537 "DWARF address size mismatch");
14538 return 0;
14539 }
14540 else if (size == DWARF2_ADDR_SIZE)
14541 op = DW_OP_deref;
14542 else
14543 op = DW_OP_deref_size;
14544
14545 if (ret)
14546 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14547 else
14548 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14549 }
14550 if (ret)
14551 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14552
14553 return list_ret;
14554 }
14555
14556 /* Same as above but return only single location expression. */
14557 static dw_loc_descr_ref
14558 loc_descriptor_from_tree (tree loc, int want_address)
14559 {
14560 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14561 if (!ret)
14562 return NULL;
14563 if (ret->dw_loc_next)
14564 {
14565 expansion_failed (loc, NULL_RTX,
14566 "Location list where only loc descriptor needed");
14567 return NULL;
14568 }
14569 return ret->expr;
14570 }
14571
14572 /* Given a value, round it up to the lowest multiple of `boundary'
14573 which is not less than the value itself. */
14574
14575 static inline HOST_WIDE_INT
14576 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14577 {
14578 return (((value + boundary - 1) / boundary) * boundary);
14579 }
14580
14581 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14582 pointer to the declared type for the relevant field variable, or return
14583 `integer_type_node' if the given node turns out to be an
14584 ERROR_MARK node. */
14585
14586 static inline tree
14587 field_type (const_tree decl)
14588 {
14589 tree type;
14590
14591 if (TREE_CODE (decl) == ERROR_MARK)
14592 return integer_type_node;
14593
14594 type = DECL_BIT_FIELD_TYPE (decl);
14595 if (type == NULL_TREE)
14596 type = TREE_TYPE (decl);
14597
14598 return type;
14599 }
14600
14601 /* Given a pointer to a tree node, return the alignment in bits for
14602 it, or else return BITS_PER_WORD if the node actually turns out to
14603 be an ERROR_MARK node. */
14604
14605 static inline unsigned
14606 simple_type_align_in_bits (const_tree type)
14607 {
14608 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14609 }
14610
14611 static inline unsigned
14612 simple_decl_align_in_bits (const_tree decl)
14613 {
14614 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14615 }
14616
14617 /* Return the result of rounding T up to ALIGN. */
14618
14619 static inline double_int
14620 round_up_to_align (double_int t, unsigned int align)
14621 {
14622 double_int alignd = double_int::from_uhwi (align);
14623 t += alignd;
14624 t += double_int_minus_one;
14625 t = t.div (alignd, true, TRUNC_DIV_EXPR);
14626 t *= alignd;
14627 return t;
14628 }
14629
14630 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14631 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14632 or return 0 if we are unable to determine what that offset is, either
14633 because the argument turns out to be a pointer to an ERROR_MARK node, or
14634 because the offset is actually variable. (We can't handle the latter case
14635 just yet). */
14636
14637 static HOST_WIDE_INT
14638 field_byte_offset (const_tree decl)
14639 {
14640 double_int object_offset_in_bits;
14641 double_int object_offset_in_bytes;
14642 double_int bitpos_int;
14643
14644 if (TREE_CODE (decl) == ERROR_MARK)
14645 return 0;
14646
14647 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14648
14649 /* We cannot yet cope with fields whose positions are variable, so
14650 for now, when we see such things, we simply return 0. Someday, we may
14651 be able to handle such cases, but it will be damn difficult. */
14652 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14653 return 0;
14654
14655 bitpos_int = tree_to_double_int (bit_position (decl));
14656
14657 #ifdef PCC_BITFIELD_TYPE_MATTERS
14658 if (PCC_BITFIELD_TYPE_MATTERS)
14659 {
14660 tree type;
14661 tree field_size_tree;
14662 double_int deepest_bitpos;
14663 double_int field_size_in_bits;
14664 unsigned int type_align_in_bits;
14665 unsigned int decl_align_in_bits;
14666 double_int type_size_in_bits;
14667
14668 type = field_type (decl);
14669 type_size_in_bits = double_int_type_size_in_bits (type);
14670 type_align_in_bits = simple_type_align_in_bits (type);
14671
14672 field_size_tree = DECL_SIZE (decl);
14673
14674 /* The size could be unspecified if there was an error, or for
14675 a flexible array member. */
14676 if (!field_size_tree)
14677 field_size_tree = bitsize_zero_node;
14678
14679 /* If the size of the field is not constant, use the type size. */
14680 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14681 field_size_in_bits = tree_to_double_int (field_size_tree);
14682 else
14683 field_size_in_bits = type_size_in_bits;
14684
14685 decl_align_in_bits = simple_decl_align_in_bits (decl);
14686
14687 /* The GCC front-end doesn't make any attempt to keep track of the
14688 starting bit offset (relative to the start of the containing
14689 structure type) of the hypothetical "containing object" for a
14690 bit-field. Thus, when computing the byte offset value for the
14691 start of the "containing object" of a bit-field, we must deduce
14692 this information on our own. This can be rather tricky to do in
14693 some cases. For example, handling the following structure type
14694 definition when compiling for an i386/i486 target (which only
14695 aligns long long's to 32-bit boundaries) can be very tricky:
14696
14697 struct S { int field1; long long field2:31; };
14698
14699 Fortunately, there is a simple rule-of-thumb which can be used
14700 in such cases. When compiling for an i386/i486, GCC will
14701 allocate 8 bytes for the structure shown above. It decides to
14702 do this based upon one simple rule for bit-field allocation.
14703 GCC allocates each "containing object" for each bit-field at
14704 the first (i.e. lowest addressed) legitimate alignment boundary
14705 (based upon the required minimum alignment for the declared
14706 type of the field) which it can possibly use, subject to the
14707 condition that there is still enough available space remaining
14708 in the containing object (when allocated at the selected point)
14709 to fully accommodate all of the bits of the bit-field itself.
14710
14711 This simple rule makes it obvious why GCC allocates 8 bytes for
14712 each object of the structure type shown above. When looking
14713 for a place to allocate the "containing object" for `field2',
14714 the compiler simply tries to allocate a 64-bit "containing
14715 object" at each successive 32-bit boundary (starting at zero)
14716 until it finds a place to allocate that 64- bit field such that
14717 at least 31 contiguous (and previously unallocated) bits remain
14718 within that selected 64 bit field. (As it turns out, for the
14719 example above, the compiler finds it is OK to allocate the
14720 "containing object" 64-bit field at bit-offset zero within the
14721 structure type.)
14722
14723 Here we attempt to work backwards from the limited set of facts
14724 we're given, and we try to deduce from those facts, where GCC
14725 must have believed that the containing object started (within
14726 the structure type). The value we deduce is then used (by the
14727 callers of this routine) to generate DW_AT_location and
14728 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14729 the case of DW_AT_location, regular fields as well). */
14730
14731 /* Figure out the bit-distance from the start of the structure to
14732 the "deepest" bit of the bit-field. */
14733 deepest_bitpos = bitpos_int + field_size_in_bits;
14734
14735 /* This is the tricky part. Use some fancy footwork to deduce
14736 where the lowest addressed bit of the containing object must
14737 be. */
14738 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14739
14740 /* Round up to type_align by default. This works best for
14741 bitfields. */
14742 object_offset_in_bits
14743 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14744
14745 if (object_offset_in_bits.ugt (bitpos_int))
14746 {
14747 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14748
14749 /* Round up to decl_align instead. */
14750 object_offset_in_bits
14751 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14752 }
14753 }
14754 else
14755 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14756 object_offset_in_bits = bitpos_int;
14757
14758 object_offset_in_bytes
14759 = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14760 true, TRUNC_DIV_EXPR);
14761 return object_offset_in_bytes.to_shwi ();
14762 }
14763 \f
14764 /* The following routines define various Dwarf attributes and any data
14765 associated with them. */
14766
14767 /* Add a location description attribute value to a DIE.
14768
14769 This emits location attributes suitable for whole variables and
14770 whole parameters. Note that the location attributes for struct fields are
14771 generated by the routine `data_member_location_attribute' below. */
14772
14773 static inline void
14774 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14775 dw_loc_list_ref descr)
14776 {
14777 if (descr == 0)
14778 return;
14779 if (single_element_loc_list_p (descr))
14780 add_AT_loc (die, attr_kind, descr->expr);
14781 else
14782 add_AT_loc_list (die, attr_kind, descr);
14783 }
14784
14785 /* Add DW_AT_accessibility attribute to DIE if needed. */
14786
14787 static void
14788 add_accessibility_attribute (dw_die_ref die, tree decl)
14789 {
14790 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14791 children, otherwise the default is DW_ACCESS_public. In DWARF2
14792 the default has always been DW_ACCESS_public. */
14793 if (TREE_PROTECTED (decl))
14794 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14795 else if (TREE_PRIVATE (decl))
14796 {
14797 if (dwarf_version == 2
14798 || die->die_parent == NULL
14799 || die->die_parent->die_tag != DW_TAG_class_type)
14800 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14801 }
14802 else if (dwarf_version > 2
14803 && die->die_parent
14804 && die->die_parent->die_tag == DW_TAG_class_type)
14805 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14806 }
14807
14808 /* Attach the specialized form of location attribute used for data members of
14809 struct and union types. In the special case of a FIELD_DECL node which
14810 represents a bit-field, the "offset" part of this special location
14811 descriptor must indicate the distance in bytes from the lowest-addressed
14812 byte of the containing struct or union type to the lowest-addressed byte of
14813 the "containing object" for the bit-field. (See the `field_byte_offset'
14814 function above).
14815
14816 For any given bit-field, the "containing object" is a hypothetical object
14817 (of some integral or enum type) within which the given bit-field lives. The
14818 type of this hypothetical "containing object" is always the same as the
14819 declared type of the individual bit-field itself (for GCC anyway... the
14820 DWARF spec doesn't actually mandate this). Note that it is the size (in
14821 bytes) of the hypothetical "containing object" which will be given in the
14822 DW_AT_byte_size attribute for this bit-field. (See the
14823 `byte_size_attribute' function below.) It is also used when calculating the
14824 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14825 function below.) */
14826
14827 static void
14828 add_data_member_location_attribute (dw_die_ref die, tree decl)
14829 {
14830 HOST_WIDE_INT offset;
14831 dw_loc_descr_ref loc_descr = 0;
14832
14833 if (TREE_CODE (decl) == TREE_BINFO)
14834 {
14835 /* We're working on the TAG_inheritance for a base class. */
14836 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14837 {
14838 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14839 aren't at a fixed offset from all (sub)objects of the same
14840 type. We need to extract the appropriate offset from our
14841 vtable. The following dwarf expression means
14842
14843 BaseAddr = ObAddr + *((*ObAddr) - Offset)
14844
14845 This is specific to the V3 ABI, of course. */
14846
14847 dw_loc_descr_ref tmp;
14848
14849 /* Make a copy of the object address. */
14850 tmp = new_loc_descr (DW_OP_dup, 0, 0);
14851 add_loc_descr (&loc_descr, tmp);
14852
14853 /* Extract the vtable address. */
14854 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14855 add_loc_descr (&loc_descr, tmp);
14856
14857 /* Calculate the address of the offset. */
14858 offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14859 gcc_assert (offset < 0);
14860
14861 tmp = int_loc_descriptor (-offset);
14862 add_loc_descr (&loc_descr, tmp);
14863 tmp = new_loc_descr (DW_OP_minus, 0, 0);
14864 add_loc_descr (&loc_descr, tmp);
14865
14866 /* Extract the offset. */
14867 tmp = new_loc_descr (DW_OP_deref, 0, 0);
14868 add_loc_descr (&loc_descr, tmp);
14869
14870 /* Add it to the object address. */
14871 tmp = new_loc_descr (DW_OP_plus, 0, 0);
14872 add_loc_descr (&loc_descr, tmp);
14873 }
14874 else
14875 offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14876 }
14877 else
14878 offset = field_byte_offset (decl);
14879
14880 if (! loc_descr)
14881 {
14882 if (dwarf_version > 2)
14883 {
14884 /* Don't need to output a location expression, just the constant. */
14885 if (offset < 0)
14886 add_AT_int (die, DW_AT_data_member_location, offset);
14887 else
14888 add_AT_unsigned (die, DW_AT_data_member_location, offset);
14889 return;
14890 }
14891 else
14892 {
14893 enum dwarf_location_atom op;
14894
14895 /* The DWARF2 standard says that we should assume that the structure
14896 address is already on the stack, so we can specify a structure
14897 field address by using DW_OP_plus_uconst. */
14898 op = DW_OP_plus_uconst;
14899 loc_descr = new_loc_descr (op, offset, 0);
14900 }
14901 }
14902
14903 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14904 }
14905
14906 /* Writes integer values to dw_vec_const array. */
14907
14908 static void
14909 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14910 {
14911 while (size != 0)
14912 {
14913 *dest++ = val & 0xff;
14914 val >>= 8;
14915 --size;
14916 }
14917 }
14918
14919 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
14920
14921 static HOST_WIDE_INT
14922 extract_int (const unsigned char *src, unsigned int size)
14923 {
14924 HOST_WIDE_INT val = 0;
14925
14926 src += size;
14927 while (size != 0)
14928 {
14929 val <<= 8;
14930 val |= *--src & 0xff;
14931 --size;
14932 }
14933 return val;
14934 }
14935
14936 /* Writes double_int values to dw_vec_const array. */
14937
14938 static void
14939 insert_double (double_int val, unsigned char *dest)
14940 {
14941 unsigned char *p0 = dest;
14942 unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14943
14944 if (WORDS_BIG_ENDIAN)
14945 {
14946 p0 = p1;
14947 p1 = dest;
14948 }
14949
14950 insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14951 insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14952 }
14953
14954 /* Writes floating point values to dw_vec_const array. */
14955
14956 static void
14957 insert_float (const_rtx rtl, unsigned char *array)
14958 {
14959 REAL_VALUE_TYPE rv;
14960 long val[4];
14961 int i;
14962
14963 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14964 real_to_target (val, &rv, GET_MODE (rtl));
14965
14966 /* real_to_target puts 32-bit pieces in each long. Pack them. */
14967 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14968 {
14969 insert_int (val[i], 4, array);
14970 array += 4;
14971 }
14972 }
14973
14974 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14975 does not have a "location" either in memory or in a register. These
14976 things can arise in GNU C when a constant is passed as an actual parameter
14977 to an inlined function. They can also arise in C++ where declared
14978 constants do not necessarily get memory "homes". */
14979
14980 static bool
14981 add_const_value_attribute (dw_die_ref die, rtx rtl)
14982 {
14983 switch (GET_CODE (rtl))
14984 {
14985 case CONST_INT:
14986 {
14987 HOST_WIDE_INT val = INTVAL (rtl);
14988
14989 if (val < 0)
14990 add_AT_int (die, DW_AT_const_value, val);
14991 else
14992 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14993 }
14994 return true;
14995
14996 case CONST_DOUBLE:
14997 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14998 floating-point constant. A CONST_DOUBLE is used whenever the
14999 constant requires more than one word in order to be adequately
15000 represented. */
15001 {
15002 enum machine_mode mode = GET_MODE (rtl);
15003
15004 if (SCALAR_FLOAT_MODE_P (mode))
15005 {
15006 unsigned int length = GET_MODE_SIZE (mode);
15007 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15008
15009 insert_float (rtl, array);
15010 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15011 }
15012 else
15013 add_AT_double (die, DW_AT_const_value,
15014 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15015 }
15016 return true;
15017
15018 case CONST_VECTOR:
15019 {
15020 enum machine_mode mode = GET_MODE (rtl);
15021 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15022 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15023 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15024 (length * elt_size);
15025 unsigned int i;
15026 unsigned char *p;
15027
15028 switch (GET_MODE_CLASS (mode))
15029 {
15030 case MODE_VECTOR_INT:
15031 for (i = 0, p = array; i < length; i++, p += elt_size)
15032 {
15033 rtx elt = CONST_VECTOR_ELT (rtl, i);
15034 double_int val = rtx_to_double_int (elt);
15035
15036 if (elt_size <= sizeof (HOST_WIDE_INT))
15037 insert_int (val.to_shwi (), elt_size, p);
15038 else
15039 {
15040 gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15041 insert_double (val, p);
15042 }
15043 }
15044 break;
15045
15046 case MODE_VECTOR_FLOAT:
15047 for (i = 0, p = array; i < length; i++, p += elt_size)
15048 {
15049 rtx elt = CONST_VECTOR_ELT (rtl, i);
15050 insert_float (elt, p);
15051 }
15052 break;
15053
15054 default:
15055 gcc_unreachable ();
15056 }
15057
15058 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15059 }
15060 return true;
15061
15062 case CONST_STRING:
15063 if (dwarf_version >= 4 || !dwarf_strict)
15064 {
15065 dw_loc_descr_ref loc_result;
15066 resolve_one_addr (&rtl, NULL);
15067 rtl_addr:
15068 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15069 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15070 add_AT_loc (die, DW_AT_location, loc_result);
15071 vec_safe_push (used_rtx_array, rtl);
15072 return true;
15073 }
15074 return false;
15075
15076 case CONST:
15077 if (CONSTANT_P (XEXP (rtl, 0)))
15078 return add_const_value_attribute (die, XEXP (rtl, 0));
15079 /* FALLTHROUGH */
15080 case SYMBOL_REF:
15081 if (!const_ok_for_output (rtl))
15082 return false;
15083 case LABEL_REF:
15084 if (dwarf_version >= 4 || !dwarf_strict)
15085 goto rtl_addr;
15086 return false;
15087
15088 case PLUS:
15089 /* In cases where an inlined instance of an inline function is passed
15090 the address of an `auto' variable (which is local to the caller) we
15091 can get a situation where the DECL_RTL of the artificial local
15092 variable (for the inlining) which acts as a stand-in for the
15093 corresponding formal parameter (of the inline function) will look
15094 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15095 exactly a compile-time constant expression, but it isn't the address
15096 of the (artificial) local variable either. Rather, it represents the
15097 *value* which the artificial local variable always has during its
15098 lifetime. We currently have no way to represent such quasi-constant
15099 values in Dwarf, so for now we just punt and generate nothing. */
15100 return false;
15101
15102 case HIGH:
15103 case CONST_FIXED:
15104 return false;
15105
15106 case MEM:
15107 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15108 && MEM_READONLY_P (rtl)
15109 && GET_MODE (rtl) == BLKmode)
15110 {
15111 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15112 return true;
15113 }
15114 return false;
15115
15116 default:
15117 /* No other kinds of rtx should be possible here. */
15118 gcc_unreachable ();
15119 }
15120 return false;
15121 }
15122
15123 /* Determine whether the evaluation of EXPR references any variables
15124 or functions which aren't otherwise used (and therefore may not be
15125 output). */
15126 static tree
15127 reference_to_unused (tree * tp, int * walk_subtrees,
15128 void * data ATTRIBUTE_UNUSED)
15129 {
15130 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15131 *walk_subtrees = 0;
15132
15133 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15134 && ! TREE_ASM_WRITTEN (*tp))
15135 return *tp;
15136 /* ??? The C++ FE emits debug information for using decls, so
15137 putting gcc_unreachable here falls over. See PR31899. For now
15138 be conservative. */
15139 else if (!cgraph_global_info_ready
15140 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15141 return *tp;
15142 else if (TREE_CODE (*tp) == VAR_DECL)
15143 {
15144 struct varpool_node *node = varpool_get_node (*tp);
15145 if (!node || !node->definition)
15146 return *tp;
15147 }
15148 else if (TREE_CODE (*tp) == FUNCTION_DECL
15149 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15150 {
15151 /* The call graph machinery must have finished analyzing,
15152 optimizing and gimplifying the CU by now.
15153 So if *TP has no call graph node associated
15154 to it, it means *TP will not be emitted. */
15155 if (!cgraph_get_node (*tp))
15156 return *tp;
15157 }
15158 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15159 return *tp;
15160
15161 return NULL_TREE;
15162 }
15163
15164 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15165 for use in a later add_const_value_attribute call. */
15166
15167 static rtx
15168 rtl_for_decl_init (tree init, tree type)
15169 {
15170 rtx rtl = NULL_RTX;
15171
15172 STRIP_NOPS (init);
15173
15174 /* If a variable is initialized with a string constant without embedded
15175 zeros, build CONST_STRING. */
15176 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15177 {
15178 tree enttype = TREE_TYPE (type);
15179 tree domain = TYPE_DOMAIN (type);
15180 enum machine_mode mode = TYPE_MODE (enttype);
15181
15182 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15183 && domain
15184 && integer_zerop (TYPE_MIN_VALUE (domain))
15185 && compare_tree_int (TYPE_MAX_VALUE (domain),
15186 TREE_STRING_LENGTH (init) - 1) == 0
15187 && ((size_t) TREE_STRING_LENGTH (init)
15188 == strlen (TREE_STRING_POINTER (init)) + 1))
15189 {
15190 rtl = gen_rtx_CONST_STRING (VOIDmode,
15191 ggc_strdup (TREE_STRING_POINTER (init)));
15192 rtl = gen_rtx_MEM (BLKmode, rtl);
15193 MEM_READONLY_P (rtl) = 1;
15194 }
15195 }
15196 /* Other aggregates, and complex values, could be represented using
15197 CONCAT: FIXME! */
15198 else if (AGGREGATE_TYPE_P (type)
15199 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15200 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15201 || TREE_CODE (type) == COMPLEX_TYPE)
15202 ;
15203 /* Vectors only work if their mode is supported by the target.
15204 FIXME: generic vectors ought to work too. */
15205 else if (TREE_CODE (type) == VECTOR_TYPE
15206 && !VECTOR_MODE_P (TYPE_MODE (type)))
15207 ;
15208 /* If the initializer is something that we know will expand into an
15209 immediate RTL constant, expand it now. We must be careful not to
15210 reference variables which won't be output. */
15211 else if (initializer_constant_valid_p (init, type)
15212 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15213 {
15214 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15215 possible. */
15216 if (TREE_CODE (type) == VECTOR_TYPE)
15217 switch (TREE_CODE (init))
15218 {
15219 case VECTOR_CST:
15220 break;
15221 case CONSTRUCTOR:
15222 if (TREE_CONSTANT (init))
15223 {
15224 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15225 bool constant_p = true;
15226 tree value;
15227 unsigned HOST_WIDE_INT ix;
15228
15229 /* Even when ctor is constant, it might contain non-*_CST
15230 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15231 belong into VECTOR_CST nodes. */
15232 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15233 if (!CONSTANT_CLASS_P (value))
15234 {
15235 constant_p = false;
15236 break;
15237 }
15238
15239 if (constant_p)
15240 {
15241 init = build_vector_from_ctor (type, elts);
15242 break;
15243 }
15244 }
15245 /* FALLTHRU */
15246
15247 default:
15248 return NULL;
15249 }
15250
15251 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15252
15253 /* If expand_expr returns a MEM, it wasn't immediate. */
15254 gcc_assert (!rtl || !MEM_P (rtl));
15255 }
15256
15257 return rtl;
15258 }
15259
15260 /* Generate RTL for the variable DECL to represent its location. */
15261
15262 static rtx
15263 rtl_for_decl_location (tree decl)
15264 {
15265 rtx rtl;
15266
15267 /* Here we have to decide where we are going to say the parameter "lives"
15268 (as far as the debugger is concerned). We only have a couple of
15269 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15270
15271 DECL_RTL normally indicates where the parameter lives during most of the
15272 activation of the function. If optimization is enabled however, this
15273 could be either NULL or else a pseudo-reg. Both of those cases indicate
15274 that the parameter doesn't really live anywhere (as far as the code
15275 generation parts of GCC are concerned) during most of the function's
15276 activation. That will happen (for example) if the parameter is never
15277 referenced within the function.
15278
15279 We could just generate a location descriptor here for all non-NULL
15280 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15281 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15282 where DECL_RTL is NULL or is a pseudo-reg.
15283
15284 Note however that we can only get away with using DECL_INCOMING_RTL as
15285 a backup substitute for DECL_RTL in certain limited cases. In cases
15286 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15287 we can be sure that the parameter was passed using the same type as it is
15288 declared to have within the function, and that its DECL_INCOMING_RTL
15289 points us to a place where a value of that type is passed.
15290
15291 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15292 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15293 because in these cases DECL_INCOMING_RTL points us to a value of some
15294 type which is *different* from the type of the parameter itself. Thus,
15295 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15296 such cases, the debugger would end up (for example) trying to fetch a
15297 `float' from a place which actually contains the first part of a
15298 `double'. That would lead to really incorrect and confusing
15299 output at debug-time.
15300
15301 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15302 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15303 are a couple of exceptions however. On little-endian machines we can
15304 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15305 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15306 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15307 when (on a little-endian machine) a non-prototyped function has a
15308 parameter declared to be of type `short' or `char'. In such cases,
15309 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15310 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15311 passed `int' value. If the debugger then uses that address to fetch
15312 a `short' or a `char' (on a little-endian machine) the result will be
15313 the correct data, so we allow for such exceptional cases below.
15314
15315 Note that our goal here is to describe the place where the given formal
15316 parameter lives during most of the function's activation (i.e. between the
15317 end of the prologue and the start of the epilogue). We'll do that as best
15318 as we can. Note however that if the given formal parameter is modified
15319 sometime during the execution of the function, then a stack backtrace (at
15320 debug-time) will show the function as having been called with the *new*
15321 value rather than the value which was originally passed in. This happens
15322 rarely enough that it is not a major problem, but it *is* a problem, and
15323 I'd like to fix it.
15324
15325 A future version of dwarf2out.c may generate two additional attributes for
15326 any given DW_TAG_formal_parameter DIE which will describe the "passed
15327 type" and the "passed location" for the given formal parameter in addition
15328 to the attributes we now generate to indicate the "declared type" and the
15329 "active location" for each parameter. This additional set of attributes
15330 could be used by debuggers for stack backtraces. Separately, note that
15331 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15332 This happens (for example) for inlined-instances of inline function formal
15333 parameters which are never referenced. This really shouldn't be
15334 happening. All PARM_DECL nodes should get valid non-NULL
15335 DECL_INCOMING_RTL values. FIXME. */
15336
15337 /* Use DECL_RTL as the "location" unless we find something better. */
15338 rtl = DECL_RTL_IF_SET (decl);
15339
15340 /* When generating abstract instances, ignore everything except
15341 constants, symbols living in memory, and symbols living in
15342 fixed registers. */
15343 if (! reload_completed)
15344 {
15345 if (rtl
15346 && (CONSTANT_P (rtl)
15347 || (MEM_P (rtl)
15348 && CONSTANT_P (XEXP (rtl, 0)))
15349 || (REG_P (rtl)
15350 && TREE_CODE (decl) == VAR_DECL
15351 && TREE_STATIC (decl))))
15352 {
15353 rtl = targetm.delegitimize_address (rtl);
15354 return rtl;
15355 }
15356 rtl = NULL_RTX;
15357 }
15358 else if (TREE_CODE (decl) == PARM_DECL)
15359 {
15360 if (rtl == NULL_RTX
15361 || is_pseudo_reg (rtl)
15362 || (MEM_P (rtl)
15363 && is_pseudo_reg (XEXP (rtl, 0))
15364 && DECL_INCOMING_RTL (decl)
15365 && MEM_P (DECL_INCOMING_RTL (decl))
15366 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15367 {
15368 tree declared_type = TREE_TYPE (decl);
15369 tree passed_type = DECL_ARG_TYPE (decl);
15370 enum machine_mode dmode = TYPE_MODE (declared_type);
15371 enum machine_mode pmode = TYPE_MODE (passed_type);
15372
15373 /* This decl represents a formal parameter which was optimized out.
15374 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15375 all cases where (rtl == NULL_RTX) just below. */
15376 if (dmode == pmode)
15377 rtl = DECL_INCOMING_RTL (decl);
15378 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15379 && SCALAR_INT_MODE_P (dmode)
15380 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15381 && DECL_INCOMING_RTL (decl))
15382 {
15383 rtx inc = DECL_INCOMING_RTL (decl);
15384 if (REG_P (inc))
15385 rtl = inc;
15386 else if (MEM_P (inc))
15387 {
15388 if (BYTES_BIG_ENDIAN)
15389 rtl = adjust_address_nv (inc, dmode,
15390 GET_MODE_SIZE (pmode)
15391 - GET_MODE_SIZE (dmode));
15392 else
15393 rtl = inc;
15394 }
15395 }
15396 }
15397
15398 /* If the parm was passed in registers, but lives on the stack, then
15399 make a big endian correction if the mode of the type of the
15400 parameter is not the same as the mode of the rtl. */
15401 /* ??? This is the same series of checks that are made in dbxout.c before
15402 we reach the big endian correction code there. It isn't clear if all
15403 of these checks are necessary here, but keeping them all is the safe
15404 thing to do. */
15405 else if (MEM_P (rtl)
15406 && XEXP (rtl, 0) != const0_rtx
15407 && ! CONSTANT_P (XEXP (rtl, 0))
15408 /* Not passed in memory. */
15409 && !MEM_P (DECL_INCOMING_RTL (decl))
15410 /* Not passed by invisible reference. */
15411 && (!REG_P (XEXP (rtl, 0))
15412 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15413 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15414 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15415 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15416 #endif
15417 )
15418 /* Big endian correction check. */
15419 && BYTES_BIG_ENDIAN
15420 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15421 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15422 < UNITS_PER_WORD))
15423 {
15424 enum machine_mode addr_mode = get_address_mode (rtl);
15425 int offset = (UNITS_PER_WORD
15426 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15427
15428 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15429 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15430 }
15431 }
15432 else if (TREE_CODE (decl) == VAR_DECL
15433 && rtl
15434 && MEM_P (rtl)
15435 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15436 && BYTES_BIG_ENDIAN)
15437 {
15438 enum machine_mode addr_mode = get_address_mode (rtl);
15439 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15440 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15441
15442 /* If a variable is declared "register" yet is smaller than
15443 a register, then if we store the variable to memory, it
15444 looks like we're storing a register-sized value, when in
15445 fact we are not. We need to adjust the offset of the
15446 storage location to reflect the actual value's bytes,
15447 else gdb will not be able to display it. */
15448 if (rsize > dsize)
15449 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15450 plus_constant (addr_mode, XEXP (rtl, 0),
15451 rsize - dsize));
15452 }
15453
15454 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15455 and will have been substituted directly into all expressions that use it.
15456 C does not have such a concept, but C++ and other languages do. */
15457 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15458 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15459
15460 if (rtl)
15461 rtl = targetm.delegitimize_address (rtl);
15462
15463 /* If we don't look past the constant pool, we risk emitting a
15464 reference to a constant pool entry that isn't referenced from
15465 code, and thus is not emitted. */
15466 if (rtl)
15467 rtl = avoid_constant_pool_reference (rtl);
15468
15469 /* Try harder to get a rtl. If this symbol ends up not being emitted
15470 in the current CU, resolve_addr will remove the expression referencing
15471 it. */
15472 if (rtl == NULL_RTX
15473 && TREE_CODE (decl) == VAR_DECL
15474 && !DECL_EXTERNAL (decl)
15475 && TREE_STATIC (decl)
15476 && DECL_NAME (decl)
15477 && !DECL_HARD_REGISTER (decl)
15478 && DECL_MODE (decl) != VOIDmode)
15479 {
15480 rtl = make_decl_rtl_for_debug (decl);
15481 if (!MEM_P (rtl)
15482 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15483 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15484 rtl = NULL_RTX;
15485 }
15486
15487 return rtl;
15488 }
15489
15490 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15491 returned. If so, the decl for the COMMON block is returned, and the
15492 value is the offset into the common block for the symbol. */
15493
15494 static tree
15495 fortran_common (tree decl, HOST_WIDE_INT *value)
15496 {
15497 tree val_expr, cvar;
15498 enum machine_mode mode;
15499 HOST_WIDE_INT bitsize, bitpos;
15500 tree offset;
15501 int unsignedp, volatilep = 0;
15502
15503 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15504 it does not have a value (the offset into the common area), or if it
15505 is thread local (as opposed to global) then it isn't common, and shouldn't
15506 be handled as such. */
15507 if (TREE_CODE (decl) != VAR_DECL
15508 || !TREE_STATIC (decl)
15509 || !DECL_HAS_VALUE_EXPR_P (decl)
15510 || !is_fortran ())
15511 return NULL_TREE;
15512
15513 val_expr = DECL_VALUE_EXPR (decl);
15514 if (TREE_CODE (val_expr) != COMPONENT_REF)
15515 return NULL_TREE;
15516
15517 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15518 &mode, &unsignedp, &volatilep, true);
15519
15520 if (cvar == NULL_TREE
15521 || TREE_CODE (cvar) != VAR_DECL
15522 || DECL_ARTIFICIAL (cvar)
15523 || !TREE_PUBLIC (cvar))
15524 return NULL_TREE;
15525
15526 *value = 0;
15527 if (offset != NULL)
15528 {
15529 if (!host_integerp (offset, 0))
15530 return NULL_TREE;
15531 *value = tree_low_cst (offset, 0);
15532 }
15533 if (bitpos != 0)
15534 *value += bitpos / BITS_PER_UNIT;
15535
15536 return cvar;
15537 }
15538
15539 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15540 data attribute for a variable or a parameter. We generate the
15541 DW_AT_const_value attribute only in those cases where the given variable
15542 or parameter does not have a true "location" either in memory or in a
15543 register. This can happen (for example) when a constant is passed as an
15544 actual argument in a call to an inline function. (It's possible that
15545 these things can crop up in other ways also.) Note that one type of
15546 constant value which can be passed into an inlined function is a constant
15547 pointer. This can happen for example if an actual argument in an inlined
15548 function call evaluates to a compile-time constant address.
15549
15550 CACHE_P is true if it is worth caching the location list for DECL,
15551 so that future calls can reuse it rather than regenerate it from scratch.
15552 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15553 since we will need to refer to them each time the function is inlined. */
15554
15555 static bool
15556 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15557 enum dwarf_attribute attr)
15558 {
15559 rtx rtl;
15560 dw_loc_list_ref list;
15561 var_loc_list *loc_list;
15562 cached_dw_loc_list *cache;
15563 void **slot;
15564
15565 if (TREE_CODE (decl) == ERROR_MARK)
15566 return false;
15567
15568 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15569 || TREE_CODE (decl) == RESULT_DECL);
15570
15571 /* Try to get some constant RTL for this decl, and use that as the value of
15572 the location. */
15573
15574 rtl = rtl_for_decl_location (decl);
15575 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15576 && add_const_value_attribute (die, rtl))
15577 return true;
15578
15579 /* See if we have single element location list that is equivalent to
15580 a constant value. That way we are better to use add_const_value_attribute
15581 rather than expanding constant value equivalent. */
15582 loc_list = lookup_decl_loc (decl);
15583 if (loc_list
15584 && loc_list->first
15585 && loc_list->first->next == NULL
15586 && NOTE_P (loc_list->first->loc)
15587 && NOTE_VAR_LOCATION (loc_list->first->loc)
15588 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15589 {
15590 struct var_loc_node *node;
15591
15592 node = loc_list->first;
15593 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15594 if (GET_CODE (rtl) == EXPR_LIST)
15595 rtl = XEXP (rtl, 0);
15596 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15597 && add_const_value_attribute (die, rtl))
15598 return true;
15599 }
15600 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15601 list several times. See if we've already cached the contents. */
15602 list = NULL;
15603 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15604 cache_p = false;
15605 if (cache_p)
15606 {
15607 cache = (cached_dw_loc_list *)
15608 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15609 if (cache)
15610 list = cache->loc_list;
15611 }
15612 if (list == NULL)
15613 {
15614 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15615 /* It is usually worth caching this result if the decl is from
15616 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15617 if (cache_p && list && list->dw_loc_next)
15618 {
15619 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15620 DECL_UID (decl), INSERT);
15621 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15622 cache->decl_id = DECL_UID (decl);
15623 cache->loc_list = list;
15624 *slot = cache;
15625 }
15626 }
15627 if (list)
15628 {
15629 add_AT_location_description (die, attr, list);
15630 return true;
15631 }
15632 /* None of that worked, so it must not really have a location;
15633 try adding a constant value attribute from the DECL_INITIAL. */
15634 return tree_add_const_value_attribute_for_decl (die, decl);
15635 }
15636
15637 /* Add VARIABLE and DIE into deferred locations list. */
15638
15639 static void
15640 defer_location (tree variable, dw_die_ref die)
15641 {
15642 deferred_locations entry;
15643 entry.variable = variable;
15644 entry.die = die;
15645 vec_safe_push (deferred_locations_list, entry);
15646 }
15647
15648 /* Helper function for tree_add_const_value_attribute. Natively encode
15649 initializer INIT into an array. Return true if successful. */
15650
15651 static bool
15652 native_encode_initializer (tree init, unsigned char *array, int size)
15653 {
15654 tree type;
15655
15656 if (init == NULL_TREE)
15657 return false;
15658
15659 STRIP_NOPS (init);
15660 switch (TREE_CODE (init))
15661 {
15662 case STRING_CST:
15663 type = TREE_TYPE (init);
15664 if (TREE_CODE (type) == ARRAY_TYPE)
15665 {
15666 tree enttype = TREE_TYPE (type);
15667 enum machine_mode mode = TYPE_MODE (enttype);
15668
15669 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15670 return false;
15671 if (int_size_in_bytes (type) != size)
15672 return false;
15673 if (size > TREE_STRING_LENGTH (init))
15674 {
15675 memcpy (array, TREE_STRING_POINTER (init),
15676 TREE_STRING_LENGTH (init));
15677 memset (array + TREE_STRING_LENGTH (init),
15678 '\0', size - TREE_STRING_LENGTH (init));
15679 }
15680 else
15681 memcpy (array, TREE_STRING_POINTER (init), size);
15682 return true;
15683 }
15684 return false;
15685 case CONSTRUCTOR:
15686 type = TREE_TYPE (init);
15687 if (int_size_in_bytes (type) != size)
15688 return false;
15689 if (TREE_CODE (type) == ARRAY_TYPE)
15690 {
15691 HOST_WIDE_INT min_index;
15692 unsigned HOST_WIDE_INT cnt;
15693 int curpos = 0, fieldsize;
15694 constructor_elt *ce;
15695
15696 if (TYPE_DOMAIN (type) == NULL_TREE
15697 || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15698 return false;
15699
15700 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15701 if (fieldsize <= 0)
15702 return false;
15703
15704 min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15705 memset (array, '\0', size);
15706 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15707 {
15708 tree val = ce->value;
15709 tree index = ce->index;
15710 int pos = curpos;
15711 if (index && TREE_CODE (index) == RANGE_EXPR)
15712 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15713 * fieldsize;
15714 else if (index)
15715 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15716
15717 if (val)
15718 {
15719 STRIP_NOPS (val);
15720 if (!native_encode_initializer (val, array + pos, fieldsize))
15721 return false;
15722 }
15723 curpos = pos + fieldsize;
15724 if (index && TREE_CODE (index) == RANGE_EXPR)
15725 {
15726 int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15727 - tree_low_cst (TREE_OPERAND (index, 0), 0);
15728 while (count-- > 0)
15729 {
15730 if (val)
15731 memcpy (array + curpos, array + pos, fieldsize);
15732 curpos += fieldsize;
15733 }
15734 }
15735 gcc_assert (curpos <= size);
15736 }
15737 return true;
15738 }
15739 else if (TREE_CODE (type) == RECORD_TYPE
15740 || TREE_CODE (type) == UNION_TYPE)
15741 {
15742 tree field = NULL_TREE;
15743 unsigned HOST_WIDE_INT cnt;
15744 constructor_elt *ce;
15745
15746 if (int_size_in_bytes (type) != size)
15747 return false;
15748
15749 if (TREE_CODE (type) == RECORD_TYPE)
15750 field = TYPE_FIELDS (type);
15751
15752 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15753 {
15754 tree val = ce->value;
15755 int pos, fieldsize;
15756
15757 if (ce->index != 0)
15758 field = ce->index;
15759
15760 if (val)
15761 STRIP_NOPS (val);
15762
15763 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15764 return false;
15765
15766 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15767 && TYPE_DOMAIN (TREE_TYPE (field))
15768 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15769 return false;
15770 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15771 || !host_integerp (DECL_SIZE_UNIT (field), 0))
15772 return false;
15773 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15774 pos = int_byte_position (field);
15775 gcc_assert (pos + fieldsize <= size);
15776 if (val
15777 && !native_encode_initializer (val, array + pos, fieldsize))
15778 return false;
15779 }
15780 return true;
15781 }
15782 return false;
15783 case VIEW_CONVERT_EXPR:
15784 case NON_LVALUE_EXPR:
15785 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15786 default:
15787 return native_encode_expr (init, array, size) == size;
15788 }
15789 }
15790
15791 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15792 attribute is the const value T. */
15793
15794 static bool
15795 tree_add_const_value_attribute (dw_die_ref die, tree t)
15796 {
15797 tree init;
15798 tree type = TREE_TYPE (t);
15799 rtx rtl;
15800
15801 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15802 return false;
15803
15804 init = t;
15805 gcc_assert (!DECL_P (init));
15806
15807 rtl = rtl_for_decl_init (init, type);
15808 if (rtl)
15809 return add_const_value_attribute (die, rtl);
15810 /* If the host and target are sane, try harder. */
15811 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15812 && initializer_constant_valid_p (init, type))
15813 {
15814 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15815 if (size > 0 && (int) size == size)
15816 {
15817 unsigned char *array = (unsigned char *)
15818 ggc_alloc_cleared_atomic (size);
15819
15820 if (native_encode_initializer (init, array, size))
15821 {
15822 add_AT_vec (die, DW_AT_const_value, size, 1, array);
15823 return true;
15824 }
15825 ggc_free (array);
15826 }
15827 }
15828 return false;
15829 }
15830
15831 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15832 attribute is the const value of T, where T is an integral constant
15833 variable with static storage duration
15834 (so it can't be a PARM_DECL or a RESULT_DECL). */
15835
15836 static bool
15837 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15838 {
15839
15840 if (!decl
15841 || (TREE_CODE (decl) != VAR_DECL
15842 && TREE_CODE (decl) != CONST_DECL)
15843 || (TREE_CODE (decl) == VAR_DECL
15844 && !TREE_STATIC (decl)))
15845 return false;
15846
15847 if (TREE_READONLY (decl)
15848 && ! TREE_THIS_VOLATILE (decl)
15849 && DECL_INITIAL (decl))
15850 /* OK */;
15851 else
15852 return false;
15853
15854 /* Don't add DW_AT_const_value if abstract origin already has one. */
15855 if (get_AT (var_die, DW_AT_const_value))
15856 return false;
15857
15858 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15859 }
15860
15861 /* Convert the CFI instructions for the current function into a
15862 location list. This is used for DW_AT_frame_base when we targeting
15863 a dwarf2 consumer that does not support the dwarf3
15864 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
15865 expressions. */
15866
15867 static dw_loc_list_ref
15868 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15869 {
15870 int ix;
15871 dw_fde_ref fde;
15872 dw_loc_list_ref list, *list_tail;
15873 dw_cfi_ref cfi;
15874 dw_cfa_location last_cfa, next_cfa;
15875 const char *start_label, *last_label, *section;
15876 dw_cfa_location remember;
15877
15878 fde = cfun->fde;
15879 gcc_assert (fde != NULL);
15880
15881 section = secname_for_decl (current_function_decl);
15882 list_tail = &list;
15883 list = NULL;
15884
15885 memset (&next_cfa, 0, sizeof (next_cfa));
15886 next_cfa.reg = INVALID_REGNUM;
15887 remember = next_cfa;
15888
15889 start_label = fde->dw_fde_begin;
15890
15891 /* ??? Bald assumption that the CIE opcode list does not contain
15892 advance opcodes. */
15893 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15894 lookup_cfa_1 (cfi, &next_cfa, &remember);
15895
15896 last_cfa = next_cfa;
15897 last_label = start_label;
15898
15899 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15900 {
15901 /* If the first partition contained no CFI adjustments, the
15902 CIE opcodes apply to the whole first partition. */
15903 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15904 fde->dw_fde_begin, fde->dw_fde_end, section);
15905 list_tail =&(*list_tail)->dw_loc_next;
15906 start_label = last_label = fde->dw_fde_second_begin;
15907 }
15908
15909 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15910 {
15911 switch (cfi->dw_cfi_opc)
15912 {
15913 case DW_CFA_set_loc:
15914 case DW_CFA_advance_loc1:
15915 case DW_CFA_advance_loc2:
15916 case DW_CFA_advance_loc4:
15917 if (!cfa_equal_p (&last_cfa, &next_cfa))
15918 {
15919 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15920 start_label, last_label, section);
15921
15922 list_tail = &(*list_tail)->dw_loc_next;
15923 last_cfa = next_cfa;
15924 start_label = last_label;
15925 }
15926 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15927 break;
15928
15929 case DW_CFA_advance_loc:
15930 /* The encoding is complex enough that we should never emit this. */
15931 gcc_unreachable ();
15932
15933 default:
15934 lookup_cfa_1 (cfi, &next_cfa, &remember);
15935 break;
15936 }
15937 if (ix + 1 == fde->dw_fde_switch_cfi_index)
15938 {
15939 if (!cfa_equal_p (&last_cfa, &next_cfa))
15940 {
15941 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15942 start_label, last_label, section);
15943
15944 list_tail = &(*list_tail)->dw_loc_next;
15945 last_cfa = next_cfa;
15946 start_label = last_label;
15947 }
15948 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15949 start_label, fde->dw_fde_end, section);
15950 list_tail = &(*list_tail)->dw_loc_next;
15951 start_label = last_label = fde->dw_fde_second_begin;
15952 }
15953 }
15954
15955 if (!cfa_equal_p (&last_cfa, &next_cfa))
15956 {
15957 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15958 start_label, last_label, section);
15959 list_tail = &(*list_tail)->dw_loc_next;
15960 start_label = last_label;
15961 }
15962
15963 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15964 start_label,
15965 fde->dw_fde_second_begin
15966 ? fde->dw_fde_second_end : fde->dw_fde_end,
15967 section);
15968
15969 if (list && list->dw_loc_next)
15970 gen_llsym (list);
15971
15972 return list;
15973 }
15974
15975 /* Compute a displacement from the "steady-state frame pointer" to the
15976 frame base (often the same as the CFA), and store it in
15977 frame_pointer_fb_offset. OFFSET is added to the displacement
15978 before the latter is negated. */
15979
15980 static void
15981 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15982 {
15983 rtx reg, elim;
15984
15985 #ifdef FRAME_POINTER_CFA_OFFSET
15986 reg = frame_pointer_rtx;
15987 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15988 #else
15989 reg = arg_pointer_rtx;
15990 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15991 #endif
15992
15993 elim = (ira_use_lra_p
15994 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15995 : eliminate_regs (reg, VOIDmode, NULL_RTX));
15996 if (GET_CODE (elim) == PLUS)
15997 {
15998 offset += INTVAL (XEXP (elim, 1));
15999 elim = XEXP (elim, 0);
16000 }
16001
16002 frame_pointer_fb_offset = -offset;
16003
16004 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16005 in which to eliminate. This is because it's stack pointer isn't
16006 directly accessible as a register within the ISA. To work around
16007 this, assume that while we cannot provide a proper value for
16008 frame_pointer_fb_offset, we won't need one either. */
16009 frame_pointer_fb_offset_valid
16010 = ((SUPPORTS_STACK_ALIGNMENT
16011 && (elim == hard_frame_pointer_rtx
16012 || elim == stack_pointer_rtx))
16013 || elim == (frame_pointer_needed
16014 ? hard_frame_pointer_rtx
16015 : stack_pointer_rtx));
16016 }
16017
16018 /* Generate a DW_AT_name attribute given some string value to be included as
16019 the value of the attribute. */
16020
16021 static void
16022 add_name_attribute (dw_die_ref die, const char *name_string)
16023 {
16024 if (name_string != NULL && *name_string != 0)
16025 {
16026 if (demangle_name_func)
16027 name_string = (*demangle_name_func) (name_string);
16028
16029 add_AT_string (die, DW_AT_name, name_string);
16030 }
16031 }
16032
16033 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16034 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16035 of TYPE accordingly.
16036
16037 ??? This is a temporary measure until after we're able to generate
16038 regular DWARF for the complex Ada type system. */
16039
16040 static void
16041 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16042 dw_die_ref context_die)
16043 {
16044 tree dtype;
16045 dw_die_ref dtype_die;
16046
16047 if (!lang_hooks.types.descriptive_type)
16048 return;
16049
16050 dtype = lang_hooks.types.descriptive_type (type);
16051 if (!dtype)
16052 return;
16053
16054 dtype_die = lookup_type_die (dtype);
16055 if (!dtype_die)
16056 {
16057 gen_type_die (dtype, context_die);
16058 dtype_die = lookup_type_die (dtype);
16059 gcc_assert (dtype_die);
16060 }
16061
16062 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16063 }
16064
16065 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16066
16067 static const char *
16068 comp_dir_string (void)
16069 {
16070 const char *wd;
16071 char *wd1;
16072 static const char *cached_wd = NULL;
16073
16074 if (cached_wd != NULL)
16075 return cached_wd;
16076
16077 wd = get_src_pwd ();
16078 if (wd == NULL)
16079 return NULL;
16080
16081 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16082 {
16083 int wdlen;
16084
16085 wdlen = strlen (wd);
16086 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16087 strcpy (wd1, wd);
16088 wd1 [wdlen] = DIR_SEPARATOR;
16089 wd1 [wdlen + 1] = 0;
16090 wd = wd1;
16091 }
16092
16093 cached_wd = remap_debug_filename (wd);
16094 return cached_wd;
16095 }
16096
16097 /* Generate a DW_AT_comp_dir attribute for DIE. */
16098
16099 static void
16100 add_comp_dir_attribute (dw_die_ref die)
16101 {
16102 const char * wd = comp_dir_string ();
16103 if (wd != NULL)
16104 add_AT_string (die, DW_AT_comp_dir, wd);
16105 }
16106
16107 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16108 default. */
16109
16110 static int
16111 lower_bound_default (void)
16112 {
16113 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16114 {
16115 case DW_LANG_C:
16116 case DW_LANG_C89:
16117 case DW_LANG_C99:
16118 case DW_LANG_C_plus_plus:
16119 case DW_LANG_ObjC:
16120 case DW_LANG_ObjC_plus_plus:
16121 case DW_LANG_Java:
16122 return 0;
16123 case DW_LANG_Fortran77:
16124 case DW_LANG_Fortran90:
16125 case DW_LANG_Fortran95:
16126 return 1;
16127 case DW_LANG_UPC:
16128 case DW_LANG_D:
16129 case DW_LANG_Python:
16130 return dwarf_version >= 4 ? 0 : -1;
16131 case DW_LANG_Ada95:
16132 case DW_LANG_Ada83:
16133 case DW_LANG_Cobol74:
16134 case DW_LANG_Cobol85:
16135 case DW_LANG_Pascal83:
16136 case DW_LANG_Modula2:
16137 case DW_LANG_PLI:
16138 return dwarf_version >= 4 ? 1 : -1;
16139 default:
16140 return -1;
16141 }
16142 }
16143
16144 /* Given a tree node describing an array bound (either lower or upper) output
16145 a representation for that bound. */
16146
16147 static void
16148 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16149 {
16150 switch (TREE_CODE (bound))
16151 {
16152 case ERROR_MARK:
16153 return;
16154
16155 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16156 case INTEGER_CST:
16157 {
16158 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16159 int dflt;
16160
16161 /* Use the default if possible. */
16162 if (bound_attr == DW_AT_lower_bound
16163 && host_integerp (bound, 0)
16164 && (dflt = lower_bound_default ()) != -1
16165 && tree_low_cst (bound, 0) == dflt)
16166 ;
16167
16168 /* Otherwise represent the bound as an unsigned value with the
16169 precision of its type. The precision and signedness of the
16170 type will be necessary to re-interpret it unambiguously. */
16171 else if (prec < HOST_BITS_PER_WIDE_INT)
16172 {
16173 unsigned HOST_WIDE_INT mask
16174 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16175 add_AT_unsigned (subrange_die, bound_attr,
16176 TREE_INT_CST_LOW (bound) & mask);
16177 }
16178 else if (prec == HOST_BITS_PER_WIDE_INT
16179 || TREE_INT_CST_HIGH (bound) == 0)
16180 add_AT_unsigned (subrange_die, bound_attr,
16181 TREE_INT_CST_LOW (bound));
16182 else
16183 add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16184 TREE_INT_CST_LOW (bound));
16185 }
16186 break;
16187
16188 CASE_CONVERT:
16189 case VIEW_CONVERT_EXPR:
16190 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16191 break;
16192
16193 case SAVE_EXPR:
16194 break;
16195
16196 case VAR_DECL:
16197 case PARM_DECL:
16198 case RESULT_DECL:
16199 {
16200 dw_die_ref decl_die = lookup_decl_die (bound);
16201
16202 /* ??? Can this happen, or should the variable have been bound
16203 first? Probably it can, since I imagine that we try to create
16204 the types of parameters in the order in which they exist in
16205 the list, and won't have created a forward reference to a
16206 later parameter. */
16207 if (decl_die != NULL)
16208 {
16209 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16210 break;
16211 }
16212 }
16213 /* FALLTHRU */
16214
16215 default:
16216 {
16217 /* Otherwise try to create a stack operation procedure to
16218 evaluate the value of the array bound. */
16219
16220 dw_die_ref ctx, decl_die;
16221 dw_loc_list_ref list;
16222
16223 list = loc_list_from_tree (bound, 2);
16224 if (list == NULL || single_element_loc_list_p (list))
16225 {
16226 /* If DW_AT_*bound is not a reference nor constant, it is
16227 a DWARF expression rather than location description.
16228 For that loc_list_from_tree (bound, 0) is needed.
16229 If that fails to give a single element list,
16230 fall back to outputting this as a reference anyway. */
16231 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16232 if (list2 && single_element_loc_list_p (list2))
16233 {
16234 add_AT_loc (subrange_die, bound_attr, list2->expr);
16235 break;
16236 }
16237 }
16238 if (list == NULL)
16239 break;
16240
16241 if (current_function_decl == 0)
16242 ctx = comp_unit_die ();
16243 else
16244 ctx = lookup_decl_die (current_function_decl);
16245
16246 decl_die = new_die (DW_TAG_variable, ctx, bound);
16247 add_AT_flag (decl_die, DW_AT_artificial, 1);
16248 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16249 add_AT_location_description (decl_die, DW_AT_location, list);
16250 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16251 break;
16252 }
16253 }
16254 }
16255
16256 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16257 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16258 Note that the block of subscript information for an array type also
16259 includes information about the element type of the given array type. */
16260
16261 static void
16262 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16263 {
16264 unsigned dimension_number;
16265 tree lower, upper;
16266 dw_die_ref subrange_die;
16267
16268 for (dimension_number = 0;
16269 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16270 type = TREE_TYPE (type), dimension_number++)
16271 {
16272 tree domain = TYPE_DOMAIN (type);
16273
16274 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16275 break;
16276
16277 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16278 and (in GNU C only) variable bounds. Handle all three forms
16279 here. */
16280 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16281 if (domain)
16282 {
16283 /* We have an array type with specified bounds. */
16284 lower = TYPE_MIN_VALUE (domain);
16285 upper = TYPE_MAX_VALUE (domain);
16286
16287 /* Define the index type. */
16288 if (TREE_TYPE (domain))
16289 {
16290 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16291 TREE_TYPE field. We can't emit debug info for this
16292 because it is an unnamed integral type. */
16293 if (TREE_CODE (domain) == INTEGER_TYPE
16294 && TYPE_NAME (domain) == NULL_TREE
16295 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16296 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16297 ;
16298 else
16299 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16300 type_die);
16301 }
16302
16303 /* ??? If upper is NULL, the array has unspecified length,
16304 but it does have a lower bound. This happens with Fortran
16305 dimension arr(N:*)
16306 Since the debugger is definitely going to need to know N
16307 to produce useful results, go ahead and output the lower
16308 bound solo, and hope the debugger can cope. */
16309
16310 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16311 if (upper)
16312 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16313 }
16314
16315 /* Otherwise we have an array type with an unspecified length. The
16316 DWARF-2 spec does not say how to handle this; let's just leave out the
16317 bounds. */
16318 }
16319 }
16320
16321 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16322
16323 static void
16324 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16325 {
16326 dw_die_ref decl_die;
16327 HOST_WIDE_INT size;
16328
16329 switch (TREE_CODE (tree_node))
16330 {
16331 case ERROR_MARK:
16332 size = 0;
16333 break;
16334 case ENUMERAL_TYPE:
16335 case RECORD_TYPE:
16336 case UNION_TYPE:
16337 case QUAL_UNION_TYPE:
16338 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16339 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16340 {
16341 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16342 return;
16343 }
16344 size = int_size_in_bytes (tree_node);
16345 break;
16346 case FIELD_DECL:
16347 /* For a data member of a struct or union, the DW_AT_byte_size is
16348 generally given as the number of bytes normally allocated for an
16349 object of the *declared* type of the member itself. This is true
16350 even for bit-fields. */
16351 size = int_size_in_bytes (field_type (tree_node));
16352 break;
16353 default:
16354 gcc_unreachable ();
16355 }
16356
16357 /* Note that `size' might be -1 when we get to this point. If it is, that
16358 indicates that the byte size of the entity in question is variable. We
16359 have no good way of expressing this fact in Dwarf at the present time,
16360 when location description was not used by the caller code instead. */
16361 if (size >= 0)
16362 add_AT_unsigned (die, DW_AT_byte_size, size);
16363 }
16364
16365 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16366 which specifies the distance in bits from the highest order bit of the
16367 "containing object" for the bit-field to the highest order bit of the
16368 bit-field itself.
16369
16370 For any given bit-field, the "containing object" is a hypothetical object
16371 (of some integral or enum type) within which the given bit-field lives. The
16372 type of this hypothetical "containing object" is always the same as the
16373 declared type of the individual bit-field itself. The determination of the
16374 exact location of the "containing object" for a bit-field is rather
16375 complicated. It's handled by the `field_byte_offset' function (above).
16376
16377 Note that it is the size (in bytes) of the hypothetical "containing object"
16378 which will be given in the DW_AT_byte_size attribute for this bit-field.
16379 (See `byte_size_attribute' above). */
16380
16381 static inline void
16382 add_bit_offset_attribute (dw_die_ref die, tree decl)
16383 {
16384 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16385 tree type = DECL_BIT_FIELD_TYPE (decl);
16386 HOST_WIDE_INT bitpos_int;
16387 HOST_WIDE_INT highest_order_object_bit_offset;
16388 HOST_WIDE_INT highest_order_field_bit_offset;
16389 HOST_WIDE_INT bit_offset;
16390
16391 /* Must be a field and a bit field. */
16392 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16393
16394 /* We can't yet handle bit-fields whose offsets are variable, so if we
16395 encounter such things, just return without generating any attribute
16396 whatsoever. Likewise for variable or too large size. */
16397 if (! host_integerp (bit_position (decl), 0)
16398 || ! host_integerp (DECL_SIZE (decl), 1))
16399 return;
16400
16401 bitpos_int = int_bit_position (decl);
16402
16403 /* Note that the bit offset is always the distance (in bits) from the
16404 highest-order bit of the "containing object" to the highest-order bit of
16405 the bit-field itself. Since the "high-order end" of any object or field
16406 is different on big-endian and little-endian machines, the computation
16407 below must take account of these differences. */
16408 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16409 highest_order_field_bit_offset = bitpos_int;
16410
16411 if (! BYTES_BIG_ENDIAN)
16412 {
16413 highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16414 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16415 }
16416
16417 bit_offset
16418 = (! BYTES_BIG_ENDIAN
16419 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16420 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16421
16422 if (bit_offset < 0)
16423 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16424 else
16425 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16426 }
16427
16428 /* For a FIELD_DECL node which represents a bit field, output an attribute
16429 which specifies the length in bits of the given field. */
16430
16431 static inline void
16432 add_bit_size_attribute (dw_die_ref die, tree decl)
16433 {
16434 /* Must be a field and a bit field. */
16435 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16436 && DECL_BIT_FIELD_TYPE (decl));
16437
16438 if (host_integerp (DECL_SIZE (decl), 1))
16439 add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16440 }
16441
16442 /* If the compiled language is ANSI C, then add a 'prototyped'
16443 attribute, if arg types are given for the parameters of a function. */
16444
16445 static inline void
16446 add_prototyped_attribute (dw_die_ref die, tree func_type)
16447 {
16448 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16449 && prototype_p (func_type))
16450 add_AT_flag (die, DW_AT_prototyped, 1);
16451 }
16452
16453 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16454 by looking in either the type declaration or object declaration
16455 equate table. */
16456
16457 static inline dw_die_ref
16458 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16459 {
16460 dw_die_ref origin_die = NULL;
16461
16462 if (TREE_CODE (origin) != FUNCTION_DECL)
16463 {
16464 /* We may have gotten separated from the block for the inlined
16465 function, if we're in an exception handler or some such; make
16466 sure that the abstract function has been written out.
16467
16468 Doing this for nested functions is wrong, however; functions are
16469 distinct units, and our context might not even be inline. */
16470 tree fn = origin;
16471
16472 if (TYPE_P (fn))
16473 fn = TYPE_STUB_DECL (fn);
16474
16475 fn = decl_function_context (fn);
16476 if (fn)
16477 dwarf2out_abstract_function (fn);
16478 }
16479
16480 if (DECL_P (origin))
16481 origin_die = lookup_decl_die (origin);
16482 else if (TYPE_P (origin))
16483 origin_die = lookup_type_die (origin);
16484
16485 /* XXX: Functions that are never lowered don't always have correct block
16486 trees (in the case of java, they simply have no block tree, in some other
16487 languages). For these functions, there is nothing we can really do to
16488 output correct debug info for inlined functions in all cases. Rather
16489 than die, we'll just produce deficient debug info now, in that we will
16490 have variables without a proper abstract origin. In the future, when all
16491 functions are lowered, we should re-add a gcc_assert (origin_die)
16492 here. */
16493
16494 if (origin_die)
16495 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16496 return origin_die;
16497 }
16498
16499 /* We do not currently support the pure_virtual attribute. */
16500
16501 static inline void
16502 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16503 {
16504 if (DECL_VINDEX (func_decl))
16505 {
16506 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16507
16508 if (host_integerp (DECL_VINDEX (func_decl), 0))
16509 add_AT_loc (die, DW_AT_vtable_elem_location,
16510 new_loc_descr (DW_OP_constu,
16511 tree_low_cst (DECL_VINDEX (func_decl), 0),
16512 0));
16513
16514 /* GNU extension: Record what type this method came from originally. */
16515 if (debug_info_level > DINFO_LEVEL_TERSE
16516 && DECL_CONTEXT (func_decl))
16517 add_AT_die_ref (die, DW_AT_containing_type,
16518 lookup_type_die (DECL_CONTEXT (func_decl)));
16519 }
16520 }
16521 \f
16522 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16523 given decl. This used to be a vendor extension until after DWARF 4
16524 standardized it. */
16525
16526 static void
16527 add_linkage_attr (dw_die_ref die, tree decl)
16528 {
16529 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16530
16531 /* Mimic what assemble_name_raw does with a leading '*'. */
16532 if (name[0] == '*')
16533 name = &name[1];
16534
16535 if (dwarf_version >= 4)
16536 add_AT_string (die, DW_AT_linkage_name, name);
16537 else
16538 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16539 }
16540
16541 /* Add source coordinate attributes for the given decl. */
16542
16543 static void
16544 add_src_coords_attributes (dw_die_ref die, tree decl)
16545 {
16546 expanded_location s;
16547
16548 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16549 return;
16550 s = expand_location (DECL_SOURCE_LOCATION (decl));
16551 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16552 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16553 }
16554
16555 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16556
16557 static void
16558 add_linkage_name (dw_die_ref die, tree decl)
16559 {
16560 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16561 && TREE_PUBLIC (decl)
16562 && !DECL_ABSTRACT (decl)
16563 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16564 && die->die_tag != DW_TAG_member)
16565 {
16566 /* Defer until we have an assembler name set. */
16567 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16568 {
16569 limbo_die_node *asm_name;
16570
16571 asm_name = ggc_alloc_cleared_limbo_die_node ();
16572 asm_name->die = die;
16573 asm_name->created_for = decl;
16574 asm_name->next = deferred_asm_name;
16575 deferred_asm_name = asm_name;
16576 }
16577 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16578 add_linkage_attr (die, decl);
16579 }
16580 }
16581
16582 /* Add a DW_AT_name attribute and source coordinate attribute for the
16583 given decl, but only if it actually has a name. */
16584
16585 static void
16586 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16587 {
16588 tree decl_name;
16589
16590 decl_name = DECL_NAME (decl);
16591 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16592 {
16593 const char *name = dwarf2_name (decl, 0);
16594 if (name)
16595 add_name_attribute (die, name);
16596 if (! DECL_ARTIFICIAL (decl))
16597 add_src_coords_attributes (die, decl);
16598
16599 add_linkage_name (die, decl);
16600 }
16601
16602 #ifdef VMS_DEBUGGING_INFO
16603 /* Get the function's name, as described by its RTL. This may be different
16604 from the DECL_NAME name used in the source file. */
16605 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16606 {
16607 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16608 XEXP (DECL_RTL (decl), 0), false);
16609 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16610 }
16611 #endif /* VMS_DEBUGGING_INFO */
16612 }
16613
16614 #ifdef VMS_DEBUGGING_INFO
16615 /* Output the debug main pointer die for VMS */
16616
16617 void
16618 dwarf2out_vms_debug_main_pointer (void)
16619 {
16620 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16621 dw_die_ref die;
16622
16623 /* Allocate the VMS debug main subprogram die. */
16624 die = ggc_alloc_cleared_die_node ();
16625 die->die_tag = DW_TAG_subprogram;
16626 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16627 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16628 current_function_funcdef_no);
16629 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16630
16631 /* Make it the first child of comp_unit_die (). */
16632 die->die_parent = comp_unit_die ();
16633 if (comp_unit_die ()->die_child)
16634 {
16635 die->die_sib = comp_unit_die ()->die_child->die_sib;
16636 comp_unit_die ()->die_child->die_sib = die;
16637 }
16638 else
16639 {
16640 die->die_sib = die;
16641 comp_unit_die ()->die_child = die;
16642 }
16643 }
16644 #endif /* VMS_DEBUGGING_INFO */
16645
16646 /* Push a new declaration scope. */
16647
16648 static void
16649 push_decl_scope (tree scope)
16650 {
16651 vec_safe_push (decl_scope_table, scope);
16652 }
16653
16654 /* Pop a declaration scope. */
16655
16656 static inline void
16657 pop_decl_scope (void)
16658 {
16659 decl_scope_table->pop ();
16660 }
16661
16662 /* walk_tree helper function for uses_local_type, below. */
16663
16664 static tree
16665 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16666 {
16667 if (!TYPE_P (*tp))
16668 *walk_subtrees = 0;
16669 else
16670 {
16671 tree name = TYPE_NAME (*tp);
16672 if (name && DECL_P (name) && decl_function_context (name))
16673 return *tp;
16674 }
16675 return NULL_TREE;
16676 }
16677
16678 /* If TYPE involves a function-local type (including a local typedef to a
16679 non-local type), returns that type; otherwise returns NULL_TREE. */
16680
16681 static tree
16682 uses_local_type (tree type)
16683 {
16684 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16685 return used;
16686 }
16687
16688 /* Return the DIE for the scope that immediately contains this type.
16689 Non-named types that do not involve a function-local type get global
16690 scope. Named types nested in namespaces or other types get their
16691 containing scope. All other types (i.e. function-local named types) get
16692 the current active scope. */
16693
16694 static dw_die_ref
16695 scope_die_for (tree t, dw_die_ref context_die)
16696 {
16697 dw_die_ref scope_die = NULL;
16698 tree containing_scope;
16699
16700 /* Non-types always go in the current scope. */
16701 gcc_assert (TYPE_P (t));
16702
16703 /* Use the scope of the typedef, rather than the scope of the type
16704 it refers to. */
16705 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16706 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16707 else
16708 containing_scope = TYPE_CONTEXT (t);
16709
16710 /* Use the containing namespace if there is one. */
16711 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16712 {
16713 if (context_die == lookup_decl_die (containing_scope))
16714 /* OK */;
16715 else if (debug_info_level > DINFO_LEVEL_TERSE)
16716 context_die = get_context_die (containing_scope);
16717 else
16718 containing_scope = NULL_TREE;
16719 }
16720
16721 /* Ignore function type "scopes" from the C frontend. They mean that
16722 a tagged type is local to a parmlist of a function declarator, but
16723 that isn't useful to DWARF. */
16724 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16725 containing_scope = NULL_TREE;
16726
16727 if (SCOPE_FILE_SCOPE_P (containing_scope))
16728 {
16729 /* If T uses a local type keep it local as well, to avoid references
16730 to function-local DIEs from outside the function. */
16731 if (current_function_decl && uses_local_type (t))
16732 scope_die = context_die;
16733 else
16734 scope_die = comp_unit_die ();
16735 }
16736 else if (TYPE_P (containing_scope))
16737 {
16738 /* For types, we can just look up the appropriate DIE. */
16739 if (debug_info_level > DINFO_LEVEL_TERSE)
16740 scope_die = get_context_die (containing_scope);
16741 else
16742 {
16743 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16744 if (scope_die == NULL)
16745 scope_die = comp_unit_die ();
16746 }
16747 }
16748 else
16749 scope_die = context_die;
16750
16751 return scope_die;
16752 }
16753
16754 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16755
16756 static inline int
16757 local_scope_p (dw_die_ref context_die)
16758 {
16759 for (; context_die; context_die = context_die->die_parent)
16760 if (context_die->die_tag == DW_TAG_inlined_subroutine
16761 || context_die->die_tag == DW_TAG_subprogram)
16762 return 1;
16763
16764 return 0;
16765 }
16766
16767 /* Returns nonzero if CONTEXT_DIE is a class. */
16768
16769 static inline int
16770 class_scope_p (dw_die_ref context_die)
16771 {
16772 return (context_die
16773 && (context_die->die_tag == DW_TAG_structure_type
16774 || context_die->die_tag == DW_TAG_class_type
16775 || context_die->die_tag == DW_TAG_interface_type
16776 || context_die->die_tag == DW_TAG_union_type));
16777 }
16778
16779 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16780 whether or not to treat a DIE in this context as a declaration. */
16781
16782 static inline int
16783 class_or_namespace_scope_p (dw_die_ref context_die)
16784 {
16785 return (class_scope_p (context_die)
16786 || (context_die && context_die->die_tag == DW_TAG_namespace));
16787 }
16788
16789 /* Many forms of DIEs require a "type description" attribute. This
16790 routine locates the proper "type descriptor" die for the type given
16791 by 'type', and adds a DW_AT_type attribute below the given die. */
16792
16793 static void
16794 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16795 int decl_volatile, dw_die_ref context_die)
16796 {
16797 enum tree_code code = TREE_CODE (type);
16798 dw_die_ref type_die = NULL;
16799
16800 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16801 or fixed-point type, use the inner type. This is because we have no
16802 support for unnamed types in base_type_die. This can happen if this is
16803 an Ada subrange type. Correct solution is emit a subrange type die. */
16804 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16805 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16806 type = TREE_TYPE (type), code = TREE_CODE (type);
16807
16808 if (code == ERROR_MARK
16809 /* Handle a special case. For functions whose return type is void, we
16810 generate *no* type attribute. (Note that no object may have type
16811 `void', so this only applies to function return types). */
16812 || code == VOID_TYPE)
16813 return;
16814
16815 type_die = modified_type_die (type,
16816 decl_const || TYPE_READONLY (type),
16817 decl_volatile || TYPE_VOLATILE (type),
16818 context_die);
16819
16820 if (type_die != NULL)
16821 add_AT_die_ref (object_die, DW_AT_type, type_die);
16822 }
16823
16824 /* Given an object die, add the calling convention attribute for the
16825 function call type. */
16826 static void
16827 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16828 {
16829 enum dwarf_calling_convention value = DW_CC_normal;
16830
16831 value = ((enum dwarf_calling_convention)
16832 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16833
16834 if (is_fortran ()
16835 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16836 {
16837 /* DWARF 2 doesn't provide a way to identify a program's source-level
16838 entry point. DW_AT_calling_convention attributes are only meant
16839 to describe functions' calling conventions. However, lacking a
16840 better way to signal the Fortran main program, we used this for
16841 a long time, following existing custom. Now, DWARF 4 has
16842 DW_AT_main_subprogram, which we add below, but some tools still
16843 rely on the old way, which we thus keep. */
16844 value = DW_CC_program;
16845
16846 if (dwarf_version >= 4 || !dwarf_strict)
16847 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16848 }
16849
16850 /* Only add the attribute if the backend requests it, and
16851 is not DW_CC_normal. */
16852 if (value && (value != DW_CC_normal))
16853 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16854 }
16855
16856 /* Given a tree pointer to a struct, class, union, or enum type node, return
16857 a pointer to the (string) tag name for the given type, or zero if the type
16858 was declared without a tag. */
16859
16860 static const char *
16861 type_tag (const_tree type)
16862 {
16863 const char *name = 0;
16864
16865 if (TYPE_NAME (type) != 0)
16866 {
16867 tree t = 0;
16868
16869 /* Find the IDENTIFIER_NODE for the type name. */
16870 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16871 && !TYPE_NAMELESS (type))
16872 t = TYPE_NAME (type);
16873
16874 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16875 a TYPE_DECL node, regardless of whether or not a `typedef' was
16876 involved. */
16877 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16878 && ! DECL_IGNORED_P (TYPE_NAME (type)))
16879 {
16880 /* We want to be extra verbose. Don't call dwarf_name if
16881 DECL_NAME isn't set. The default hook for decl_printable_name
16882 doesn't like that, and in this context it's correct to return
16883 0, instead of "<anonymous>" or the like. */
16884 if (DECL_NAME (TYPE_NAME (type))
16885 && !DECL_NAMELESS (TYPE_NAME (type)))
16886 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16887 }
16888
16889 /* Now get the name as a string, or invent one. */
16890 if (!name && t != 0)
16891 name = IDENTIFIER_POINTER (t);
16892 }
16893
16894 return (name == 0 || *name == '\0') ? 0 : name;
16895 }
16896
16897 /* Return the type associated with a data member, make a special check
16898 for bit field types. */
16899
16900 static inline tree
16901 member_declared_type (const_tree member)
16902 {
16903 return (DECL_BIT_FIELD_TYPE (member)
16904 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16905 }
16906
16907 /* Get the decl's label, as described by its RTL. This may be different
16908 from the DECL_NAME name used in the source file. */
16909
16910 #if 0
16911 static const char *
16912 decl_start_label (tree decl)
16913 {
16914 rtx x;
16915 const char *fnname;
16916
16917 x = DECL_RTL (decl);
16918 gcc_assert (MEM_P (x));
16919
16920 x = XEXP (x, 0);
16921 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16922
16923 fnname = XSTR (x, 0);
16924 return fnname;
16925 }
16926 #endif
16927 \f
16928 /* These routines generate the internal representation of the DIE's for
16929 the compilation unit. Debugging information is collected by walking
16930 the declaration trees passed in from dwarf2out_decl(). */
16931
16932 static void
16933 gen_array_type_die (tree type, dw_die_ref context_die)
16934 {
16935 dw_die_ref scope_die = scope_die_for (type, context_die);
16936 dw_die_ref array_die;
16937
16938 /* GNU compilers represent multidimensional array types as sequences of one
16939 dimensional array types whose element types are themselves array types.
16940 We sometimes squish that down to a single array_type DIE with multiple
16941 subscripts in the Dwarf debugging info. The draft Dwarf specification
16942 say that we are allowed to do this kind of compression in C, because
16943 there is no difference between an array of arrays and a multidimensional
16944 array. We don't do this for Ada to remain as close as possible to the
16945 actual representation, which is especially important against the language
16946 flexibilty wrt arrays of variable size. */
16947
16948 bool collapse_nested_arrays = !is_ada ();
16949 tree element_type;
16950
16951 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16952 DW_TAG_string_type doesn't have DW_AT_type attribute). */
16953 if (TYPE_STRING_FLAG (type)
16954 && TREE_CODE (type) == ARRAY_TYPE
16955 && is_fortran ()
16956 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16957 {
16958 HOST_WIDE_INT size;
16959
16960 array_die = new_die (DW_TAG_string_type, scope_die, type);
16961 add_name_attribute (array_die, type_tag (type));
16962 equate_type_number_to_die (type, array_die);
16963 size = int_size_in_bytes (type);
16964 if (size >= 0)
16965 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16966 else if (TYPE_DOMAIN (type) != NULL_TREE
16967 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16968 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16969 {
16970 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16971 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16972
16973 size = int_size_in_bytes (TREE_TYPE (szdecl));
16974 if (loc && size > 0)
16975 {
16976 add_AT_location_description (array_die, DW_AT_string_length, loc);
16977 if (size != DWARF2_ADDR_SIZE)
16978 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16979 }
16980 }
16981 return;
16982 }
16983
16984 array_die = new_die (DW_TAG_array_type, scope_die, type);
16985 add_name_attribute (array_die, type_tag (type));
16986 equate_type_number_to_die (type, array_die);
16987
16988 if (TREE_CODE (type) == VECTOR_TYPE)
16989 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16990
16991 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
16992 if (is_fortran ()
16993 && TREE_CODE (type) == ARRAY_TYPE
16994 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16995 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16996 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16997
16998 #if 0
16999 /* We default the array ordering. SDB will probably do
17000 the right things even if DW_AT_ordering is not present. It's not even
17001 an issue until we start to get into multidimensional arrays anyway. If
17002 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17003 then we'll have to put the DW_AT_ordering attribute back in. (But if
17004 and when we find out that we need to put these in, we will only do so
17005 for multidimensional arrays. */
17006 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17007 #endif
17008
17009 if (TREE_CODE (type) == VECTOR_TYPE)
17010 {
17011 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17012 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17013 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17014 add_bound_info (subrange_die, DW_AT_upper_bound,
17015 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17016 }
17017 else
17018 add_subscript_info (array_die, type, collapse_nested_arrays);
17019
17020 /* Add representation of the type of the elements of this array type and
17021 emit the corresponding DIE if we haven't done it already. */
17022 element_type = TREE_TYPE (type);
17023 if (collapse_nested_arrays)
17024 while (TREE_CODE (element_type) == ARRAY_TYPE)
17025 {
17026 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17027 break;
17028 element_type = TREE_TYPE (element_type);
17029 }
17030
17031 add_type_attribute (array_die, element_type, 0, 0, context_die);
17032
17033 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17034 if (TYPE_ARTIFICIAL (type))
17035 add_AT_flag (array_die, DW_AT_artificial, 1);
17036
17037 if (get_AT (array_die, DW_AT_name))
17038 add_pubtype (type, array_die);
17039 }
17040
17041 static dw_loc_descr_ref
17042 descr_info_loc (tree val, tree base_decl)
17043 {
17044 HOST_WIDE_INT size;
17045 dw_loc_descr_ref loc, loc2;
17046 enum dwarf_location_atom op;
17047
17048 if (val == base_decl)
17049 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17050
17051 switch (TREE_CODE (val))
17052 {
17053 CASE_CONVERT:
17054 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17055 case VAR_DECL:
17056 return loc_descriptor_from_tree (val, 0);
17057 case INTEGER_CST:
17058 if (host_integerp (val, 0))
17059 return int_loc_descriptor (tree_low_cst (val, 0));
17060 break;
17061 case INDIRECT_REF:
17062 size = int_size_in_bytes (TREE_TYPE (val));
17063 if (size < 0)
17064 break;
17065 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17066 if (!loc)
17067 break;
17068 if (size == DWARF2_ADDR_SIZE)
17069 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17070 else
17071 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17072 return loc;
17073 case POINTER_PLUS_EXPR:
17074 case PLUS_EXPR:
17075 if (host_integerp (TREE_OPERAND (val, 1), 1)
17076 && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17077 < 16384)
17078 {
17079 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17080 if (!loc)
17081 break;
17082 loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17083 }
17084 else
17085 {
17086 op = DW_OP_plus;
17087 do_binop:
17088 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17089 if (!loc)
17090 break;
17091 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17092 if (!loc2)
17093 break;
17094 add_loc_descr (&loc, loc2);
17095 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17096 }
17097 return loc;
17098 case MINUS_EXPR:
17099 op = DW_OP_minus;
17100 goto do_binop;
17101 case MULT_EXPR:
17102 op = DW_OP_mul;
17103 goto do_binop;
17104 case EQ_EXPR:
17105 op = DW_OP_eq;
17106 goto do_binop;
17107 case NE_EXPR:
17108 op = DW_OP_ne;
17109 goto do_binop;
17110 default:
17111 break;
17112 }
17113 return NULL;
17114 }
17115
17116 static void
17117 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17118 tree val, tree base_decl)
17119 {
17120 dw_loc_descr_ref loc;
17121
17122 if (host_integerp (val, 0))
17123 {
17124 add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17125 return;
17126 }
17127
17128 loc = descr_info_loc (val, base_decl);
17129 if (!loc)
17130 return;
17131
17132 add_AT_loc (die, attr, loc);
17133 }
17134
17135 /* This routine generates DIE for array with hidden descriptor, details
17136 are filled into *info by a langhook. */
17137
17138 static void
17139 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17140 dw_die_ref context_die)
17141 {
17142 dw_die_ref scope_die = scope_die_for (type, context_die);
17143 dw_die_ref array_die;
17144 int dim;
17145
17146 array_die = new_die (DW_TAG_array_type, scope_die, type);
17147 add_name_attribute (array_die, type_tag (type));
17148 equate_type_number_to_die (type, array_die);
17149
17150 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17151 if (is_fortran ()
17152 && info->ndimensions >= 2)
17153 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17154
17155 if (info->data_location)
17156 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17157 info->base_decl);
17158 if (info->associated)
17159 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17160 info->base_decl);
17161 if (info->allocated)
17162 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17163 info->base_decl);
17164
17165 for (dim = 0; dim < info->ndimensions; dim++)
17166 {
17167 dw_die_ref subrange_die
17168 = new_die (DW_TAG_subrange_type, array_die, NULL);
17169
17170 if (info->dimen[dim].lower_bound)
17171 {
17172 /* If it is the default value, omit it. */
17173 int dflt;
17174
17175 if (host_integerp (info->dimen[dim].lower_bound, 0)
17176 && (dflt = lower_bound_default ()) != -1
17177 && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
17178 ;
17179 else
17180 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17181 info->dimen[dim].lower_bound,
17182 info->base_decl);
17183 }
17184 if (info->dimen[dim].upper_bound)
17185 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17186 info->dimen[dim].upper_bound,
17187 info->base_decl);
17188 if (info->dimen[dim].stride)
17189 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17190 info->dimen[dim].stride,
17191 info->base_decl);
17192 }
17193
17194 gen_type_die (info->element_type, context_die);
17195 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17196
17197 if (get_AT (array_die, DW_AT_name))
17198 add_pubtype (type, array_die);
17199 }
17200
17201 #if 0
17202 static void
17203 gen_entry_point_die (tree decl, dw_die_ref context_die)
17204 {
17205 tree origin = decl_ultimate_origin (decl);
17206 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17207
17208 if (origin != NULL)
17209 add_abstract_origin_attribute (decl_die, origin);
17210 else
17211 {
17212 add_name_and_src_coords_attributes (decl_die, decl);
17213 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17214 0, 0, context_die);
17215 }
17216
17217 if (DECL_ABSTRACT (decl))
17218 equate_decl_number_to_die (decl, decl_die);
17219 else
17220 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17221 }
17222 #endif
17223
17224 /* Walk through the list of incomplete types again, trying once more to
17225 emit full debugging info for them. */
17226
17227 static void
17228 retry_incomplete_types (void)
17229 {
17230 int i;
17231
17232 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17233 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17234 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17235 }
17236
17237 /* Determine what tag to use for a record type. */
17238
17239 static enum dwarf_tag
17240 record_type_tag (tree type)
17241 {
17242 if (! lang_hooks.types.classify_record)
17243 return DW_TAG_structure_type;
17244
17245 switch (lang_hooks.types.classify_record (type))
17246 {
17247 case RECORD_IS_STRUCT:
17248 return DW_TAG_structure_type;
17249
17250 case RECORD_IS_CLASS:
17251 return DW_TAG_class_type;
17252
17253 case RECORD_IS_INTERFACE:
17254 if (dwarf_version >= 3 || !dwarf_strict)
17255 return DW_TAG_interface_type;
17256 return DW_TAG_structure_type;
17257
17258 default:
17259 gcc_unreachable ();
17260 }
17261 }
17262
17263 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17264 include all of the information about the enumeration values also. Each
17265 enumerated type name/value is listed as a child of the enumerated type
17266 DIE. */
17267
17268 static dw_die_ref
17269 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17270 {
17271 dw_die_ref type_die = lookup_type_die (type);
17272
17273 if (type_die == NULL)
17274 {
17275 type_die = new_die (DW_TAG_enumeration_type,
17276 scope_die_for (type, context_die), type);
17277 equate_type_number_to_die (type, type_die);
17278 add_name_attribute (type_die, type_tag (type));
17279 if (dwarf_version >= 4 || !dwarf_strict)
17280 {
17281 if (ENUM_IS_SCOPED (type))
17282 add_AT_flag (type_die, DW_AT_enum_class, 1);
17283 if (ENUM_IS_OPAQUE (type))
17284 add_AT_flag (type_die, DW_AT_declaration, 1);
17285 }
17286 }
17287 else if (! TYPE_SIZE (type))
17288 return type_die;
17289 else
17290 remove_AT (type_die, DW_AT_declaration);
17291
17292 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17293 given enum type is incomplete, do not generate the DW_AT_byte_size
17294 attribute or the DW_AT_element_list attribute. */
17295 if (TYPE_SIZE (type))
17296 {
17297 tree link;
17298
17299 TREE_ASM_WRITTEN (type) = 1;
17300 add_byte_size_attribute (type_die, type);
17301 if (TYPE_STUB_DECL (type) != NULL_TREE)
17302 {
17303 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17304 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17305 }
17306
17307 /* If the first reference to this type was as the return type of an
17308 inline function, then it may not have a parent. Fix this now. */
17309 if (type_die->die_parent == NULL)
17310 add_child_die (scope_die_for (type, context_die), type_die);
17311
17312 for (link = TYPE_VALUES (type);
17313 link != NULL; link = TREE_CHAIN (link))
17314 {
17315 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17316 tree value = TREE_VALUE (link);
17317
17318 add_name_attribute (enum_die,
17319 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17320
17321 if (TREE_CODE (value) == CONST_DECL)
17322 value = DECL_INITIAL (value);
17323
17324 if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value)))
17325 && (simple_type_size_in_bits (TREE_TYPE (value))
17326 <= HOST_BITS_PER_WIDE_INT || host_integerp (value, 0)))
17327 /* DWARF2 does not provide a way of indicating whether or
17328 not enumeration constants are signed or unsigned. GDB
17329 always assumes the values are signed, so we output all
17330 values as if they were signed. That means that
17331 enumeration constants with very large unsigned values
17332 will appear to have negative values in the debugger.
17333
17334 TODO: the above comment is wrong, DWARF2 does provide
17335 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17336 This should be re-worked to use correct signed/unsigned
17337 int/double tags for all cases, instead of always treating as
17338 signed. */
17339 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17340 else
17341 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17342 that here. */
17343 add_AT_double (enum_die, DW_AT_const_value,
17344 TREE_INT_CST_HIGH (value), TREE_INT_CST_LOW (value));
17345 }
17346
17347 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17348 if (TYPE_ARTIFICIAL (type))
17349 add_AT_flag (type_die, DW_AT_artificial, 1);
17350 }
17351 else
17352 add_AT_flag (type_die, DW_AT_declaration, 1);
17353
17354 add_pubtype (type, type_die);
17355
17356 return type_die;
17357 }
17358
17359 /* Generate a DIE to represent either a real live formal parameter decl or to
17360 represent just the type of some formal parameter position in some function
17361 type.
17362
17363 Note that this routine is a bit unusual because its argument may be a
17364 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17365 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17366 node. If it's the former then this function is being called to output a
17367 DIE to represent a formal parameter object (or some inlining thereof). If
17368 it's the latter, then this function is only being called to output a
17369 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17370 argument type of some subprogram type.
17371 If EMIT_NAME_P is true, name and source coordinate attributes
17372 are emitted. */
17373
17374 static dw_die_ref
17375 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17376 dw_die_ref context_die)
17377 {
17378 tree node_or_origin = node ? node : origin;
17379 tree ultimate_origin;
17380 dw_die_ref parm_die
17381 = new_die (DW_TAG_formal_parameter, context_die, node);
17382
17383 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17384 {
17385 case tcc_declaration:
17386 ultimate_origin = decl_ultimate_origin (node_or_origin);
17387 if (node || ultimate_origin)
17388 origin = ultimate_origin;
17389 if (origin != NULL)
17390 add_abstract_origin_attribute (parm_die, origin);
17391 else if (emit_name_p)
17392 add_name_and_src_coords_attributes (parm_die, node);
17393 if (origin == NULL
17394 || (! DECL_ABSTRACT (node_or_origin)
17395 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17396 decl_function_context
17397 (node_or_origin))))
17398 {
17399 tree type = TREE_TYPE (node_or_origin);
17400 if (decl_by_reference_p (node_or_origin))
17401 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17402 context_die);
17403 else
17404 add_type_attribute (parm_die, type,
17405 TREE_READONLY (node_or_origin),
17406 TREE_THIS_VOLATILE (node_or_origin),
17407 context_die);
17408 }
17409 if (origin == NULL && DECL_ARTIFICIAL (node))
17410 add_AT_flag (parm_die, DW_AT_artificial, 1);
17411
17412 if (node && node != origin)
17413 equate_decl_number_to_die (node, parm_die);
17414 if (! DECL_ABSTRACT (node_or_origin))
17415 add_location_or_const_value_attribute (parm_die, node_or_origin,
17416 node == NULL, DW_AT_location);
17417
17418 break;
17419
17420 case tcc_type:
17421 /* We were called with some kind of a ..._TYPE node. */
17422 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17423 break;
17424
17425 default:
17426 gcc_unreachable ();
17427 }
17428
17429 return parm_die;
17430 }
17431
17432 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17433 children DW_TAG_formal_parameter DIEs representing the arguments of the
17434 parameter pack.
17435
17436 PARM_PACK must be a function parameter pack.
17437 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17438 must point to the subsequent arguments of the function PACK_ARG belongs to.
17439 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17440 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17441 following the last one for which a DIE was generated. */
17442
17443 static dw_die_ref
17444 gen_formal_parameter_pack_die (tree parm_pack,
17445 tree pack_arg,
17446 dw_die_ref subr_die,
17447 tree *next_arg)
17448 {
17449 tree arg;
17450 dw_die_ref parm_pack_die;
17451
17452 gcc_assert (parm_pack
17453 && lang_hooks.function_parameter_pack_p (parm_pack)
17454 && subr_die);
17455
17456 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17457 add_src_coords_attributes (parm_pack_die, parm_pack);
17458
17459 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17460 {
17461 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17462 parm_pack))
17463 break;
17464 gen_formal_parameter_die (arg, NULL,
17465 false /* Don't emit name attribute. */,
17466 parm_pack_die);
17467 }
17468 if (next_arg)
17469 *next_arg = arg;
17470 return parm_pack_die;
17471 }
17472
17473 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17474 at the end of an (ANSI prototyped) formal parameters list. */
17475
17476 static void
17477 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17478 {
17479 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17480 }
17481
17482 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17483 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17484 parameters as specified in some function type specification (except for
17485 those which appear as part of a function *definition*). */
17486
17487 static void
17488 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17489 {
17490 tree link;
17491 tree formal_type = NULL;
17492 tree first_parm_type;
17493 tree arg;
17494
17495 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17496 {
17497 arg = DECL_ARGUMENTS (function_or_method_type);
17498 function_or_method_type = TREE_TYPE (function_or_method_type);
17499 }
17500 else
17501 arg = NULL_TREE;
17502
17503 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17504
17505 /* Make our first pass over the list of formal parameter types and output a
17506 DW_TAG_formal_parameter DIE for each one. */
17507 for (link = first_parm_type; link; )
17508 {
17509 dw_die_ref parm_die;
17510
17511 formal_type = TREE_VALUE (link);
17512 if (formal_type == void_type_node)
17513 break;
17514
17515 /* Output a (nameless) DIE to represent the formal parameter itself. */
17516 parm_die = gen_formal_parameter_die (formal_type, NULL,
17517 true /* Emit name attribute. */,
17518 context_die);
17519 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17520 && link == first_parm_type)
17521 {
17522 add_AT_flag (parm_die, DW_AT_artificial, 1);
17523 if (dwarf_version >= 3 || !dwarf_strict)
17524 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17525 }
17526 else if (arg && DECL_ARTIFICIAL (arg))
17527 add_AT_flag (parm_die, DW_AT_artificial, 1);
17528
17529 link = TREE_CHAIN (link);
17530 if (arg)
17531 arg = DECL_CHAIN (arg);
17532 }
17533
17534 /* If this function type has an ellipsis, add a
17535 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17536 if (formal_type != void_type_node)
17537 gen_unspecified_parameters_die (function_or_method_type, context_die);
17538
17539 /* Make our second (and final) pass over the list of formal parameter types
17540 and output DIEs to represent those types (as necessary). */
17541 for (link = TYPE_ARG_TYPES (function_or_method_type);
17542 link && TREE_VALUE (link);
17543 link = TREE_CHAIN (link))
17544 gen_type_die (TREE_VALUE (link), context_die);
17545 }
17546
17547 /* We want to generate the DIE for TYPE so that we can generate the
17548 die for MEMBER, which has been defined; we will need to refer back
17549 to the member declaration nested within TYPE. If we're trying to
17550 generate minimal debug info for TYPE, processing TYPE won't do the
17551 trick; we need to attach the member declaration by hand. */
17552
17553 static void
17554 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17555 {
17556 gen_type_die (type, context_die);
17557
17558 /* If we're trying to avoid duplicate debug info, we may not have
17559 emitted the member decl for this function. Emit it now. */
17560 if (TYPE_STUB_DECL (type)
17561 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17562 && ! lookup_decl_die (member))
17563 {
17564 dw_die_ref type_die;
17565 gcc_assert (!decl_ultimate_origin (member));
17566
17567 push_decl_scope (type);
17568 type_die = lookup_type_die_strip_naming_typedef (type);
17569 if (TREE_CODE (member) == FUNCTION_DECL)
17570 gen_subprogram_die (member, type_die);
17571 else if (TREE_CODE (member) == FIELD_DECL)
17572 {
17573 /* Ignore the nameless fields that are used to skip bits but handle
17574 C++ anonymous unions and structs. */
17575 if (DECL_NAME (member) != NULL_TREE
17576 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17577 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17578 {
17579 gen_type_die (member_declared_type (member), type_die);
17580 gen_field_die (member, type_die);
17581 }
17582 }
17583 else
17584 gen_variable_die (member, NULL_TREE, type_die);
17585
17586 pop_decl_scope ();
17587 }
17588 }
17589 \f
17590 /* Forward declare these functions, because they are mutually recursive
17591 with their set_block_* pairing functions. */
17592 static void set_decl_origin_self (tree);
17593 static void set_decl_abstract_flags (tree, int);
17594
17595 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17596 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17597 that it points to the node itself, thus indicating that the node is its
17598 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17599 the given node is NULL, recursively descend the decl/block tree which
17600 it is the root of, and for each other ..._DECL or BLOCK node contained
17601 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17602 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17603 values to point to themselves. */
17604
17605 static void
17606 set_block_origin_self (tree stmt)
17607 {
17608 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17609 {
17610 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17611
17612 {
17613 tree local_decl;
17614
17615 for (local_decl = BLOCK_VARS (stmt);
17616 local_decl != NULL_TREE;
17617 local_decl = DECL_CHAIN (local_decl))
17618 if (! DECL_EXTERNAL (local_decl))
17619 set_decl_origin_self (local_decl); /* Potential recursion. */
17620 }
17621
17622 {
17623 tree subblock;
17624
17625 for (subblock = BLOCK_SUBBLOCKS (stmt);
17626 subblock != NULL_TREE;
17627 subblock = BLOCK_CHAIN (subblock))
17628 set_block_origin_self (subblock); /* Recurse. */
17629 }
17630 }
17631 }
17632
17633 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17634 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17635 node to so that it points to the node itself, thus indicating that the
17636 node represents its own (abstract) origin. Additionally, if the
17637 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17638 the decl/block tree of which the given node is the root of, and for
17639 each other ..._DECL or BLOCK node contained therein whose
17640 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17641 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17642 point to themselves. */
17643
17644 static void
17645 set_decl_origin_self (tree decl)
17646 {
17647 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17648 {
17649 DECL_ABSTRACT_ORIGIN (decl) = decl;
17650 if (TREE_CODE (decl) == FUNCTION_DECL)
17651 {
17652 tree arg;
17653
17654 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17655 DECL_ABSTRACT_ORIGIN (arg) = arg;
17656 if (DECL_INITIAL (decl) != NULL_TREE
17657 && DECL_INITIAL (decl) != error_mark_node)
17658 set_block_origin_self (DECL_INITIAL (decl));
17659 }
17660 }
17661 }
17662 \f
17663 /* Given a pointer to some BLOCK node, and a boolean value to set the
17664 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17665 the given block, and for all local decls and all local sub-blocks
17666 (recursively) which are contained therein. */
17667
17668 static void
17669 set_block_abstract_flags (tree stmt, int setting)
17670 {
17671 tree local_decl;
17672 tree subblock;
17673 unsigned int i;
17674
17675 BLOCK_ABSTRACT (stmt) = setting;
17676
17677 for (local_decl = BLOCK_VARS (stmt);
17678 local_decl != NULL_TREE;
17679 local_decl = DECL_CHAIN (local_decl))
17680 if (! DECL_EXTERNAL (local_decl))
17681 set_decl_abstract_flags (local_decl, setting);
17682
17683 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17684 {
17685 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17686 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17687 || TREE_CODE (local_decl) == PARM_DECL)
17688 set_decl_abstract_flags (local_decl, setting);
17689 }
17690
17691 for (subblock = BLOCK_SUBBLOCKS (stmt);
17692 subblock != NULL_TREE;
17693 subblock = BLOCK_CHAIN (subblock))
17694 set_block_abstract_flags (subblock, setting);
17695 }
17696
17697 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17698 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17699 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17700 set the abstract flags for all of the parameters, local vars, local
17701 blocks and sub-blocks (recursively) to the same setting. */
17702
17703 static void
17704 set_decl_abstract_flags (tree decl, int setting)
17705 {
17706 DECL_ABSTRACT (decl) = setting;
17707 if (TREE_CODE (decl) == FUNCTION_DECL)
17708 {
17709 tree arg;
17710
17711 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17712 DECL_ABSTRACT (arg) = setting;
17713 if (DECL_INITIAL (decl) != NULL_TREE
17714 && DECL_INITIAL (decl) != error_mark_node)
17715 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17716 }
17717 }
17718
17719 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17720 may later generate inlined and/or out-of-line instances of. */
17721
17722 static void
17723 dwarf2out_abstract_function (tree decl)
17724 {
17725 dw_die_ref old_die;
17726 tree save_fn;
17727 tree context;
17728 int was_abstract;
17729 htab_t old_decl_loc_table;
17730 htab_t old_cached_dw_loc_list_table;
17731 int old_call_site_count, old_tail_call_site_count;
17732 struct call_arg_loc_node *old_call_arg_locations;
17733
17734 /* Make sure we have the actual abstract inline, not a clone. */
17735 decl = DECL_ORIGIN (decl);
17736
17737 old_die = lookup_decl_die (decl);
17738 if (old_die && get_AT (old_die, DW_AT_inline))
17739 /* We've already generated the abstract instance. */
17740 return;
17741
17742 /* We can be called while recursively when seeing block defining inlined subroutine
17743 DIE. Be sure to not clobber the outer location table nor use it or we would
17744 get locations in abstract instantces. */
17745 old_decl_loc_table = decl_loc_table;
17746 decl_loc_table = NULL;
17747 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17748 cached_dw_loc_list_table = NULL;
17749 old_call_arg_locations = call_arg_locations;
17750 call_arg_locations = NULL;
17751 old_call_site_count = call_site_count;
17752 call_site_count = -1;
17753 old_tail_call_site_count = tail_call_site_count;
17754 tail_call_site_count = -1;
17755
17756 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17757 we don't get confused by DECL_ABSTRACT. */
17758 if (debug_info_level > DINFO_LEVEL_TERSE)
17759 {
17760 context = decl_class_context (decl);
17761 if (context)
17762 gen_type_die_for_member
17763 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17764 }
17765
17766 /* Pretend we've just finished compiling this function. */
17767 save_fn = current_function_decl;
17768 current_function_decl = decl;
17769
17770 was_abstract = DECL_ABSTRACT (decl);
17771 set_decl_abstract_flags (decl, 1);
17772 dwarf2out_decl (decl);
17773 if (! was_abstract)
17774 set_decl_abstract_flags (decl, 0);
17775
17776 current_function_decl = save_fn;
17777 decl_loc_table = old_decl_loc_table;
17778 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17779 call_arg_locations = old_call_arg_locations;
17780 call_site_count = old_call_site_count;
17781 tail_call_site_count = old_tail_call_site_count;
17782 }
17783
17784 /* Helper function of premark_used_types() which gets called through
17785 htab_traverse.
17786
17787 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17788 marked as unused by prune_unused_types. */
17789
17790 static int
17791 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17792 {
17793 tree type;
17794 dw_die_ref die;
17795
17796 type = (tree) *slot;
17797 die = lookup_type_die (type);
17798 if (die != NULL)
17799 die->die_perennial_p = 1;
17800 return 1;
17801 }
17802
17803 /* Helper function of premark_types_used_by_global_vars which gets called
17804 through htab_traverse.
17805
17806 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17807 marked as unused by prune_unused_types. The DIE of the type is marked
17808 only if the global variable using the type will actually be emitted. */
17809
17810 static int
17811 premark_types_used_by_global_vars_helper (void **slot,
17812 void *data ATTRIBUTE_UNUSED)
17813 {
17814 struct types_used_by_vars_entry *entry;
17815 dw_die_ref die;
17816
17817 entry = (struct types_used_by_vars_entry *) *slot;
17818 gcc_assert (entry->type != NULL
17819 && entry->var_decl != NULL);
17820 die = lookup_type_die (entry->type);
17821 if (die)
17822 {
17823 /* Ask cgraph if the global variable really is to be emitted.
17824 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17825 struct varpool_node *node = varpool_get_node (entry->var_decl);
17826 if (node && node->definition)
17827 {
17828 die->die_perennial_p = 1;
17829 /* Keep the parent DIEs as well. */
17830 while ((die = die->die_parent) && die->die_perennial_p == 0)
17831 die->die_perennial_p = 1;
17832 }
17833 }
17834 return 1;
17835 }
17836
17837 /* Mark all members of used_types_hash as perennial. */
17838
17839 static void
17840 premark_used_types (struct function *fun)
17841 {
17842 if (fun && fun->used_types_hash)
17843 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17844 }
17845
17846 /* Mark all members of types_used_by_vars_entry as perennial. */
17847
17848 static void
17849 premark_types_used_by_global_vars (void)
17850 {
17851 if (types_used_by_vars_hash)
17852 htab_traverse (types_used_by_vars_hash,
17853 premark_types_used_by_global_vars_helper, NULL);
17854 }
17855
17856 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17857 for CA_LOC call arg loc node. */
17858
17859 static dw_die_ref
17860 gen_call_site_die (tree decl, dw_die_ref subr_die,
17861 struct call_arg_loc_node *ca_loc)
17862 {
17863 dw_die_ref stmt_die = NULL, die;
17864 tree block = ca_loc->block;
17865
17866 while (block
17867 && block != DECL_INITIAL (decl)
17868 && TREE_CODE (block) == BLOCK)
17869 {
17870 if (block_map.length () > BLOCK_NUMBER (block))
17871 stmt_die = block_map[BLOCK_NUMBER (block)];
17872 if (stmt_die)
17873 break;
17874 block = BLOCK_SUPERCONTEXT (block);
17875 }
17876 if (stmt_die == NULL)
17877 stmt_die = subr_die;
17878 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17879 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17880 if (ca_loc->tail_call_p)
17881 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17882 if (ca_loc->symbol_ref)
17883 {
17884 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17885 if (tdie)
17886 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17887 else
17888 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17889 }
17890 return die;
17891 }
17892
17893 /* Generate a DIE to represent a declared function (either file-scope or
17894 block-local). */
17895
17896 static void
17897 gen_subprogram_die (tree decl, dw_die_ref context_die)
17898 {
17899 tree origin = decl_ultimate_origin (decl);
17900 dw_die_ref subr_die;
17901 tree outer_scope;
17902 dw_die_ref old_die = lookup_decl_die (decl);
17903 int declaration = (current_function_decl != decl
17904 || class_or_namespace_scope_p (context_die));
17905
17906 premark_used_types (DECL_STRUCT_FUNCTION (decl));
17907
17908 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17909 started to generate the abstract instance of an inline, decided to output
17910 its containing class, and proceeded to emit the declaration of the inline
17911 from the member list for the class. If so, DECLARATION takes priority;
17912 we'll get back to the abstract instance when done with the class. */
17913
17914 /* The class-scope declaration DIE must be the primary DIE. */
17915 if (origin && declaration && class_or_namespace_scope_p (context_die))
17916 {
17917 origin = NULL;
17918 gcc_assert (!old_die);
17919 }
17920
17921 /* Now that the C++ front end lazily declares artificial member fns, we
17922 might need to retrofit the declaration into its class. */
17923 if (!declaration && !origin && !old_die
17924 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17925 && !class_or_namespace_scope_p (context_die)
17926 && debug_info_level > DINFO_LEVEL_TERSE)
17927 old_die = force_decl_die (decl);
17928
17929 if (origin != NULL)
17930 {
17931 gcc_assert (!declaration || local_scope_p (context_die));
17932
17933 /* Fixup die_parent for the abstract instance of a nested
17934 inline function. */
17935 if (old_die && old_die->die_parent == NULL)
17936 add_child_die (context_die, old_die);
17937
17938 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17939 add_abstract_origin_attribute (subr_die, origin);
17940 /* This is where the actual code for a cloned function is.
17941 Let's emit linkage name attribute for it. This helps
17942 debuggers to e.g, set breakpoints into
17943 constructors/destructors when the user asks "break
17944 K::K". */
17945 add_linkage_name (subr_die, decl);
17946 }
17947 else if (old_die)
17948 {
17949 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17950 struct dwarf_file_data * file_index = lookup_filename (s.file);
17951
17952 if (!get_AT_flag (old_die, DW_AT_declaration)
17953 /* We can have a normal definition following an inline one in the
17954 case of redefinition of GNU C extern inlines.
17955 It seems reasonable to use AT_specification in this case. */
17956 && !get_AT (old_die, DW_AT_inline))
17957 {
17958 /* Detect and ignore this case, where we are trying to output
17959 something we have already output. */
17960 return;
17961 }
17962
17963 /* If the definition comes from the same place as the declaration,
17964 maybe use the old DIE. We always want the DIE for this function
17965 that has the *_pc attributes to be under comp_unit_die so the
17966 debugger can find it. We also need to do this for abstract
17967 instances of inlines, since the spec requires the out-of-line copy
17968 to have the same parent. For local class methods, this doesn't
17969 apply; we just use the old DIE. */
17970 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17971 && (DECL_ARTIFICIAL (decl)
17972 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17973 && (get_AT_unsigned (old_die, DW_AT_decl_line)
17974 == (unsigned) s.line))))
17975 {
17976 subr_die = old_die;
17977
17978 /* Clear out the declaration attribute and the formal parameters.
17979 Do not remove all children, because it is possible that this
17980 declaration die was forced using force_decl_die(). In such
17981 cases die that forced declaration die (e.g. TAG_imported_module)
17982 is one of the children that we do not want to remove. */
17983 remove_AT (subr_die, DW_AT_declaration);
17984 remove_AT (subr_die, DW_AT_object_pointer);
17985 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17986 }
17987 else
17988 {
17989 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17990 add_AT_specification (subr_die, old_die);
17991 add_pubname (decl, subr_die);
17992 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17993 add_AT_file (subr_die, DW_AT_decl_file, file_index);
17994 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17995 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17996 }
17997 }
17998 else
17999 {
18000 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18001
18002 if (TREE_PUBLIC (decl))
18003 add_AT_flag (subr_die, DW_AT_external, 1);
18004
18005 add_name_and_src_coords_attributes (subr_die, decl);
18006 add_pubname (decl, subr_die);
18007 if (debug_info_level > DINFO_LEVEL_TERSE)
18008 {
18009 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18010 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18011 0, 0, context_die);
18012 }
18013
18014 add_pure_or_virtual_attribute (subr_die, decl);
18015 if (DECL_ARTIFICIAL (decl))
18016 add_AT_flag (subr_die, DW_AT_artificial, 1);
18017
18018 add_accessibility_attribute (subr_die, decl);
18019 }
18020
18021 if (declaration)
18022 {
18023 if (!old_die || !get_AT (old_die, DW_AT_inline))
18024 {
18025 add_AT_flag (subr_die, DW_AT_declaration, 1);
18026
18027 /* If this is an explicit function declaration then generate
18028 a DW_AT_explicit attribute. */
18029 if (lang_hooks.decls.function_decl_explicit_p (decl)
18030 && (dwarf_version >= 3 || !dwarf_strict))
18031 add_AT_flag (subr_die, DW_AT_explicit, 1);
18032
18033 /* The first time we see a member function, it is in the context of
18034 the class to which it belongs. We make sure of this by emitting
18035 the class first. The next time is the definition, which is
18036 handled above. The two may come from the same source text.
18037
18038 Note that force_decl_die() forces function declaration die. It is
18039 later reused to represent definition. */
18040 equate_decl_number_to_die (decl, subr_die);
18041 }
18042 }
18043 else if (DECL_ABSTRACT (decl))
18044 {
18045 if (DECL_DECLARED_INLINE_P (decl))
18046 {
18047 if (cgraph_function_possibly_inlined_p (decl))
18048 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18049 else
18050 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18051 }
18052 else
18053 {
18054 if (cgraph_function_possibly_inlined_p (decl))
18055 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18056 else
18057 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18058 }
18059
18060 if (DECL_DECLARED_INLINE_P (decl)
18061 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18062 add_AT_flag (subr_die, DW_AT_artificial, 1);
18063
18064 equate_decl_number_to_die (decl, subr_die);
18065 }
18066 else if (!DECL_EXTERNAL (decl))
18067 {
18068 HOST_WIDE_INT cfa_fb_offset;
18069 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18070
18071 if (!old_die || !get_AT (old_die, DW_AT_inline))
18072 equate_decl_number_to_die (decl, subr_die);
18073
18074 gcc_checking_assert (fun);
18075 if (!flag_reorder_blocks_and_partition)
18076 {
18077 dw_fde_ref fde = fun->fde;
18078 if (fde->dw_fde_begin)
18079 {
18080 /* We have already generated the labels. */
18081 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18082 fde->dw_fde_end, false);
18083 }
18084 else
18085 {
18086 /* Create start/end labels and add the range. */
18087 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18088 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18089 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18090 current_function_funcdef_no);
18091 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18092 current_function_funcdef_no);
18093 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18094 false);
18095 }
18096
18097 #if VMS_DEBUGGING_INFO
18098 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18099 Section 2.3 Prologue and Epilogue Attributes:
18100 When a breakpoint is set on entry to a function, it is generally
18101 desirable for execution to be suspended, not on the very first
18102 instruction of the function, but rather at a point after the
18103 function's frame has been set up, after any language defined local
18104 declaration processing has been completed, and before execution of
18105 the first statement of the function begins. Debuggers generally
18106 cannot properly determine where this point is. Similarly for a
18107 breakpoint set on exit from a function. The prologue and epilogue
18108 attributes allow a compiler to communicate the location(s) to use. */
18109
18110 {
18111 if (fde->dw_fde_vms_end_prologue)
18112 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18113 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18114
18115 if (fde->dw_fde_vms_begin_epilogue)
18116 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18117 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18118 }
18119 #endif
18120
18121 }
18122 else
18123 {
18124 /* Generate pubnames entries for the split function code ranges. */
18125 dw_fde_ref fde = fun->fde;
18126
18127 if (fde->dw_fde_second_begin)
18128 {
18129 if (dwarf_version >= 3 || !dwarf_strict)
18130 {
18131 /* We should use ranges for non-contiguous code section
18132 addresses. Use the actual code range for the initial
18133 section, since the HOT/COLD labels might precede an
18134 alignment offset. */
18135 bool range_list_added = false;
18136 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18137 fde->dw_fde_end, &range_list_added,
18138 false);
18139 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18140 fde->dw_fde_second_end,
18141 &range_list_added, false);
18142 if (range_list_added)
18143 add_ranges (NULL);
18144 }
18145 else
18146 {
18147 /* There is no real support in DW2 for this .. so we make
18148 a work-around. First, emit the pub name for the segment
18149 containing the function label. Then make and emit a
18150 simplified subprogram DIE for the second segment with the
18151 name pre-fixed by __hot/cold_sect_of_. We use the same
18152 linkage name for the second die so that gdb will find both
18153 sections when given "b foo". */
18154 const char *name = NULL;
18155 tree decl_name = DECL_NAME (decl);
18156 dw_die_ref seg_die;
18157
18158 /* Do the 'primary' section. */
18159 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18160 fde->dw_fde_end, false);
18161
18162 /* Build a minimal DIE for the secondary section. */
18163 seg_die = new_die (DW_TAG_subprogram,
18164 subr_die->die_parent, decl);
18165
18166 if (TREE_PUBLIC (decl))
18167 add_AT_flag (seg_die, DW_AT_external, 1);
18168
18169 if (decl_name != NULL
18170 && IDENTIFIER_POINTER (decl_name) != NULL)
18171 {
18172 name = dwarf2_name (decl, 1);
18173 if (! DECL_ARTIFICIAL (decl))
18174 add_src_coords_attributes (seg_die, decl);
18175
18176 add_linkage_name (seg_die, decl);
18177 }
18178 gcc_assert (name != NULL);
18179 add_pure_or_virtual_attribute (seg_die, decl);
18180 if (DECL_ARTIFICIAL (decl))
18181 add_AT_flag (seg_die, DW_AT_artificial, 1);
18182
18183 name = concat ("__second_sect_of_", name, NULL);
18184 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18185 fde->dw_fde_second_end, false);
18186 add_name_attribute (seg_die, name);
18187 if (want_pubnames ())
18188 add_pubname_string (name, seg_die);
18189 }
18190 }
18191 else
18192 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18193 false);
18194 }
18195
18196 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18197
18198 /* We define the "frame base" as the function's CFA. This is more
18199 convenient for several reasons: (1) It's stable across the prologue
18200 and epilogue, which makes it better than just a frame pointer,
18201 (2) With dwarf3, there exists a one-byte encoding that allows us
18202 to reference the .debug_frame data by proxy, but failing that,
18203 (3) We can at least reuse the code inspection and interpretation
18204 code that determines the CFA position at various points in the
18205 function. */
18206 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18207 {
18208 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18209 add_AT_loc (subr_die, DW_AT_frame_base, op);
18210 }
18211 else
18212 {
18213 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18214 if (list->dw_loc_next)
18215 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18216 else
18217 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18218 }
18219
18220 /* Compute a displacement from the "steady-state frame pointer" to
18221 the CFA. The former is what all stack slots and argument slots
18222 will reference in the rtl; the latter is what we've told the
18223 debugger about. We'll need to adjust all frame_base references
18224 by this displacement. */
18225 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18226
18227 if (fun->static_chain_decl)
18228 add_AT_location_description (subr_die, DW_AT_static_link,
18229 loc_list_from_tree (fun->static_chain_decl, 2));
18230 }
18231
18232 /* Generate child dies for template paramaters. */
18233 if (debug_info_level > DINFO_LEVEL_TERSE)
18234 gen_generic_params_dies (decl);
18235
18236 /* Now output descriptions of the arguments for this function. This gets
18237 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18238 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18239 `...' at the end of the formal parameter list. In order to find out if
18240 there was a trailing ellipsis or not, we must instead look at the type
18241 associated with the FUNCTION_DECL. This will be a node of type
18242 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18243 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18244 an ellipsis at the end. */
18245
18246 /* In the case where we are describing a mere function declaration, all we
18247 need to do here (and all we *can* do here) is to describe the *types* of
18248 its formal parameters. */
18249 if (debug_info_level <= DINFO_LEVEL_TERSE)
18250 ;
18251 else if (declaration)
18252 gen_formal_types_die (decl, subr_die);
18253 else
18254 {
18255 /* Generate DIEs to represent all known formal parameters. */
18256 tree parm = DECL_ARGUMENTS (decl);
18257 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18258 tree generic_decl_parm = generic_decl
18259 ? DECL_ARGUMENTS (generic_decl)
18260 : NULL;
18261
18262 /* Now we want to walk the list of parameters of the function and
18263 emit their relevant DIEs.
18264
18265 We consider the case of DECL being an instance of a generic function
18266 as well as it being a normal function.
18267
18268 If DECL is an instance of a generic function we walk the
18269 parameters of the generic function declaration _and_ the parameters of
18270 DECL itself. This is useful because we want to emit specific DIEs for
18271 function parameter packs and those are declared as part of the
18272 generic function declaration. In that particular case,
18273 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18274 That DIE has children DIEs representing the set of arguments
18275 of the pack. Note that the set of pack arguments can be empty.
18276 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18277 children DIE.
18278
18279 Otherwise, we just consider the parameters of DECL. */
18280 while (generic_decl_parm || parm)
18281 {
18282 if (generic_decl_parm
18283 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18284 gen_formal_parameter_pack_die (generic_decl_parm,
18285 parm, subr_die,
18286 &parm);
18287 else if (parm)
18288 {
18289 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18290
18291 if (parm == DECL_ARGUMENTS (decl)
18292 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18293 && parm_die
18294 && (dwarf_version >= 3 || !dwarf_strict))
18295 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18296
18297 parm = DECL_CHAIN (parm);
18298 }
18299
18300 if (generic_decl_parm)
18301 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18302 }
18303
18304 /* Decide whether we need an unspecified_parameters DIE at the end.
18305 There are 2 more cases to do this for: 1) the ansi ... declaration -
18306 this is detectable when the end of the arg list is not a
18307 void_type_node 2) an unprototyped function declaration (not a
18308 definition). This just means that we have no info about the
18309 parameters at all. */
18310 if (prototype_p (TREE_TYPE (decl)))
18311 {
18312 /* This is the prototyped case, check for.... */
18313 if (stdarg_p (TREE_TYPE (decl)))
18314 gen_unspecified_parameters_die (decl, subr_die);
18315 }
18316 else if (DECL_INITIAL (decl) == NULL_TREE)
18317 gen_unspecified_parameters_die (decl, subr_die);
18318 }
18319
18320 /* Output Dwarf info for all of the stuff within the body of the function
18321 (if it has one - it may be just a declaration). */
18322 outer_scope = DECL_INITIAL (decl);
18323
18324 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18325 a function. This BLOCK actually represents the outermost binding contour
18326 for the function, i.e. the contour in which the function's formal
18327 parameters and labels get declared. Curiously, it appears that the front
18328 end doesn't actually put the PARM_DECL nodes for the current function onto
18329 the BLOCK_VARS list for this outer scope, but are strung off of the
18330 DECL_ARGUMENTS list for the function instead.
18331
18332 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18333 the LABEL_DECL nodes for the function however, and we output DWARF info
18334 for those in decls_for_scope. Just within the `outer_scope' there will be
18335 a BLOCK node representing the function's outermost pair of curly braces,
18336 and any blocks used for the base and member initializers of a C++
18337 constructor function. */
18338 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18339 {
18340 int call_site_note_count = 0;
18341 int tail_call_site_note_count = 0;
18342
18343 /* Emit a DW_TAG_variable DIE for a named return value. */
18344 if (DECL_NAME (DECL_RESULT (decl)))
18345 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18346
18347 current_function_has_inlines = 0;
18348 decls_for_scope (outer_scope, subr_die, 0);
18349
18350 if (call_arg_locations && !dwarf_strict)
18351 {
18352 struct call_arg_loc_node *ca_loc;
18353 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18354 {
18355 dw_die_ref die = NULL;
18356 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18357 rtx arg, next_arg;
18358
18359 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18360 arg; arg = next_arg)
18361 {
18362 dw_loc_descr_ref reg, val;
18363 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18364 dw_die_ref cdie, tdie = NULL;
18365
18366 next_arg = XEXP (arg, 1);
18367 if (REG_P (XEXP (XEXP (arg, 0), 0))
18368 && next_arg
18369 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18370 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18371 && REGNO (XEXP (XEXP (arg, 0), 0))
18372 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18373 next_arg = XEXP (next_arg, 1);
18374 if (mode == VOIDmode)
18375 {
18376 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18377 if (mode == VOIDmode)
18378 mode = GET_MODE (XEXP (arg, 0));
18379 }
18380 if (mode == VOIDmode || mode == BLKmode)
18381 continue;
18382 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18383 {
18384 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18385 tloc = XEXP (XEXP (arg, 0), 1);
18386 continue;
18387 }
18388 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18389 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18390 {
18391 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18392 tlocc = XEXP (XEXP (arg, 0), 1);
18393 continue;
18394 }
18395 reg = NULL;
18396 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18397 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18398 VAR_INIT_STATUS_INITIALIZED);
18399 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18400 {
18401 rtx mem = XEXP (XEXP (arg, 0), 0);
18402 reg = mem_loc_descriptor (XEXP (mem, 0),
18403 get_address_mode (mem),
18404 GET_MODE (mem),
18405 VAR_INIT_STATUS_INITIALIZED);
18406 }
18407 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18408 == DEBUG_PARAMETER_REF)
18409 {
18410 tree tdecl
18411 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18412 tdie = lookup_decl_die (tdecl);
18413 if (tdie == NULL)
18414 continue;
18415 }
18416 else
18417 continue;
18418 if (reg == NULL
18419 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18420 != DEBUG_PARAMETER_REF)
18421 continue;
18422 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18423 VOIDmode,
18424 VAR_INIT_STATUS_INITIALIZED);
18425 if (val == NULL)
18426 continue;
18427 if (die == NULL)
18428 die = gen_call_site_die (decl, subr_die, ca_loc);
18429 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18430 NULL_TREE);
18431 if (reg != NULL)
18432 add_AT_loc (cdie, DW_AT_location, reg);
18433 else if (tdie != NULL)
18434 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18435 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18436 if (next_arg != XEXP (arg, 1))
18437 {
18438 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18439 if (mode == VOIDmode)
18440 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18441 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18442 0), 1),
18443 mode, VOIDmode,
18444 VAR_INIT_STATUS_INITIALIZED);
18445 if (val != NULL)
18446 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18447 }
18448 }
18449 if (die == NULL
18450 && (ca_loc->symbol_ref || tloc))
18451 die = gen_call_site_die (decl, subr_die, ca_loc);
18452 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18453 {
18454 dw_loc_descr_ref tval = NULL;
18455
18456 if (tloc != NULL_RTX)
18457 tval = mem_loc_descriptor (tloc,
18458 GET_MODE (tloc) == VOIDmode
18459 ? Pmode : GET_MODE (tloc),
18460 VOIDmode,
18461 VAR_INIT_STATUS_INITIALIZED);
18462 if (tval)
18463 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18464 else if (tlocc != NULL_RTX)
18465 {
18466 tval = mem_loc_descriptor (tlocc,
18467 GET_MODE (tlocc) == VOIDmode
18468 ? Pmode : GET_MODE (tlocc),
18469 VOIDmode,
18470 VAR_INIT_STATUS_INITIALIZED);
18471 if (tval)
18472 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18473 tval);
18474 }
18475 }
18476 if (die != NULL)
18477 {
18478 call_site_note_count++;
18479 if (ca_loc->tail_call_p)
18480 tail_call_site_note_count++;
18481 }
18482 }
18483 }
18484 call_arg_locations = NULL;
18485 call_arg_loc_last = NULL;
18486 if (tail_call_site_count >= 0
18487 && tail_call_site_count == tail_call_site_note_count
18488 && !dwarf_strict)
18489 {
18490 if (call_site_count >= 0
18491 && call_site_count == call_site_note_count)
18492 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18493 else
18494 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18495 }
18496 call_site_count = -1;
18497 tail_call_site_count = -1;
18498 }
18499 /* Add the calling convention attribute if requested. */
18500 add_calling_convention_attribute (subr_die, decl);
18501
18502 }
18503
18504 /* Returns a hash value for X (which really is a die_struct). */
18505
18506 static hashval_t
18507 common_block_die_table_hash (const void *x)
18508 {
18509 const_dw_die_ref d = (const_dw_die_ref) x;
18510 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18511 }
18512
18513 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18514 as decl_id and die_parent of die_struct Y. */
18515
18516 static int
18517 common_block_die_table_eq (const void *x, const void *y)
18518 {
18519 const_dw_die_ref d = (const_dw_die_ref) x;
18520 const_dw_die_ref e = (const_dw_die_ref) y;
18521 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18522 }
18523
18524 /* Generate a DIE to represent a declared data object.
18525 Either DECL or ORIGIN must be non-null. */
18526
18527 static void
18528 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18529 {
18530 HOST_WIDE_INT off = 0;
18531 tree com_decl;
18532 tree decl_or_origin = decl ? decl : origin;
18533 tree ultimate_origin;
18534 dw_die_ref var_die;
18535 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18536 dw_die_ref origin_die;
18537 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18538 || class_or_namespace_scope_p (context_die));
18539 bool specialization_p = false;
18540
18541 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18542 if (decl || ultimate_origin)
18543 origin = ultimate_origin;
18544 com_decl = fortran_common (decl_or_origin, &off);
18545
18546 /* Symbol in common gets emitted as a child of the common block, in the form
18547 of a data member. */
18548 if (com_decl)
18549 {
18550 dw_die_ref com_die;
18551 dw_loc_list_ref loc;
18552 die_node com_die_arg;
18553
18554 var_die = lookup_decl_die (decl_or_origin);
18555 if (var_die)
18556 {
18557 if (get_AT (var_die, DW_AT_location) == NULL)
18558 {
18559 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18560 if (loc)
18561 {
18562 if (off)
18563 {
18564 /* Optimize the common case. */
18565 if (single_element_loc_list_p (loc)
18566 && loc->expr->dw_loc_opc == DW_OP_addr
18567 && loc->expr->dw_loc_next == NULL
18568 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18569 == SYMBOL_REF)
18570 {
18571 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18572 loc->expr->dw_loc_oprnd1.v.val_addr
18573 = plus_constant (GET_MODE (x), x , off);
18574 }
18575 else
18576 loc_list_plus_const (loc, off);
18577 }
18578 add_AT_location_description (var_die, DW_AT_location, loc);
18579 remove_AT (var_die, DW_AT_declaration);
18580 }
18581 }
18582 return;
18583 }
18584
18585 if (common_block_die_table == NULL)
18586 common_block_die_table
18587 = htab_create_ggc (10, common_block_die_table_hash,
18588 common_block_die_table_eq, NULL);
18589
18590 com_die_arg.decl_id = DECL_UID (com_decl);
18591 com_die_arg.die_parent = context_die;
18592 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18593 loc = loc_list_from_tree (com_decl, 2);
18594 if (com_die == NULL)
18595 {
18596 const char *cnam
18597 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18598 void **slot;
18599
18600 com_die = new_die (DW_TAG_common_block, context_die, decl);
18601 add_name_and_src_coords_attributes (com_die, com_decl);
18602 if (loc)
18603 {
18604 add_AT_location_description (com_die, DW_AT_location, loc);
18605 /* Avoid sharing the same loc descriptor between
18606 DW_TAG_common_block and DW_TAG_variable. */
18607 loc = loc_list_from_tree (com_decl, 2);
18608 }
18609 else if (DECL_EXTERNAL (decl))
18610 add_AT_flag (com_die, DW_AT_declaration, 1);
18611 if (want_pubnames ())
18612 add_pubname_string (cnam, com_die); /* ??? needed? */
18613 com_die->decl_id = DECL_UID (com_decl);
18614 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18615 *slot = (void *) com_die;
18616 }
18617 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18618 {
18619 add_AT_location_description (com_die, DW_AT_location, loc);
18620 loc = loc_list_from_tree (com_decl, 2);
18621 remove_AT (com_die, DW_AT_declaration);
18622 }
18623 var_die = new_die (DW_TAG_variable, com_die, decl);
18624 add_name_and_src_coords_attributes (var_die, decl);
18625 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18626 TREE_THIS_VOLATILE (decl), context_die);
18627 add_AT_flag (var_die, DW_AT_external, 1);
18628 if (loc)
18629 {
18630 if (off)
18631 {
18632 /* Optimize the common case. */
18633 if (single_element_loc_list_p (loc)
18634 && loc->expr->dw_loc_opc == DW_OP_addr
18635 && loc->expr->dw_loc_next == NULL
18636 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18637 {
18638 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18639 loc->expr->dw_loc_oprnd1.v.val_addr
18640 = plus_constant (GET_MODE (x), x, off);
18641 }
18642 else
18643 loc_list_plus_const (loc, off);
18644 }
18645 add_AT_location_description (var_die, DW_AT_location, loc);
18646 }
18647 else if (DECL_EXTERNAL (decl))
18648 add_AT_flag (var_die, DW_AT_declaration, 1);
18649 equate_decl_number_to_die (decl, var_die);
18650 return;
18651 }
18652
18653 /* If the compiler emitted a definition for the DECL declaration
18654 and if we already emitted a DIE for it, don't emit a second
18655 DIE for it again. Allow re-declarations of DECLs that are
18656 inside functions, though. */
18657 if (old_die && declaration && !local_scope_p (context_die))
18658 return;
18659
18660 /* For static data members, the declaration in the class is supposed
18661 to have DW_TAG_member tag; the specification should still be
18662 DW_TAG_variable referencing the DW_TAG_member DIE. */
18663 if (declaration && class_scope_p (context_die))
18664 var_die = new_die (DW_TAG_member, context_die, decl);
18665 else
18666 var_die = new_die (DW_TAG_variable, context_die, decl);
18667
18668 origin_die = NULL;
18669 if (origin != NULL)
18670 origin_die = add_abstract_origin_attribute (var_die, origin);
18671
18672 /* Loop unrolling can create multiple blocks that refer to the same
18673 static variable, so we must test for the DW_AT_declaration flag.
18674
18675 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18676 copy decls and set the DECL_ABSTRACT flag on them instead of
18677 sharing them.
18678
18679 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18680
18681 ??? The declare_in_namespace support causes us to get two DIEs for one
18682 variable, both of which are declarations. We want to avoid considering
18683 one to be a specification, so we must test that this DIE is not a
18684 declaration. */
18685 else if (old_die && TREE_STATIC (decl) && ! declaration
18686 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18687 {
18688 /* This is a definition of a C++ class level static. */
18689 add_AT_specification (var_die, old_die);
18690 specialization_p = true;
18691 if (DECL_NAME (decl))
18692 {
18693 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18694 struct dwarf_file_data * file_index = lookup_filename (s.file);
18695
18696 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18697 add_AT_file (var_die, DW_AT_decl_file, file_index);
18698
18699 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18700 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18701
18702 if (old_die->die_tag == DW_TAG_member)
18703 add_linkage_name (var_die, decl);
18704 }
18705 }
18706 else
18707 add_name_and_src_coords_attributes (var_die, decl);
18708
18709 if ((origin == NULL && !specialization_p)
18710 || (origin != NULL
18711 && !DECL_ABSTRACT (decl_or_origin)
18712 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18713 decl_function_context
18714 (decl_or_origin))))
18715 {
18716 tree type = TREE_TYPE (decl_or_origin);
18717
18718 if (decl_by_reference_p (decl_or_origin))
18719 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18720 else
18721 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18722 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18723 }
18724
18725 if (origin == NULL && !specialization_p)
18726 {
18727 if (TREE_PUBLIC (decl))
18728 add_AT_flag (var_die, DW_AT_external, 1);
18729
18730 if (DECL_ARTIFICIAL (decl))
18731 add_AT_flag (var_die, DW_AT_artificial, 1);
18732
18733 add_accessibility_attribute (var_die, decl);
18734 }
18735
18736 if (declaration)
18737 add_AT_flag (var_die, DW_AT_declaration, 1);
18738
18739 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18740 equate_decl_number_to_die (decl, var_die);
18741
18742 if (! declaration
18743 && (! DECL_ABSTRACT (decl_or_origin)
18744 /* Local static vars are shared between all clones/inlines,
18745 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18746 already set. */
18747 || (TREE_CODE (decl_or_origin) == VAR_DECL
18748 && TREE_STATIC (decl_or_origin)
18749 && DECL_RTL_SET_P (decl_or_origin)))
18750 /* When abstract origin already has DW_AT_location attribute, no need
18751 to add it again. */
18752 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18753 {
18754 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18755 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18756 defer_location (decl_or_origin, var_die);
18757 else
18758 add_location_or_const_value_attribute (var_die, decl_or_origin,
18759 decl == NULL, DW_AT_location);
18760 add_pubname (decl_or_origin, var_die);
18761 }
18762 else
18763 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18764 }
18765
18766 /* Generate a DIE to represent a named constant. */
18767
18768 static void
18769 gen_const_die (tree decl, dw_die_ref context_die)
18770 {
18771 dw_die_ref const_die;
18772 tree type = TREE_TYPE (decl);
18773
18774 const_die = new_die (DW_TAG_constant, context_die, decl);
18775 add_name_and_src_coords_attributes (const_die, decl);
18776 add_type_attribute (const_die, type, 1, 0, context_die);
18777 if (TREE_PUBLIC (decl))
18778 add_AT_flag (const_die, DW_AT_external, 1);
18779 if (DECL_ARTIFICIAL (decl))
18780 add_AT_flag (const_die, DW_AT_artificial, 1);
18781 tree_add_const_value_attribute_for_decl (const_die, decl);
18782 }
18783
18784 /* Generate a DIE to represent a label identifier. */
18785
18786 static void
18787 gen_label_die (tree decl, dw_die_ref context_die)
18788 {
18789 tree origin = decl_ultimate_origin (decl);
18790 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18791 rtx insn;
18792 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18793
18794 if (origin != NULL)
18795 add_abstract_origin_attribute (lbl_die, origin);
18796 else
18797 add_name_and_src_coords_attributes (lbl_die, decl);
18798
18799 if (DECL_ABSTRACT (decl))
18800 equate_decl_number_to_die (decl, lbl_die);
18801 else
18802 {
18803 insn = DECL_RTL_IF_SET (decl);
18804
18805 /* Deleted labels are programmer specified labels which have been
18806 eliminated because of various optimizations. We still emit them
18807 here so that it is possible to put breakpoints on them. */
18808 if (insn
18809 && (LABEL_P (insn)
18810 || ((NOTE_P (insn)
18811 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18812 {
18813 /* When optimization is enabled (via -O) some parts of the compiler
18814 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18815 represent source-level labels which were explicitly declared by
18816 the user. This really shouldn't be happening though, so catch
18817 it if it ever does happen. */
18818 gcc_assert (!INSN_DELETED_P (insn));
18819
18820 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18821 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18822 }
18823 else if (insn
18824 && NOTE_P (insn)
18825 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18826 && CODE_LABEL_NUMBER (insn) != -1)
18827 {
18828 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18829 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18830 }
18831 }
18832 }
18833
18834 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
18835 attributes to the DIE for a block STMT, to describe where the inlined
18836 function was called from. This is similar to add_src_coords_attributes. */
18837
18838 static inline void
18839 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18840 {
18841 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18842
18843 if (dwarf_version >= 3 || !dwarf_strict)
18844 {
18845 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18846 add_AT_unsigned (die, DW_AT_call_line, s.line);
18847 }
18848 }
18849
18850
18851 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18852 Add low_pc and high_pc attributes to the DIE for a block STMT. */
18853
18854 static inline void
18855 add_high_low_attributes (tree stmt, dw_die_ref die)
18856 {
18857 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18858
18859 if (BLOCK_FRAGMENT_CHAIN (stmt)
18860 && (dwarf_version >= 3 || !dwarf_strict))
18861 {
18862 tree chain, superblock = NULL_TREE;
18863 dw_die_ref pdie;
18864 dw_attr_ref attr = NULL;
18865
18866 if (inlined_function_outer_scope_p (stmt))
18867 {
18868 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18869 BLOCK_NUMBER (stmt));
18870 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18871 }
18872
18873 /* Optimize duplicate .debug_ranges lists or even tails of
18874 lists. If this BLOCK has same ranges as its supercontext,
18875 lookup DW_AT_ranges attribute in the supercontext (and
18876 recursively so), verify that the ranges_table contains the
18877 right values and use it instead of adding a new .debug_range. */
18878 for (chain = stmt, pdie = die;
18879 BLOCK_SAME_RANGE (chain);
18880 chain = BLOCK_SUPERCONTEXT (chain))
18881 {
18882 dw_attr_ref new_attr;
18883
18884 pdie = pdie->die_parent;
18885 if (pdie == NULL)
18886 break;
18887 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18888 break;
18889 new_attr = get_AT (pdie, DW_AT_ranges);
18890 if (new_attr == NULL
18891 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18892 break;
18893 attr = new_attr;
18894 superblock = BLOCK_SUPERCONTEXT (chain);
18895 }
18896 if (attr != NULL
18897 && (ranges_table[attr->dw_attr_val.v.val_offset
18898 / 2 / DWARF2_ADDR_SIZE].num
18899 == BLOCK_NUMBER (superblock))
18900 && BLOCK_FRAGMENT_CHAIN (superblock))
18901 {
18902 unsigned long off = attr->dw_attr_val.v.val_offset
18903 / 2 / DWARF2_ADDR_SIZE;
18904 unsigned long supercnt = 0, thiscnt = 0;
18905 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18906 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18907 {
18908 ++supercnt;
18909 gcc_checking_assert (ranges_table[off + supercnt].num
18910 == BLOCK_NUMBER (chain));
18911 }
18912 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18913 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18914 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18915 ++thiscnt;
18916 gcc_assert (supercnt >= thiscnt);
18917 add_AT_range_list (die, DW_AT_ranges,
18918 ((off + supercnt - thiscnt)
18919 * 2 * DWARF2_ADDR_SIZE),
18920 false);
18921 return;
18922 }
18923
18924 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18925
18926 chain = BLOCK_FRAGMENT_CHAIN (stmt);
18927 do
18928 {
18929 add_ranges (chain);
18930 chain = BLOCK_FRAGMENT_CHAIN (chain);
18931 }
18932 while (chain);
18933 add_ranges (NULL);
18934 }
18935 else
18936 {
18937 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18938 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18939 BLOCK_NUMBER (stmt));
18940 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18941 BLOCK_NUMBER (stmt));
18942 add_AT_low_high_pc (die, label, label_high, false);
18943 }
18944 }
18945
18946 /* Generate a DIE for a lexical block. */
18947
18948 static void
18949 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18950 {
18951 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18952
18953 if (call_arg_locations)
18954 {
18955 if (block_map.length () <= BLOCK_NUMBER (stmt))
18956 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18957 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18958 }
18959
18960 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18961 add_high_low_attributes (stmt, stmt_die);
18962
18963 decls_for_scope (stmt, stmt_die, depth);
18964 }
18965
18966 /* Generate a DIE for an inlined subprogram. */
18967
18968 static void
18969 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18970 {
18971 tree decl;
18972
18973 /* The instance of function that is effectively being inlined shall not
18974 be abstract. */
18975 gcc_assert (! BLOCK_ABSTRACT (stmt));
18976
18977 decl = block_ultimate_origin (stmt);
18978
18979 /* Emit info for the abstract instance first, if we haven't yet. We
18980 must emit this even if the block is abstract, otherwise when we
18981 emit the block below (or elsewhere), we may end up trying to emit
18982 a die whose origin die hasn't been emitted, and crashing. */
18983 dwarf2out_abstract_function (decl);
18984
18985 if (! BLOCK_ABSTRACT (stmt))
18986 {
18987 dw_die_ref subr_die
18988 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18989
18990 if (call_arg_locations)
18991 {
18992 if (block_map.length () <= BLOCK_NUMBER (stmt))
18993 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18994 block_map[BLOCK_NUMBER (stmt)] = subr_die;
18995 }
18996 add_abstract_origin_attribute (subr_die, decl);
18997 if (TREE_ASM_WRITTEN (stmt))
18998 add_high_low_attributes (stmt, subr_die);
18999 add_call_src_coords_attributes (stmt, subr_die);
19000
19001 decls_for_scope (stmt, subr_die, depth);
19002 current_function_has_inlines = 1;
19003 }
19004 }
19005
19006 /* Generate a DIE for a field in a record, or structure. */
19007
19008 static void
19009 gen_field_die (tree decl, dw_die_ref context_die)
19010 {
19011 dw_die_ref decl_die;
19012
19013 if (TREE_TYPE (decl) == error_mark_node)
19014 return;
19015
19016 decl_die = new_die (DW_TAG_member, context_die, decl);
19017 add_name_and_src_coords_attributes (decl_die, decl);
19018 add_type_attribute (decl_die, member_declared_type (decl),
19019 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19020 context_die);
19021
19022 if (DECL_BIT_FIELD_TYPE (decl))
19023 {
19024 add_byte_size_attribute (decl_die, decl);
19025 add_bit_size_attribute (decl_die, decl);
19026 add_bit_offset_attribute (decl_die, decl);
19027 }
19028
19029 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19030 add_data_member_location_attribute (decl_die, decl);
19031
19032 if (DECL_ARTIFICIAL (decl))
19033 add_AT_flag (decl_die, DW_AT_artificial, 1);
19034
19035 add_accessibility_attribute (decl_die, decl);
19036
19037 /* Equate decl number to die, so that we can look up this decl later on. */
19038 equate_decl_number_to_die (decl, decl_die);
19039 }
19040
19041 #if 0
19042 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19043 Use modified_type_die instead.
19044 We keep this code here just in case these types of DIEs may be needed to
19045 represent certain things in other languages (e.g. Pascal) someday. */
19046
19047 static void
19048 gen_pointer_type_die (tree type, dw_die_ref context_die)
19049 {
19050 dw_die_ref ptr_die
19051 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19052
19053 equate_type_number_to_die (type, ptr_die);
19054 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19055 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19056 }
19057
19058 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19059 Use modified_type_die instead.
19060 We keep this code here just in case these types of DIEs may be needed to
19061 represent certain things in other languages (e.g. Pascal) someday. */
19062
19063 static void
19064 gen_reference_type_die (tree type, dw_die_ref context_die)
19065 {
19066 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19067
19068 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19069 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19070 else
19071 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19072
19073 equate_type_number_to_die (type, ref_die);
19074 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19075 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19076 }
19077 #endif
19078
19079 /* Generate a DIE for a pointer to a member type. */
19080
19081 static void
19082 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19083 {
19084 dw_die_ref ptr_die
19085 = new_die (DW_TAG_ptr_to_member_type,
19086 scope_die_for (type, context_die), type);
19087
19088 equate_type_number_to_die (type, ptr_die);
19089 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19090 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19091 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19092 }
19093
19094 typedef const char *dchar_p; /* For DEF_VEC_P. */
19095
19096 static char *producer_string;
19097
19098 /* Return a heap allocated producer string including command line options
19099 if -grecord-gcc-switches. */
19100
19101 static char *
19102 gen_producer_string (void)
19103 {
19104 size_t j;
19105 vec<dchar_p> switches = vNULL;
19106 const char *language_string = lang_hooks.name;
19107 char *producer, *tail;
19108 const char *p;
19109 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19110 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19111
19112 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19113 switch (save_decoded_options[j].opt_index)
19114 {
19115 case OPT_o:
19116 case OPT_d:
19117 case OPT_dumpbase:
19118 case OPT_dumpdir:
19119 case OPT_auxbase:
19120 case OPT_auxbase_strip:
19121 case OPT_quiet:
19122 case OPT_version:
19123 case OPT_v:
19124 case OPT_w:
19125 case OPT_L:
19126 case OPT_D:
19127 case OPT_I:
19128 case OPT_U:
19129 case OPT_SPECIAL_unknown:
19130 case OPT_SPECIAL_ignore:
19131 case OPT_SPECIAL_program_name:
19132 case OPT_SPECIAL_input_file:
19133 case OPT_grecord_gcc_switches:
19134 case OPT_gno_record_gcc_switches:
19135 case OPT__output_pch_:
19136 case OPT_fdiagnostics_show_location_:
19137 case OPT_fdiagnostics_show_option:
19138 case OPT_fdiagnostics_show_caret:
19139 case OPT_fdiagnostics_color_:
19140 case OPT_fverbose_asm:
19141 case OPT____:
19142 case OPT__sysroot_:
19143 case OPT_nostdinc:
19144 case OPT_nostdinc__:
19145 /* Ignore these. */
19146 continue;
19147 default:
19148 if (cl_options[save_decoded_options[j].opt_index].flags
19149 & CL_NO_DWARF_RECORD)
19150 continue;
19151 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19152 == '-');
19153 switch (save_decoded_options[j].canonical_option[0][1])
19154 {
19155 case 'M':
19156 case 'i':
19157 case 'W':
19158 continue;
19159 case 'f':
19160 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19161 "dump", 4) == 0)
19162 continue;
19163 break;
19164 default:
19165 break;
19166 }
19167 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19168 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19169 break;
19170 }
19171
19172 producer = XNEWVEC (char, plen + 1 + len + 1);
19173 tail = producer;
19174 sprintf (tail, "%s %s", language_string, version_string);
19175 tail += plen;
19176
19177 FOR_EACH_VEC_ELT (switches, j, p)
19178 {
19179 len = strlen (p);
19180 *tail = ' ';
19181 memcpy (tail + 1, p, len);
19182 tail += len + 1;
19183 }
19184
19185 *tail = '\0';
19186 switches.release ();
19187 return producer;
19188 }
19189
19190 /* Generate the DIE for the compilation unit. */
19191
19192 static dw_die_ref
19193 gen_compile_unit_die (const char *filename)
19194 {
19195 dw_die_ref die;
19196 const char *language_string = lang_hooks.name;
19197 int language;
19198
19199 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19200
19201 if (filename)
19202 {
19203 add_name_attribute (die, filename);
19204 /* Don't add cwd for <built-in>. */
19205 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19206 add_comp_dir_attribute (die);
19207 }
19208
19209 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19210
19211 /* If our producer is LTO try to figure out a common language to use
19212 from the global list of translation units. */
19213 if (strcmp (language_string, "GNU GIMPLE") == 0)
19214 {
19215 unsigned i;
19216 tree t;
19217 const char *common_lang = NULL;
19218
19219 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19220 {
19221 if (!TRANSLATION_UNIT_LANGUAGE (t))
19222 continue;
19223 if (!common_lang)
19224 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19225 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19226 ;
19227 else if (strncmp (common_lang, "GNU C", 5) == 0
19228 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19229 /* Mixing C and C++ is ok, use C++ in that case. */
19230 common_lang = "GNU C++";
19231 else
19232 {
19233 /* Fall back to C. */
19234 common_lang = NULL;
19235 break;
19236 }
19237 }
19238
19239 if (common_lang)
19240 language_string = common_lang;
19241 }
19242
19243 language = DW_LANG_C89;
19244 if (strcmp (language_string, "GNU C++") == 0)
19245 language = DW_LANG_C_plus_plus;
19246 else if (strcmp (language_string, "GNU F77") == 0)
19247 language = DW_LANG_Fortran77;
19248 else if (strcmp (language_string, "GNU Pascal") == 0)
19249 language = DW_LANG_Pascal83;
19250 else if (dwarf_version >= 3 || !dwarf_strict)
19251 {
19252 if (strcmp (language_string, "GNU Ada") == 0)
19253 language = DW_LANG_Ada95;
19254 else if (strcmp (language_string, "GNU Fortran") == 0)
19255 language = DW_LANG_Fortran95;
19256 else if (strcmp (language_string, "GNU Java") == 0)
19257 language = DW_LANG_Java;
19258 else if (strcmp (language_string, "GNU Objective-C") == 0)
19259 language = DW_LANG_ObjC;
19260 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19261 language = DW_LANG_ObjC_plus_plus;
19262 else if (dwarf_version >= 5 || !dwarf_strict)
19263 {
19264 if (strcmp (language_string, "GNU Go") == 0)
19265 language = DW_LANG_Go;
19266 }
19267 }
19268 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19269 else if (strcmp (language_string, "GNU Fortran") == 0)
19270 language = DW_LANG_Fortran90;
19271
19272 add_AT_unsigned (die, DW_AT_language, language);
19273
19274 switch (language)
19275 {
19276 case DW_LANG_Fortran77:
19277 case DW_LANG_Fortran90:
19278 case DW_LANG_Fortran95:
19279 /* Fortran has case insensitive identifiers and the front-end
19280 lowercases everything. */
19281 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19282 break;
19283 default:
19284 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19285 break;
19286 }
19287 return die;
19288 }
19289
19290 /* Generate the DIE for a base class. */
19291
19292 static void
19293 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19294 {
19295 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19296
19297 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19298 add_data_member_location_attribute (die, binfo);
19299
19300 if (BINFO_VIRTUAL_P (binfo))
19301 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19302
19303 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19304 children, otherwise the default is DW_ACCESS_public. In DWARF2
19305 the default has always been DW_ACCESS_private. */
19306 if (access == access_public_node)
19307 {
19308 if (dwarf_version == 2
19309 || context_die->die_tag == DW_TAG_class_type)
19310 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19311 }
19312 else if (access == access_protected_node)
19313 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19314 else if (dwarf_version > 2
19315 && context_die->die_tag != DW_TAG_class_type)
19316 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19317 }
19318
19319 /* Generate a DIE for a class member. */
19320
19321 static void
19322 gen_member_die (tree type, dw_die_ref context_die)
19323 {
19324 tree member;
19325 tree binfo = TYPE_BINFO (type);
19326 dw_die_ref child;
19327
19328 /* If this is not an incomplete type, output descriptions of each of its
19329 members. Note that as we output the DIEs necessary to represent the
19330 members of this record or union type, we will also be trying to output
19331 DIEs to represent the *types* of those members. However the `type'
19332 function (above) will specifically avoid generating type DIEs for member
19333 types *within* the list of member DIEs for this (containing) type except
19334 for those types (of members) which are explicitly marked as also being
19335 members of this (containing) type themselves. The g++ front- end can
19336 force any given type to be treated as a member of some other (containing)
19337 type by setting the TYPE_CONTEXT of the given (member) type to point to
19338 the TREE node representing the appropriate (containing) type. */
19339
19340 /* First output info about the base classes. */
19341 if (binfo)
19342 {
19343 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19344 int i;
19345 tree base;
19346
19347 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19348 gen_inheritance_die (base,
19349 (accesses ? (*accesses)[i] : access_public_node),
19350 context_die);
19351 }
19352
19353 /* Now output info about the data members and type members. */
19354 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19355 {
19356 /* If we thought we were generating minimal debug info for TYPE
19357 and then changed our minds, some of the member declarations
19358 may have already been defined. Don't define them again, but
19359 do put them in the right order. */
19360
19361 child = lookup_decl_die (member);
19362 if (child)
19363 splice_child_die (context_die, child);
19364 else
19365 gen_decl_die (member, NULL, context_die);
19366 }
19367
19368 /* Now output info about the function members (if any). */
19369 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19370 {
19371 /* Don't include clones in the member list. */
19372 if (DECL_ABSTRACT_ORIGIN (member))
19373 continue;
19374
19375 child = lookup_decl_die (member);
19376 if (child)
19377 splice_child_die (context_die, child);
19378 else
19379 gen_decl_die (member, NULL, context_die);
19380 }
19381 }
19382
19383 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19384 is set, we pretend that the type was never defined, so we only get the
19385 member DIEs needed by later specification DIEs. */
19386
19387 static void
19388 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19389 enum debug_info_usage usage)
19390 {
19391 dw_die_ref type_die = lookup_type_die (type);
19392 dw_die_ref scope_die = 0;
19393 int nested = 0;
19394 int complete = (TYPE_SIZE (type)
19395 && (! TYPE_STUB_DECL (type)
19396 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19397 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19398 complete = complete && should_emit_struct_debug (type, usage);
19399
19400 if (type_die && ! complete)
19401 return;
19402
19403 if (TYPE_CONTEXT (type) != NULL_TREE
19404 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19405 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19406 nested = 1;
19407
19408 scope_die = scope_die_for (type, context_die);
19409
19410 /* Generate child dies for template paramaters. */
19411 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19412 schedule_generic_params_dies_gen (type);
19413
19414 if (! type_die || (nested && is_cu_die (scope_die)))
19415 /* First occurrence of type or toplevel definition of nested class. */
19416 {
19417 dw_die_ref old_die = type_die;
19418
19419 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19420 ? record_type_tag (type) : DW_TAG_union_type,
19421 scope_die, type);
19422 equate_type_number_to_die (type, type_die);
19423 if (old_die)
19424 add_AT_specification (type_die, old_die);
19425 else
19426 add_name_attribute (type_die, type_tag (type));
19427 }
19428 else
19429 remove_AT (type_die, DW_AT_declaration);
19430
19431 /* If this type has been completed, then give it a byte_size attribute and
19432 then give a list of members. */
19433 if (complete && !ns_decl)
19434 {
19435 /* Prevent infinite recursion in cases where the type of some member of
19436 this type is expressed in terms of this type itself. */
19437 TREE_ASM_WRITTEN (type) = 1;
19438 add_byte_size_attribute (type_die, type);
19439 if (TYPE_STUB_DECL (type) != NULL_TREE)
19440 {
19441 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19442 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19443 }
19444
19445 /* If the first reference to this type was as the return type of an
19446 inline function, then it may not have a parent. Fix this now. */
19447 if (type_die->die_parent == NULL)
19448 add_child_die (scope_die, type_die);
19449
19450 push_decl_scope (type);
19451 gen_member_die (type, type_die);
19452 pop_decl_scope ();
19453
19454 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19455 if (TYPE_ARTIFICIAL (type))
19456 add_AT_flag (type_die, DW_AT_artificial, 1);
19457
19458 /* GNU extension: Record what type our vtable lives in. */
19459 if (TYPE_VFIELD (type))
19460 {
19461 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19462
19463 gen_type_die (vtype, context_die);
19464 add_AT_die_ref (type_die, DW_AT_containing_type,
19465 lookup_type_die (vtype));
19466 }
19467 }
19468 else
19469 {
19470 add_AT_flag (type_die, DW_AT_declaration, 1);
19471
19472 /* We don't need to do this for function-local types. */
19473 if (TYPE_STUB_DECL (type)
19474 && ! decl_function_context (TYPE_STUB_DECL (type)))
19475 vec_safe_push (incomplete_types, type);
19476 }
19477
19478 if (get_AT (type_die, DW_AT_name))
19479 add_pubtype (type, type_die);
19480 }
19481
19482 /* Generate a DIE for a subroutine _type_. */
19483
19484 static void
19485 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19486 {
19487 tree return_type = TREE_TYPE (type);
19488 dw_die_ref subr_die
19489 = new_die (DW_TAG_subroutine_type,
19490 scope_die_for (type, context_die), type);
19491
19492 equate_type_number_to_die (type, subr_die);
19493 add_prototyped_attribute (subr_die, type);
19494 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19495 gen_formal_types_die (type, subr_die);
19496
19497 if (get_AT (subr_die, DW_AT_name))
19498 add_pubtype (type, subr_die);
19499 }
19500
19501 /* Generate a DIE for a type definition. */
19502
19503 static void
19504 gen_typedef_die (tree decl, dw_die_ref context_die)
19505 {
19506 dw_die_ref type_die;
19507 tree origin;
19508
19509 if (TREE_ASM_WRITTEN (decl))
19510 return;
19511
19512 TREE_ASM_WRITTEN (decl) = 1;
19513 type_die = new_die (DW_TAG_typedef, context_die, decl);
19514 origin = decl_ultimate_origin (decl);
19515 if (origin != NULL)
19516 add_abstract_origin_attribute (type_die, origin);
19517 else
19518 {
19519 tree type;
19520
19521 add_name_and_src_coords_attributes (type_die, decl);
19522 if (DECL_ORIGINAL_TYPE (decl))
19523 {
19524 type = DECL_ORIGINAL_TYPE (decl);
19525
19526 gcc_assert (type != TREE_TYPE (decl));
19527 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19528 }
19529 else
19530 {
19531 type = TREE_TYPE (decl);
19532
19533 if (is_naming_typedef_decl (TYPE_NAME (type)))
19534 {
19535 /* Here, we are in the case of decl being a typedef naming
19536 an anonymous type, e.g:
19537 typedef struct {...} foo;
19538 In that case TREE_TYPE (decl) is not a typedef variant
19539 type and TYPE_NAME of the anonymous type is set to the
19540 TYPE_DECL of the typedef. This construct is emitted by
19541 the C++ FE.
19542
19543 TYPE is the anonymous struct named by the typedef
19544 DECL. As we need the DW_AT_type attribute of the
19545 DW_TAG_typedef to point to the DIE of TYPE, let's
19546 generate that DIE right away. add_type_attribute
19547 called below will then pick (via lookup_type_die) that
19548 anonymous struct DIE. */
19549 if (!TREE_ASM_WRITTEN (type))
19550 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19551
19552 /* This is a GNU Extension. We are adding a
19553 DW_AT_linkage_name attribute to the DIE of the
19554 anonymous struct TYPE. The value of that attribute
19555 is the name of the typedef decl naming the anonymous
19556 struct. This greatly eases the work of consumers of
19557 this debug info. */
19558 add_linkage_attr (lookup_type_die (type), decl);
19559 }
19560 }
19561
19562 add_type_attribute (type_die, type, TREE_READONLY (decl),
19563 TREE_THIS_VOLATILE (decl), context_die);
19564
19565 if (is_naming_typedef_decl (decl))
19566 /* We want that all subsequent calls to lookup_type_die with
19567 TYPE in argument yield the DW_TAG_typedef we have just
19568 created. */
19569 equate_type_number_to_die (type, type_die);
19570
19571 add_accessibility_attribute (type_die, decl);
19572 }
19573
19574 if (DECL_ABSTRACT (decl))
19575 equate_decl_number_to_die (decl, type_die);
19576
19577 if (get_AT (type_die, DW_AT_name))
19578 add_pubtype (decl, type_die);
19579 }
19580
19581 /* Generate a DIE for a struct, class, enum or union type. */
19582
19583 static void
19584 gen_tagged_type_die (tree type,
19585 dw_die_ref context_die,
19586 enum debug_info_usage usage)
19587 {
19588 int need_pop;
19589
19590 if (type == NULL_TREE
19591 || !is_tagged_type (type))
19592 return;
19593
19594 /* If this is a nested type whose containing class hasn't been written
19595 out yet, writing it out will cover this one, too. This does not apply
19596 to instantiations of member class templates; they need to be added to
19597 the containing class as they are generated. FIXME: This hurts the
19598 idea of combining type decls from multiple TUs, since we can't predict
19599 what set of template instantiations we'll get. */
19600 if (TYPE_CONTEXT (type)
19601 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19602 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19603 {
19604 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19605
19606 if (TREE_ASM_WRITTEN (type))
19607 return;
19608
19609 /* If that failed, attach ourselves to the stub. */
19610 push_decl_scope (TYPE_CONTEXT (type));
19611 context_die = lookup_type_die (TYPE_CONTEXT (type));
19612 need_pop = 1;
19613 }
19614 else if (TYPE_CONTEXT (type) != NULL_TREE
19615 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19616 {
19617 /* If this type is local to a function that hasn't been written
19618 out yet, use a NULL context for now; it will be fixed up in
19619 decls_for_scope. */
19620 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19621 /* A declaration DIE doesn't count; nested types need to go in the
19622 specification. */
19623 if (context_die && is_declaration_die (context_die))
19624 context_die = NULL;
19625 need_pop = 0;
19626 }
19627 else
19628 {
19629 context_die = declare_in_namespace (type, context_die);
19630 need_pop = 0;
19631 }
19632
19633 if (TREE_CODE (type) == ENUMERAL_TYPE)
19634 {
19635 /* This might have been written out by the call to
19636 declare_in_namespace. */
19637 if (!TREE_ASM_WRITTEN (type))
19638 gen_enumeration_type_die (type, context_die);
19639 }
19640 else
19641 gen_struct_or_union_type_die (type, context_die, usage);
19642
19643 if (need_pop)
19644 pop_decl_scope ();
19645
19646 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19647 it up if it is ever completed. gen_*_type_die will set it for us
19648 when appropriate. */
19649 }
19650
19651 /* Generate a type description DIE. */
19652
19653 static void
19654 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19655 enum debug_info_usage usage)
19656 {
19657 struct array_descr_info info;
19658
19659 if (type == NULL_TREE || type == error_mark_node)
19660 return;
19661
19662 if (TYPE_NAME (type) != NULL_TREE
19663 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19664 && is_redundant_typedef (TYPE_NAME (type))
19665 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19666 /* The DECL of this type is a typedef we don't want to emit debug
19667 info for but we want debug info for its underlying typedef.
19668 This can happen for e.g, the injected-class-name of a C++
19669 type. */
19670 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19671
19672 /* If TYPE is a typedef type variant, let's generate debug info
19673 for the parent typedef which TYPE is a type of. */
19674 if (typedef_variant_p (type))
19675 {
19676 if (TREE_ASM_WRITTEN (type))
19677 return;
19678
19679 /* Prevent broken recursion; we can't hand off to the same type. */
19680 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19681
19682 /* Give typedefs the right scope. */
19683 context_die = scope_die_for (type, context_die);
19684
19685 TREE_ASM_WRITTEN (type) = 1;
19686
19687 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19688 return;
19689 }
19690
19691 /* If type is an anonymous tagged type named by a typedef, let's
19692 generate debug info for the typedef. */
19693 if (is_naming_typedef_decl (TYPE_NAME (type)))
19694 {
19695 /* Use the DIE of the containing namespace as the parent DIE of
19696 the type description DIE we want to generate. */
19697 if (DECL_CONTEXT (TYPE_NAME (type))
19698 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19699 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19700
19701 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19702 return;
19703 }
19704
19705 /* If this is an array type with hidden descriptor, handle it first. */
19706 if (!TREE_ASM_WRITTEN (type)
19707 && lang_hooks.types.get_array_descr_info
19708 && lang_hooks.types.get_array_descr_info (type, &info)
19709 && (dwarf_version >= 3 || !dwarf_strict))
19710 {
19711 gen_descr_array_type_die (type, &info, context_die);
19712 TREE_ASM_WRITTEN (type) = 1;
19713 return;
19714 }
19715
19716 /* We are going to output a DIE to represent the unqualified version
19717 of this type (i.e. without any const or volatile qualifiers) so
19718 get the main variant (i.e. the unqualified version) of this type
19719 now. (Vectors are special because the debugging info is in the
19720 cloned type itself). */
19721 if (TREE_CODE (type) != VECTOR_TYPE)
19722 type = type_main_variant (type);
19723
19724 if (TREE_ASM_WRITTEN (type))
19725 return;
19726
19727 switch (TREE_CODE (type))
19728 {
19729 case ERROR_MARK:
19730 break;
19731
19732 case POINTER_TYPE:
19733 case REFERENCE_TYPE:
19734 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19735 ensures that the gen_type_die recursion will terminate even if the
19736 type is recursive. Recursive types are possible in Ada. */
19737 /* ??? We could perhaps do this for all types before the switch
19738 statement. */
19739 TREE_ASM_WRITTEN (type) = 1;
19740
19741 /* For these types, all that is required is that we output a DIE (or a
19742 set of DIEs) to represent the "basis" type. */
19743 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19744 DINFO_USAGE_IND_USE);
19745 break;
19746
19747 case OFFSET_TYPE:
19748 /* This code is used for C++ pointer-to-data-member types.
19749 Output a description of the relevant class type. */
19750 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19751 DINFO_USAGE_IND_USE);
19752
19753 /* Output a description of the type of the object pointed to. */
19754 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19755 DINFO_USAGE_IND_USE);
19756
19757 /* Now output a DIE to represent this pointer-to-data-member type
19758 itself. */
19759 gen_ptr_to_mbr_type_die (type, context_die);
19760 break;
19761
19762 case FUNCTION_TYPE:
19763 /* Force out return type (in case it wasn't forced out already). */
19764 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19765 DINFO_USAGE_DIR_USE);
19766 gen_subroutine_type_die (type, context_die);
19767 break;
19768
19769 case METHOD_TYPE:
19770 /* Force out return type (in case it wasn't forced out already). */
19771 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19772 DINFO_USAGE_DIR_USE);
19773 gen_subroutine_type_die (type, context_die);
19774 break;
19775
19776 case ARRAY_TYPE:
19777 gen_array_type_die (type, context_die);
19778 break;
19779
19780 case VECTOR_TYPE:
19781 gen_array_type_die (type, context_die);
19782 break;
19783
19784 case ENUMERAL_TYPE:
19785 case RECORD_TYPE:
19786 case UNION_TYPE:
19787 case QUAL_UNION_TYPE:
19788 gen_tagged_type_die (type, context_die, usage);
19789 return;
19790
19791 case VOID_TYPE:
19792 case INTEGER_TYPE:
19793 case REAL_TYPE:
19794 case FIXED_POINT_TYPE:
19795 case COMPLEX_TYPE:
19796 case BOOLEAN_TYPE:
19797 /* No DIEs needed for fundamental types. */
19798 break;
19799
19800 case NULLPTR_TYPE:
19801 case LANG_TYPE:
19802 /* Just use DW_TAG_unspecified_type. */
19803 {
19804 dw_die_ref type_die = lookup_type_die (type);
19805 if (type_die == NULL)
19806 {
19807 tree name = TYPE_NAME (type);
19808 if (TREE_CODE (name) == TYPE_DECL)
19809 name = DECL_NAME (name);
19810 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19811 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19812 equate_type_number_to_die (type, type_die);
19813 }
19814 }
19815 break;
19816
19817 default:
19818 gcc_unreachable ();
19819 }
19820
19821 TREE_ASM_WRITTEN (type) = 1;
19822 }
19823
19824 static void
19825 gen_type_die (tree type, dw_die_ref context_die)
19826 {
19827 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19828 }
19829
19830 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19831 things which are local to the given block. */
19832
19833 static void
19834 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19835 {
19836 int must_output_die = 0;
19837 bool inlined_func;
19838
19839 /* Ignore blocks that are NULL. */
19840 if (stmt == NULL_TREE)
19841 return;
19842
19843 inlined_func = inlined_function_outer_scope_p (stmt);
19844
19845 /* If the block is one fragment of a non-contiguous block, do not
19846 process the variables, since they will have been done by the
19847 origin block. Do process subblocks. */
19848 if (BLOCK_FRAGMENT_ORIGIN (stmt))
19849 {
19850 tree sub;
19851
19852 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19853 gen_block_die (sub, context_die, depth + 1);
19854
19855 return;
19856 }
19857
19858 /* Determine if we need to output any Dwarf DIEs at all to represent this
19859 block. */
19860 if (inlined_func)
19861 /* The outer scopes for inlinings *must* always be represented. We
19862 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
19863 must_output_die = 1;
19864 else
19865 {
19866 /* Determine if this block directly contains any "significant"
19867 local declarations which we will need to output DIEs for. */
19868 if (debug_info_level > DINFO_LEVEL_TERSE)
19869 /* We are not in terse mode so *any* local declaration counts
19870 as being a "significant" one. */
19871 must_output_die = ((BLOCK_VARS (stmt) != NULL
19872 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19873 && (TREE_USED (stmt)
19874 || TREE_ASM_WRITTEN (stmt)
19875 || BLOCK_ABSTRACT (stmt)));
19876 else if ((TREE_USED (stmt)
19877 || TREE_ASM_WRITTEN (stmt)
19878 || BLOCK_ABSTRACT (stmt))
19879 && !dwarf2out_ignore_block (stmt))
19880 must_output_die = 1;
19881 }
19882
19883 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19884 DIE for any block which contains no significant local declarations at
19885 all. Rather, in such cases we just call `decls_for_scope' so that any
19886 needed Dwarf info for any sub-blocks will get properly generated. Note
19887 that in terse mode, our definition of what constitutes a "significant"
19888 local declaration gets restricted to include only inlined function
19889 instances and local (nested) function definitions. */
19890 if (must_output_die)
19891 {
19892 if (inlined_func)
19893 {
19894 /* If STMT block is abstract, that means we have been called
19895 indirectly from dwarf2out_abstract_function.
19896 That function rightfully marks the descendent blocks (of
19897 the abstract function it is dealing with) as being abstract,
19898 precisely to prevent us from emitting any
19899 DW_TAG_inlined_subroutine DIE as a descendent
19900 of an abstract function instance. So in that case, we should
19901 not call gen_inlined_subroutine_die.
19902
19903 Later though, when cgraph asks dwarf2out to emit info
19904 for the concrete instance of the function decl into which
19905 the concrete instance of STMT got inlined, the later will lead
19906 to the generation of a DW_TAG_inlined_subroutine DIE. */
19907 if (! BLOCK_ABSTRACT (stmt))
19908 gen_inlined_subroutine_die (stmt, context_die, depth);
19909 }
19910 else
19911 gen_lexical_block_die (stmt, context_die, depth);
19912 }
19913 else
19914 decls_for_scope (stmt, context_die, depth);
19915 }
19916
19917 /* Process variable DECL (or variable with origin ORIGIN) within
19918 block STMT and add it to CONTEXT_DIE. */
19919 static void
19920 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19921 {
19922 dw_die_ref die;
19923 tree decl_or_origin = decl ? decl : origin;
19924
19925 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19926 die = lookup_decl_die (decl_or_origin);
19927 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19928 && TYPE_DECL_IS_STUB (decl_or_origin))
19929 die = lookup_type_die (TREE_TYPE (decl_or_origin));
19930 else
19931 die = NULL;
19932
19933 if (die != NULL && die->die_parent == NULL)
19934 add_child_die (context_die, die);
19935 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19936 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19937 stmt, context_die);
19938 else
19939 gen_decl_die (decl, origin, context_die);
19940 }
19941
19942 /* Generate all of the decls declared within a given scope and (recursively)
19943 all of its sub-blocks. */
19944
19945 static void
19946 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19947 {
19948 tree decl;
19949 unsigned int i;
19950 tree subblocks;
19951
19952 /* Ignore NULL blocks. */
19953 if (stmt == NULL_TREE)
19954 return;
19955
19956 /* Output the DIEs to represent all of the data objects and typedefs
19957 declared directly within this block but not within any nested
19958 sub-blocks. Also, nested function and tag DIEs have been
19959 generated with a parent of NULL; fix that up now. */
19960 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19961 process_scope_var (stmt, decl, NULL_TREE, context_die);
19962 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19963 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19964 context_die);
19965
19966 /* If we're at -g1, we're not interested in subblocks. */
19967 if (debug_info_level <= DINFO_LEVEL_TERSE)
19968 return;
19969
19970 /* Output the DIEs to represent all sub-blocks (and the items declared
19971 therein) of this block. */
19972 for (subblocks = BLOCK_SUBBLOCKS (stmt);
19973 subblocks != NULL;
19974 subblocks = BLOCK_CHAIN (subblocks))
19975 gen_block_die (subblocks, context_die, depth + 1);
19976 }
19977
19978 /* Is this a typedef we can avoid emitting? */
19979
19980 static inline int
19981 is_redundant_typedef (const_tree decl)
19982 {
19983 if (TYPE_DECL_IS_STUB (decl))
19984 return 1;
19985
19986 if (DECL_ARTIFICIAL (decl)
19987 && DECL_CONTEXT (decl)
19988 && is_tagged_type (DECL_CONTEXT (decl))
19989 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19990 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19991 /* Also ignore the artificial member typedef for the class name. */
19992 return 1;
19993
19994 return 0;
19995 }
19996
19997 /* Return TRUE if TYPE is a typedef that names a type for linkage
19998 purposes. This kind of typedefs is produced by the C++ FE for
19999 constructs like:
20000
20001 typedef struct {...} foo;
20002
20003 In that case, there is no typedef variant type produced for foo.
20004 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20005 struct type. */
20006
20007 static bool
20008 is_naming_typedef_decl (const_tree decl)
20009 {
20010 if (decl == NULL_TREE
20011 || TREE_CODE (decl) != TYPE_DECL
20012 || !is_tagged_type (TREE_TYPE (decl))
20013 || DECL_IS_BUILTIN (decl)
20014 || is_redundant_typedef (decl)
20015 /* It looks like Ada produces TYPE_DECLs that are very similar
20016 to C++ naming typedefs but that have different
20017 semantics. Let's be specific to c++ for now. */
20018 || !is_cxx ())
20019 return FALSE;
20020
20021 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20022 && TYPE_NAME (TREE_TYPE (decl)) == decl
20023 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20024 != TYPE_NAME (TREE_TYPE (decl))));
20025 }
20026
20027 /* Returns the DIE for a context. */
20028
20029 static inline dw_die_ref
20030 get_context_die (tree context)
20031 {
20032 if (context)
20033 {
20034 /* Find die that represents this context. */
20035 if (TYPE_P (context))
20036 {
20037 context = TYPE_MAIN_VARIANT (context);
20038 return strip_naming_typedef (context, force_type_die (context));
20039 }
20040 else
20041 return force_decl_die (context);
20042 }
20043 return comp_unit_die ();
20044 }
20045
20046 /* Returns the DIE for decl. A DIE will always be returned. */
20047
20048 static dw_die_ref
20049 force_decl_die (tree decl)
20050 {
20051 dw_die_ref decl_die;
20052 unsigned saved_external_flag;
20053 tree save_fn = NULL_TREE;
20054 decl_die = lookup_decl_die (decl);
20055 if (!decl_die)
20056 {
20057 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20058
20059 decl_die = lookup_decl_die (decl);
20060 if (decl_die)
20061 return decl_die;
20062
20063 switch (TREE_CODE (decl))
20064 {
20065 case FUNCTION_DECL:
20066 /* Clear current_function_decl, so that gen_subprogram_die thinks
20067 that this is a declaration. At this point, we just want to force
20068 declaration die. */
20069 save_fn = current_function_decl;
20070 current_function_decl = NULL_TREE;
20071 gen_subprogram_die (decl, context_die);
20072 current_function_decl = save_fn;
20073 break;
20074
20075 case VAR_DECL:
20076 /* Set external flag to force declaration die. Restore it after
20077 gen_decl_die() call. */
20078 saved_external_flag = DECL_EXTERNAL (decl);
20079 DECL_EXTERNAL (decl) = 1;
20080 gen_decl_die (decl, NULL, context_die);
20081 DECL_EXTERNAL (decl) = saved_external_flag;
20082 break;
20083
20084 case NAMESPACE_DECL:
20085 if (dwarf_version >= 3 || !dwarf_strict)
20086 dwarf2out_decl (decl);
20087 else
20088 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20089 decl_die = comp_unit_die ();
20090 break;
20091
20092 case TRANSLATION_UNIT_DECL:
20093 decl_die = comp_unit_die ();
20094 break;
20095
20096 default:
20097 gcc_unreachable ();
20098 }
20099
20100 /* We should be able to find the DIE now. */
20101 if (!decl_die)
20102 decl_die = lookup_decl_die (decl);
20103 gcc_assert (decl_die);
20104 }
20105
20106 return decl_die;
20107 }
20108
20109 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20110 always returned. */
20111
20112 static dw_die_ref
20113 force_type_die (tree type)
20114 {
20115 dw_die_ref type_die;
20116
20117 type_die = lookup_type_die (type);
20118 if (!type_die)
20119 {
20120 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20121
20122 type_die = modified_type_die (type, TYPE_READONLY (type),
20123 TYPE_VOLATILE (type), context_die);
20124 gcc_assert (type_die);
20125 }
20126 return type_die;
20127 }
20128
20129 /* Force out any required namespaces to be able to output DECL,
20130 and return the new context_die for it, if it's changed. */
20131
20132 static dw_die_ref
20133 setup_namespace_context (tree thing, dw_die_ref context_die)
20134 {
20135 tree context = (DECL_P (thing)
20136 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20137 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20138 /* Force out the namespace. */
20139 context_die = force_decl_die (context);
20140
20141 return context_die;
20142 }
20143
20144 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20145 type) within its namespace, if appropriate.
20146
20147 For compatibility with older debuggers, namespace DIEs only contain
20148 declarations; all definitions are emitted at CU scope. */
20149
20150 static dw_die_ref
20151 declare_in_namespace (tree thing, dw_die_ref context_die)
20152 {
20153 dw_die_ref ns_context;
20154
20155 if (debug_info_level <= DINFO_LEVEL_TERSE)
20156 return context_die;
20157
20158 /* If this decl is from an inlined function, then don't try to emit it in its
20159 namespace, as we will get confused. It would have already been emitted
20160 when the abstract instance of the inline function was emitted anyways. */
20161 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20162 return context_die;
20163
20164 ns_context = setup_namespace_context (thing, context_die);
20165
20166 if (ns_context != context_die)
20167 {
20168 if (is_fortran ())
20169 return ns_context;
20170 if (DECL_P (thing))
20171 gen_decl_die (thing, NULL, ns_context);
20172 else
20173 gen_type_die (thing, ns_context);
20174 }
20175 return context_die;
20176 }
20177
20178 /* Generate a DIE for a namespace or namespace alias. */
20179
20180 static void
20181 gen_namespace_die (tree decl, dw_die_ref context_die)
20182 {
20183 dw_die_ref namespace_die;
20184
20185 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20186 they are an alias of. */
20187 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20188 {
20189 /* Output a real namespace or module. */
20190 context_die = setup_namespace_context (decl, comp_unit_die ());
20191 namespace_die = new_die (is_fortran ()
20192 ? DW_TAG_module : DW_TAG_namespace,
20193 context_die, decl);
20194 /* For Fortran modules defined in different CU don't add src coords. */
20195 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20196 {
20197 const char *name = dwarf2_name (decl, 0);
20198 if (name)
20199 add_name_attribute (namespace_die, name);
20200 }
20201 else
20202 add_name_and_src_coords_attributes (namespace_die, decl);
20203 if (DECL_EXTERNAL (decl))
20204 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20205 equate_decl_number_to_die (decl, namespace_die);
20206 }
20207 else
20208 {
20209 /* Output a namespace alias. */
20210
20211 /* Force out the namespace we are an alias of, if necessary. */
20212 dw_die_ref origin_die
20213 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20214
20215 if (DECL_FILE_SCOPE_P (decl)
20216 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20217 context_die = setup_namespace_context (decl, comp_unit_die ());
20218 /* Now create the namespace alias DIE. */
20219 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20220 add_name_and_src_coords_attributes (namespace_die, decl);
20221 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20222 equate_decl_number_to_die (decl, namespace_die);
20223 }
20224 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20225 if (want_pubnames ())
20226 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20227 }
20228
20229 /* Generate Dwarf debug information for a decl described by DECL.
20230 The return value is currently only meaningful for PARM_DECLs,
20231 for all other decls it returns NULL. */
20232
20233 static dw_die_ref
20234 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20235 {
20236 tree decl_or_origin = decl ? decl : origin;
20237 tree class_origin = NULL, ultimate_origin;
20238
20239 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20240 return NULL;
20241
20242 switch (TREE_CODE (decl_or_origin))
20243 {
20244 case ERROR_MARK:
20245 break;
20246
20247 case CONST_DECL:
20248 if (!is_fortran () && !is_ada ())
20249 {
20250 /* The individual enumerators of an enum type get output when we output
20251 the Dwarf representation of the relevant enum type itself. */
20252 break;
20253 }
20254
20255 /* Emit its type. */
20256 gen_type_die (TREE_TYPE (decl), context_die);
20257
20258 /* And its containing namespace. */
20259 context_die = declare_in_namespace (decl, context_die);
20260
20261 gen_const_die (decl, context_die);
20262 break;
20263
20264 case FUNCTION_DECL:
20265 /* Don't output any DIEs to represent mere function declarations,
20266 unless they are class members or explicit block externs. */
20267 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20268 && DECL_FILE_SCOPE_P (decl_or_origin)
20269 && (current_function_decl == NULL_TREE
20270 || DECL_ARTIFICIAL (decl_or_origin)))
20271 break;
20272
20273 #if 0
20274 /* FIXME */
20275 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20276 on local redeclarations of global functions. That seems broken. */
20277 if (current_function_decl != decl)
20278 /* This is only a declaration. */;
20279 #endif
20280
20281 /* If we're emitting a clone, emit info for the abstract instance. */
20282 if (origin || DECL_ORIGIN (decl) != decl)
20283 dwarf2out_abstract_function (origin
20284 ? DECL_ORIGIN (origin)
20285 : DECL_ABSTRACT_ORIGIN (decl));
20286
20287 /* If we're emitting an out-of-line copy of an inline function,
20288 emit info for the abstract instance and set up to refer to it. */
20289 else if (cgraph_function_possibly_inlined_p (decl)
20290 && ! DECL_ABSTRACT (decl)
20291 && ! class_or_namespace_scope_p (context_die)
20292 /* dwarf2out_abstract_function won't emit a die if this is just
20293 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20294 that case, because that works only if we have a die. */
20295 && DECL_INITIAL (decl) != NULL_TREE)
20296 {
20297 dwarf2out_abstract_function (decl);
20298 set_decl_origin_self (decl);
20299 }
20300
20301 /* Otherwise we're emitting the primary DIE for this decl. */
20302 else if (debug_info_level > DINFO_LEVEL_TERSE)
20303 {
20304 /* Before we describe the FUNCTION_DECL itself, make sure that we
20305 have its containing type. */
20306 if (!origin)
20307 origin = decl_class_context (decl);
20308 if (origin != NULL_TREE)
20309 gen_type_die (origin, context_die);
20310
20311 /* And its return type. */
20312 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20313
20314 /* And its virtual context. */
20315 if (DECL_VINDEX (decl) != NULL_TREE)
20316 gen_type_die (DECL_CONTEXT (decl), context_die);
20317
20318 /* Make sure we have a member DIE for decl. */
20319 if (origin != NULL_TREE)
20320 gen_type_die_for_member (origin, decl, context_die);
20321
20322 /* And its containing namespace. */
20323 context_die = declare_in_namespace (decl, context_die);
20324 }
20325
20326 /* Now output a DIE to represent the function itself. */
20327 if (decl)
20328 gen_subprogram_die (decl, context_die);
20329 break;
20330
20331 case TYPE_DECL:
20332 /* If we are in terse mode, don't generate any DIEs to represent any
20333 actual typedefs. */
20334 if (debug_info_level <= DINFO_LEVEL_TERSE)
20335 break;
20336
20337 /* In the special case of a TYPE_DECL node representing the declaration
20338 of some type tag, if the given TYPE_DECL is marked as having been
20339 instantiated from some other (original) TYPE_DECL node (e.g. one which
20340 was generated within the original definition of an inline function) we
20341 used to generate a special (abbreviated) DW_TAG_structure_type,
20342 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20343 should be actually referencing those DIEs, as variable DIEs with that
20344 type would be emitted already in the abstract origin, so it was always
20345 removed during unused type prunning. Don't add anything in this
20346 case. */
20347 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20348 break;
20349
20350 if (is_redundant_typedef (decl))
20351 gen_type_die (TREE_TYPE (decl), context_die);
20352 else
20353 /* Output a DIE to represent the typedef itself. */
20354 gen_typedef_die (decl, context_die);
20355 break;
20356
20357 case LABEL_DECL:
20358 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20359 gen_label_die (decl, context_die);
20360 break;
20361
20362 case VAR_DECL:
20363 case RESULT_DECL:
20364 /* If we are in terse mode, don't generate any DIEs to represent any
20365 variable declarations or definitions. */
20366 if (debug_info_level <= DINFO_LEVEL_TERSE)
20367 break;
20368
20369 /* Output any DIEs that are needed to specify the type of this data
20370 object. */
20371 if (decl_by_reference_p (decl_or_origin))
20372 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20373 else
20374 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20375
20376 /* And its containing type. */
20377 class_origin = decl_class_context (decl_or_origin);
20378 if (class_origin != NULL_TREE)
20379 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20380
20381 /* And its containing namespace. */
20382 context_die = declare_in_namespace (decl_or_origin, context_die);
20383
20384 /* Now output the DIE to represent the data object itself. This gets
20385 complicated because of the possibility that the VAR_DECL really
20386 represents an inlined instance of a formal parameter for an inline
20387 function. */
20388 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20389 if (ultimate_origin != NULL_TREE
20390 && TREE_CODE (ultimate_origin) == PARM_DECL)
20391 gen_formal_parameter_die (decl, origin,
20392 true /* Emit name attribute. */,
20393 context_die);
20394 else
20395 gen_variable_die (decl, origin, context_die);
20396 break;
20397
20398 case FIELD_DECL:
20399 /* Ignore the nameless fields that are used to skip bits but handle C++
20400 anonymous unions and structs. */
20401 if (DECL_NAME (decl) != NULL_TREE
20402 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20403 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20404 {
20405 gen_type_die (member_declared_type (decl), context_die);
20406 gen_field_die (decl, context_die);
20407 }
20408 break;
20409
20410 case PARM_DECL:
20411 if (DECL_BY_REFERENCE (decl_or_origin))
20412 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20413 else
20414 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20415 return gen_formal_parameter_die (decl, origin,
20416 true /* Emit name attribute. */,
20417 context_die);
20418
20419 case NAMESPACE_DECL:
20420 case IMPORTED_DECL:
20421 if (dwarf_version >= 3 || !dwarf_strict)
20422 gen_namespace_die (decl, context_die);
20423 break;
20424
20425 default:
20426 /* Probably some frontend-internal decl. Assume we don't care. */
20427 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20428 break;
20429 }
20430
20431 return NULL;
20432 }
20433 \f
20434 /* Output debug information for global decl DECL. Called from toplev.c after
20435 compilation proper has finished. */
20436
20437 static void
20438 dwarf2out_global_decl (tree decl)
20439 {
20440 /* Output DWARF2 information for file-scope tentative data object
20441 declarations, file-scope (extern) function declarations (which
20442 had no corresponding body) and file-scope tagged type declarations
20443 and definitions which have not yet been forced out. */
20444 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20445 dwarf2out_decl (decl);
20446 }
20447
20448 /* Output debug information for type decl DECL. Called from toplev.c
20449 and from language front ends (to record built-in types). */
20450 static void
20451 dwarf2out_type_decl (tree decl, int local)
20452 {
20453 if (!local)
20454 dwarf2out_decl (decl);
20455 }
20456
20457 /* Output debug information for imported module or decl DECL.
20458 NAME is non-NULL name in the lexical block if the decl has been renamed.
20459 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20460 that DECL belongs to.
20461 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20462 static void
20463 dwarf2out_imported_module_or_decl_1 (tree decl,
20464 tree name,
20465 tree lexical_block,
20466 dw_die_ref lexical_block_die)
20467 {
20468 expanded_location xloc;
20469 dw_die_ref imported_die = NULL;
20470 dw_die_ref at_import_die;
20471
20472 if (TREE_CODE (decl) == IMPORTED_DECL)
20473 {
20474 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20475 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20476 gcc_assert (decl);
20477 }
20478 else
20479 xloc = expand_location (input_location);
20480
20481 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20482 {
20483 at_import_die = force_type_die (TREE_TYPE (decl));
20484 /* For namespace N { typedef void T; } using N::T; base_type_die
20485 returns NULL, but DW_TAG_imported_declaration requires
20486 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20487 if (!at_import_die)
20488 {
20489 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20490 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20491 at_import_die = lookup_type_die (TREE_TYPE (decl));
20492 gcc_assert (at_import_die);
20493 }
20494 }
20495 else
20496 {
20497 at_import_die = lookup_decl_die (decl);
20498 if (!at_import_die)
20499 {
20500 /* If we're trying to avoid duplicate debug info, we may not have
20501 emitted the member decl for this field. Emit it now. */
20502 if (TREE_CODE (decl) == FIELD_DECL)
20503 {
20504 tree type = DECL_CONTEXT (decl);
20505
20506 if (TYPE_CONTEXT (type)
20507 && TYPE_P (TYPE_CONTEXT (type))
20508 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20509 DINFO_USAGE_DIR_USE))
20510 return;
20511 gen_type_die_for_member (type, decl,
20512 get_context_die (TYPE_CONTEXT (type)));
20513 }
20514 at_import_die = force_decl_die (decl);
20515 }
20516 }
20517
20518 if (TREE_CODE (decl) == NAMESPACE_DECL)
20519 {
20520 if (dwarf_version >= 3 || !dwarf_strict)
20521 imported_die = new_die (DW_TAG_imported_module,
20522 lexical_block_die,
20523 lexical_block);
20524 else
20525 return;
20526 }
20527 else
20528 imported_die = new_die (DW_TAG_imported_declaration,
20529 lexical_block_die,
20530 lexical_block);
20531
20532 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20533 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20534 if (name)
20535 add_AT_string (imported_die, DW_AT_name,
20536 IDENTIFIER_POINTER (name));
20537 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20538 }
20539
20540 /* Output debug information for imported module or decl DECL.
20541 NAME is non-NULL name in context if the decl has been renamed.
20542 CHILD is true if decl is one of the renamed decls as part of
20543 importing whole module. */
20544
20545 static void
20546 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20547 bool child)
20548 {
20549 /* dw_die_ref at_import_die; */
20550 dw_die_ref scope_die;
20551
20552 if (debug_info_level <= DINFO_LEVEL_TERSE)
20553 return;
20554
20555 gcc_assert (decl);
20556
20557 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20558 We need decl DIE for reference and scope die. First, get DIE for the decl
20559 itself. */
20560
20561 /* Get the scope die for decl context. Use comp_unit_die for global module
20562 or decl. If die is not found for non globals, force new die. */
20563 if (context
20564 && TYPE_P (context)
20565 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20566 return;
20567
20568 if (!(dwarf_version >= 3 || !dwarf_strict))
20569 return;
20570
20571 scope_die = get_context_die (context);
20572
20573 if (child)
20574 {
20575 gcc_assert (scope_die->die_child);
20576 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20577 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20578 scope_die = scope_die->die_child;
20579 }
20580
20581 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20582 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20583
20584 }
20585
20586 /* Write the debugging output for DECL. */
20587
20588 void
20589 dwarf2out_decl (tree decl)
20590 {
20591 dw_die_ref context_die = comp_unit_die ();
20592
20593 switch (TREE_CODE (decl))
20594 {
20595 case ERROR_MARK:
20596 return;
20597
20598 case FUNCTION_DECL:
20599 /* What we would really like to do here is to filter out all mere
20600 file-scope declarations of file-scope functions which are never
20601 referenced later within this translation unit (and keep all of ones
20602 that *are* referenced later on) but we aren't clairvoyant, so we have
20603 no idea which functions will be referenced in the future (i.e. later
20604 on within the current translation unit). So here we just ignore all
20605 file-scope function declarations which are not also definitions. If
20606 and when the debugger needs to know something about these functions,
20607 it will have to hunt around and find the DWARF information associated
20608 with the definition of the function.
20609
20610 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20611 nodes represent definitions and which ones represent mere
20612 declarations. We have to check DECL_INITIAL instead. That's because
20613 the C front-end supports some weird semantics for "extern inline"
20614 function definitions. These can get inlined within the current
20615 translation unit (and thus, we need to generate Dwarf info for their
20616 abstract instances so that the Dwarf info for the concrete inlined
20617 instances can have something to refer to) but the compiler never
20618 generates any out-of-lines instances of such things (despite the fact
20619 that they *are* definitions).
20620
20621 The important point is that the C front-end marks these "extern
20622 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20623 them anyway. Note that the C++ front-end also plays some similar games
20624 for inline function definitions appearing within include files which
20625 also contain `#pragma interface' pragmas.
20626
20627 If we are called from dwarf2out_abstract_function output a DIE
20628 anyway. We can end up here this way with early inlining and LTO
20629 where the inlined function is output in a different LTRANS unit
20630 or not at all. */
20631 if (DECL_INITIAL (decl) == NULL_TREE
20632 && ! DECL_ABSTRACT (decl))
20633 return;
20634
20635 /* If we're a nested function, initially use a parent of NULL; if we're
20636 a plain function, this will be fixed up in decls_for_scope. If
20637 we're a method, it will be ignored, since we already have a DIE. */
20638 if (decl_function_context (decl)
20639 /* But if we're in terse mode, we don't care about scope. */
20640 && debug_info_level > DINFO_LEVEL_TERSE)
20641 context_die = NULL;
20642 break;
20643
20644 case VAR_DECL:
20645 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20646 declaration and if the declaration was never even referenced from
20647 within this entire compilation unit. We suppress these DIEs in
20648 order to save space in the .debug section (by eliminating entries
20649 which are probably useless). Note that we must not suppress
20650 block-local extern declarations (whether used or not) because that
20651 would screw-up the debugger's name lookup mechanism and cause it to
20652 miss things which really ought to be in scope at a given point. */
20653 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20654 return;
20655
20656 /* For local statics lookup proper context die. */
20657 if (TREE_STATIC (decl)
20658 && DECL_CONTEXT (decl)
20659 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20660 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20661
20662 /* If we are in terse mode, don't generate any DIEs to represent any
20663 variable declarations or definitions. */
20664 if (debug_info_level <= DINFO_LEVEL_TERSE)
20665 return;
20666 break;
20667
20668 case CONST_DECL:
20669 if (debug_info_level <= DINFO_LEVEL_TERSE)
20670 return;
20671 if (!is_fortran () && !is_ada ())
20672 return;
20673 if (TREE_STATIC (decl) && decl_function_context (decl))
20674 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20675 break;
20676
20677 case NAMESPACE_DECL:
20678 case IMPORTED_DECL:
20679 if (debug_info_level <= DINFO_LEVEL_TERSE)
20680 return;
20681 if (lookup_decl_die (decl) != NULL)
20682 return;
20683 break;
20684
20685 case TYPE_DECL:
20686 /* Don't emit stubs for types unless they are needed by other DIEs. */
20687 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20688 return;
20689
20690 /* Don't bother trying to generate any DIEs to represent any of the
20691 normal built-in types for the language we are compiling. */
20692 if (DECL_IS_BUILTIN (decl))
20693 return;
20694
20695 /* If we are in terse mode, don't generate any DIEs for types. */
20696 if (debug_info_level <= DINFO_LEVEL_TERSE)
20697 return;
20698
20699 /* If we're a function-scope tag, initially use a parent of NULL;
20700 this will be fixed up in decls_for_scope. */
20701 if (decl_function_context (decl))
20702 context_die = NULL;
20703
20704 break;
20705
20706 default:
20707 return;
20708 }
20709
20710 gen_decl_die (decl, NULL, context_die);
20711 }
20712
20713 /* Write the debugging output for DECL. */
20714
20715 static void
20716 dwarf2out_function_decl (tree decl)
20717 {
20718 dwarf2out_decl (decl);
20719 call_arg_locations = NULL;
20720 call_arg_loc_last = NULL;
20721 call_site_count = -1;
20722 tail_call_site_count = -1;
20723 block_map.release ();
20724 htab_empty (decl_loc_table);
20725 htab_empty (cached_dw_loc_list_table);
20726 }
20727
20728 /* Output a marker (i.e. a label) for the beginning of the generated code for
20729 a lexical block. */
20730
20731 static void
20732 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20733 unsigned int blocknum)
20734 {
20735 switch_to_section (current_function_section ());
20736 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20737 }
20738
20739 /* Output a marker (i.e. a label) for the end of the generated code for a
20740 lexical block. */
20741
20742 static void
20743 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20744 {
20745 switch_to_section (current_function_section ());
20746 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20747 }
20748
20749 /* Returns nonzero if it is appropriate not to emit any debugging
20750 information for BLOCK, because it doesn't contain any instructions.
20751
20752 Don't allow this for blocks with nested functions or local classes
20753 as we would end up with orphans, and in the presence of scheduling
20754 we may end up calling them anyway. */
20755
20756 static bool
20757 dwarf2out_ignore_block (const_tree block)
20758 {
20759 tree decl;
20760 unsigned int i;
20761
20762 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20763 if (TREE_CODE (decl) == FUNCTION_DECL
20764 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20765 return 0;
20766 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20767 {
20768 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20769 if (TREE_CODE (decl) == FUNCTION_DECL
20770 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20771 return 0;
20772 }
20773
20774 return 1;
20775 }
20776
20777 /* Hash table routines for file_hash. */
20778
20779 static int
20780 file_table_eq (const void *p1_p, const void *p2_p)
20781 {
20782 const struct dwarf_file_data *const p1 =
20783 (const struct dwarf_file_data *) p1_p;
20784 const char *const p2 = (const char *) p2_p;
20785 return filename_cmp (p1->filename, p2) == 0;
20786 }
20787
20788 static hashval_t
20789 file_table_hash (const void *p_p)
20790 {
20791 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20792 return htab_hash_string (p->filename);
20793 }
20794
20795 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20796 dwarf2out.c) and return its "index". The index of each (known) filename is
20797 just a unique number which is associated with only that one filename. We
20798 need such numbers for the sake of generating labels (in the .debug_sfnames
20799 section) and references to those files numbers (in the .debug_srcinfo
20800 and.debug_macinfo sections). If the filename given as an argument is not
20801 found in our current list, add it to the list and assign it the next
20802 available unique index number. In order to speed up searches, we remember
20803 the index of the filename was looked up last. This handles the majority of
20804 all searches. */
20805
20806 static struct dwarf_file_data *
20807 lookup_filename (const char *file_name)
20808 {
20809 void ** slot;
20810 struct dwarf_file_data * created;
20811
20812 /* Check to see if the file name that was searched on the previous
20813 call matches this file name. If so, return the index. */
20814 if (file_table_last_lookup
20815 && (file_name == file_table_last_lookup->filename
20816 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20817 return file_table_last_lookup;
20818
20819 /* Didn't match the previous lookup, search the table. */
20820 slot = htab_find_slot_with_hash (file_table, file_name,
20821 htab_hash_string (file_name), INSERT);
20822 if (*slot)
20823 return (struct dwarf_file_data *) *slot;
20824
20825 created = ggc_alloc_dwarf_file_data ();
20826 created->filename = file_name;
20827 created->emitted_number = 0;
20828 *slot = created;
20829 return created;
20830 }
20831
20832 /* If the assembler will construct the file table, then translate the compiler
20833 internal file table number into the assembler file table number, and emit
20834 a .file directive if we haven't already emitted one yet. The file table
20835 numbers are different because we prune debug info for unused variables and
20836 types, which may include filenames. */
20837
20838 static int
20839 maybe_emit_file (struct dwarf_file_data * fd)
20840 {
20841 if (! fd->emitted_number)
20842 {
20843 if (last_emitted_file)
20844 fd->emitted_number = last_emitted_file->emitted_number + 1;
20845 else
20846 fd->emitted_number = 1;
20847 last_emitted_file = fd;
20848
20849 if (DWARF2_ASM_LINE_DEBUG_INFO)
20850 {
20851 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20852 output_quoted_string (asm_out_file,
20853 remap_debug_filename (fd->filename));
20854 fputc ('\n', asm_out_file);
20855 }
20856 }
20857
20858 return fd->emitted_number;
20859 }
20860
20861 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20862 That generation should happen after function debug info has been
20863 generated. The value of the attribute is the constant value of ARG. */
20864
20865 static void
20866 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20867 {
20868 die_arg_entry entry;
20869
20870 if (!die || !arg)
20871 return;
20872
20873 if (!tmpl_value_parm_die_table)
20874 vec_alloc (tmpl_value_parm_die_table, 32);
20875
20876 entry.die = die;
20877 entry.arg = arg;
20878 vec_safe_push (tmpl_value_parm_die_table, entry);
20879 }
20880
20881 /* Return TRUE if T is an instance of generic type, FALSE
20882 otherwise. */
20883
20884 static bool
20885 generic_type_p (tree t)
20886 {
20887 if (t == NULL_TREE || !TYPE_P (t))
20888 return false;
20889 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20890 }
20891
20892 /* Schedule the generation of the generic parameter dies for the
20893 instance of generic type T. The proper generation itself is later
20894 done by gen_scheduled_generic_parms_dies. */
20895
20896 static void
20897 schedule_generic_params_dies_gen (tree t)
20898 {
20899 if (!generic_type_p (t))
20900 return;
20901
20902 if (!generic_type_instances)
20903 vec_alloc (generic_type_instances, 256);
20904
20905 vec_safe_push (generic_type_instances, t);
20906 }
20907
20908 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20909 by append_entry_to_tmpl_value_parm_die_table. This function must
20910 be called after function DIEs have been generated. */
20911
20912 static void
20913 gen_remaining_tmpl_value_param_die_attribute (void)
20914 {
20915 if (tmpl_value_parm_die_table)
20916 {
20917 unsigned i;
20918 die_arg_entry *e;
20919
20920 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20921 tree_add_const_value_attribute (e->die, e->arg);
20922 }
20923 }
20924
20925 /* Generate generic parameters DIEs for instances of generic types
20926 that have been previously scheduled by
20927 schedule_generic_params_dies_gen. This function must be called
20928 after all the types of the CU have been laid out. */
20929
20930 static void
20931 gen_scheduled_generic_parms_dies (void)
20932 {
20933 unsigned i;
20934 tree t;
20935
20936 if (!generic_type_instances)
20937 return;
20938
20939 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20940 if (COMPLETE_TYPE_P (t))
20941 gen_generic_params_dies (t);
20942 }
20943
20944
20945 /* Replace DW_AT_name for the decl with name. */
20946
20947 static void
20948 dwarf2out_set_name (tree decl, tree name)
20949 {
20950 dw_die_ref die;
20951 dw_attr_ref attr;
20952 const char *dname;
20953
20954 die = TYPE_SYMTAB_DIE (decl);
20955 if (!die)
20956 return;
20957
20958 dname = dwarf2_name (name, 0);
20959 if (!dname)
20960 return;
20961
20962 attr = get_AT (die, DW_AT_name);
20963 if (attr)
20964 {
20965 struct indirect_string_node *node;
20966
20967 node = find_AT_string (dname);
20968 /* replace the string. */
20969 attr->dw_attr_val.v.val_str = node;
20970 }
20971
20972 else
20973 add_name_attribute (die, dname);
20974 }
20975
20976 /* True if before or during processing of the first function being emitted. */
20977 static bool in_first_function_p = true;
20978 /* True if loc_note during dwarf2out_var_location call might still be
20979 before first real instruction at address equal to .Ltext0. */
20980 static bool maybe_at_text_label_p = true;
20981 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
20982 static unsigned int first_loclabel_num_not_at_text_label;
20983
20984 /* Called by the final INSN scan whenever we see a var location. We
20985 use it to drop labels in the right places, and throw the location in
20986 our lookup table. */
20987
20988 static void
20989 dwarf2out_var_location (rtx loc_note)
20990 {
20991 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20992 struct var_loc_node *newloc;
20993 rtx next_real, next_note;
20994 static const char *last_label;
20995 static const char *last_postcall_label;
20996 static bool last_in_cold_section_p;
20997 static rtx expected_next_loc_note;
20998 tree decl;
20999 bool var_loc_p;
21000
21001 if (!NOTE_P (loc_note))
21002 {
21003 if (CALL_P (loc_note))
21004 {
21005 call_site_count++;
21006 if (SIBLING_CALL_P (loc_note))
21007 tail_call_site_count++;
21008 }
21009 return;
21010 }
21011
21012 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21013 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21014 return;
21015
21016 /* Optimize processing a large consecutive sequence of location
21017 notes so we don't spend too much time in next_real_insn. If the
21018 next insn is another location note, remember the next_real_insn
21019 calculation for next time. */
21020 next_real = cached_next_real_insn;
21021 if (next_real)
21022 {
21023 if (expected_next_loc_note != loc_note)
21024 next_real = NULL_RTX;
21025 }
21026
21027 next_note = NEXT_INSN (loc_note);
21028 if (! next_note
21029 || INSN_DELETED_P (next_note)
21030 || ! NOTE_P (next_note)
21031 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21032 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21033 next_note = NULL_RTX;
21034
21035 if (! next_real)
21036 next_real = next_real_insn (loc_note);
21037
21038 if (next_note)
21039 {
21040 expected_next_loc_note = next_note;
21041 cached_next_real_insn = next_real;
21042 }
21043 else
21044 cached_next_real_insn = NULL_RTX;
21045
21046 /* If there are no instructions which would be affected by this note,
21047 don't do anything. */
21048 if (var_loc_p
21049 && next_real == NULL_RTX
21050 && !NOTE_DURING_CALL_P (loc_note))
21051 return;
21052
21053 if (next_real == NULL_RTX)
21054 next_real = get_last_insn ();
21055
21056 /* If there were any real insns between note we processed last time
21057 and this note (or if it is the first note), clear
21058 last_{,postcall_}label so that they are not reused this time. */
21059 if (last_var_location_insn == NULL_RTX
21060 || last_var_location_insn != next_real
21061 || last_in_cold_section_p != in_cold_section_p)
21062 {
21063 last_label = NULL;
21064 last_postcall_label = NULL;
21065 }
21066
21067 if (var_loc_p)
21068 {
21069 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21070 newloc = add_var_loc_to_decl (decl, loc_note,
21071 NOTE_DURING_CALL_P (loc_note)
21072 ? last_postcall_label : last_label);
21073 if (newloc == NULL)
21074 return;
21075 }
21076 else
21077 {
21078 decl = NULL_TREE;
21079 newloc = NULL;
21080 }
21081
21082 /* If there were no real insns between note we processed last time
21083 and this note, use the label we emitted last time. Otherwise
21084 create a new label and emit it. */
21085 if (last_label == NULL)
21086 {
21087 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21088 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21089 loclabel_num++;
21090 last_label = ggc_strdup (loclabel);
21091 /* See if loclabel might be equal to .Ltext0. If yes,
21092 bump first_loclabel_num_not_at_text_label. */
21093 if (!have_multiple_function_sections
21094 && in_first_function_p
21095 && maybe_at_text_label_p)
21096 {
21097 static rtx last_start;
21098 rtx insn;
21099 for (insn = loc_note; insn; insn = previous_insn (insn))
21100 if (insn == last_start)
21101 break;
21102 else if (!NONDEBUG_INSN_P (insn))
21103 continue;
21104 else
21105 {
21106 rtx body = PATTERN (insn);
21107 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21108 continue;
21109 /* Inline asm could occupy zero bytes. */
21110 else if (GET_CODE (body) == ASM_INPUT
21111 || asm_noperands (body) >= 0)
21112 continue;
21113 #ifdef HAVE_attr_length
21114 else if (get_attr_min_length (insn) == 0)
21115 continue;
21116 #endif
21117 else
21118 {
21119 /* Assume insn has non-zero length. */
21120 maybe_at_text_label_p = false;
21121 break;
21122 }
21123 }
21124 if (maybe_at_text_label_p)
21125 {
21126 last_start = loc_note;
21127 first_loclabel_num_not_at_text_label = loclabel_num;
21128 }
21129 }
21130 }
21131
21132 if (!var_loc_p)
21133 {
21134 struct call_arg_loc_node *ca_loc
21135 = ggc_alloc_cleared_call_arg_loc_node ();
21136 rtx prev = prev_real_insn (loc_note), x;
21137 ca_loc->call_arg_loc_note = loc_note;
21138 ca_loc->next = NULL;
21139 ca_loc->label = last_label;
21140 gcc_assert (prev
21141 && (CALL_P (prev)
21142 || (NONJUMP_INSN_P (prev)
21143 && GET_CODE (PATTERN (prev)) == SEQUENCE
21144 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21145 if (!CALL_P (prev))
21146 prev = XVECEXP (PATTERN (prev), 0, 0);
21147 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21148 x = get_call_rtx_from (PATTERN (prev));
21149 if (x)
21150 {
21151 x = XEXP (XEXP (x, 0), 0);
21152 if (GET_CODE (x) == SYMBOL_REF
21153 && SYMBOL_REF_DECL (x)
21154 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21155 ca_loc->symbol_ref = x;
21156 }
21157 ca_loc->block = insn_scope (prev);
21158 if (call_arg_locations)
21159 call_arg_loc_last->next = ca_loc;
21160 else
21161 call_arg_locations = ca_loc;
21162 call_arg_loc_last = ca_loc;
21163 }
21164 else if (!NOTE_DURING_CALL_P (loc_note))
21165 newloc->label = last_label;
21166 else
21167 {
21168 if (!last_postcall_label)
21169 {
21170 sprintf (loclabel, "%s-1", last_label);
21171 last_postcall_label = ggc_strdup (loclabel);
21172 }
21173 newloc->label = last_postcall_label;
21174 }
21175
21176 last_var_location_insn = next_real;
21177 last_in_cold_section_p = in_cold_section_p;
21178 }
21179
21180 /* Note in one location list that text section has changed. */
21181
21182 static int
21183 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21184 {
21185 var_loc_list *list = (var_loc_list *) *slot;
21186 if (list->first)
21187 list->last_before_switch
21188 = list->last->next ? list->last->next : list->last;
21189 return 1;
21190 }
21191
21192 /* Note in all location lists that text section has changed. */
21193
21194 static void
21195 var_location_switch_text_section (void)
21196 {
21197 if (decl_loc_table == NULL)
21198 return;
21199
21200 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21201 }
21202
21203 /* Create a new line number table. */
21204
21205 static dw_line_info_table *
21206 new_line_info_table (void)
21207 {
21208 dw_line_info_table *table;
21209
21210 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21211 table->file_num = 1;
21212 table->line_num = 1;
21213 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21214
21215 return table;
21216 }
21217
21218 /* Lookup the "current" table into which we emit line info, so
21219 that we don't have to do it for every source line. */
21220
21221 static void
21222 set_cur_line_info_table (section *sec)
21223 {
21224 dw_line_info_table *table;
21225
21226 if (sec == text_section)
21227 table = text_section_line_info;
21228 else if (sec == cold_text_section)
21229 {
21230 table = cold_text_section_line_info;
21231 if (!table)
21232 {
21233 cold_text_section_line_info = table = new_line_info_table ();
21234 table->end_label = cold_end_label;
21235 }
21236 }
21237 else
21238 {
21239 const char *end_label;
21240
21241 if (flag_reorder_blocks_and_partition)
21242 {
21243 if (in_cold_section_p)
21244 end_label = crtl->subsections.cold_section_end_label;
21245 else
21246 end_label = crtl->subsections.hot_section_end_label;
21247 }
21248 else
21249 {
21250 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21251 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21252 current_function_funcdef_no);
21253 end_label = ggc_strdup (label);
21254 }
21255
21256 table = new_line_info_table ();
21257 table->end_label = end_label;
21258
21259 vec_safe_push (separate_line_info, table);
21260 }
21261
21262 if (DWARF2_ASM_LINE_DEBUG_INFO)
21263 table->is_stmt = (cur_line_info_table
21264 ? cur_line_info_table->is_stmt
21265 : DWARF_LINE_DEFAULT_IS_STMT_START);
21266 cur_line_info_table = table;
21267 }
21268
21269
21270 /* We need to reset the locations at the beginning of each
21271 function. We can't do this in the end_function hook, because the
21272 declarations that use the locations won't have been output when
21273 that hook is called. Also compute have_multiple_function_sections here. */
21274
21275 static void
21276 dwarf2out_begin_function (tree fun)
21277 {
21278 section *sec = function_section (fun);
21279
21280 if (sec != text_section)
21281 have_multiple_function_sections = true;
21282
21283 if (flag_reorder_blocks_and_partition && !cold_text_section)
21284 {
21285 gcc_assert (current_function_decl == fun);
21286 cold_text_section = unlikely_text_section ();
21287 switch_to_section (cold_text_section);
21288 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21289 switch_to_section (sec);
21290 }
21291
21292 dwarf2out_note_section_used ();
21293 call_site_count = 0;
21294 tail_call_site_count = 0;
21295
21296 set_cur_line_info_table (sec);
21297 }
21298
21299 /* Helper function of dwarf2out_end_function, called only after emitting
21300 the very first function into assembly. Check if some .debug_loc range
21301 might end with a .LVL* label that could be equal to .Ltext0.
21302 In that case we must force using absolute addresses in .debug_loc ranges,
21303 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21304 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21305 list terminator.
21306 Set have_multiple_function_sections to true in that case and
21307 terminate htab traversal. */
21308
21309 static int
21310 find_empty_loc_ranges_at_text_label (void **slot, void *)
21311 {
21312 var_loc_list *entry;
21313 struct var_loc_node *node;
21314
21315 entry = (var_loc_list *) *slot;
21316 node = entry->first;
21317 if (node && node->next && node->next->label)
21318 {
21319 unsigned int i;
21320 const char *label = node->next->label;
21321 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21322
21323 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21324 {
21325 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21326 if (strcmp (label, loclabel) == 0)
21327 {
21328 have_multiple_function_sections = true;
21329 return 0;
21330 }
21331 }
21332 }
21333 return 1;
21334 }
21335
21336 /* Hook called after emitting a function into assembly.
21337 This does something only for the very first function emitted. */
21338
21339 static void
21340 dwarf2out_end_function (unsigned int)
21341 {
21342 if (in_first_function_p
21343 && !have_multiple_function_sections
21344 && first_loclabel_num_not_at_text_label
21345 && decl_loc_table)
21346 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21347 NULL);
21348 in_first_function_p = false;
21349 maybe_at_text_label_p = false;
21350 }
21351
21352 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21353
21354 static void
21355 push_dw_line_info_entry (dw_line_info_table *table,
21356 enum dw_line_info_opcode opcode, unsigned int val)
21357 {
21358 dw_line_info_entry e;
21359 e.opcode = opcode;
21360 e.val = val;
21361 vec_safe_push (table->entries, e);
21362 }
21363
21364 /* Output a label to mark the beginning of a source code line entry
21365 and record information relating to this source line, in
21366 'line_info_table' for later output of the .debug_line section. */
21367 /* ??? The discriminator parameter ought to be unsigned. */
21368
21369 static void
21370 dwarf2out_source_line (unsigned int line, const char *filename,
21371 int discriminator, bool is_stmt)
21372 {
21373 unsigned int file_num;
21374 dw_line_info_table *table;
21375
21376 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21377 return;
21378
21379 /* The discriminator column was added in dwarf4. Simplify the below
21380 by simply removing it if we're not supposed to output it. */
21381 if (dwarf_version < 4 && dwarf_strict)
21382 discriminator = 0;
21383
21384 table = cur_line_info_table;
21385 file_num = maybe_emit_file (lookup_filename (filename));
21386
21387 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21388 the debugger has used the second (possibly duplicate) line number
21389 at the beginning of the function to mark the end of the prologue.
21390 We could eliminate any other duplicates within the function. For
21391 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21392 that second line number entry. */
21393 /* Recall that this end-of-prologue indication is *not* the same thing
21394 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21395 to which the hook corresponds, follows the last insn that was
21396 emitted by gen_prologue. What we need is to precede the first insn
21397 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21398 insn that corresponds to something the user wrote. These may be
21399 very different locations once scheduling is enabled. */
21400
21401 if (0 && file_num == table->file_num
21402 && line == table->line_num
21403 && discriminator == table->discrim_num
21404 && is_stmt == table->is_stmt)
21405 return;
21406
21407 switch_to_section (current_function_section ());
21408
21409 /* If requested, emit something human-readable. */
21410 if (flag_debug_asm)
21411 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21412
21413 if (DWARF2_ASM_LINE_DEBUG_INFO)
21414 {
21415 /* Emit the .loc directive understood by GNU as. */
21416 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21417 file_num, line, is_stmt, discriminator */
21418 fputs ("\t.loc ", asm_out_file);
21419 fprint_ul (asm_out_file, file_num);
21420 putc (' ', asm_out_file);
21421 fprint_ul (asm_out_file, line);
21422 putc (' ', asm_out_file);
21423 putc ('0', asm_out_file);
21424
21425 if (is_stmt != table->is_stmt)
21426 {
21427 fputs (" is_stmt ", asm_out_file);
21428 putc (is_stmt ? '1' : '0', asm_out_file);
21429 }
21430 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21431 {
21432 gcc_assert (discriminator > 0);
21433 fputs (" discriminator ", asm_out_file);
21434 fprint_ul (asm_out_file, (unsigned long) discriminator);
21435 }
21436 putc ('\n', asm_out_file);
21437 }
21438 else
21439 {
21440 unsigned int label_num = ++line_info_label_num;
21441
21442 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21443
21444 push_dw_line_info_entry (table, LI_set_address, label_num);
21445 if (file_num != table->file_num)
21446 push_dw_line_info_entry (table, LI_set_file, file_num);
21447 if (discriminator != table->discrim_num)
21448 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21449 if (is_stmt != table->is_stmt)
21450 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21451 push_dw_line_info_entry (table, LI_set_line, line);
21452 }
21453
21454 table->file_num = file_num;
21455 table->line_num = line;
21456 table->discrim_num = discriminator;
21457 table->is_stmt = is_stmt;
21458 table->in_use = true;
21459 }
21460
21461 /* Record the beginning of a new source file. */
21462
21463 static void
21464 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21465 {
21466 if (flag_eliminate_dwarf2_dups)
21467 {
21468 /* Record the beginning of the file for break_out_includes. */
21469 dw_die_ref bincl_die;
21470
21471 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21472 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21473 }
21474
21475 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21476 {
21477 macinfo_entry e;
21478 e.code = DW_MACINFO_start_file;
21479 e.lineno = lineno;
21480 e.info = ggc_strdup (filename);
21481 vec_safe_push (macinfo_table, e);
21482 }
21483 }
21484
21485 /* Record the end of a source file. */
21486
21487 static void
21488 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21489 {
21490 if (flag_eliminate_dwarf2_dups)
21491 /* Record the end of the file for break_out_includes. */
21492 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21493
21494 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21495 {
21496 macinfo_entry e;
21497 e.code = DW_MACINFO_end_file;
21498 e.lineno = lineno;
21499 e.info = NULL;
21500 vec_safe_push (macinfo_table, e);
21501 }
21502 }
21503
21504 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21505 the tail part of the directive line, i.e. the part which is past the
21506 initial whitespace, #, whitespace, directive-name, whitespace part. */
21507
21508 static void
21509 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21510 const char *buffer ATTRIBUTE_UNUSED)
21511 {
21512 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21513 {
21514 macinfo_entry e;
21515 /* Insert a dummy first entry to be able to optimize the whole
21516 predefined macro block using DW_MACRO_GNU_transparent_include. */
21517 if (macinfo_table->is_empty () && lineno <= 1)
21518 {
21519 e.code = 0;
21520 e.lineno = 0;
21521 e.info = NULL;
21522 vec_safe_push (macinfo_table, e);
21523 }
21524 e.code = DW_MACINFO_define;
21525 e.lineno = lineno;
21526 e.info = ggc_strdup (buffer);
21527 vec_safe_push (macinfo_table, e);
21528 }
21529 }
21530
21531 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21532 the tail part of the directive line, i.e. the part which is past the
21533 initial whitespace, #, whitespace, directive-name, whitespace part. */
21534
21535 static void
21536 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21537 const char *buffer ATTRIBUTE_UNUSED)
21538 {
21539 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21540 {
21541 macinfo_entry e;
21542 /* Insert a dummy first entry to be able to optimize the whole
21543 predefined macro block using DW_MACRO_GNU_transparent_include. */
21544 if (macinfo_table->is_empty () && lineno <= 1)
21545 {
21546 e.code = 0;
21547 e.lineno = 0;
21548 e.info = NULL;
21549 vec_safe_push (macinfo_table, e);
21550 }
21551 e.code = DW_MACINFO_undef;
21552 e.lineno = lineno;
21553 e.info = ggc_strdup (buffer);
21554 vec_safe_push (macinfo_table, e);
21555 }
21556 }
21557
21558 /* Helpers to manipulate hash table of CUs. */
21559
21560 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21561 {
21562 typedef macinfo_entry value_type;
21563 typedef macinfo_entry compare_type;
21564 static inline hashval_t hash (const value_type *);
21565 static inline bool equal (const value_type *, const compare_type *);
21566 };
21567
21568 inline hashval_t
21569 macinfo_entry_hasher::hash (const value_type *entry)
21570 {
21571 return htab_hash_string (entry->info);
21572 }
21573
21574 inline bool
21575 macinfo_entry_hasher::equal (const value_type *entry1,
21576 const compare_type *entry2)
21577 {
21578 return !strcmp (entry1->info, entry2->info);
21579 }
21580
21581 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21582
21583 /* Output a single .debug_macinfo entry. */
21584
21585 static void
21586 output_macinfo_op (macinfo_entry *ref)
21587 {
21588 int file_num;
21589 size_t len;
21590 struct indirect_string_node *node;
21591 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21592 struct dwarf_file_data *fd;
21593
21594 switch (ref->code)
21595 {
21596 case DW_MACINFO_start_file:
21597 fd = lookup_filename (ref->info);
21598 file_num = maybe_emit_file (fd);
21599 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21600 dw2_asm_output_data_uleb128 (ref->lineno,
21601 "Included from line number %lu",
21602 (unsigned long) ref->lineno);
21603 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21604 break;
21605 case DW_MACINFO_end_file:
21606 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21607 break;
21608 case DW_MACINFO_define:
21609 case DW_MACINFO_undef:
21610 len = strlen (ref->info) + 1;
21611 if (!dwarf_strict
21612 && len > DWARF_OFFSET_SIZE
21613 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21614 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21615 {
21616 ref->code = ref->code == DW_MACINFO_define
21617 ? DW_MACRO_GNU_define_indirect
21618 : DW_MACRO_GNU_undef_indirect;
21619 output_macinfo_op (ref);
21620 return;
21621 }
21622 dw2_asm_output_data (1, ref->code,
21623 ref->code == DW_MACINFO_define
21624 ? "Define macro" : "Undefine macro");
21625 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21626 (unsigned long) ref->lineno);
21627 dw2_asm_output_nstring (ref->info, -1, "The macro");
21628 break;
21629 case DW_MACRO_GNU_define_indirect:
21630 case DW_MACRO_GNU_undef_indirect:
21631 node = find_AT_string (ref->info);
21632 gcc_assert (node
21633 && ((node->form == DW_FORM_strp)
21634 || (node->form == DW_FORM_GNU_str_index)));
21635 dw2_asm_output_data (1, ref->code,
21636 ref->code == DW_MACRO_GNU_define_indirect
21637 ? "Define macro indirect"
21638 : "Undefine macro indirect");
21639 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21640 (unsigned long) ref->lineno);
21641 if (node->form == DW_FORM_strp)
21642 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21643 debug_str_section, "The macro: \"%s\"",
21644 ref->info);
21645 else
21646 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21647 ref->info);
21648 break;
21649 case DW_MACRO_GNU_transparent_include:
21650 dw2_asm_output_data (1, ref->code, "Transparent include");
21651 ASM_GENERATE_INTERNAL_LABEL (label,
21652 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21653 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21654 break;
21655 default:
21656 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21657 ASM_COMMENT_START, (unsigned long) ref->code);
21658 break;
21659 }
21660 }
21661
21662 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21663 other compilation unit .debug_macinfo sections. IDX is the first
21664 index of a define/undef, return the number of ops that should be
21665 emitted in a comdat .debug_macinfo section and emit
21666 a DW_MACRO_GNU_transparent_include entry referencing it.
21667 If the define/undef entry should be emitted normally, return 0. */
21668
21669 static unsigned
21670 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21671 macinfo_hash_type *macinfo_htab)
21672 {
21673 macinfo_entry *first, *second, *cur, *inc;
21674 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21675 unsigned char checksum[16];
21676 struct md5_ctx ctx;
21677 char *grp_name, *tail;
21678 const char *base;
21679 unsigned int i, count, encoded_filename_len, linebuf_len;
21680 macinfo_entry **slot;
21681
21682 first = &(*macinfo_table)[idx];
21683 second = &(*macinfo_table)[idx + 1];
21684
21685 /* Optimize only if there are at least two consecutive define/undef ops,
21686 and either all of them are before first DW_MACINFO_start_file
21687 with lineno {0,1} (i.e. predefined macro block), or all of them are
21688 in some included header file. */
21689 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21690 return 0;
21691 if (vec_safe_is_empty (files))
21692 {
21693 if (first->lineno > 1 || second->lineno > 1)
21694 return 0;
21695 }
21696 else if (first->lineno == 0)
21697 return 0;
21698
21699 /* Find the last define/undef entry that can be grouped together
21700 with first and at the same time compute md5 checksum of their
21701 codes, linenumbers and strings. */
21702 md5_init_ctx (&ctx);
21703 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21704 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21705 break;
21706 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21707 break;
21708 else
21709 {
21710 unsigned char code = cur->code;
21711 md5_process_bytes (&code, 1, &ctx);
21712 checksum_uleb128 (cur->lineno, &ctx);
21713 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21714 }
21715 md5_finish_ctx (&ctx, checksum);
21716 count = i - idx;
21717
21718 /* From the containing include filename (if any) pick up just
21719 usable characters from its basename. */
21720 if (vec_safe_is_empty (files))
21721 base = "";
21722 else
21723 base = lbasename (files->last ().info);
21724 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21725 if (ISIDNUM (base[i]) || base[i] == '.')
21726 encoded_filename_len++;
21727 /* Count . at the end. */
21728 if (encoded_filename_len)
21729 encoded_filename_len++;
21730
21731 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21732 linebuf_len = strlen (linebuf);
21733
21734 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21735 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21736 + 16 * 2 + 1);
21737 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21738 tail = grp_name + 4;
21739 if (encoded_filename_len)
21740 {
21741 for (i = 0; base[i]; i++)
21742 if (ISIDNUM (base[i]) || base[i] == '.')
21743 *tail++ = base[i];
21744 *tail++ = '.';
21745 }
21746 memcpy (tail, linebuf, linebuf_len);
21747 tail += linebuf_len;
21748 *tail++ = '.';
21749 for (i = 0; i < 16; i++)
21750 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21751
21752 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21753 in the empty vector entry before the first define/undef. */
21754 inc = &(*macinfo_table)[idx - 1];
21755 inc->code = DW_MACRO_GNU_transparent_include;
21756 inc->lineno = 0;
21757 inc->info = ggc_strdup (grp_name);
21758 if (!macinfo_htab->is_created ())
21759 macinfo_htab->create (10);
21760 /* Avoid emitting duplicates. */
21761 slot = macinfo_htab->find_slot (inc, INSERT);
21762 if (*slot != NULL)
21763 {
21764 inc->code = 0;
21765 inc->info = NULL;
21766 /* If such an entry has been used before, just emit
21767 a DW_MACRO_GNU_transparent_include op. */
21768 inc = *slot;
21769 output_macinfo_op (inc);
21770 /* And clear all macinfo_entry in the range to avoid emitting them
21771 in the second pass. */
21772 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21773 {
21774 cur->code = 0;
21775 cur->info = NULL;
21776 }
21777 }
21778 else
21779 {
21780 *slot = inc;
21781 inc->lineno = macinfo_htab->elements ();
21782 output_macinfo_op (inc);
21783 }
21784 return count;
21785 }
21786
21787 /* Save any strings needed by the macinfo table in the debug str
21788 table. All strings must be collected into the table by the time
21789 index_string is called. */
21790
21791 static void
21792 save_macinfo_strings (void)
21793 {
21794 unsigned len;
21795 unsigned i;
21796 macinfo_entry *ref;
21797
21798 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21799 {
21800 switch (ref->code)
21801 {
21802 /* Match the logic in output_macinfo_op to decide on
21803 indirect strings. */
21804 case DW_MACINFO_define:
21805 case DW_MACINFO_undef:
21806 len = strlen (ref->info) + 1;
21807 if (!dwarf_strict
21808 && len > DWARF_OFFSET_SIZE
21809 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21810 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21811 set_indirect_string (find_AT_string (ref->info));
21812 break;
21813 case DW_MACRO_GNU_define_indirect:
21814 case DW_MACRO_GNU_undef_indirect:
21815 set_indirect_string (find_AT_string (ref->info));
21816 break;
21817 default:
21818 break;
21819 }
21820 }
21821 }
21822
21823 /* Output macinfo section(s). */
21824
21825 static void
21826 output_macinfo (void)
21827 {
21828 unsigned i;
21829 unsigned long length = vec_safe_length (macinfo_table);
21830 macinfo_entry *ref;
21831 vec<macinfo_entry, va_gc> *files = NULL;
21832 macinfo_hash_type macinfo_htab;
21833
21834 if (! length)
21835 return;
21836
21837 /* output_macinfo* uses these interchangeably. */
21838 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21839 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21840 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21841 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21842
21843 /* For .debug_macro emit the section header. */
21844 if (!dwarf_strict)
21845 {
21846 dw2_asm_output_data (2, 4, "DWARF macro version number");
21847 if (DWARF_OFFSET_SIZE == 8)
21848 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21849 else
21850 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21851 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21852 (!dwarf_split_debug_info ? debug_line_section_label
21853 : debug_skeleton_line_section_label),
21854 debug_line_section, NULL);
21855 }
21856
21857 /* In the first loop, it emits the primary .debug_macinfo section
21858 and after each emitted op the macinfo_entry is cleared.
21859 If a longer range of define/undef ops can be optimized using
21860 DW_MACRO_GNU_transparent_include, the
21861 DW_MACRO_GNU_transparent_include op is emitted and kept in
21862 the vector before the first define/undef in the range and the
21863 whole range of define/undef ops is not emitted and kept. */
21864 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21865 {
21866 switch (ref->code)
21867 {
21868 case DW_MACINFO_start_file:
21869 vec_safe_push (files, *ref);
21870 break;
21871 case DW_MACINFO_end_file:
21872 if (!vec_safe_is_empty (files))
21873 files->pop ();
21874 break;
21875 case DW_MACINFO_define:
21876 case DW_MACINFO_undef:
21877 if (!dwarf_strict
21878 && HAVE_COMDAT_GROUP
21879 && vec_safe_length (files) != 1
21880 && i > 0
21881 && i + 1 < length
21882 && (*macinfo_table)[i - 1].code == 0)
21883 {
21884 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21885 if (count)
21886 {
21887 i += count - 1;
21888 continue;
21889 }
21890 }
21891 break;
21892 case 0:
21893 /* A dummy entry may be inserted at the beginning to be able
21894 to optimize the whole block of predefined macros. */
21895 if (i == 0)
21896 continue;
21897 default:
21898 break;
21899 }
21900 output_macinfo_op (ref);
21901 ref->info = NULL;
21902 ref->code = 0;
21903 }
21904
21905 if (!macinfo_htab.is_created ())
21906 return;
21907
21908 macinfo_htab.dispose ();
21909
21910 /* If any DW_MACRO_GNU_transparent_include were used, on those
21911 DW_MACRO_GNU_transparent_include entries terminate the
21912 current chain and switch to a new comdat .debug_macinfo
21913 section and emit the define/undef entries within it. */
21914 for (i = 0; macinfo_table->iterate (i, &ref); i++)
21915 switch (ref->code)
21916 {
21917 case 0:
21918 continue;
21919 case DW_MACRO_GNU_transparent_include:
21920 {
21921 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21922 tree comdat_key = get_identifier (ref->info);
21923 /* Terminate the previous .debug_macinfo section. */
21924 dw2_asm_output_data (1, 0, "End compilation unit");
21925 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21926 SECTION_DEBUG
21927 | SECTION_LINKONCE,
21928 comdat_key);
21929 ASM_GENERATE_INTERNAL_LABEL (label,
21930 DEBUG_MACRO_SECTION_LABEL,
21931 ref->lineno);
21932 ASM_OUTPUT_LABEL (asm_out_file, label);
21933 ref->code = 0;
21934 ref->info = NULL;
21935 dw2_asm_output_data (2, 4, "DWARF macro version number");
21936 if (DWARF_OFFSET_SIZE == 8)
21937 dw2_asm_output_data (1, 1, "Flags: 64-bit");
21938 else
21939 dw2_asm_output_data (1, 0, "Flags: 32-bit");
21940 }
21941 break;
21942 case DW_MACINFO_define:
21943 case DW_MACINFO_undef:
21944 output_macinfo_op (ref);
21945 ref->code = 0;
21946 ref->info = NULL;
21947 break;
21948 default:
21949 gcc_unreachable ();
21950 }
21951 }
21952
21953 /* Set up for Dwarf output at the start of compilation. */
21954
21955 static void
21956 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21957 {
21958 /* Allocate the file_table. */
21959 file_table = htab_create_ggc (50, file_table_hash,
21960 file_table_eq, NULL);
21961
21962 /* Allocate the decl_die_table. */
21963 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21964 decl_die_table_eq, NULL);
21965
21966 /* Allocate the decl_loc_table. */
21967 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21968 decl_loc_table_eq, NULL);
21969
21970 /* Allocate the cached_dw_loc_list_table. */
21971 cached_dw_loc_list_table
21972 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21973 cached_dw_loc_list_table_eq, NULL);
21974
21975 /* Allocate the initial hunk of the decl_scope_table. */
21976 vec_alloc (decl_scope_table, 256);
21977
21978 /* Allocate the initial hunk of the abbrev_die_table. */
21979 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21980 (ABBREV_DIE_TABLE_INCREMENT);
21981 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21982 /* Zero-th entry is allocated, but unused. */
21983 abbrev_die_table_in_use = 1;
21984
21985 /* Allocate the pubtypes and pubnames vectors. */
21986 vec_alloc (pubname_table, 32);
21987 vec_alloc (pubtype_table, 32);
21988
21989 vec_alloc (incomplete_types, 64);
21990
21991 vec_alloc (used_rtx_array, 32);
21992
21993 if (!dwarf_split_debug_info)
21994 {
21995 debug_info_section = get_section (DEBUG_INFO_SECTION,
21996 SECTION_DEBUG, NULL);
21997 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21998 SECTION_DEBUG, NULL);
21999 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22000 SECTION_DEBUG, NULL);
22001 }
22002 else
22003 {
22004 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22005 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22006 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22007 SECTION_DEBUG | SECTION_EXCLUDE,
22008 NULL);
22009 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22010 SECTION_DEBUG, NULL);
22011 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22012 SECTION_DEBUG, NULL);
22013 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22014 SECTION_DEBUG, NULL);
22015 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22016 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22017
22018 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22019 the main .o, but the skeleton_line goes into the split off dwo. */
22020 debug_skeleton_line_section
22021 = get_section (DEBUG_DWO_LINE_SECTION,
22022 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22023 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22024 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22025 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22026 SECTION_DEBUG | SECTION_EXCLUDE,
22027 NULL);
22028 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22029 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22030 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22031 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22032 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22033 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22034 }
22035 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22036 SECTION_DEBUG, NULL);
22037 debug_macinfo_section = get_section (dwarf_strict
22038 ? DEBUG_MACINFO_SECTION
22039 : DEBUG_MACRO_SECTION,
22040 DEBUG_MACRO_SECTION_FLAGS, NULL);
22041 debug_line_section = get_section (DEBUG_LINE_SECTION,
22042 SECTION_DEBUG, NULL);
22043 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22044 SECTION_DEBUG, NULL);
22045 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22046 SECTION_DEBUG, NULL);
22047 debug_str_section = get_section (DEBUG_STR_SECTION,
22048 DEBUG_STR_SECTION_FLAGS, NULL);
22049 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22050 SECTION_DEBUG, NULL);
22051 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22052 SECTION_DEBUG, NULL);
22053
22054 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22055 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22056 DEBUG_ABBREV_SECTION_LABEL, 0);
22057 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22058 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22059 COLD_TEXT_SECTION_LABEL, 0);
22060 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22061
22062 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22063 DEBUG_INFO_SECTION_LABEL, 0);
22064 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22065 DEBUG_LINE_SECTION_LABEL, 0);
22066 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22067 DEBUG_RANGES_SECTION_LABEL, 0);
22068 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22069 DEBUG_ADDR_SECTION_LABEL, 0);
22070 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22071 dwarf_strict
22072 ? DEBUG_MACINFO_SECTION_LABEL
22073 : DEBUG_MACRO_SECTION_LABEL, 0);
22074 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22075
22076 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22077 vec_alloc (macinfo_table, 64);
22078
22079 switch_to_section (text_section);
22080 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22081
22082 /* Make sure the line number table for .text always exists. */
22083 text_section_line_info = new_line_info_table ();
22084 text_section_line_info->end_label = text_end_label;
22085 }
22086
22087 /* Called before compile () starts outputtting functions, variables
22088 and toplevel asms into assembly. */
22089
22090 static void
22091 dwarf2out_assembly_start (void)
22092 {
22093 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22094 && dwarf2out_do_cfi_asm ()
22095 && (!(flag_unwind_tables || flag_exceptions)
22096 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22097 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22098 }
22099
22100 /* A helper function for dwarf2out_finish called through
22101 htab_traverse. Assign a string its index. All strings must be
22102 collected into the table by the time index_string is called,
22103 because the indexing code relies on htab_traverse to traverse nodes
22104 in the same order for each run. */
22105
22106 static int
22107 index_string (void **h, void *v)
22108 {
22109 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22110 unsigned int *index = (unsigned int *) v;
22111
22112 find_string_form (node);
22113 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22114 {
22115 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22116 node->index = *index;
22117 *index += 1;
22118 }
22119 return 1;
22120 }
22121
22122 /* A helper function for output_indirect_strings called through
22123 htab_traverse. Output the offset to a string and update the
22124 current offset. */
22125
22126 static int
22127 output_index_string_offset (void **h, void *v)
22128 {
22129 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22130 unsigned int *offset = (unsigned int *) v;
22131
22132 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22133 {
22134 /* Assert that this node has been assigned an index. */
22135 gcc_assert (node->index != NO_INDEX_ASSIGNED
22136 && node->index != NOT_INDEXED);
22137 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22138 "indexed string 0x%x: %s", node->index, node->str);
22139 *offset += strlen (node->str) + 1;
22140 }
22141 return 1;
22142 }
22143
22144 /* A helper function for dwarf2out_finish called through
22145 htab_traverse. Output the indexed string. */
22146
22147 static int
22148 output_index_string (void **h, void *v)
22149 {
22150 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22151 unsigned int *cur_idx = (unsigned int *) v;
22152
22153 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22154 {
22155 /* Assert that the strings are output in the same order as their
22156 indexes were assigned. */
22157 gcc_assert (*cur_idx == node->index);
22158 assemble_string (node->str, strlen (node->str) + 1);
22159 *cur_idx += 1;
22160 }
22161 return 1;
22162 }
22163
22164 /* A helper function for dwarf2out_finish called through
22165 htab_traverse. Emit one queued .debug_str string. */
22166
22167 static int
22168 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22169 {
22170 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22171
22172 node->form = find_string_form (node);
22173 if (node->form == DW_FORM_strp && node->refcount > 0)
22174 {
22175 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22176 assemble_string (node->str, strlen (node->str) + 1);
22177 }
22178
22179 return 1;
22180 }
22181
22182 /* Output the indexed string table. */
22183
22184 static void
22185 output_indirect_strings (void)
22186 {
22187 switch_to_section (debug_str_section);
22188 if (!dwarf_split_debug_info)
22189 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22190 else
22191 {
22192 unsigned int offset = 0;
22193 unsigned int cur_idx = 0;
22194
22195 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22196
22197 switch_to_section (debug_str_offsets_section);
22198 htab_traverse_noresize (debug_str_hash,
22199 output_index_string_offset,
22200 &offset);
22201 switch_to_section (debug_str_dwo_section);
22202 htab_traverse_noresize (debug_str_hash,
22203 output_index_string,
22204 &cur_idx);
22205 }
22206 }
22207
22208 /* Callback for htab_traverse to assign an index to an entry in the
22209 table, and to write that entry to the .debug_addr section. */
22210
22211 static int
22212 output_addr_table_entry (void **slot, void *data)
22213 {
22214 addr_table_entry *entry = (addr_table_entry *) *slot;
22215 unsigned int *cur_index = (unsigned int *)data;
22216
22217 if (entry->refcount == 0)
22218 {
22219 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22220 || entry->index == NOT_INDEXED);
22221 return 1;
22222 }
22223
22224 gcc_assert (entry->index == *cur_index);
22225 (*cur_index)++;
22226
22227 switch (entry->kind)
22228 {
22229 case ate_kind_rtx:
22230 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22231 "0x%x", entry->index);
22232 break;
22233 case ate_kind_rtx_dtprel:
22234 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22235 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22236 DWARF2_ADDR_SIZE,
22237 entry->addr.rtl);
22238 fputc ('\n', asm_out_file);
22239 break;
22240 case ate_kind_label:
22241 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22242 "0x%x", entry->index);
22243 break;
22244 default:
22245 gcc_unreachable ();
22246 }
22247 return 1;
22248 }
22249
22250 /* Produce the .debug_addr section. */
22251
22252 static void
22253 output_addr_table (void)
22254 {
22255 unsigned int index = 0;
22256 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22257 return;
22258
22259 switch_to_section (debug_addr_section);
22260 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22261 }
22262
22263 #if ENABLE_ASSERT_CHECKING
22264 /* Verify that all marks are clear. */
22265
22266 static void
22267 verify_marks_clear (dw_die_ref die)
22268 {
22269 dw_die_ref c;
22270
22271 gcc_assert (! die->die_mark);
22272 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22273 }
22274 #endif /* ENABLE_ASSERT_CHECKING */
22275
22276 /* Clear the marks for a die and its children.
22277 Be cool if the mark isn't set. */
22278
22279 static void
22280 prune_unmark_dies (dw_die_ref die)
22281 {
22282 dw_die_ref c;
22283
22284 if (die->die_mark)
22285 die->die_mark = 0;
22286 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22287 }
22288
22289 /* Given DIE that we're marking as used, find any other dies
22290 it references as attributes and mark them as used. */
22291
22292 static void
22293 prune_unused_types_walk_attribs (dw_die_ref die)
22294 {
22295 dw_attr_ref a;
22296 unsigned ix;
22297
22298 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22299 {
22300 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22301 {
22302 /* A reference to another DIE.
22303 Make sure that it will get emitted.
22304 If it was broken out into a comdat group, don't follow it. */
22305 if (! AT_ref (a)->comdat_type_p
22306 || a->dw_attr == DW_AT_specification)
22307 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22308 }
22309 /* Set the string's refcount to 0 so that prune_unused_types_mark
22310 accounts properly for it. */
22311 if (AT_class (a) == dw_val_class_str)
22312 a->dw_attr_val.v.val_str->refcount = 0;
22313 }
22314 }
22315
22316 /* Mark the generic parameters and arguments children DIEs of DIE. */
22317
22318 static void
22319 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22320 {
22321 dw_die_ref c;
22322
22323 if (die == NULL || die->die_child == NULL)
22324 return;
22325 c = die->die_child;
22326 do
22327 {
22328 if (is_template_parameter (c))
22329 prune_unused_types_mark (c, 1);
22330 c = c->die_sib;
22331 } while (c && c != die->die_child);
22332 }
22333
22334 /* Mark DIE as being used. If DOKIDS is true, then walk down
22335 to DIE's children. */
22336
22337 static void
22338 prune_unused_types_mark (dw_die_ref die, int dokids)
22339 {
22340 dw_die_ref c;
22341
22342 if (die->die_mark == 0)
22343 {
22344 /* We haven't done this node yet. Mark it as used. */
22345 die->die_mark = 1;
22346 /* If this is the DIE of a generic type instantiation,
22347 mark the children DIEs that describe its generic parms and
22348 args. */
22349 prune_unused_types_mark_generic_parms_dies (die);
22350
22351 /* We also have to mark its parents as used.
22352 (But we don't want to mark our parent's kids due to this,
22353 unless it is a class.) */
22354 if (die->die_parent)
22355 prune_unused_types_mark (die->die_parent,
22356 class_scope_p (die->die_parent));
22357
22358 /* Mark any referenced nodes. */
22359 prune_unused_types_walk_attribs (die);
22360
22361 /* If this node is a specification,
22362 also mark the definition, if it exists. */
22363 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22364 prune_unused_types_mark (die->die_definition, 1);
22365 }
22366
22367 if (dokids && die->die_mark != 2)
22368 {
22369 /* We need to walk the children, but haven't done so yet.
22370 Remember that we've walked the kids. */
22371 die->die_mark = 2;
22372
22373 /* If this is an array type, we need to make sure our
22374 kids get marked, even if they're types. If we're
22375 breaking out types into comdat sections, do this
22376 for all type definitions. */
22377 if (die->die_tag == DW_TAG_array_type
22378 || (use_debug_types
22379 && is_type_die (die) && ! is_declaration_die (die)))
22380 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22381 else
22382 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22383 }
22384 }
22385
22386 /* For local classes, look if any static member functions were emitted
22387 and if so, mark them. */
22388
22389 static void
22390 prune_unused_types_walk_local_classes (dw_die_ref die)
22391 {
22392 dw_die_ref c;
22393
22394 if (die->die_mark == 2)
22395 return;
22396
22397 switch (die->die_tag)
22398 {
22399 case DW_TAG_structure_type:
22400 case DW_TAG_union_type:
22401 case DW_TAG_class_type:
22402 break;
22403
22404 case DW_TAG_subprogram:
22405 if (!get_AT_flag (die, DW_AT_declaration)
22406 || die->die_definition != NULL)
22407 prune_unused_types_mark (die, 1);
22408 return;
22409
22410 default:
22411 return;
22412 }
22413
22414 /* Mark children. */
22415 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22416 }
22417
22418 /* Walk the tree DIE and mark types that we actually use. */
22419
22420 static void
22421 prune_unused_types_walk (dw_die_ref die)
22422 {
22423 dw_die_ref c;
22424
22425 /* Don't do anything if this node is already marked and
22426 children have been marked as well. */
22427 if (die->die_mark == 2)
22428 return;
22429
22430 switch (die->die_tag)
22431 {
22432 case DW_TAG_structure_type:
22433 case DW_TAG_union_type:
22434 case DW_TAG_class_type:
22435 if (die->die_perennial_p)
22436 break;
22437
22438 for (c = die->die_parent; c; c = c->die_parent)
22439 if (c->die_tag == DW_TAG_subprogram)
22440 break;
22441
22442 /* Finding used static member functions inside of classes
22443 is needed just for local classes, because for other classes
22444 static member function DIEs with DW_AT_specification
22445 are emitted outside of the DW_TAG_*_type. If we ever change
22446 it, we'd need to call this even for non-local classes. */
22447 if (c)
22448 prune_unused_types_walk_local_classes (die);
22449
22450 /* It's a type node --- don't mark it. */
22451 return;
22452
22453 case DW_TAG_const_type:
22454 case DW_TAG_packed_type:
22455 case DW_TAG_pointer_type:
22456 case DW_TAG_reference_type:
22457 case DW_TAG_rvalue_reference_type:
22458 case DW_TAG_volatile_type:
22459 case DW_TAG_typedef:
22460 case DW_TAG_array_type:
22461 case DW_TAG_interface_type:
22462 case DW_TAG_friend:
22463 case DW_TAG_variant_part:
22464 case DW_TAG_enumeration_type:
22465 case DW_TAG_subroutine_type:
22466 case DW_TAG_string_type:
22467 case DW_TAG_set_type:
22468 case DW_TAG_subrange_type:
22469 case DW_TAG_ptr_to_member_type:
22470 case DW_TAG_file_type:
22471 if (die->die_perennial_p)
22472 break;
22473
22474 /* It's a type node --- don't mark it. */
22475 return;
22476
22477 default:
22478 /* Mark everything else. */
22479 break;
22480 }
22481
22482 if (die->die_mark == 0)
22483 {
22484 die->die_mark = 1;
22485
22486 /* Now, mark any dies referenced from here. */
22487 prune_unused_types_walk_attribs (die);
22488 }
22489
22490 die->die_mark = 2;
22491
22492 /* Mark children. */
22493 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22494 }
22495
22496 /* Increment the string counts on strings referred to from DIE's
22497 attributes. */
22498
22499 static void
22500 prune_unused_types_update_strings (dw_die_ref die)
22501 {
22502 dw_attr_ref a;
22503 unsigned ix;
22504
22505 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22506 if (AT_class (a) == dw_val_class_str)
22507 {
22508 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22509 s->refcount++;
22510 /* Avoid unnecessarily putting strings that are used less than
22511 twice in the hash table. */
22512 if (s->refcount
22513 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22514 {
22515 void ** slot;
22516 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22517 htab_hash_string (s->str),
22518 INSERT);
22519 gcc_assert (*slot == NULL);
22520 *slot = s;
22521 }
22522 }
22523 }
22524
22525 /* Remove from the tree DIE any dies that aren't marked. */
22526
22527 static void
22528 prune_unused_types_prune (dw_die_ref die)
22529 {
22530 dw_die_ref c;
22531
22532 gcc_assert (die->die_mark);
22533 prune_unused_types_update_strings (die);
22534
22535 if (! die->die_child)
22536 return;
22537
22538 c = die->die_child;
22539 do {
22540 dw_die_ref prev = c;
22541 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22542 if (c == die->die_child)
22543 {
22544 /* No marked children between 'prev' and the end of the list. */
22545 if (prev == c)
22546 /* No marked children at all. */
22547 die->die_child = NULL;
22548 else
22549 {
22550 prev->die_sib = c->die_sib;
22551 die->die_child = prev;
22552 }
22553 return;
22554 }
22555
22556 if (c != prev->die_sib)
22557 prev->die_sib = c;
22558 prune_unused_types_prune (c);
22559 } while (c != die->die_child);
22560 }
22561
22562 /* Remove dies representing declarations that we never use. */
22563
22564 static void
22565 prune_unused_types (void)
22566 {
22567 unsigned int i;
22568 limbo_die_node *node;
22569 comdat_type_node *ctnode;
22570 pubname_ref pub;
22571 dw_die_ref base_type;
22572
22573 #if ENABLE_ASSERT_CHECKING
22574 /* All the marks should already be clear. */
22575 verify_marks_clear (comp_unit_die ());
22576 for (node = limbo_die_list; node; node = node->next)
22577 verify_marks_clear (node->die);
22578 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22579 verify_marks_clear (ctnode->root_die);
22580 #endif /* ENABLE_ASSERT_CHECKING */
22581
22582 /* Mark types that are used in global variables. */
22583 premark_types_used_by_global_vars ();
22584
22585 /* Set the mark on nodes that are actually used. */
22586 prune_unused_types_walk (comp_unit_die ());
22587 for (node = limbo_die_list; node; node = node->next)
22588 prune_unused_types_walk (node->die);
22589 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22590 {
22591 prune_unused_types_walk (ctnode->root_die);
22592 prune_unused_types_mark (ctnode->type_die, 1);
22593 }
22594
22595 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22596 are unusual in that they are pubnames that are the children of pubtypes.
22597 They should only be marked via their parent DW_TAG_enumeration_type die,
22598 not as roots in themselves. */
22599 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22600 if (pub->die->die_tag != DW_TAG_enumerator)
22601 prune_unused_types_mark (pub->die, 1);
22602 for (i = 0; base_types.iterate (i, &base_type); i++)
22603 prune_unused_types_mark (base_type, 1);
22604
22605 if (debug_str_hash)
22606 htab_empty (debug_str_hash);
22607 if (skeleton_debug_str_hash)
22608 htab_empty (skeleton_debug_str_hash);
22609 prune_unused_types_prune (comp_unit_die ());
22610 for (node = limbo_die_list; node; node = node->next)
22611 prune_unused_types_prune (node->die);
22612 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22613 prune_unused_types_prune (ctnode->root_die);
22614
22615 /* Leave the marks clear. */
22616 prune_unmark_dies (comp_unit_die ());
22617 for (node = limbo_die_list; node; node = node->next)
22618 prune_unmark_dies (node->die);
22619 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22620 prune_unmark_dies (ctnode->root_die);
22621 }
22622
22623 /* Set the parameter to true if there are any relative pathnames in
22624 the file table. */
22625 static int
22626 file_table_relative_p (void ** slot, void *param)
22627 {
22628 bool *p = (bool *) param;
22629 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22630 if (!IS_ABSOLUTE_PATH (d->filename))
22631 {
22632 *p = true;
22633 return 0;
22634 }
22635 return 1;
22636 }
22637
22638 /* Helpers to manipulate hash table of comdat type units. */
22639
22640 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22641 {
22642 typedef comdat_type_node value_type;
22643 typedef comdat_type_node compare_type;
22644 static inline hashval_t hash (const value_type *);
22645 static inline bool equal (const value_type *, const compare_type *);
22646 };
22647
22648 inline hashval_t
22649 comdat_type_hasher::hash (const value_type *type_node)
22650 {
22651 hashval_t h;
22652 memcpy (&h, type_node->signature, sizeof (h));
22653 return h;
22654 }
22655
22656 inline bool
22657 comdat_type_hasher::equal (const value_type *type_node_1,
22658 const compare_type *type_node_2)
22659 {
22660 return (! memcmp (type_node_1->signature, type_node_2->signature,
22661 DWARF_TYPE_SIGNATURE_SIZE));
22662 }
22663
22664 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22665 to the location it would have been added, should we know its
22666 DECL_ASSEMBLER_NAME when we added other attributes. This will
22667 probably improve compactness of debug info, removing equivalent
22668 abbrevs, and hide any differences caused by deferring the
22669 computation of the assembler name, triggered by e.g. PCH. */
22670
22671 static inline void
22672 move_linkage_attr (dw_die_ref die)
22673 {
22674 unsigned ix = vec_safe_length (die->die_attr);
22675 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22676
22677 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22678 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22679
22680 while (--ix > 0)
22681 {
22682 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22683
22684 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22685 break;
22686 }
22687
22688 if (ix != vec_safe_length (die->die_attr) - 1)
22689 {
22690 die->die_attr->pop ();
22691 die->die_attr->quick_insert (ix, linkage);
22692 }
22693 }
22694
22695 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22696 referenced from typed stack ops and count how often they are used. */
22697
22698 static void
22699 mark_base_types (dw_loc_descr_ref loc)
22700 {
22701 dw_die_ref base_type = NULL;
22702
22703 for (; loc; loc = loc->dw_loc_next)
22704 {
22705 switch (loc->dw_loc_opc)
22706 {
22707 case DW_OP_GNU_regval_type:
22708 case DW_OP_GNU_deref_type:
22709 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22710 break;
22711 case DW_OP_GNU_convert:
22712 case DW_OP_GNU_reinterpret:
22713 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22714 continue;
22715 /* FALLTHRU */
22716 case DW_OP_GNU_const_type:
22717 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22718 break;
22719 case DW_OP_GNU_entry_value:
22720 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22721 continue;
22722 default:
22723 continue;
22724 }
22725 gcc_assert (base_type->die_parent == comp_unit_die ());
22726 if (base_type->die_mark)
22727 base_type->die_mark++;
22728 else
22729 {
22730 base_types.safe_push (base_type);
22731 base_type->die_mark = 1;
22732 }
22733 }
22734 }
22735
22736 /* Comparison function for sorting marked base types. */
22737
22738 static int
22739 base_type_cmp (const void *x, const void *y)
22740 {
22741 dw_die_ref dx = *(const dw_die_ref *) x;
22742 dw_die_ref dy = *(const dw_die_ref *) y;
22743 unsigned int byte_size1, byte_size2;
22744 unsigned int encoding1, encoding2;
22745 if (dx->die_mark > dy->die_mark)
22746 return -1;
22747 if (dx->die_mark < dy->die_mark)
22748 return 1;
22749 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22750 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22751 if (byte_size1 < byte_size2)
22752 return 1;
22753 if (byte_size1 > byte_size2)
22754 return -1;
22755 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22756 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22757 if (encoding1 < encoding2)
22758 return 1;
22759 if (encoding1 > encoding2)
22760 return -1;
22761 return 0;
22762 }
22763
22764 /* Move base types marked by mark_base_types as early as possible
22765 in the CU, sorted by decreasing usage count both to make the
22766 uleb128 references as small as possible and to make sure they
22767 will have die_offset already computed by calc_die_sizes when
22768 sizes of typed stack loc ops is computed. */
22769
22770 static void
22771 move_marked_base_types (void)
22772 {
22773 unsigned int i;
22774 dw_die_ref base_type, die, c;
22775
22776 if (base_types.is_empty ())
22777 return;
22778
22779 /* Sort by decreasing usage count, they will be added again in that
22780 order later on. */
22781 base_types.qsort (base_type_cmp);
22782 die = comp_unit_die ();
22783 c = die->die_child;
22784 do
22785 {
22786 dw_die_ref prev = c;
22787 c = c->die_sib;
22788 while (c->die_mark)
22789 {
22790 remove_child_with_prev (c, prev);
22791 /* As base types got marked, there must be at least
22792 one node other than DW_TAG_base_type. */
22793 gcc_assert (c != c->die_sib);
22794 c = c->die_sib;
22795 }
22796 }
22797 while (c != die->die_child);
22798 gcc_assert (die->die_child);
22799 c = die->die_child;
22800 for (i = 0; base_types.iterate (i, &base_type); i++)
22801 {
22802 base_type->die_mark = 0;
22803 base_type->die_sib = c->die_sib;
22804 c->die_sib = base_type;
22805 c = base_type;
22806 }
22807 }
22808
22809 /* Helper function for resolve_addr, attempt to resolve
22810 one CONST_STRING, return non-zero if not successful. Similarly verify that
22811 SYMBOL_REFs refer to variables emitted in the current CU. */
22812
22813 static int
22814 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22815 {
22816 rtx rtl = *addr;
22817
22818 if (GET_CODE (rtl) == CONST_STRING)
22819 {
22820 size_t len = strlen (XSTR (rtl, 0)) + 1;
22821 tree t = build_string (len, XSTR (rtl, 0));
22822 tree tlen = size_int (len - 1);
22823 TREE_TYPE (t)
22824 = build_array_type (char_type_node, build_index_type (tlen));
22825 rtl = lookup_constant_def (t);
22826 if (!rtl || !MEM_P (rtl))
22827 return 1;
22828 rtl = XEXP (rtl, 0);
22829 if (GET_CODE (rtl) == SYMBOL_REF
22830 && SYMBOL_REF_DECL (rtl)
22831 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22832 return 1;
22833 vec_safe_push (used_rtx_array, rtl);
22834 *addr = rtl;
22835 return 0;
22836 }
22837
22838 if (GET_CODE (rtl) == SYMBOL_REF
22839 && SYMBOL_REF_DECL (rtl))
22840 {
22841 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22842 {
22843 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22844 return 1;
22845 }
22846 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22847 return 1;
22848 }
22849
22850 if (GET_CODE (rtl) == CONST
22851 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22852 return 1;
22853
22854 return 0;
22855 }
22856
22857 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
22858 if possible, and create DW_TAG_dwarf_procedure that can be referenced
22859 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
22860
22861 static rtx
22862 string_cst_pool_decl (tree t)
22863 {
22864 rtx rtl = output_constant_def (t, 1);
22865 unsigned char *array;
22866 dw_loc_descr_ref l;
22867 tree decl;
22868 size_t len;
22869 dw_die_ref ref;
22870
22871 if (!rtl || !MEM_P (rtl))
22872 return NULL_RTX;
22873 rtl = XEXP (rtl, 0);
22874 if (GET_CODE (rtl) != SYMBOL_REF
22875 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
22876 return NULL_RTX;
22877
22878 decl = SYMBOL_REF_DECL (rtl);
22879 if (!lookup_decl_die (decl))
22880 {
22881 len = TREE_STRING_LENGTH (t);
22882 vec_safe_push (used_rtx_array, rtl);
22883 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
22884 array = (unsigned char *) ggc_alloc_atomic (len);
22885 memcpy (array, TREE_STRING_POINTER (t), len);
22886 l = new_loc_descr (DW_OP_implicit_value, len, 0);
22887 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
22888 l->dw_loc_oprnd2.v.val_vec.length = len;
22889 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
22890 l->dw_loc_oprnd2.v.val_vec.array = array;
22891 add_AT_loc (ref, DW_AT_location, l);
22892 equate_decl_number_to_die (decl, ref);
22893 }
22894 return rtl;
22895 }
22896
22897 /* Helper function of resolve_addr_in_expr. LOC is
22898 a DW_OP_addr followed by DW_OP_stack_value, either at the start
22899 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
22900 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
22901 with DW_OP_GNU_implicit_pointer if possible
22902 and return true, if unsuccessful, return false. */
22903
22904 static bool
22905 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
22906 {
22907 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
22908 HOST_WIDE_INT offset = 0;
22909 dw_die_ref ref = NULL;
22910 tree decl;
22911
22912 if (GET_CODE (rtl) == CONST
22913 && GET_CODE (XEXP (rtl, 0)) == PLUS
22914 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
22915 {
22916 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
22917 rtl = XEXP (XEXP (rtl, 0), 0);
22918 }
22919 if (GET_CODE (rtl) == CONST_STRING)
22920 {
22921 size_t len = strlen (XSTR (rtl, 0)) + 1;
22922 tree t = build_string (len, XSTR (rtl, 0));
22923 tree tlen = size_int (len - 1);
22924
22925 TREE_TYPE (t)
22926 = build_array_type (char_type_node, build_index_type (tlen));
22927 rtl = string_cst_pool_decl (t);
22928 if (!rtl)
22929 return false;
22930 }
22931 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
22932 {
22933 decl = SYMBOL_REF_DECL (rtl);
22934 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
22935 {
22936 ref = lookup_decl_die (decl);
22937 if (ref && (get_AT (ref, DW_AT_location)
22938 || get_AT (ref, DW_AT_const_value)))
22939 {
22940 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
22941 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22942 loc->dw_loc_oprnd1.val_entry = NULL;
22943 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22944 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22945 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22946 loc->dw_loc_oprnd2.v.val_int = offset;
22947 return true;
22948 }
22949 }
22950 }
22951 return false;
22952 }
22953
22954 /* Helper function for resolve_addr, handle one location
22955 expression, return false if at least one CONST_STRING or SYMBOL_REF in
22956 the location list couldn't be resolved. */
22957
22958 static bool
22959 resolve_addr_in_expr (dw_loc_descr_ref loc)
22960 {
22961 dw_loc_descr_ref keep = NULL;
22962 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
22963 switch (loc->dw_loc_opc)
22964 {
22965 case DW_OP_addr:
22966 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22967 {
22968 if ((prev == NULL
22969 || prev->dw_loc_opc == DW_OP_piece
22970 || prev->dw_loc_opc == DW_OP_bit_piece)
22971 && loc->dw_loc_next
22972 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
22973 && !dwarf_strict
22974 && optimize_one_addr_into_implicit_ptr (loc))
22975 break;
22976 return false;
22977 }
22978 break;
22979 case DW_OP_GNU_addr_index:
22980 case DW_OP_GNU_const_index:
22981 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22982 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22983 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22984 NULL))
22985 return false;
22986 break;
22987 case DW_OP_const4u:
22988 case DW_OP_const8u:
22989 if (loc->dtprel
22990 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22991 return false;
22992 break;
22993 case DW_OP_plus_uconst:
22994 if (size_of_loc_descr (loc)
22995 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22996 + 1
22997 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22998 {
22999 dw_loc_descr_ref repl
23000 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23001 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23002 add_loc_descr (&repl, loc->dw_loc_next);
23003 *loc = *repl;
23004 }
23005 break;
23006 case DW_OP_implicit_value:
23007 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23008 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23009 return false;
23010 break;
23011 case DW_OP_GNU_implicit_pointer:
23012 case DW_OP_GNU_parameter_ref:
23013 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23014 {
23015 dw_die_ref ref
23016 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23017 if (ref == NULL)
23018 return false;
23019 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23020 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23021 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23022 }
23023 break;
23024 case DW_OP_GNU_const_type:
23025 case DW_OP_GNU_regval_type:
23026 case DW_OP_GNU_deref_type:
23027 case DW_OP_GNU_convert:
23028 case DW_OP_GNU_reinterpret:
23029 while (loc->dw_loc_next
23030 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23031 {
23032 dw_die_ref base1, base2;
23033 unsigned enc1, enc2, size1, size2;
23034 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23035 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23036 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23037 else if (loc->dw_loc_oprnd1.val_class
23038 == dw_val_class_unsigned_const)
23039 break;
23040 else
23041 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23042 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23043 == dw_val_class_unsigned_const)
23044 break;
23045 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23046 gcc_assert (base1->die_tag == DW_TAG_base_type
23047 && base2->die_tag == DW_TAG_base_type);
23048 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23049 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23050 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23051 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23052 if (size1 == size2
23053 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23054 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23055 && loc != keep)
23056 || enc1 == enc2))
23057 {
23058 /* Optimize away next DW_OP_GNU_convert after
23059 adjusting LOC's base type die reference. */
23060 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23061 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23062 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23063 else
23064 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23065 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23066 continue;
23067 }
23068 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23069 point typed stack entry. */
23070 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23071 keep = loc->dw_loc_next;
23072 break;
23073 }
23074 break;
23075 default:
23076 break;
23077 }
23078 return true;
23079 }
23080
23081 /* Helper function of resolve_addr. DIE had DW_AT_location of
23082 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23083 and DW_OP_addr couldn't be resolved. resolve_addr has already
23084 removed the DW_AT_location attribute. This function attempts to
23085 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23086 to it or DW_AT_const_value attribute, if possible. */
23087
23088 static void
23089 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23090 {
23091 if (TREE_CODE (decl) != VAR_DECL
23092 || lookup_decl_die (decl) != die
23093 || DECL_EXTERNAL (decl)
23094 || !TREE_STATIC (decl)
23095 || DECL_INITIAL (decl) == NULL_TREE
23096 || DECL_P (DECL_INITIAL (decl))
23097 || get_AT (die, DW_AT_const_value))
23098 return;
23099
23100 tree init = DECL_INITIAL (decl);
23101 HOST_WIDE_INT offset = 0;
23102 /* For variables that have been optimized away and thus
23103 don't have a memory location, see if we can emit
23104 DW_AT_const_value instead. */
23105 if (tree_add_const_value_attribute (die, init))
23106 return;
23107 if (dwarf_strict)
23108 return;
23109 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23110 and ADDR_EXPR refers to a decl that has DW_AT_location or
23111 DW_AT_const_value (but isn't addressable, otherwise
23112 resolving the original DW_OP_addr wouldn't fail), see if
23113 we can add DW_OP_GNU_implicit_pointer. */
23114 STRIP_NOPS (init);
23115 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23116 && host_integerp (TREE_OPERAND (init, 1), 0))
23117 {
23118 offset = tree_low_cst (TREE_OPERAND (init, 1), 0);
23119 init = TREE_OPERAND (init, 0);
23120 STRIP_NOPS (init);
23121 }
23122 if (TREE_CODE (init) != ADDR_EXPR)
23123 return;
23124 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23125 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23126 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23127 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23128 && TREE_OPERAND (init, 0) != decl))
23129 {
23130 dw_die_ref ref;
23131 dw_loc_descr_ref l;
23132
23133 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23134 {
23135 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23136 if (!rtl)
23137 return;
23138 decl = SYMBOL_REF_DECL (rtl);
23139 }
23140 else
23141 decl = TREE_OPERAND (init, 0);
23142 ref = lookup_decl_die (decl);
23143 if (ref == NULL
23144 || (!get_AT (ref, DW_AT_location)
23145 && !get_AT (ref, DW_AT_const_value)))
23146 return;
23147 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23148 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23149 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23150 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23151 add_AT_loc (die, DW_AT_location, l);
23152 }
23153 }
23154
23155 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23156 an address in .rodata section if the string literal is emitted there,
23157 or remove the containing location list or replace DW_AT_const_value
23158 with DW_AT_location and empty location expression, if it isn't found
23159 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23160 to something that has been emitted in the current CU. */
23161
23162 static void
23163 resolve_addr (dw_die_ref die)
23164 {
23165 dw_die_ref c;
23166 dw_attr_ref a;
23167 dw_loc_list_ref *curr, *start, loc;
23168 unsigned ix;
23169
23170 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23171 switch (AT_class (a))
23172 {
23173 case dw_val_class_loc_list:
23174 start = curr = AT_loc_list_ptr (a);
23175 loc = *curr;
23176 gcc_assert (loc);
23177 /* The same list can be referenced more than once. See if we have
23178 already recorded the result from a previous pass. */
23179 if (loc->replaced)
23180 *curr = loc->dw_loc_next;
23181 else if (!loc->resolved_addr)
23182 {
23183 /* As things stand, we do not expect or allow one die to
23184 reference a suffix of another die's location list chain.
23185 References must be identical or completely separate.
23186 There is therefore no need to cache the result of this
23187 pass on any list other than the first; doing so
23188 would lead to unnecessary writes. */
23189 while (*curr)
23190 {
23191 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23192 if (!resolve_addr_in_expr ((*curr)->expr))
23193 {
23194 dw_loc_list_ref next = (*curr)->dw_loc_next;
23195 dw_loc_descr_ref l = (*curr)->expr;
23196
23197 if (next && (*curr)->ll_symbol)
23198 {
23199 gcc_assert (!next->ll_symbol);
23200 next->ll_symbol = (*curr)->ll_symbol;
23201 }
23202 if (dwarf_split_debug_info)
23203 remove_loc_list_addr_table_entries (l);
23204 *curr = next;
23205 }
23206 else
23207 {
23208 mark_base_types ((*curr)->expr);
23209 curr = &(*curr)->dw_loc_next;
23210 }
23211 }
23212 if (loc == *start)
23213 loc->resolved_addr = 1;
23214 else
23215 {
23216 loc->replaced = 1;
23217 loc->dw_loc_next = *start;
23218 }
23219 }
23220 if (!*start)
23221 {
23222 remove_AT (die, a->dw_attr);
23223 ix--;
23224 }
23225 break;
23226 case dw_val_class_loc:
23227 {
23228 dw_loc_descr_ref l = AT_loc (a);
23229 /* For -gdwarf-2 don't attempt to optimize
23230 DW_AT_data_member_location containing
23231 DW_OP_plus_uconst - older consumers might
23232 rely on it being that op instead of a more complex,
23233 but shorter, location description. */
23234 if ((dwarf_version > 2
23235 || a->dw_attr != DW_AT_data_member_location
23236 || l == NULL
23237 || l->dw_loc_opc != DW_OP_plus_uconst
23238 || l->dw_loc_next != NULL)
23239 && !resolve_addr_in_expr (l))
23240 {
23241 if (dwarf_split_debug_info)
23242 remove_loc_list_addr_table_entries (l);
23243 if (l != NULL
23244 && l->dw_loc_next == NULL
23245 && l->dw_loc_opc == DW_OP_addr
23246 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23247 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23248 && a->dw_attr == DW_AT_location)
23249 {
23250 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23251 remove_AT (die, a->dw_attr);
23252 ix--;
23253 optimize_location_into_implicit_ptr (die, decl);
23254 break;
23255 }
23256 remove_AT (die, a->dw_attr);
23257 ix--;
23258 }
23259 else
23260 mark_base_types (l);
23261 }
23262 break;
23263 case dw_val_class_addr:
23264 if (a->dw_attr == DW_AT_const_value
23265 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23266 {
23267 if (AT_index (a) != NOT_INDEXED)
23268 remove_addr_table_entry (a->dw_attr_val.val_entry);
23269 remove_AT (die, a->dw_attr);
23270 ix--;
23271 }
23272 if (die->die_tag == DW_TAG_GNU_call_site
23273 && a->dw_attr == DW_AT_abstract_origin)
23274 {
23275 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23276 dw_die_ref tdie = lookup_decl_die (tdecl);
23277 if (tdie == NULL
23278 && DECL_EXTERNAL (tdecl)
23279 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23280 {
23281 force_decl_die (tdecl);
23282 tdie = lookup_decl_die (tdecl);
23283 }
23284 if (tdie)
23285 {
23286 a->dw_attr_val.val_class = dw_val_class_die_ref;
23287 a->dw_attr_val.v.val_die_ref.die = tdie;
23288 a->dw_attr_val.v.val_die_ref.external = 0;
23289 }
23290 else
23291 {
23292 if (AT_index (a) != NOT_INDEXED)
23293 remove_addr_table_entry (a->dw_attr_val.val_entry);
23294 remove_AT (die, a->dw_attr);
23295 ix--;
23296 }
23297 }
23298 break;
23299 default:
23300 break;
23301 }
23302
23303 FOR_EACH_CHILD (die, c, resolve_addr (c));
23304 }
23305 \f
23306 /* Helper routines for optimize_location_lists.
23307 This pass tries to share identical local lists in .debug_loc
23308 section. */
23309
23310 /* Iteratively hash operands of LOC opcode. */
23311
23312 static hashval_t
23313 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23314 {
23315 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23316 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23317
23318 switch (loc->dw_loc_opc)
23319 {
23320 case DW_OP_const4u:
23321 case DW_OP_const8u:
23322 if (loc->dtprel)
23323 goto hash_addr;
23324 /* FALLTHRU */
23325 case DW_OP_const1u:
23326 case DW_OP_const1s:
23327 case DW_OP_const2u:
23328 case DW_OP_const2s:
23329 case DW_OP_const4s:
23330 case DW_OP_const8s:
23331 case DW_OP_constu:
23332 case DW_OP_consts:
23333 case DW_OP_pick:
23334 case DW_OP_plus_uconst:
23335 case DW_OP_breg0:
23336 case DW_OP_breg1:
23337 case DW_OP_breg2:
23338 case DW_OP_breg3:
23339 case DW_OP_breg4:
23340 case DW_OP_breg5:
23341 case DW_OP_breg6:
23342 case DW_OP_breg7:
23343 case DW_OP_breg8:
23344 case DW_OP_breg9:
23345 case DW_OP_breg10:
23346 case DW_OP_breg11:
23347 case DW_OP_breg12:
23348 case DW_OP_breg13:
23349 case DW_OP_breg14:
23350 case DW_OP_breg15:
23351 case DW_OP_breg16:
23352 case DW_OP_breg17:
23353 case DW_OP_breg18:
23354 case DW_OP_breg19:
23355 case DW_OP_breg20:
23356 case DW_OP_breg21:
23357 case DW_OP_breg22:
23358 case DW_OP_breg23:
23359 case DW_OP_breg24:
23360 case DW_OP_breg25:
23361 case DW_OP_breg26:
23362 case DW_OP_breg27:
23363 case DW_OP_breg28:
23364 case DW_OP_breg29:
23365 case DW_OP_breg30:
23366 case DW_OP_breg31:
23367 case DW_OP_regx:
23368 case DW_OP_fbreg:
23369 case DW_OP_piece:
23370 case DW_OP_deref_size:
23371 case DW_OP_xderef_size:
23372 hash = iterative_hash_object (val1->v.val_int, hash);
23373 break;
23374 case DW_OP_skip:
23375 case DW_OP_bra:
23376 {
23377 int offset;
23378
23379 gcc_assert (val1->val_class == dw_val_class_loc);
23380 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23381 hash = iterative_hash_object (offset, hash);
23382 }
23383 break;
23384 case DW_OP_implicit_value:
23385 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23386 switch (val2->val_class)
23387 {
23388 case dw_val_class_const:
23389 hash = iterative_hash_object (val2->v.val_int, hash);
23390 break;
23391 case dw_val_class_vec:
23392 {
23393 unsigned int elt_size = val2->v.val_vec.elt_size;
23394 unsigned int len = val2->v.val_vec.length;
23395
23396 hash = iterative_hash_object (elt_size, hash);
23397 hash = iterative_hash_object (len, hash);
23398 hash = iterative_hash (val2->v.val_vec.array,
23399 len * elt_size, hash);
23400 }
23401 break;
23402 case dw_val_class_const_double:
23403 hash = iterative_hash_object (val2->v.val_double.low, hash);
23404 hash = iterative_hash_object (val2->v.val_double.high, hash);
23405 break;
23406 case dw_val_class_addr:
23407 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23408 break;
23409 default:
23410 gcc_unreachable ();
23411 }
23412 break;
23413 case DW_OP_bregx:
23414 case DW_OP_bit_piece:
23415 hash = iterative_hash_object (val1->v.val_int, hash);
23416 hash = iterative_hash_object (val2->v.val_int, hash);
23417 break;
23418 case DW_OP_addr:
23419 hash_addr:
23420 if (loc->dtprel)
23421 {
23422 unsigned char dtprel = 0xd1;
23423 hash = iterative_hash_object (dtprel, hash);
23424 }
23425 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23426 break;
23427 case DW_OP_GNU_addr_index:
23428 case DW_OP_GNU_const_index:
23429 {
23430 if (loc->dtprel)
23431 {
23432 unsigned char dtprel = 0xd1;
23433 hash = iterative_hash_object (dtprel, hash);
23434 }
23435 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23436 }
23437 break;
23438 case DW_OP_GNU_implicit_pointer:
23439 hash = iterative_hash_object (val2->v.val_int, hash);
23440 break;
23441 case DW_OP_GNU_entry_value:
23442 hash = hash_loc_operands (val1->v.val_loc, hash);
23443 break;
23444 case DW_OP_GNU_regval_type:
23445 case DW_OP_GNU_deref_type:
23446 {
23447 unsigned int byte_size
23448 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23449 unsigned int encoding
23450 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23451 hash = iterative_hash_object (val1->v.val_int, hash);
23452 hash = iterative_hash_object (byte_size, hash);
23453 hash = iterative_hash_object (encoding, hash);
23454 }
23455 break;
23456 case DW_OP_GNU_convert:
23457 case DW_OP_GNU_reinterpret:
23458 if (val1->val_class == dw_val_class_unsigned_const)
23459 {
23460 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23461 break;
23462 }
23463 /* FALLTHRU */
23464 case DW_OP_GNU_const_type:
23465 {
23466 unsigned int byte_size
23467 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23468 unsigned int encoding
23469 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23470 hash = iterative_hash_object (byte_size, hash);
23471 hash = iterative_hash_object (encoding, hash);
23472 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23473 break;
23474 hash = iterative_hash_object (val2->val_class, hash);
23475 switch (val2->val_class)
23476 {
23477 case dw_val_class_const:
23478 hash = iterative_hash_object (val2->v.val_int, hash);
23479 break;
23480 case dw_val_class_vec:
23481 {
23482 unsigned int elt_size = val2->v.val_vec.elt_size;
23483 unsigned int len = val2->v.val_vec.length;
23484
23485 hash = iterative_hash_object (elt_size, hash);
23486 hash = iterative_hash_object (len, hash);
23487 hash = iterative_hash (val2->v.val_vec.array,
23488 len * elt_size, hash);
23489 }
23490 break;
23491 case dw_val_class_const_double:
23492 hash = iterative_hash_object (val2->v.val_double.low, hash);
23493 hash = iterative_hash_object (val2->v.val_double.high, hash);
23494 break;
23495 default:
23496 gcc_unreachable ();
23497 }
23498 }
23499 break;
23500
23501 default:
23502 /* Other codes have no operands. */
23503 break;
23504 }
23505 return hash;
23506 }
23507
23508 /* Iteratively hash the whole DWARF location expression LOC. */
23509
23510 static inline hashval_t
23511 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23512 {
23513 dw_loc_descr_ref l;
23514 bool sizes_computed = false;
23515 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23516 size_of_locs (loc);
23517
23518 for (l = loc; l != NULL; l = l->dw_loc_next)
23519 {
23520 enum dwarf_location_atom opc = l->dw_loc_opc;
23521 hash = iterative_hash_object (opc, hash);
23522 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23523 {
23524 size_of_locs (loc);
23525 sizes_computed = true;
23526 }
23527 hash = hash_loc_operands (l, hash);
23528 }
23529 return hash;
23530 }
23531
23532 /* Compute hash of the whole location list LIST_HEAD. */
23533
23534 static inline void
23535 hash_loc_list (dw_loc_list_ref list_head)
23536 {
23537 dw_loc_list_ref curr = list_head;
23538 hashval_t hash = 0;
23539
23540 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23541 {
23542 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23543 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23544 if (curr->section)
23545 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23546 hash);
23547 hash = hash_locs (curr->expr, hash);
23548 }
23549 list_head->hash = hash;
23550 }
23551
23552 /* Return true if X and Y opcodes have the same operands. */
23553
23554 static inline bool
23555 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23556 {
23557 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23558 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23559 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23560 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23561
23562 switch (x->dw_loc_opc)
23563 {
23564 case DW_OP_const4u:
23565 case DW_OP_const8u:
23566 if (x->dtprel)
23567 goto hash_addr;
23568 /* FALLTHRU */
23569 case DW_OP_const1u:
23570 case DW_OP_const1s:
23571 case DW_OP_const2u:
23572 case DW_OP_const2s:
23573 case DW_OP_const4s:
23574 case DW_OP_const8s:
23575 case DW_OP_constu:
23576 case DW_OP_consts:
23577 case DW_OP_pick:
23578 case DW_OP_plus_uconst:
23579 case DW_OP_breg0:
23580 case DW_OP_breg1:
23581 case DW_OP_breg2:
23582 case DW_OP_breg3:
23583 case DW_OP_breg4:
23584 case DW_OP_breg5:
23585 case DW_OP_breg6:
23586 case DW_OP_breg7:
23587 case DW_OP_breg8:
23588 case DW_OP_breg9:
23589 case DW_OP_breg10:
23590 case DW_OP_breg11:
23591 case DW_OP_breg12:
23592 case DW_OP_breg13:
23593 case DW_OP_breg14:
23594 case DW_OP_breg15:
23595 case DW_OP_breg16:
23596 case DW_OP_breg17:
23597 case DW_OP_breg18:
23598 case DW_OP_breg19:
23599 case DW_OP_breg20:
23600 case DW_OP_breg21:
23601 case DW_OP_breg22:
23602 case DW_OP_breg23:
23603 case DW_OP_breg24:
23604 case DW_OP_breg25:
23605 case DW_OP_breg26:
23606 case DW_OP_breg27:
23607 case DW_OP_breg28:
23608 case DW_OP_breg29:
23609 case DW_OP_breg30:
23610 case DW_OP_breg31:
23611 case DW_OP_regx:
23612 case DW_OP_fbreg:
23613 case DW_OP_piece:
23614 case DW_OP_deref_size:
23615 case DW_OP_xderef_size:
23616 return valx1->v.val_int == valy1->v.val_int;
23617 case DW_OP_skip:
23618 case DW_OP_bra:
23619 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23620 can cause irrelevant differences in dw_loc_addr. */
23621 gcc_assert (valx1->val_class == dw_val_class_loc
23622 && valy1->val_class == dw_val_class_loc
23623 && (dwarf_split_debug_info
23624 || x->dw_loc_addr == y->dw_loc_addr));
23625 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23626 case DW_OP_implicit_value:
23627 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23628 || valx2->val_class != valy2->val_class)
23629 return false;
23630 switch (valx2->val_class)
23631 {
23632 case dw_val_class_const:
23633 return valx2->v.val_int == valy2->v.val_int;
23634 case dw_val_class_vec:
23635 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23636 && valx2->v.val_vec.length == valy2->v.val_vec.length
23637 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23638 valx2->v.val_vec.elt_size
23639 * valx2->v.val_vec.length) == 0;
23640 case dw_val_class_const_double:
23641 return valx2->v.val_double.low == valy2->v.val_double.low
23642 && valx2->v.val_double.high == valy2->v.val_double.high;
23643 case dw_val_class_addr:
23644 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23645 default:
23646 gcc_unreachable ();
23647 }
23648 case DW_OP_bregx:
23649 case DW_OP_bit_piece:
23650 return valx1->v.val_int == valy1->v.val_int
23651 && valx2->v.val_int == valy2->v.val_int;
23652 case DW_OP_addr:
23653 hash_addr:
23654 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23655 case DW_OP_GNU_addr_index:
23656 case DW_OP_GNU_const_index:
23657 {
23658 rtx ax1 = valx1->val_entry->addr.rtl;
23659 rtx ay1 = valy1->val_entry->addr.rtl;
23660 return rtx_equal_p (ax1, ay1);
23661 }
23662 case DW_OP_GNU_implicit_pointer:
23663 return valx1->val_class == dw_val_class_die_ref
23664 && valx1->val_class == valy1->val_class
23665 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23666 && valx2->v.val_int == valy2->v.val_int;
23667 case DW_OP_GNU_entry_value:
23668 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23669 case DW_OP_GNU_const_type:
23670 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23671 || valx2->val_class != valy2->val_class)
23672 return false;
23673 switch (valx2->val_class)
23674 {
23675 case dw_val_class_const:
23676 return valx2->v.val_int == valy2->v.val_int;
23677 case dw_val_class_vec:
23678 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23679 && valx2->v.val_vec.length == valy2->v.val_vec.length
23680 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23681 valx2->v.val_vec.elt_size
23682 * valx2->v.val_vec.length) == 0;
23683 case dw_val_class_const_double:
23684 return valx2->v.val_double.low == valy2->v.val_double.low
23685 && valx2->v.val_double.high == valy2->v.val_double.high;
23686 default:
23687 gcc_unreachable ();
23688 }
23689 case DW_OP_GNU_regval_type:
23690 case DW_OP_GNU_deref_type:
23691 return valx1->v.val_int == valy1->v.val_int
23692 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23693 case DW_OP_GNU_convert:
23694 case DW_OP_GNU_reinterpret:
23695 if (valx1->val_class != valy1->val_class)
23696 return false;
23697 if (valx1->val_class == dw_val_class_unsigned_const)
23698 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23699 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23700 case DW_OP_GNU_parameter_ref:
23701 return valx1->val_class == dw_val_class_die_ref
23702 && valx1->val_class == valy1->val_class
23703 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23704 default:
23705 /* Other codes have no operands. */
23706 return true;
23707 }
23708 }
23709
23710 /* Return true if DWARF location expressions X and Y are the same. */
23711
23712 static inline bool
23713 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23714 {
23715 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23716 if (x->dw_loc_opc != y->dw_loc_opc
23717 || x->dtprel != y->dtprel
23718 || !compare_loc_operands (x, y))
23719 break;
23720 return x == NULL && y == NULL;
23721 }
23722
23723 /* Hashtable helpers. */
23724
23725 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23726 {
23727 typedef dw_loc_list_struct value_type;
23728 typedef dw_loc_list_struct compare_type;
23729 static inline hashval_t hash (const value_type *);
23730 static inline bool equal (const value_type *, const compare_type *);
23731 };
23732
23733 /* Return precomputed hash of location list X. */
23734
23735 inline hashval_t
23736 loc_list_hasher::hash (const value_type *x)
23737 {
23738 return x->hash;
23739 }
23740
23741 /* Return true if location lists A and B are the same. */
23742
23743 inline bool
23744 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23745 {
23746 if (a == b)
23747 return 1;
23748 if (a->hash != b->hash)
23749 return 0;
23750 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23751 if (strcmp (a->begin, b->begin) != 0
23752 || strcmp (a->end, b->end) != 0
23753 || (a->section == NULL) != (b->section == NULL)
23754 || (a->section && strcmp (a->section, b->section) != 0)
23755 || !compare_locs (a->expr, b->expr))
23756 break;
23757 return a == NULL && b == NULL;
23758 }
23759
23760 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23761
23762
23763 /* Recursively optimize location lists referenced from DIE
23764 children and share them whenever possible. */
23765
23766 static void
23767 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23768 {
23769 dw_die_ref c;
23770 dw_attr_ref a;
23771 unsigned ix;
23772 dw_loc_list_struct **slot;
23773
23774 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23775 if (AT_class (a) == dw_val_class_loc_list)
23776 {
23777 dw_loc_list_ref list = AT_loc_list (a);
23778 /* TODO: perform some optimizations here, before hashing
23779 it and storing into the hash table. */
23780 hash_loc_list (list);
23781 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23782 if (*slot == NULL)
23783 *slot = list;
23784 else
23785 a->dw_attr_val.v.val_loc_list = *slot;
23786 }
23787
23788 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23789 }
23790
23791
23792 /* Recursively assign each location list a unique index into the debug_addr
23793 section. */
23794
23795 static void
23796 index_location_lists (dw_die_ref die)
23797 {
23798 dw_die_ref c;
23799 dw_attr_ref a;
23800 unsigned ix;
23801
23802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23803 if (AT_class (a) == dw_val_class_loc_list)
23804 {
23805 dw_loc_list_ref list = AT_loc_list (a);
23806 dw_loc_list_ref curr;
23807 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23808 {
23809 /* Don't index an entry that has already been indexed
23810 or won't be output. */
23811 if (curr->begin_entry != NULL
23812 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23813 continue;
23814
23815 curr->begin_entry
23816 = add_addr_table_entry (xstrdup (curr->begin),
23817 ate_kind_label);
23818 }
23819 }
23820
23821 FOR_EACH_CHILD (die, c, index_location_lists (c));
23822 }
23823
23824 /* Optimize location lists referenced from DIE
23825 children and share them whenever possible. */
23826
23827 static void
23828 optimize_location_lists (dw_die_ref die)
23829 {
23830 loc_list_hash_type htab;
23831 htab.create (500);
23832 optimize_location_lists_1 (die, htab);
23833 htab.dispose ();
23834 }
23835 \f
23836 /* Output stuff that dwarf requires at the end of every file,
23837 and generate the DWARF-2 debugging info. */
23838
23839 static void
23840 dwarf2out_finish (const char *filename)
23841 {
23842 limbo_die_node *node, *next_node;
23843 comdat_type_node *ctnode;
23844 hash_table <comdat_type_hasher> comdat_type_table;
23845 unsigned int i;
23846 dw_die_ref main_comp_unit_die;
23847
23848 /* PCH might result in DW_AT_producer string being restored from the
23849 header compilation, so always fill it with empty string initially
23850 and overwrite only here. */
23851 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23852 producer_string = gen_producer_string ();
23853 producer->dw_attr_val.v.val_str->refcount--;
23854 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23855
23856 gen_scheduled_generic_parms_dies ();
23857 gen_remaining_tmpl_value_param_die_attribute ();
23858
23859 /* Add the name for the main input file now. We delayed this from
23860 dwarf2out_init to avoid complications with PCH. */
23861 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23862 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23863 add_comp_dir_attribute (comp_unit_die ());
23864 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23865 {
23866 bool p = false;
23867 htab_traverse (file_table, file_table_relative_p, &p);
23868 if (p)
23869 add_comp_dir_attribute (comp_unit_die ());
23870 }
23871
23872 if (deferred_locations_list)
23873 for (i = 0; i < deferred_locations_list->length (); i++)
23874 {
23875 add_location_or_const_value_attribute (
23876 (*deferred_locations_list)[i].die,
23877 (*deferred_locations_list)[i].variable,
23878 false,
23879 DW_AT_location);
23880 }
23881
23882 /* Traverse the limbo die list, and add parent/child links. The only
23883 dies without parents that should be here are concrete instances of
23884 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
23885 For concrete instances, we can get the parent die from the abstract
23886 instance. */
23887 for (node = limbo_die_list; node; node = next_node)
23888 {
23889 dw_die_ref die = node->die;
23890 next_node = node->next;
23891
23892 if (die->die_parent == NULL)
23893 {
23894 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23895
23896 if (origin && origin->die_parent)
23897 add_child_die (origin->die_parent, die);
23898 else if (is_cu_die (die))
23899 ;
23900 else if (seen_error ())
23901 /* It's OK to be confused by errors in the input. */
23902 add_child_die (comp_unit_die (), die);
23903 else
23904 {
23905 /* In certain situations, the lexical block containing a
23906 nested function can be optimized away, which results
23907 in the nested function die being orphaned. Likewise
23908 with the return type of that nested function. Force
23909 this to be a child of the containing function.
23910
23911 It may happen that even the containing function got fully
23912 inlined and optimized out. In that case we are lost and
23913 assign the empty child. This should not be big issue as
23914 the function is likely unreachable too. */
23915 gcc_assert (node->created_for);
23916
23917 if (DECL_P (node->created_for))
23918 origin = get_context_die (DECL_CONTEXT (node->created_for));
23919 else if (TYPE_P (node->created_for))
23920 origin = scope_die_for (node->created_for, comp_unit_die ());
23921 else
23922 origin = comp_unit_die ();
23923
23924 add_child_die (origin, die);
23925 }
23926 }
23927 }
23928
23929 limbo_die_list = NULL;
23930
23931 #if ENABLE_ASSERT_CHECKING
23932 {
23933 dw_die_ref die = comp_unit_die (), c;
23934 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23935 }
23936 #endif
23937 resolve_addr (comp_unit_die ());
23938 move_marked_base_types ();
23939
23940 for (node = deferred_asm_name; node; node = node->next)
23941 {
23942 tree decl = node->created_for;
23943 /* When generating LTO bytecode we can not generate new assembler
23944 names at this point and all important decls got theirs via
23945 free-lang-data. */
23946 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23947 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23948 {
23949 add_linkage_attr (node->die, decl);
23950 move_linkage_attr (node->die);
23951 }
23952 }
23953
23954 deferred_asm_name = NULL;
23955
23956 /* Walk through the list of incomplete types again, trying once more to
23957 emit full debugging info for them. */
23958 retry_incomplete_types ();
23959
23960 if (flag_eliminate_unused_debug_types)
23961 prune_unused_types ();
23962
23963 /* Generate separate COMDAT sections for type DIEs. */
23964 if (use_debug_types)
23965 {
23966 break_out_comdat_types (comp_unit_die ());
23967
23968 /* Each new type_unit DIE was added to the limbo die list when created.
23969 Since these have all been added to comdat_type_list, clear the
23970 limbo die list. */
23971 limbo_die_list = NULL;
23972
23973 /* For each new comdat type unit, copy declarations for incomplete
23974 types to make the new unit self-contained (i.e., no direct
23975 references to the main compile unit). */
23976 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23977 copy_decls_for_unworthy_types (ctnode->root_die);
23978 copy_decls_for_unworthy_types (comp_unit_die ());
23979
23980 /* In the process of copying declarations from one unit to another,
23981 we may have left some declarations behind that are no longer
23982 referenced. Prune them. */
23983 prune_unused_types ();
23984 }
23985
23986 /* Generate separate CUs for each of the include files we've seen.
23987 They will go into limbo_die_list. */
23988 if (flag_eliminate_dwarf2_dups)
23989 break_out_includes (comp_unit_die ());
23990
23991 /* Traverse the DIE's and add add sibling attributes to those DIE's
23992 that have children. */
23993 add_sibling_attributes (comp_unit_die ());
23994 for (node = limbo_die_list; node; node = node->next)
23995 add_sibling_attributes (node->die);
23996 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23997 add_sibling_attributes (ctnode->root_die);
23998
23999 /* When splitting DWARF info, we put some attributes in the
24000 skeleton compile_unit DIE that remains in the .o, while
24001 most attributes go in the DWO compile_unit_die. */
24002 if (dwarf_split_debug_info)
24003 main_comp_unit_die = gen_compile_unit_die (NULL);
24004 else
24005 main_comp_unit_die = comp_unit_die ();
24006
24007 /* Output a terminator label for the .text section. */
24008 switch_to_section (text_section);
24009 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24010 if (cold_text_section)
24011 {
24012 switch_to_section (cold_text_section);
24013 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24014 }
24015
24016 /* We can only use the low/high_pc attributes if all of the code was
24017 in .text. */
24018 if (!have_multiple_function_sections
24019 || (dwarf_version < 3 && dwarf_strict))
24020 {
24021 /* Don't add if the CU has no associated code. */
24022 if (text_section_used)
24023 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24024 text_end_label, true);
24025 }
24026 else
24027 {
24028 unsigned fde_idx;
24029 dw_fde_ref fde;
24030 bool range_list_added = false;
24031
24032 if (text_section_used)
24033 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24034 text_end_label, &range_list_added, true);
24035 if (cold_text_section_used)
24036 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24037 cold_end_label, &range_list_added, true);
24038
24039 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24040 {
24041 if (DECL_IGNORED_P (fde->decl))
24042 continue;
24043 if (!fde->in_std_section)
24044 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24045 fde->dw_fde_end, &range_list_added,
24046 true);
24047 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24048 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24049 fde->dw_fde_second_end, &range_list_added,
24050 true);
24051 }
24052
24053 if (range_list_added)
24054 {
24055 /* We need to give .debug_loc and .debug_ranges an appropriate
24056 "base address". Use zero so that these addresses become
24057 absolute. Historically, we've emitted the unexpected
24058 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24059 Emit both to give time for other tools to adapt. */
24060 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24061 if (! dwarf_strict && dwarf_version < 4)
24062 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24063
24064 add_ranges (NULL);
24065 }
24066 }
24067
24068 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24069 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24070 debug_line_section_label);
24071
24072 if (have_macinfo)
24073 add_AT_macptr (comp_unit_die (),
24074 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24075 macinfo_section_label);
24076
24077 if (dwarf_split_debug_info && addr_index_table != NULL)
24078 {
24079 /* optimize_location_lists calculates the size of the lists,
24080 so index them first, and assign indices to the entries.
24081 Although optimize_location_lists will remove entries from
24082 the table, it only does so for duplicates, and therefore
24083 only reduces ref_counts to 1. */
24084 unsigned int index = 0;
24085 index_location_lists (comp_unit_die ());
24086 htab_traverse_noresize (addr_index_table,
24087 index_addr_table_entry, &index);
24088 }
24089 if (have_location_lists)
24090 optimize_location_lists (comp_unit_die ());
24091
24092 save_macinfo_strings ();
24093
24094 if (dwarf_split_debug_info)
24095 {
24096 unsigned int index = 0;
24097
24098 /* Add attributes common to skeleton compile_units and
24099 type_units. Because these attributes include strings, it
24100 must be done before freezing the string table. Top-level
24101 skeleton die attrs are added when the skeleton type unit is
24102 created, so ensure it is created by this point. */
24103 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24104 (void) get_skeleton_type_unit ();
24105 htab_traverse_noresize (debug_str_hash, index_string, &index);
24106 }
24107
24108 /* Output all of the compilation units. We put the main one last so that
24109 the offsets are available to output_pubnames. */
24110 for (node = limbo_die_list; node; node = node->next)
24111 output_comp_unit (node->die, 0);
24112
24113 comdat_type_table.create (100);
24114 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24115 {
24116 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24117
24118 /* Don't output duplicate types. */
24119 if (*slot != HTAB_EMPTY_ENTRY)
24120 continue;
24121
24122 /* Add a pointer to the line table for the main compilation unit
24123 so that the debugger can make sense of DW_AT_decl_file
24124 attributes. */
24125 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24126 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24127 (!dwarf_split_debug_info
24128 ? debug_line_section_label
24129 : debug_skeleton_line_section_label));
24130
24131 output_comdat_type_unit (ctnode);
24132 *slot = ctnode;
24133 }
24134 comdat_type_table.dispose ();
24135
24136 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24137 both the main_cu and all skeleton TUs. Making this call unconditional
24138 would end up either adding a second copy of the AT_pubnames attribute, or
24139 requiring a special case in add_top_level_skeleton_die_attrs. */
24140 if (!dwarf_split_debug_info)
24141 add_AT_pubnames (comp_unit_die ());
24142
24143 if (dwarf_split_debug_info)
24144 {
24145 int mark;
24146 unsigned char checksum[16];
24147 struct md5_ctx ctx;
24148
24149 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24150 md5_init_ctx (&ctx);
24151 mark = 0;
24152 die_checksum (comp_unit_die (), &ctx, &mark);
24153 unmark_all_dies (comp_unit_die ());
24154 md5_finish_ctx (&ctx, checksum);
24155
24156 /* Use the first 8 bytes of the checksum as the dwo_id,
24157 and add it to both comp-unit DIEs. */
24158 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24159 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24160
24161 /* Add the base offset of the ranges table to the skeleton
24162 comp-unit DIE. */
24163 if (ranges_table_in_use)
24164 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24165 ranges_section_label);
24166
24167 switch_to_section (debug_addr_section);
24168 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24169 output_addr_table ();
24170 }
24171
24172 /* Output the main compilation unit if non-empty or if .debug_macinfo
24173 or .debug_macro will be emitted. */
24174 output_comp_unit (comp_unit_die (), have_macinfo);
24175
24176 if (dwarf_split_debug_info && info_section_emitted)
24177 output_skeleton_debug_sections (main_comp_unit_die);
24178
24179 /* Output the abbreviation table. */
24180 if (abbrev_die_table_in_use != 1)
24181 {
24182 switch_to_section (debug_abbrev_section);
24183 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24184 output_abbrev_section ();
24185 }
24186
24187 /* Output location list section if necessary. */
24188 if (have_location_lists)
24189 {
24190 /* Output the location lists info. */
24191 switch_to_section (debug_loc_section);
24192 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24193 output_location_lists (comp_unit_die ());
24194 }
24195
24196 output_pubtables ();
24197
24198 /* Output the address range information if a CU (.debug_info section)
24199 was emitted. We output an empty table even if we had no functions
24200 to put in it. This because the consumer has no way to tell the
24201 difference between an empty table that we omitted and failure to
24202 generate a table that would have contained data. */
24203 if (info_section_emitted)
24204 {
24205 unsigned long aranges_length = size_of_aranges ();
24206
24207 switch_to_section (debug_aranges_section);
24208 output_aranges (aranges_length);
24209 }
24210
24211 /* Output ranges section if necessary. */
24212 if (ranges_table_in_use)
24213 {
24214 switch_to_section (debug_ranges_section);
24215 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24216 output_ranges ();
24217 }
24218
24219 /* Have to end the macro section. */
24220 if (have_macinfo)
24221 {
24222 switch_to_section (debug_macinfo_section);
24223 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24224 output_macinfo ();
24225 dw2_asm_output_data (1, 0, "End compilation unit");
24226 }
24227
24228 /* Output the source line correspondence table. We must do this
24229 even if there is no line information. Otherwise, on an empty
24230 translation unit, we will generate a present, but empty,
24231 .debug_info section. IRIX 6.5 `nm' will then complain when
24232 examining the file. This is done late so that any filenames
24233 used by the debug_info section are marked as 'used'. */
24234 switch_to_section (debug_line_section);
24235 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24236 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24237 output_line_info (false);
24238
24239 if (dwarf_split_debug_info && info_section_emitted)
24240 {
24241 switch_to_section (debug_skeleton_line_section);
24242 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24243 output_line_info (true);
24244 }
24245
24246 /* If we emitted any indirect strings, output the string table too. */
24247 if (debug_str_hash || skeleton_debug_str_hash)
24248 output_indirect_strings ();
24249 }
24250
24251 #include "gt-dwarf2out.h"